lib/pdk/generators/module.rb in pdk-0.6.0 vs lib/pdk/generators/module.rb in pdk-1.0.0
- old
+ new
@@ -13,11 +13,19 @@
require 'pdk/util/version'
module PDK
module Generate
class Module
- DEFAULT_TEMPLATE = 'https://github.com/puppetlabs/pdk-module-template'.freeze
+ 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
def self.invoke(opts = {})
target_dir = File.expand_path(opts[:target_dir])
if File.exist?(target_dir)
@@ -40,13 +48,13 @@
temp_target_dir = PDK::Util.make_tmpdir_name('pdk-module-target')
prepare_module_directory(temp_target_dir)
- template_url = opts.fetch(:'template-url', PDK.answers['template-url'] || DEFAULT_TEMPLATE)
+ template_url = opts.fetch(:'template-url', default_template_url)
- PDK::Module::TemplateDir.new(template_url) do |templates|
+ PDK::Module::TemplateDir.new(template_url, metadata.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
@@ -77,11 +85,11 @@
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}') % {
+ 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
@@ -94,10 +102,13 @@
'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
@@ -128,12 +139,12 @@
def self.module_interview(metadata, opts = {})
questions = [
{
name: 'name',
- 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.'),
+ 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'] || metadata.data['author'],
},
@@ -147,11 +158,11 @@
default: metadata.data['version'],
},
{
name: 'author',
question: _('Who wrote this module?'),
- help: _('The person who gets credit for creating the module. '),
+ help: _('This will be used to credit the module\'s author.'),
required: true,
default: metadata.data['author'],
},
{
name: 'license',
@@ -160,32 +171,32 @@
required: true,
default: metadata.data['license'],
},
{
name: 'summary',
- question: _('How would you describe this module in a single sentence?'),
- help: _('To help other Puppet users understand what the module does.'),
+ 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: metadata.data['summary'],
},
{
name: 'source',
- question: _("Where is this modules's source code repository?"),
- help: _('Usually a GitHub URL'),
+ 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: metadata.data['source'],
},
{
name: 'project_page',
- question: _('Where can others go to learn more about this module?'),
- help: _('A web site that offers full information about your module.'),
+ 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: metadata.data['project_page'],
},
{
name: 'issues_url',
- question: _('Where can others go to file issues about this module?'),
- help: _('A web site with a public bug tracker for your module.'),
+ 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: metadata.data['issues_url'],
},
]
prompt = TTY::Prompt.new(help_color: :cyan)
@@ -195,12 +206,15 @@
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, just leave the answer blank.\n\n",
+ "\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?
@@ -218,10 +232,14 @@
puts '-' * 40
puts metadata.to_json
puts '-' * 40
puts
- unless prompt.yes?(_('About to generate this module; continue?'))
+ 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!(