summaryrefslogtreecommitdiff
path: root/rake_tasks/diff.rake
blob: f0af55af43b274b1f1bae36cb7653326097654fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# A simple differ using svn. Handles externals.
class Differ < Hash
  
  include Rake::DSL if defined? Rake::DSL
  
  def initialize path
    @path = path
    super 0
  end
  
  def count key, value
    self[key] += value
    value
  end
  
  FORMAT = '  %-30s %8d lines, %3d changes in %2d files'
  
  def scan(path = @path)
    Dir.chdir path do
      diff_file_name = 'diff'
      if File.directory? 'diff'
        diff_file_name = 'diff.diff'
      end
      system "svn diff > #{diff_file_name}"
      if File.size? diff_file_name
        puts FORMAT %
          [
            path,
            count(:LOC, `wc -l #{diff_file_name}`.to_i),
            count(:changes, `grep ^@@ #{diff_file_name} | wc -l`.to_i),
            count(:files, `grep ^Index #{diff_file_name} | wc -l`.to_i),
          ]
      else
        rm diff_file_name
      end
    end
  end
  
  def scan_with_externals(path = @path)
    scan path
    `svn status`.scan(/^X\s*(.*)/) do |external,|
      scan external
    end
  end
  
  def clean(path = @path)
    Dir.chdir path do
      rm 'diff' if File.file? 'diff'
      rm 'diff.diff' if File.file? 'diff.diff'
    end
  end
  
  def clean_with_externals(path = @path)
    clean path
    `svn status`.scan(/^X\s*(.*)/) do |external,|
      clean external
    end
  end
  
  def differences?
    self[:LOC] > 0
  end
  
  def inspect
    FORMAT %
      [ 'Total', self[:LOC], self[:changes], self[:files] ]
  end
  
end

namespace :diff do
  
  desc 'Make a diff and print a summary'
  task :summary do
    differ = Differ.new '.'
    differ.scan_with_externals
    if differ.empty?
      puts 'No differences found.'
    else
      p differ
    end
  end
  
  desc 'Remove all diffs'
  task :clean do
    differ = Differ.new '.'
    differ.clean_with_externals
  end
  
end

desc 'Make a diff and print a summary'
task :diff => 'diff:summary'