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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/pdk/generate/module.rb', line 21
def self.invoke(opts = {})
require 'pdk/util'
require 'pdk/util/template_uri'
require 'pathname'
validate_options(opts) unless opts[:module_name].nil?
metadata = prepare_metadata(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
raise PDK::CLI::FatalError, _("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 _(
"Using the %{method} template-url and template-ref '%{template_uri}'." % {
method: opts.key?(:'template-url') ? _('specified') : _('saved'),
template_uri: template_uri.metadata_format,
},
)
end
begin
context = PDK::Context::None.new(temp_target_dir)
PDK::Template.with(template_uri, context) do |template_dir|
template_dir.render_new_module(metadata.data['name'], metadata.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
metadata.update!(template_dir.metadata)
metadata.write!(File.join(temp_target_dir, 'metadata.json'))
end
rescue ArgumentError => e
raise PDK::CLI::ExitWithError, e
end
require 'pdk/answer_file'
if template_uri.default? && template_uri.default_ref?
PDK.config.set(%w[user module_defaults template-url], nil) if opts.key?(:'template-url')
else
PDK.config.set(%w[user module_defaults template-url], template_uri.metadata_format)
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 _("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, _("Failed to move '%{source}' to '%{target}': %{message}") % {
source: temp_target_dir,
target: target_dir,
message: e.message,
}
end
end
|