Class: PDK::Generate::Module
- Inherits:
-
Object
- Object
- PDK::Generate::Module
- Defined in:
- lib/pdk/generators/module.rb
Class Method Summary collapse
- .default_template_url ⇒ Object
- .invoke(opts = {}) ⇒ Object
- .module_interview(metadata, opts = {}) ⇒ Object
- .prepare_metadata(opts) ⇒ Object
- .prepare_module_directory(target_dir) ⇒ Object
- .username_from_login ⇒ Object
Class Method Details
.default_template_url ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/pdk/generators/module.rb', line 18 def self.default_template_url if !PDK.answers['template-url'].nil? PDK.answers['template-url'] elsif PDK::Util.package_install? 'file://' + File.join(PDK::Util.package_cachedir, 'pdk-module-template.git') else 'https://github.com/puppetlabs/pdk-module-template' end end |
.invoke(opts = {}) ⇒ Object
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 |
# File 'lib/pdk/generators/module.rb', line 28 def self.invoke(opts = {}) target_dir = File.(opts[:target_dir]) if File.exist?(target_dir) raise PDK::CLI::FatalError, _("The destination directory '%{dir}' already exists") % { dir: target_dir } end parent_dir = File.dirname(target_dir) begin test_file = File.join(parent_dir, '.pdk-test-writable') File.open(test_file, 'w') { |f| f.write('This file was created by the Puppet Development Kit to test if this folder was writable, you can safely remove this file.') } FileUtils.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 = (opts) 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_url) PDK::Module::TemplateDir.new(template_url, .data) 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 PDK.answers.update!('template-url' => template_url) begin FileUtils.mv(temp_target_dir, target_dir) rescue Errno::EACCES => e raise PDK::CLI::FatalError, _("Failed to move '%{source}' to '%{target}': %{message}") % { source: temp_target_dir, target: target_dir, message: e., } end end |
.module_interview(metadata, opts = {}) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/pdk/generators/module.rb', line 140 def self.module_interview(, opts = {}) questions = [ { name: 'name', question: _('If you have a Puppet Forge username, add it here.'), help: _('We can use this to upload your module to the Forge when it\'s complete.'), required: true, validate_pattern: %r{\A[a-z0-9]+\Z}i, validate_message: _('Forge usernames can only contain lowercase letters and numbers'), default: PDK.answers['forge-username'] || .data['author'], }, { name: 'version', question: _('What version is this module?'), help: _('Puppet uses Semantic Versioning (semver.org) to version modules.'), required: true, validate_pattern: %r{\A[0-9]+\.[0-9]+\.[0-9]+}, validate_message: _('Semantic Version numbers must be in the form MAJOR.MINOR.PATCH'), default: .data['version'], }, { name: 'author', question: _('Who wrote this module?'), help: _('This will be used to credit the module\'s author.'), required: true, default: .data['author'], }, { name: 'license', question: _('What license does this module code fall under?'), help: _('This should be an identifier from https://spdk.org/licenses/. Common values are "Apache-2.0", "MIT", or "proprietary".'), required: true, default: .data['license'], }, { name: 'summary', question: _('Please summarize the purpose of this module in a single sentence.'), help: _('This will help other Puppet users understand what the module does.'), required: true, default: .data['summary'], }, { name: 'source', question: _('If there is a source code repository for this module, enter the URL here.'), help: _('Skip this if none exists yet, you can update this later in the metadata.json.'), required: true, default: .data['source'], }, { name: 'project_page', question: _('If there is a URL where others can learn more about this module, enter it here.'), help: _('Optional. As with all questions above, you can update this later in the metadata.json.'), default: .data['project_page'], }, { name: 'issues_url', question: _('If there is a public issue tracker for this module, enter its URL here.'), help: _('Optional. As with all questions above, you can update this later in the metadata.json.'), default: .data['issues_url'], }, ] prompt = TTY::Prompt.new(help_color: :cyan) interview = PDK::CLI::Util::Interview.new(prompt) questions.reject! { |q| q[:name] == 'license' } if opts.key?(:license) interview.add_questions(questions) puts _( "\nWe need to create a metadata.json file for this module, so we\'re going to ask you %{count} quick " \ "questions.\n" \ 'If the question is not applicable to this module, simply leave the answer blank and skip. A default option ' \ 'is shown after each question. You can modify this or any other answers at any time by manually updating ' \ "the metadata.json file.\n\n", ) % { count: interview.num_questions } answers = interview.run if answers.nil? PDK.logger.info _('Interview cancelled, not generating the module.') exit 0 end forge_username = answers['name'] answers['name'] = "#{answers['name']}-#{opts[:name]}" answers['license'] = opts[:license] if opts.key?(:license) .update!(answers) puts '-' * 40 puts _('SUMMARY') puts '-' * 40 puts .to_json puts '-' * 40 puts continue = prompt.yes?(_('About to generate this module; continue?')) do |q| q.validate(proc { |value| [true, false].include?(value) || value =~ %r{\A(?:yes|y|no|n)\Z}i }, 'Please answer "yes" or "no"') end unless continue PDK.logger.info _('Module not generated.') exit 0 end PDK.answers.update!( 'forge-username' => forge_username, 'author' => answers['author'], 'license' => answers['license'], ) end |
.prepare_metadata(opts) ⇒ Object
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 |
# File 'lib/pdk/generators/module.rb', line 98 def self.(opts) username = PDK.answers['forge-username'] || username_from_login defaults = { 'name' => "#{username}-#{opts[:name]}", 'version' => '0.1.0', 'dependencies' => [ { 'name' => 'puppetlabs-stdlib', 'version_requirement' => '>= 4.13.1 < 5.0.0' }, ], 'requirements' => [ { 'name' => 'puppet', 'version_requirement' => '>= 4.7.0 < 6.0.0' }, ], } defaults['author'] = PDK.answers['author'] unless PDK.answers['author'].nil? defaults['license'] = PDK.answers['license'] unless PDK.answers['license'].nil? defaults['license'] = opts[:license] if opts.key? :license = PDK::Module::Metadata.new(defaults) module_interview(, opts) unless opts[:'skip-interview'] .update!('pdk-version' => PDK::Util::Version.version_string) end |
.prepare_module_directory(target_dir) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/pdk/generators/module.rb', line 124 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 => e raise PDK::CLI::FatalError, _("Unable to create directory '%{dir}': %{message}") % { dir: dir, message: e., } end end end |
.username_from_login ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/pdk/generators/module.rb', line 84 def self.username_from_login login = Etc.getlogin || '' login_clean = login.gsub(%r{[^0-9a-z]}i, '') login_clean = 'username' if login_clean.empty? if login_clean != login PDK.logger.warn _('Your username is not a valid Forge username, proceeding with the username %{username}. You can fix this afterwards in metadata.json.') % { username: login_clean, } end login_clean end |