Skip to content
Permalink
Browse files

(PUP-2284) Add an interview to the PMT `generate` action.

Prior to this commit, when you generated a new module, it created a simple
metadata.json file with the same standard defaults for every module.  Since
JSON may not be the most familiar language for all of our users, we now do
some work up front to pre-fill that JSON file with useful data based on user
input.
  • Loading branch information
pvande committed Apr 16, 2014
1 parent 7d78de2 commit 41c88a2732b546f570414ff06279b3be1393e7e2
@@ -15,12 +15,61 @@
Generate a new module in the current directory:
$ puppet module generate puppetlabs-ssh
notice: Generating module at /Users/kelseyhightower/puppetlabs-ssh
puppetlabs-ssh
puppetlabs-ssh/Modulefile
puppetlabs-ssh/README
We need to create a metadata.json file for this module. Please answer the
following questions; if the question is not applicable to this module, feel free
to leave it blank.
Puppet uses Semantic Versioning (semver.org) to version modules.
What version is this module? [0.0.1]
-->
Who wrote this module? [puppetlabs]
-->
What license does this module code fall under? [Apache 2.0]
-->
How would you describe this module in a single sentence?
-->
Where is this module's source code repository?
-->
Where can others go to learn more about this module?
-->
Where can others go to file issues about this module?
-->
----------------------------------------
{
"name": "puppetlabs-ssh",
"version": "0.0.1",
"author": "puppetlabs",
"summary": null,
"license": "Apache 2.0",
"source": "",
"project_page": null,
"issues_url": null,
"dependencies": [
{
"name": "puppetlabs-stdlib",
"version_range": ">= 1.0.0"
}
]
}
----------------------------------------
About to generate this metadata; continue? [n/Y]
-->
Notice: Generating module at /Users/username/Projects/puppet/puppetlabs-ssh...
Notice: Populating ERB templates...
Finished; module generated in puppetlabs-ssh.
puppetlabs-ssh/manifests
puppetlabs-ssh/manifests/init.pp
puppetlabs-ssh/metadata.json
puppetlabs-ssh/README.md
puppetlabs-ssh/spec
puppetlabs-ssh/spec/spec_helper.rb
puppetlabs-ssh/tests
@@ -30,12 +79,141 @@
arguments "<name>"

when_invoked do |name, options|
"This format is not supported by this action."
end

when_rendering :console do |_, name, options|
Puppet::ModuleTool.set_option_defaults options
Puppet::ModuleTool::Applications::Generator.run(name, options)

begin
metadata = Puppet::ModuleTool::Metadata.new.update(
'name' => name,
'version' => '0.0.1',
'dependencies' => [
{ :name => 'puppetlabs-stdlib', :version_range => '>= 1.0.0' }
]
)
rescue ArgumentError
msg = "Could not generate directory #{name.inspect}, you must specify a dash-separated username and module name."
raise $!, msg, $!.backtrace
end

dest = Puppet::ModuleTool::Generate.destination(metadata)
result = Puppet::ModuleTool::Generate.generate(metadata).join("\n")

path = dest.relative_path_from(Pathname.pwd)
puts "Finished; module generated in #{path}."
result
end
end
end

module Puppet::ModuleTool::Generate
module_function

when_rendering :console do |return_value|
return_value.map {|f| f.to_s }.join("\n")
def generate(metadata)
interview(metadata)
destination = generate_module(metadata)

return Dir[destination.basename + '**/*']
end

def interview(metadata)
puts "We need to create a metadata.json file for this module. Please answer the"
puts "following questions; if the question is not applicable to this module, feel free"
puts "to leave it blank."

loop do
begin
puts
puts "Puppet uses Semantic Versioning (semver.org) to version modules."
puts "What version is this module? [#{metadata.version}]"
metadata.update 'version' => user_input(metadata.version)
break
rescue
Puppet.err "We're sorry, we could not parse that as a Semantic Version."
next
end
end

puts
puts "Who wrote this module? [#{metadata.author}]"
metadata.update 'author' => user_input(metadata.author)

puts
puts "What license does this module code fall under? [#{metadata.license}]"
metadata.update 'license' => user_input(metadata.license)

puts
puts "How would you describe this module in a single sentence?"
metadata.update 'summary' => user_input(metadata.summary)

puts
puts "Where is this module's source code repository?"
metadata.update 'source' => user_input(metadata.source)

puts
puts "Where can others go to learn more about this module?#{ metadata.project_page && " [#{metadata.project_page}]" }"
metadata.update 'project_page' => user_input(metadata.project_page)

puts
puts "Where can others go to file issues about this module?#{ metadata.issues_url && " [#{metadata.issues_url}]" }"
metadata.update 'issues_url' => user_input(metadata.issues_url)

puts
puts '-' * 40
puts metadata.to_json
puts '-' * 40
puts
puts "About to generate this metadata; continue? [n/Y]"

if user_input('Y') !~ /^y(es)?$/i
puts "Aborting..."
exit 0
end
end

def user_input(default=nil)
print '--> '
input = STDIN.gets.chomp
input = default if input == ''
return input
end

def destination(metadata)
return @dest if defined? @dest
@dest = Pathname.pwd + metadata.dashed_name
raise ArgumentError, "#{@dest} already exists." if @dest.exist?
return @dest
end

def generate_module(metadata)
dest = destination(metadata)

puts
Puppet.notice "Generating module at #{dest}..."
FileUtils.cp_r skeleton_path, dest

populate_erb_templates(metadata, dest)
return dest
end

def populate_erb_templates(metadata, destination)
Puppet.notice "Populating ERB templates..."
Dir[destination + '**/*.erb'].each do |erb|
path = Pathname.new(erb)
content = ERB.new(path.read).result(binding)

target = path.parent + path.basename('.erb')
target.open('w') { |f| f.write(content) }
path.unlink
end
end

def skeleton_path
return @path if defined? @path
path = Pathname(Puppet.settings[:module_skeleton_dir])
path = Pathname(__FILE__).dirname + '../../module_tool/skeleton/templates/generator' unless path.directory?
@path = path
end
end
@@ -139,5 +139,4 @@ def self.set_option_defaults(options)
require 'puppet/module_tool/dependency'
require 'puppet/module_tool/metadata'
require 'puppet/module_tool/modulefile'
require 'puppet/module_tool/skeleton'
require 'puppet/forge'
@@ -5,7 +5,6 @@ module Applications
require 'puppet/module_tool/applications/application'
require 'puppet/module_tool/applications/builder'
require 'puppet/module_tool/applications/checksummer'
require 'puppet/module_tool/applications/generator'
require 'puppet/module_tool/applications/installer'
require 'puppet/module_tool/applications/searcher'
require 'puppet/module_tool/applications/unpacker'
@@ -40,7 +40,7 @@ def archive_file
def pack
FileUtils.rm archive_file rescue nil

tar = Puppet::ModuleTool::Tar.instance(metadata.to_hash['name'])
tar = Puppet::ModuleTool::Tar.instance(metadata.dashed_name)
Dir.chdir(@pkg_path) do
tar.pack(metadata.release_name, archive_file)
end
@@ -71,7 +71,7 @@ def write_json
unless File.exist?(metadata_path)
# Legacy build: Metadata was parsed from Modulefile; write it out
File.open(metadata_path, 'w') do |f|
f.write(PSON.pretty_generate(metadata))
f.write(metadata.to_json)
end
end

This file was deleted.

0 comments on commit 41c88a2

Please sign in to comment.
You can’t perform that action at this time.