module Puppet::ModuleTool::Generate

Public Instance Methods

destination(metadata) click to toggle source
# File lib/puppet/face/module/generate.rb, line 205
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
duplicate_skeleton(metadata) click to toggle source
# File lib/puppet/face/module/generate.rb, line 212
def duplicate_skeleton(metadata)
  dest = destination(metadata)

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

  populate_templates(metadata, dest)
  return dest
end
generate(metadata, skip_interview = false) click to toggle source
# File lib/puppet/face/module/generate.rb, line 138
def generate(metadata, skip_interview = false)
  interview(metadata) unless skip_interview
  destination = duplicate_skeleton(metadata)
  all_files = destination.basename + '**/*'

  return Dir[all_files.to_s]
end
interview(metadata) click to toggle source
# File lib/puppet/face/module/generate.rb, line 146
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."

  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)
  rescue
    Puppet.err "We're sorry, we could not parse that as a Semantic Version."
    retry
  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)?$/
    puts "Aborting..."
    exit 0
  end
end
populate_templates(metadata, destination) click to toggle source
# File lib/puppet/face/module/generate.rb, line 223
def populate_templates(metadata, destination)
  Puppet.notice "Populating templates..."

  formatters = {
    :erb      => proc { |data, ctx| ERB.new(data).result(ctx) },
    :template => proc { |data, _| data },
  }

  formatters.each do |type, block|
    templates = destination + "**/*.#{type}"

    Dir.glob(templates.to_s, File::FNM_DOTMATCH).each do |erb|
      path = Pathname.new(erb)
      content = block[path.read, binding]

      target = path.parent + path.basename(".#{type}")
      target.open('w') { |f| f.write(content) }
      path.unlink
    end
  end
end
skeleton_path() click to toggle source
# File lib/puppet/face/module/generate.rb, line 245
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
user_input(default=nil) click to toggle source
# File lib/puppet/face/module/generate.rb, line 198
def user_input(default=nil)
  print '--> '
  input = STDIN.gets.chomp.strip
  input = default if input == ''
  return input
end