Parent

Templater::CLI::Generator

Public Class Methods

new(generator_name, generator_class, destination_root, name, version) click to toggle source
    # File lib/templater/cli/generator.rb, line 7
 7:       def initialize(generator_name, generator_class, destination_root, name, version)
 8:         @generator_name = generator_name
 9:         @destination_root = destination_root
10:         @generator_class = generator_class
11:         @name = name
12:         @version = version
13:       end

Public Instance Methods

help() click to toggle source

outputs a helpful message and quits

    # File lib/templater/cli/generator.rb, line 21
21:       def help
22:         puts "Usage: #{@name} #{@generator_name} [options] [args]"
23:         puts ''
24:         puts @generator_class.desc
25:         puts ''
26:         puts @options[:opts]
27:         puts ''
28:         exit
29:       end
run(arguments) click to toggle source
    # File lib/templater/cli/generator.rb, line 31
31:       def run(arguments)
32:         generator_class = @generator_class # FIXME: closure wizardry, there has got to be a better way than this?
33:         
34:         @options = Templater::CLI::Parser.parse(arguments) do |opts, options|
35:           opts.separator "Options specific for this generator:"
36:           # the reason this is reversed is so that the 'main' generator will always have the last word
37:           # on the description of the option
38:           generator_class.generators.reverse.each do |generator|
39:             # Loop through this generator's options and add them as valid command line options
40:             # so that they show up in help messages and such
41:             generator.options.each do |option|
42:               name = option.name.to_s.gsub('_', '-')
43:               if option.options[:as] == :boolean
44:                 opts.on("--#{name}", option.options[:desc]) do |s|
45:                   options[option.name] = s
46:                 end
47:               else
48:                 opts.on("--#{name}=value", option.options[:desc]) do |s|
49:                   options[option.name] = s.gsub('-', '_').to_sym
50:                 end
51:               end
52:             end
53:           end
54:         end
55: 
56:         self.help if @options[:help]
57:         self.version if @options[:version]
58: 
59:         # Try to instantiate a generator, if the arguments to it were incorrect: show a help message
60:         begin
61:           @generator = @generator_class.new(@destination_root, @options, *arguments)
62:         rescue Templater::ArgumentError => e
63:           if @options[:debug]
64:             raise e
65:           else
66:             self.help
67:           end
68:         end
69: 
70:         if @options[:pretend] 
71:           puts "Generating with #{@generator_name} generator (just pretending):"
72:         else
73:           puts "Generating with #{@generator_name} generator:" 
74:         end
75:         step_through_templates
76: 
77:         # Run hooks
78:         @generator.after_run
79:         @generator.after_generation unless @options[:delete]
80:         @generator.after_deletion   if     @options[:delete]
81:       end
step_through_templates() click to toggle source
     # File lib/templater/cli/generator.rb, line 83
 83:       def step_through_templates
 84:         @generator.all_actions.each do |action|
 85:           if @options[:delete]
 86:             action.revoke! unless @options[:pretend]
 87:             say_status('deleted', action, :red)
 88:           else
 89:             if action.identical?
 90:               say_status('identical', action, :blue)
 91:             elsif action.exists?
 92:               if @options[:force]
 93:                 action.invoke! unless @options[:pretend]
 94:                 say_status('forced', action, :yellow)
 95:               elsif @options[:skip]
 96:                 say_status('skipped', action, :yellow)
 97:               else
 98:                 say_status('conflict', action, :red)
 99:                 conflict_menu(action)
100:               end
101:             else
102:               action.invoke! unless @options[:pretend]
103:               say_status('added', action, :green)
104:             end
105:           end
106:         end
107:       end
version() click to toggle source
    # File lib/templater/cli/generator.rb, line 15
15:       def version
16:         puts @version
17:         exit
18:       end

Protected Instance Methods

conflict_menu(template) click to toggle source
     # File lib/templater/cli/generator.rb, line 111
111:       def conflict_menu(template)
112:         choose do |menu|
113:           menu.prompt = "How do you wish to proceed with this file?"
114: 
115:           menu.choice(:skip) do
116:             say("Skipped file")
117:           end
118:           menu.choice(:overwrite) do
119:             say("Overwritten")
120:             template.invoke! unless @options[:pretend]
121:           end
122:           menu.choice(:render) do
123:             puts "Rendering " + template.relative_destination
124:             puts ""
125:             # outputs each line of the file with the row number prepended
126:             template.render.to_lines.each_with_index do |line, i|
127:               puts((i+1).to_s.rjust(4) + ':  ' + line)
128:             end
129:             puts ""
130:             puts ""
131:             conflict_menu(template)
132:           end
133:           menu.choice(:diff) do
134:             puts "Showing differences for " + template.relative_destination
135:             puts ""
136: 
137:             diffs = Diff::LCS.diff(::File.read(template.destination).to_s.to_lines, template.render.to_lines).first
138: 
139:             diffs.each do |diff|
140:               output_diff_line(diff)
141:             end
142: 
143:             puts ""
144:             puts ""
145:             conflict_menu(template)
146:           end
147:           menu.choice(:abort) do
148:             say("Aborted!")
149:             exit
150:           end
151:         end
152:       end
output_diff_line(diff) click to toggle source
     # File lib/templater/cli/generator.rb, line 163
163:       def output_diff_line(diff)
164:         case diff.action
165:         when '-'
166:           say "<%= color( %(-  #{diff.element.chomp}), :red) %>"
167:         when '+'
168:           say "<%= color( %(+  #{diff.element.chomp}), :green) %>"
169:         else
170:           say "#{diff.action}  #{diff.element.chomp}"
171:         end
172:       end
say_status(status, template, color = nil) click to toggle source
     # File lib/templater/cli/generator.rb, line 154
154:       def say_status(status, template, color = nil)
155:         status_flag = "[#{status.to_s.upcase}]".rjust(12)
156:         if color and not @options[:no_color]
157:           say "<%= color('#{status_flag}', :#{color}) %>  " + template.relative_destination
158:         else
159:           say "#{status_flag}  " + template.relative_destination
160:         end
161:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.