Module: Lydown::CLI::Diff

Defined in:
lib/lydown/cli/diff.rb

Constant Summary collapse

CACHE =
{}

Class Method Summary collapse

Class Method Details

.cached_content(path) ⇒ Object



7
8
9
# File 'lib/lydown/cli/diff.rb', line 7

def cached_content(path)
  CACHE[File.absolute_path(path)] || []
end

.diff_line_range(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lydown/cli/diff.rb', line 28

def diff_line_range(path)
  new_version = read_content(path)
  old_version = cached_content(path)
  
  first = nil
  last = nil
  
  diffs = Diff::LCS.diff(old_version, new_version).each do |d|
    d.each do |r|
      line = r.to_a[1]
      first = line if first.nil? || line < first
      last = line if last.nil? || line > last
    end
  end
  
  set_cached_content(path, new_version)
  
  first += 1 if first
  last += 1 if last
  
  [first, last]
rescue => e
  $stderr.puts e.message
  $stderr.puts e.backtrace.join("\n")
  nil..nil
end

.fill_cache(dir) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/lydown/cli/diff.rb', line 15

def fill_cache(dir)
  count = 0
  Dir["#{dir}/**/*.ld"].each do |path|
    set_cached_content(path, read_content(path)) rescue nil
    count += 1
  end
  # $stderr.puts "Cached #{count} files."
end

.read_content(path) ⇒ Object



24
25
26
# File 'lib/lydown/cli/diff.rb', line 24

def read_content(path)
  IO.read(path).lines.map {|l| l.chomp}
end

.set_cached_content(path, content) ⇒ Object



11
12
13
# File 'lib/lydown/cli/diff.rb', line 11

def set_cached_content(path, content)
  CACHE[File.absolute_path(path)] = content
end