def execute
if ARGV == [ "--version" ]
puts FPM::VERSION
return 0
end
logger.level = :warn
logger.level = :info if verbose?
logger.level = :debug if debug?
if log_level
logger.level = log_level.to_sym
end
if (stray_flags = args.grep(/^-/); stray_flags.any?)
logger.warn("All flags should be before the first argument " \
"(stray flags found: #{stray_flags}")
end
if input_type == "dir" and args.empty? and !chdir.nil?
logger.info("No args, but -s dir and -C are given, assuming '.' as input")
args << "."
end
logger.info("Setting workdir", :workdir => workdir)
ENV["TMP"] = workdir
validator = Validator.new(self)
if !validator.ok?
validator.messages.each do |message|
logger.warn(message)
end
logger.fatal("Fix the above problems, and you'll be rolling packages in no time!")
return 1
end
input_class = FPM::Package.types[input_type]
output_class = FPM::Package.types[output_type]
input = input_class.new
input.attributes ||= {}
self.class.declared_options.each do |option|
with(option.attribute_name) do |attr|
next if attr == "help"
flag_given = instance_variable_defined?("@#{attr}")
input.attributes["#{attr}_given?".to_sym] = flag_given
attr = "#{attr}?" if !respond_to?(attr)
input.attributes[attr.to_sym] = send(attr) if respond_to?(attr)
logger.debug("Setting attribute", attr.to_sym => send(attr))
end
end
args.each do |arg|
input.input(arg)
end
if !inputs.nil?
if !File.exists?(inputs)
logger.fatal("File given for --inputs does not exist (#{inputs})")
return 1
end
File.new(inputs, "r").each_line do |line|
input.input(line.strip)
end
end
set = proc do |object, attribute|
if object.send(attribute).nil? || send(attribute) != send("default_#{attribute}")
logger.info("Setting from flags: #{attribute}=#{send(attribute)}")
object.send("#{attribute}=", send(attribute))
end
end
set.call(input, :architecture)
set.call(input, :category)
set.call(input, :description)
set.call(input, :epoch)
set.call(input, :iteration)
set.call(input, :license)
set.call(input, :maintainer)
set.call(input, :name)
set.call(input, :url)
set.call(input, :vendor)
set.call(input, :version)
set.call(input, :architecture)
input.conflicts += conflicts
input.dependencies += dependencies
input.provides += provides
input.replaces += replaces
input.config_files += config_files
input.directories += directories
h = {}
attrs.each do | e |
s = e.split(':', 2)
h[s.last] = s.first
end
input.attrs = h
script_errors = []
setscript = proc do |scriptname|
path = self.send(scriptname)
next if path.nil?
if !File.exists?(path)
logger.error("No such file (for #{scriptname.to_s}): #{path.inspect}")
script_errors << path
end
input.scripts[scriptname] = File.read(path)
end
setscript.call(:before_install)
setscript.call(:after_install)
setscript.call(:before_remove)
setscript.call(:after_remove)
setscript.call(:before_upgrade)
setscript.call(:after_upgrade)
return 1 if script_errors.any?
if input.name.nil? or input.name.empty?
logger.fatal("No name given for this package (set name with '-n', " \
"for example, '-n packagename')")
return 1
end
output = input.convert(output_class)
if template_scripts?
template_value_list.each do |key, value|
(class << output; self; end).send(:define_method, key) { value }
end
end
if ! package.nil? && File.directory?(package)
package_file = File.join(package, output.to_s)
else
package_file = output.to_s(package)
end
begin
output.output(package_file)
rescue FPM::Package::FileAlreadyExists => e
logger.fatal(e.message)
return 1
rescue FPM::Package::ParentDirectoryMissing => e
logger.fatal(e.message)
return 1
end
logger.log("Created package", :path => package_file)
return 0
rescue FPM::Util::ExecutableNotFound => e
logger.error("Need executable '#{e}' to convert #{input_type} to #{output_type}")
return 1
rescue FPM::InvalidPackageConfiguration => e
logger.error("Invalid package configuration: #{e}")
return 1
rescue FPM::Util::ProcessFailed => e
logger.error("Process failed: #{e}")
return 1
ensure
if debug_workspace?
[input, output].each do |plugin|
next if plugin.nil?
[:staging_path, :build_path].each do |pathtype|
path = plugin.send(pathtype)
next unless Dir.open(path).to_a.size > 2
logger.log("plugin directory", :plugin => plugin.type, :pathtype => pathtype, :path => path)
end
end
else
input.cleanup unless input.nil?
output.cleanup unless output.nil?
end
end