diff --git a/lib/puppet/module_tool/applications/application.rb b/lib/puppet/module_tool/applications/application.rb index af35a5d11..d175f563c 100644 --- a/lib/puppet/module_tool/applications/application.rb +++ b/lib/puppet/module_tool/applications/application.rb @@ -1,98 +1,99 @@ require 'net/http' require 'semver' require 'json' require 'puppet/util/colors' module Puppet::ModuleTool module Applications class Application include Puppet::Util::Colors def self.run(*args) new(*args).run end attr_accessor :options def initialize(options = {}) @options = options end def run raise NotImplementedError, "Should be implemented in child classes." end def discuss(response, success, failure) case response when Net::HTTPOK, Net::HTTPCreated Puppet.notice success else errors = JSON.parse(response.body)['error'] rescue "HTTP #{response.code}, #{response.body}" Puppet.warning "#{failure} (#{errors})" end end def metadata(require_metadata = false) return @metadata if @metadata @metadata = Puppet::ModuleTool::Metadata.new unless @path raise ArgumentError, "Could not determine module path" end modulefile_path = File.join(@path, 'Modulefile') metadata_path = File.join(@path, 'metadata.json') - if File.file?(modulefile_path) - if File.file?(metadata_path) - Puppet.warning "Modulefile is deprecated. Using metadata.json." - else - Puppet.warning "Modulefile is deprecated. Building metadata.json from Modulefile." - end - end - if File.file?(metadata_path) File.open(metadata_path) do |f| begin @metadata.update(JSON.load(f)) rescue JSON::ParserError => ex raise ArgumentError, "Could not parse JSON #{metadata_path}", ex.backtrace end end + end + + if File.file?(modulefile_path) + if File.file?(metadata_path) + Puppet.warning "Modulefile is deprecated. Merging your Modulefile and metadata.json." + else + Puppet.warning "Modulefile is deprecated. Building metadata.json from Modulefile." + end - elsif File.file?(modulefile_path) Puppet::ModuleTool::ModulefileReader.evaluate(@metadata, modulefile_path) + end - elsif require_metadata + has_metadata = File.file?(modulefile_path) || File.file?(metadata_path) + if !has_metadata && require_metadata raise ArgumentError, "No metadata found for module #{@path}" end return @metadata end def load_metadata! @metadata = nil metadata(true) end def parse_filename(filename) if match = /^((.*?)-(.*?))-(\d+\.\d+\.\d+.*?)$/.match(File.basename(filename,'.tar.gz')) module_name, author, shortname, version = match.captures else raise ArgumentError, "Could not parse filename to obtain the username, module name and version. (#{@release_name})" end unless SemVer.valid?(version) raise ArgumentError, "Invalid version format: #{version} (Semantic Versions are acceptable: http://semver.org)" end return { :module_name => module_name, :author => author, :dir_name => shortname, :version => version } end end end end diff --git a/lib/puppet/module_tool/applications/builder.rb b/lib/puppet/module_tool/applications/builder.rb index 66e9af3b0..30d40e968 100644 --- a/lib/puppet/module_tool/applications/builder.rb +++ b/lib/puppet/module_tool/applications/builder.rb @@ -1,89 +1,89 @@ require 'fileutils' require 'json' module Puppet::ModuleTool module Applications class Builder < Application def initialize(path, options = {}) @path = File.expand_path(path) @pkg_path = File.join(@path, 'pkg') super(options) end def run load_metadata! create_directory copy_contents write_json Puppet.notice "Building #{@path} for release" pack relative = Pathname.new(archive_file).relative_path_from(Pathname.new(File.expand_path(Dir.pwd))) # Return the Pathname object representing the path to the release # archive just created. This return value is used by the module_tool # face build action, and displayed to on the console using the to_s # method. # # Example return value: # # # relative end private def archive_file File.join(@pkg_path, "#{metadata.release_name}.tar.gz") end def pack FileUtils.rm archive_file rescue nil tar = Puppet::ModuleTool::Tar.instance Dir.chdir(@pkg_path) do tar.pack(metadata.release_name, archive_file) end end def create_directory FileUtils.mkdir(@pkg_path) rescue nil if File.directory?(build_path) FileUtils.rm_rf(build_path, :secure => true) end FileUtils.mkdir(build_path) end def copy_contents Dir[File.join(@path, '*')].each do |path| case File.basename(path) when *Puppet::ModuleTool::ARTIFACTS next else FileUtils.cp_r path, build_path, :preserve => true end end end def write_json metadata_path = File.join(build_path, 'metadata.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(metadata.to_json) - end + # TODO: This may necessarily change the order in which the metadata.json + # file is packaged from what was written by the user. This is a + # regretable, but required for now. + File.open(metadata_path, 'w') do |f| + f.write(metadata.to_json) end File.open(File.join(build_path, 'checksums.json'), 'w') do |f| f.write(PSON.pretty_generate(Checksums.new(@path))) end end def build_path @build_path ||= File.join(@pkg_path, metadata.release_name) end end end end diff --git a/lib/puppet/module_tool/dependency.rb b/lib/puppet/module_tool/dependency.rb index 7535a17d9..c213e55ce 100644 --- a/lib/puppet/module_tool/dependency.rb +++ b/lib/puppet/module_tool/dependency.rb @@ -1,29 +1,30 @@ require 'puppet/module_tool' require 'puppet/network/format_support' module Puppet::ModuleTool class Dependency include Puppet::Network::FormatSupport + alias :to_json :to_pson attr_reader :full_module_name, :username, :name, :version_requirement, :repository # Instantiates a new module dependency with a +full_module_name+ (e.g. # "myuser-mymodule"), and optional +version_requirement+ (e.g. "0.0.1") and # optional repository (a URL string). def initialize(full_module_name, version_requirement = nil, repository = nil) @full_module_name = full_module_name # TODO: add error checking, the next line raises ArgumentError when +full_module_name+ is invalid @username, @name = Puppet::ModuleTool.username_and_modname_from(full_module_name) @version_requirement = version_requirement @repository = repository ? Puppet::Forge::Repository.new(repository) : nil end def to_data_hash result = { :name => @full_module_name } result[:version_requirement] = @version_requirement if @version_requirement && ! @version_requirement.nil? result[:repository] = @repository.to_s if @repository && ! @repository.nil? result end end end