Class: PDK::Generate::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/generators/module.rb

Constant Summary collapse

DEFAULT_TEMPLATE =
'https://github.com/puppetlabs/pdk-module-template'.freeze

Class Method Summary collapse

Class Method Details

.invoke(opts = {}) ⇒ Object



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
# File 'lib/pdk/generators/module.rb', line 19

def self.invoke(opts = {})
  defaults = {
    'name'         => "#{Etc.getlogin}-#{opts[:name]}",
    'version'      => '0.1.0',
    'dependencies' => [
      { 'name' => 'puppetlabs-stdlib', 'version_requirement' => '>= 4.13.1 < 5.0.0' },
    ],
  }

  defaults['license'] = opts[:license] if opts.key? :license
  target_dir = File.expand_path(opts[:target_dir])

  if File.exist?(target_dir)
    raise PDK::CLI::FatalError, _("The destination directory '%{dir}' already exists") % { dir: target_dir }
  end

   = PDK::Module::Metadata.new(defaults)

  module_interview(, opts) unless opts[:'skip-interview'] # @todo Build way to get info by answers file

  .update!('pdk-version' => PDK::Util::Version.version_string)

  temp_target_dir = PDK::Util.make_tmpdir_name('pdk-module-target')

  prepare_module_directory(temp_target_dir)

  template_url = opts.fetch(:'template-url', DEFAULT_TEMPLATE)

  PDK::Module::TemplateDir.new(template_url) do |templates|
    templates.render do |file_path, file_content|
      file = Pathname.new(temp_target_dir) + file_path
      file.dirname.mkpath
      file.write(file_content)
    end

    # Add information about the template used to generate the module to the
    # metadata (for a future update command).
    .update!(templates.)

    File.open(File.join(temp_target_dir, 'metadata.json'), 'w') do ||
      .puts .to_json
    end
  end

  FileUtils.mv(temp_target_dir, target_dir)
end

.module_interview(metadata, opts = {}) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/pdk/generators/module.rb', line 79

def self.module_interview(, opts = {})
  puts _(
    'We need to create a metadata.json file for this module. Please answer the ' \
    'following questions; if the question is not applicable to this module, feel free ' \
    'to leave it blank.',
  )

  begin
    puts ''
    forge_user = PDK::CLI::Input.get(_('What is your Puppet Forge username?'), .data['author'])
    .update!('name' => "#{forge_user}-#{opts[:name]}")
  rescue StandardError => e
    PDK.logger.error(_("We're sorry, we could not parse your module name: %{message}") % { message: e.message })
    retry
  end

  begin
    puts "\n" + _('Puppet uses Semantic Versioning (semver.org) to version modules.')
    module_version = PDK::CLI::Input.get(_('What version is this module?'), .data['version'])
    .update!('version' => module_version)
  rescue StandardError => e
    PDK.logger.error(_("We're sorry, we could not parse that as a Semantic Version: %{message}") % { message: e.message })
    retry
  end

  puts ''
  module_author = PDK::CLI::Input.get(_('Who wrote this module?'), .data['author'])
  .update!('author' => module_author)

  unless opts.key?(:license)
    puts ''
    module_license = PDK::CLI::Input.get(_('What license does this module code fall under?'), .data['license'])
    .update!('license' => module_license)
  end

  puts ''
  module_summary = PDK::CLI::Input.get(_('How would you describe this module in a single sentence?'), .data['summary'])
  .update!('summary' => module_summary)

  puts ''
  module_source = PDK::CLI::Input.get(_("Where is this module's source code repository?"), .data['source'])
  .update!('source' => module_source)

  puts ''
  module_page = PDK::CLI::Input.get(_('Where can others go to learn more about this module?'), .data['project_page'])
  .update!('project_page' => module_page)

  puts ''
  module_issues = PDK::CLI::Input.get(_('Where can others go to file issues about this module?'), .data['issues_url'])
  .update!('issues_url' => module_issues)

  puts
  puts '-' * 40
  puts .to_json
  puts '-' * 40
  puts

  unless PDK::CLI::Input.get(_('About to generate this module; continue?'), 'Y') =~ %r{^y(es)?$}i # rubocop:disable Style/GuardClause
    puts _('Aborting...')
    exit 0
  end
end

.prepare_module_directory(target_dir) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pdk/generators/module.rb', line 66

def self.prepare_module_directory(target_dir)
  [
    File.join(target_dir, 'manifests'),
    File.join(target_dir, 'templates'),
  ].each do |dir|
    begin
      FileUtils.mkdir_p(dir)
    rescue SystemCallError
      raise PDK::CLI::FatalError, _("Unable to create directory '%{dir}'") % { dir: dir }
    end
  end
end