public
Description: My set of personal Thor scripts.
Homepage:
Clone URL: git://github.com/crnixon/thor_tasks.git
Clinton R. Nixon (author)
Sat Sep 19 08:31:34 -0700 2009
thor_tasks / meta.thor
100644 71 lines (62 sloc) 2.079 kb
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
# module: meta
# based off code at http://github.com/cldwalker/thor-tasks/blob/master/thor.thor
 
class Meta < Thor
  desc 'show_module MODULE', 'Print module'
  def show_module(tmodule)
    if (record = get_module_record(tmodule))
      filename = File.join(Thor::Util.thor_root, record[:filename])
      puts File.read(filename)
    end
  end
 
  desc "edit_module MODULE", "Edit module's original file"
  method_options :no_update=>:boolean
  def edit_module(tmodule)
    if (record = get_module_record(tmodule))
      system(ENV['EDITOR'], record[:location])
      Thor::Runner.new.update(get_module_name(tmodule)) unless options['no_update']
    end
  end
  
  desc "stale_modules", "Show stale modules with # of diff lines they are stale."
  def stale_modules
    stats = thor_yaml.keys.map do |tmodule|
      result = (diff_module(tmodule, true) || '').chomp
      lines = result.split("\n").reject {|e| e.nil? || e.empty? }.size
      [tmodule, lines]
    end
    stats.select {|m, l| l > 0 }.each do |m,l|
      puts "#{m} : #{l} lines"
    end
  end
 
  desc 'diff_module MODULE', "Diff between module's cached and original file"
  def diff_module(tmodule, return_string=false)
    if (record = get_module_record(tmodule))
      cached_file = File.join(Thor::Util.thor_root, record[:filename])
      if record[:location] =~ /^(http|www)/
        puts "Can't diff web files right now"
        return nil
      end
      return_string ? `diff #{cached_file} #{record[:location]}` : system("diff", cached_file, record[:location])
    end
  end
 
  desc "config", "Edit thor's config file"
  def config
    system(ENV['EDITOR'], File.join(Thor::Util.thor_root, 'thor.yml'))
  end
 
  private
  def get_module_name(tmodule)
    # tmodule += '.thor' unless tmodule.include?('.thor')
    tmodule
  end
 
  def get_module_record(tmodule)
    tmodule = get_module_name(tmodule)
    if (thor_yaml[tmodule])
      thor_yaml[tmodule]
    else
      puts "Thor module '#{tmodule}' not found"
      nil
    end
  end
 
  def thor_yaml
    @y ||= Thor::Runner.new.send(:thor_yaml)
  end
end