Class: Templater::CLI::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/templater/cli/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(generator_name, generator_class, destination_root, name, version) ⇒ Generator

Returns a new instance of Generator.



7
8
9
10
11
12
13
# File 'lib/templater/cli/generator.rb', line 7

def initialize(generator_name, generator_class, destination_root, name, version)
  @generator_name = generator_name
  @destination_root = destination_root
  @generator_class = generator_class
  @name = name
  @version = version
end

Instance Method Details

#helpObject

outputs a helpful message and quits



21
22
23
24
25
26
27
28
29
# File 'lib/templater/cli/generator.rb', line 21

def help
  puts "Usage: #{@name} #{@generator_name} [options] [args]"
  puts ''
  puts @generator_class.desc
  puts ''
  puts @options[:opts]
  puts ''
  exit
end

#run(arguments) ⇒ Object



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
# File 'lib/templater/cli/generator.rb', line 31

def run(arguments)
  generator_class = @generator_class # FIXME: closure wizardry, there has got to be a better way than this?
  
  @options = Templater::CLI::Parser.parse(arguments) do |opts, options|
    opts.separator "Options specific for this generator:"
    # the reason this is reversed is so that the 'main' generator will always have the last word
    # on the description of the option
    generator_class.generators.reverse.each do |generator|
      # Loop through this generator's options and add them as valid command line options
      # so that they show up in help messages and such
      generator.options.each do |option|
        name = option.name.to_s.gsub('_', '-')
        if option.options[:as] == :boolean
          opts.on("--#{name}", option.options[:desc]) do |s|
            options[option.name] = s
          end
        else
          opts.on("--#{name}=value", option.options[:desc]) do |s|
            options[option.name] = s.gsub('-', '_').to_sym
          end
        end
      end
    end
  end

  self.help if @options[:help]
  self.version if @options[:version]

  # Try to instantiate a generator, if the arguments to it were incorrect: show a help message
  begin
    @generator = @generator_class.new(@destination_root, @options, *arguments)
  rescue Templater::ArgumentError => e
    if @options[:debug]
      raise e
    else
      self.help
    end
  end

  if @options[:pretend] 
    puts "Generating with #{@generator_name} generator (just pretending):"
  else
    puts "Generating with #{@generator_name} generator:" 
  end
  step_through_templates

  # Run hooks
  @generator.after_run
  @generator.after_generation unless @options[:delete]
  @generator.after_deletion   if     @options[:delete]
end

#step_through_templatesObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/templater/cli/generator.rb', line 83

def step_through_templates
  @generator.all_actions.each do |action|
    if @options[:delete]
      action.revoke! unless @options[:pretend]
      say_status('deleted', action, :red)
    else
      if action.identical?
        say_status('identical', action, :blue)
      elsif action.exists?
        if @options[:force]
          action.invoke! unless @options[:pretend]
          say_status('forced', action, :yellow)
        elsif @options[:skip]
          say_status('skipped', action, :yellow)
        else
          say_status('conflict', action, :red)
          conflict_menu(action)
        end
      else
        action.invoke! unless @options[:pretend]
        say_status('added', action, :green)
      end
    end
  end
end

#versionObject



15
16
17
18
# File 'lib/templater/cli/generator.rb', line 15

def version
  puts @version
  exit
end