lib/pdk/generators/module.rb in pdk-0.3.0 vs lib/pdk/generators/module.rb in pdk-0.4.0

- old
+ new

@@ -28,11 +28,11 @@ 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) + template_url = opts.fetch(:'template-url', PDK.answers['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 @@ -46,32 +46,46 @@ File.open(File.join(temp_target_dir, 'metadata.json'), 'w') do |metadata_file| metadata_file.puts metadata.to_json end end + PDK.answers.update!('template-url' => template_url) + FileUtils.mv(temp_target_dir, target_dir) end - def self.prepare_metadata(opts) - username = Etc.getlogin.gsub(%r{[^0-9a-z]}i, '') - username = 'username' if username == '' - if Etc.getlogin != username - PDK.logger.warn(_('Your username is not a valid Forge username, proceeding with the username %{username}' % { username: username })) + 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 _('You username is not a valid Forge username, proceeding with the username %{username}') % { + username: login_clean, + } end + login_clean + end + + def self.prepare_metadata(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' }, ], } + 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 metadata = PDK::Module::Metadata.new(defaults) - module_interview(metadata, opts) unless opts[:'skip-interview'] # @todo Build way to get info by answers file + module_interview(metadata, opts) unless opts[:'skip-interview'] metadata.update!('pdk-version' => PDK::Util::Version.version_string) metadata end @@ -96,11 +110,11 @@ question: _('What is your Puppet Forge username?'), help: _('This will be used when uploading your module to the Forge. You can opt out of this at any time.'), required: true, validate_pattern: %r{\A[a-z0-9]+\Z}i, validate_message: _('Forge usernames can only contain lowercase letters and numbers'), - default: metadata.data['author'], + default: PDK.answers['forge-username'] || metadata.data['author'], }, { name: 'version', question: _('What version is this module?'), help: _('Puppet uses Semantic Versioning (semver.org) to version modules.'), @@ -165,27 +179,35 @@ ) % { count: interview.num_questions } answers = interview.run if answers.nil? - puts _('Interview cancelled, aborting...') + 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) metadata.update!(answers) puts '-' * 40 puts _('SUMMARY') puts '-' * 40 puts metadata.to_json puts '-' * 40 puts - unless prompt.yes?(_('About to generate this module; continue?')) # rubocop:disable Style/GuardClause - puts _('Aborting...') + unless prompt.yes?(_('About to generate this module; continue?')) + PDK.logger.info _('Module not generated.') exit 0 end + + PDK.answers.update!( + 'forge-username' => forge_username, + 'author' => answers['author'], + 'license' => answers['license'], + ) end end end end