Class: Pfm::Command::Generate

Inherits:
Base
  • Object
show all
Defined in:
lib/idlc-sdk-pfm/command/generate.rb

Defined Under Namespace

Classes: GeneratorCommand

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#build_base_dir, #build_dir, #build_exists?, #build_setup, #deploy_setup, #deploy_setupv2, #inf_base_dir, #needs_version?, #run_with_default_options, #templates_dir, #verbose?

Methods included from Helpers

debug, err, msg, system_command

Constructor Details

#initialize(*args) ⇒ Generate

pfm generate app path/to/basename –skel=path/to/skeleton –example pfm generate file name [path/to/cookbook_root] (inferred from cwd) –from=source_file



41
42
43
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 41

def initialize(*args)
  super
end

Class Method Details



34
35
36
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 34

def self.banner
  banner_headline + generator_list + "\n"
end


22
23
24
25
26
27
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 22

def self.banner_headline
  <<-E
Usage: pfm generate GENERATOR [options]
Available generators:
E
end

.generator(name, class_name, description) ⇒ Object



16
17
18
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 16

def self.generator(name, class_name, description)
  generators << GeneratorCommand.new(name, class_name, description)
end

.generator_listObject



29
30
31
32
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 29

def self.generator_list
  justify_size = generators.map { |g| g.name.size }.max + 2
  generators.map { |g| "  #{g.name.to_s.ljust(justify_size)}#{g.description}" }.join("\n")
end

.generatorsObject



12
13
14
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 12

def self.generators
  @generators ||= []
end

Instance Method Details

#generator_for(arg) ⇒ Object



64
65
66
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 64

def generator_for(arg)
  self.class.generators.find { |g| g.name.to_s == arg }
end

#have_generator?(name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 77

def have_generator?(name)
  self.class.generators.map { |g| g.name.to_s }.include?(name)
end

#needs_help?(params) ⇒ Boolean

In the Base class, this is defined to be true if any args match “-h” or “–help”. Here we override that behavior such that if the first argument is a valid generator name, like ‘pfm generate cookbook -h`, we delegate the request to the specified generator.

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 72

def needs_help?(params)
  return false if have_generator?(params[0])
  super
end

#run(params) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/idlc-sdk-pfm/command/generate.rb', line 45

def run(params)
  if generator_spec = generator_for(params[0])
    params.shift
    generator = GeneratorCommands.build(generator_spec.class_name, params)
    generator.run
  else
    msg(banner)
    1
  end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
  # Pfm::Command::Base also handles this error in the same way, but it
  # does not have access to the correct option parser, so it cannot print
  # the usage correctly. Therefore, invalid CLI usage needs to be handled
  # here.
  err("ERROR: #{e.message}\n")
  msg(generator.opt_parser)
  1
end