Class: Retrospec::Puppet::Generators::ModuleGenerator

Inherits:
Retrospec::Plugins::V1::Plugin
  • Object
show all
Defined in:
lib/retrospec/plugins/v1/plugin/generators/module_generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_path, spec_object = {}) ⇒ ModuleGenerator

retrospec will initilalize this class so its up to you to set any additional variables you need to get the job done.



12
13
14
15
16
17
18
19
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 12

def initialize(module_path, spec_object = {})
  super
  # below is the Spec Object which serves as a context for template rendering
  # you will need to initialize this object, so the erb templates can get the binding
  # the SpecObject can be customized to your liking as its different for every plugin gem.
  @template_dir = spec_object[:template_dir]
  @context = OpenStruct.new(:module_name => spec_object[:name])
end

Instance Attribute Details

#config_dataObject (readonly)

Returns the value of attribute config_data.



8
9
10
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 8

def config_data
  @config_data
end

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 8

def context
  @context
end

#fact_nameObject (readonly)

Returns the value of attribute fact_name.



8
9
10
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 8

def fact_name
  @fact_name
end

#template_dirObject (readonly)

Returns the value of attribute template_dir.



8
9
10
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 8

def template_dir
  @template_dir
end

Class Method Details

.generate_metadata_file(mod_name, config_data) ⇒ Object



71
72
73
74
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 71

def self.(mod_name, config_data)
  f = Retrospec::Puppet::Generators::ModuleGenerator.new(config_data[:module_path], config_data)
  f.(mod_name, config_data)
end

.run_cli(global_opts, args = ARGV) ⇒ Object

used to display subcommand options to the cli the global options are passed in for your usage trollop.rubyforge.org all options here are available in the config passed into config object returns a new instance of this class



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 26

def self.run_cli(global_opts, args=ARGV)
  namespace     = global_opts['plugins::puppet::namespace'] || 'namespace'
  author        = global_opts['plugins::puppet::author'] || 'author_name'
  license       = global_opts['plugins::puppet::default_license'] || 'Apache-2.0'
  sub_command_opts = Trollop.options(args) do
    banner <<-EOS
Generates a new module with the given name and namespace
    EOS
    opt :name, 'The name of the module you wish to create', :type => :string, :required => true, :short => '-n',
                                                            :default => File.basename(global_opts[:module_path])
    opt :namespace, 'The namespace to use only when creating a new module', :default => namespace, :required => false,
                                                                            :type => :string
    opt :author, 'The full name of the module author', :default => author, :required => false, :short => '-a',
                                                       :type => :string
    opt :license, "The license type for the module", :default => license, :type => :string, :short => '-l'
  end
  unless sub_command_opts[:name]
    Trollop.educate
    exit 1
  end
  plugin_data = global_opts.merge(sub_command_opts)
end

Instance Method Details

#create_manifest_file(dest, content) ⇒ Object



64
65
66
67
68
69
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 64

def create_manifest_file(dest, content)
  # replace the name in the target file with the module_name from this class
  # I would have just used a template but the context does not exist yet
  new_content = content.gsub('CLASS_NAME', config_data[:name])
  safe_create_file(dest, new_content)
end

#generate_metadata_file(mod_name, config_data) ⇒ Object

generates the metadata file in the module directory



77
78
79
80
81
82
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
108
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 77

def (mod_name, config_data)
  require 'puppet/module_tool/metadata'
  # make sure the metadata file exists
  module_path = config_data[:module_path]
  license = config_data[:license] || config_data['plugins::puppet::default_license'] || 'Apache-2.0'
  author = config_data[:author] || config_data['plugins::puppet::author'] || 'your_name'
  namespace = config_data[:namespace] || config_data['plugins::puppet::namespace'] || 'namespace'
   = File.join(module_path, 'metadata.json')
  unless File.exist?()
    # by default the module tool metadata checks for a namespece
    if !mod_name.include?('-')
      name = "#{namespace}-#{mod_name}"
    else
      name = mod_name
    end
    begin
       = ::Puppet::ModuleTool::Metadata.new.update(
        'name' => name.downcase,
        'version' => '0.1.0',
        'author'  => author,
        'license' => license,
        'dependencies' => [
          { 'name' => 'puppetlabs-stdlib', 'version_requirement' => '>= 4.11.0' }
        ]
      )
    rescue ArgumentError => e
      puts e.message
      exit -1
    end
    safe_create_file(, .to_json)
  end
end

#run(manifest_dir) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/retrospec/plugins/v1/plugin/generators/module_generator.rb', line 49

def run(manifest_dir)
  unless File.exist?(manifest_dir)
    init_class = File.join(manifest_dir, 'init.pp')
    content = File.read(File.join(template_dir, 'manifest_file.pp'))
    unless ENV['RETROSPEC_PUPPET_AUTO_GENERATE'] == 'true'
      print "The module located at: #{module_path} does not exist, do you wish to create it? (y/n): "
      answer = gets.chomp
      exit 1 unless answer =~ /y/i
    end

    create_manifest_file(init_class, content)
    (config_data[:name], config_data)
  end
end