Module: DiffInfluence::Core

Defined in:
lib/diff_influence/core.rb,
lib/diff_influence/core_1x.rb

Defined Under Namespace

Classes: EMeth

Class Method Summary collapse

Class Method Details

.configObject



5
6
7
# File 'lib/diff_influence/core.rb', line 5

def self.config
  DiffInfluence::Config
end

.debug_log(msg) ⇒ Object



9
10
11
# File 'lib/diff_influence/core.rb', line 9

def self.debug_log(msg)
  puts "[DEBUG] #{msg}" if self.config.debug
end

.execObject



113
114
115
116
117
118
# File 'lib/diff_influence/core.rb', line 113

def self.exec
  self.file_paths.each do |fp|
    self.influence_search fp
  end
  exit 0
end

.file_pathsObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/diff_influence/core.rb', line 17

def self.file_paths
  @@files ||= self.git_status.lines.map {|line|
    path = line.split.last
    case path
    when *self.config.search_directories.map{|d| /\A#{d}/}
      path
    else
      nil
    end
  }.compact
end

.filesObject



83
84
85
# File 'lib/diff_influence/core.rb', line 83

def self.files
  @@files ||= Dir.glob(self.config.search_directories.map{|d| "#{d}/**/**.{#{self.config.search_extensions.join(',')}}"})
end

.git_diff(file_path) ⇒ Object



29
30
31
# File 'lib/diff_influence/core.rb', line 29

def self.git_diff(file_path)
  `git --no-pager diff --no-ext-diff -U1000000 #{self.config.commits.join(' ')} #{file_path}`
end

.git_statusObject



13
14
15
# File 'lib/diff_influence/core.rb', line 13

def self.git_status
  `git status | grep modified`
end

.grep(method_name) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/diff_influence/core.rb', line 105

def self.grep(method_name)
  if self.config.os_grep
    self.os_grep method_name
  else
    self.native_grep method_name
  end
end

.influence_search(file_path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/diff_influence/core.rb', line 87

def self.influence_search(file_path)
  searched_methods = []
  self.search_methods(file_path).each do |method|
    if(
      method.name.nil? || method.name.empty? ||
      self.config.ignore_methods.include?(method.name) ||
      searched_methods.include?(method.name)
    )
      next
    else
      searched_methods.push method.name
    end
    puts "###  Searching method[#{method.name}] (from #{file_path}:#{method.index})"
    self.grep method.name
    puts
  end
end

.native_grep(keyword = '') ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/diff_influence/core.rb', line 73

def self.native_grep(keyword='')
  self.files.each do |file|
    File.readlines(file).each_with_index do |line, idx|
      if line =~ /(\.|@)#{Regexp.escape(keyword)}(\s|\()/
        puts "#{file}:#{idx+1} #{line}"
      end
    end
  end
end

.os_grep(keyword = '') ⇒ Object



67
68
69
70
71
# File 'lib/diff_influence/core.rb', line 67

def self.os_grep(keyword='')
  self.config.search_directories.each do |pd|
    puts `grep -r -E '(\\.|@)#{Regexp.escape(keyword)}(\\s|\\()' #{pd}`
  end
end

.search_methods(file_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/diff_influence/core.rb', line 33

def self.search_methods(file_path)
  methods = []
  last_method = nil
  cnt = 0
  lines = self.git_diff(file_path).lines
  lines.each_with_index do |line, idx|
    method_line = line =~ /(\s|\t|;)def\s/
    if method_line
      last_method = lines[idx].split('def ').last.chomp.gsub('self.', '').gsub(/\(.*\z/, '')
      self.debug_log "Method line => #{last_method}"
    end
    case line
    when /\A\+\+\+/, /\A---/, /\Adiff/, /\Aindex/, /\A@@/
      self.debug_log "Ignore line => #{line}"
      next
    when /\A\+/, /\A\-/
      cnt += 1 if line =~ /\A\+/
      self.debug_log "idx:#{idx}, cnt:#{cnt}, #{line}"
      
      t = if method_line
            line =~ /\A\-/ ? 'remove' : 'add'
          else
            'effect'
          end

      methods.push EMeth.new(last_method,t,line,cnt)
    else
      cnt += 1
    end
  end
  self.debug_log methods.to_s
  methods
end