Method: PDK::Generate::Module.invoke

Defined in:
lib/pdk/generate/module.rb

.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
65
66
67
68
69
70
71
72
73
74
75
76
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
# File 'lib/pdk/generate/module.rb', line 19

def self.invoke(opts = {})
  require 'pdk/util'
  require 'pdk/util/template_uri'
  require 'pathname'

  validate_options(opts) unless opts[:module_name].nil?

   = (opts)

  target_dir = PDK::Util::Filesystem.expand_path(opts[:target_dir] || opts[:module_name])
  parent_dir = File.dirname(target_dir)

  begin
    test_file = File.join(parent_dir, '.pdk-test-writable')
    PDK::Util::Filesystem.write_file(test_file, 'This file was created by the Puppet Development Kit to test if this folder was writable, you can safely remove this file.')
    PDK::Util::Filesystem.rm_f(test_file)
  rescue Errno::EACCES || Errno::EROFS
    raise PDK::CLI::FatalError, format("You do not have permission to write to '%{parent_dir}'", parent_dir: parent_dir)
  end

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

  prepare_module_directory(temp_target_dir)

  template_uri = PDK::Util::TemplateURI.new(opts)

  if template_uri.default? && template_uri.default_ref?
    PDK.logger.info 'Using the default template-url and template-ref.'
  else
    PDK.logger.info format("Using the %{method} template-url and template-ref '%{template_uri}'.", method: opts.key?(:'template-url') ? 'specified' : 'saved',
                                                                                                   template_uri: template_uri.)
  end

  begin
    context = PDK::Context::None.new(temp_target_dir)
    PDK::Template.with(template_uri, context) do |template_dir|
      template_dir.render_new_module(.data['name'], .data) do |relative_file_path, file_content, file_status|
        next if [:delete, :unmanage].include?(file_status)

        file = Pathname.new(temp_target_dir) + relative_file_path
        file.dirname.mkpath
        PDK::Util::Filesystem.write_file(file, file_content)
      end

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

      .write!(File.join(temp_target_dir, 'metadata.json'))
    end
  rescue ArgumentError => e
    raise PDK::CLI::ExitWithError, e
  end

  # Only update the answers files after metadata has been written.
  require 'pdk/answer_file'
  if template_uri.default? && template_uri.default_ref?
    # If the user specifies our default template url via the command
    # line, remove the saved template-url answer so that the template_uri
    # resolution can find new default URLs in the future.
    PDK.config.set(['user', 'module_defaults', 'template-url'], nil) if opts.key?(:'template-url')
  else
    # Save the template-url answers if the module was generated using a
    # template/reference other than ours.
    PDK.config.set(['user', 'module_defaults', 'template-url'], template_uri.)
  end

  begin
    if PDK::Util::Filesystem.mv(temp_target_dir, target_dir)
      unless opts[:'skip-bundle-install']
        Dir.chdir(target_dir) do
          require 'pdk/util/bundler'
          PDK::Util::Bundler.ensure_bundle!
        end
      end

      PDK.logger.info format("Module '%{name}' generated at path '%{path}'.", name: opts[:module_name], path: target_dir)
      PDK.logger.info "In your module directory, add classes with the 'pdk new class' command."
    end
  rescue Errno::EACCES => e
    raise PDK::CLI::FatalError, format("Failed to move '%{source}' to '%{target}': %{message}", source: temp_target_dir, target: target_dir, message: e.message)
  end
end