#!/usr/bin/env ruby $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")) require 'rubygems' require 'net/ftp' require 'fig/options' require 'fig/environment' require 'fig/repository' require 'fig/os' require 'fig/parser' require 'fig/windows' include Fig def parse_descriptor(descriptor) # todo should use treetop for these: package_name = descriptor =~ /^([^:\/]+)/ ? $1 : nil config_name = descriptor =~ /:([^:\/]+)/ ? $1 : nil version_name = descriptor =~ /\/([^:\/]+)/ ? $1 : nil return package_name, config_name, version_name end shell_command = nil ARGV.each_with_index do |arg, i| if arg == "-" # $stderr.puts "Use of single dash (-) is deprecated. Use double dash (--) instead" # exit 1 elsif arg == "--" shell_command = ARGV[(i+1)..-1] ARGV.slice!(i..-1) break end end options, argv = parse_options(ARGV) vars = {} ENV.each {|key,value| vars[key]=value } remote_url = nil if options[:update] || options[:publish] || options[:update_if_missing] || options[:list_remote] remote_url = ENV['FIG_REMOTE_URL'] if remote_url.nil? $stderr.puts "Please define the FIG_REMOTE_URL environment variable" exit 1 end end remote_user = nil if options[:publish] # remote_user = ENV['FIG_REMOTE_USER'] # if remote_user.nil? # $stderr.puts "Please define the FIG_REMOTE_USER environment variable" # exit 1 # end end os = OS.new repos = Repository.new(os, File.expand_path(File.join(options[:home], 'repos')), remote_url, remote_user, options[:update], options[:update_if_missing]) env = Environment.new(os, repos, vars) options[:modifiers].each do |modifier| env.apply_config_statement(nil, modifier) end if File.exist?(".fig") $stderr.puts "The '.fig' file is deprecated. Please rename to 'package.fig'" exit 1 end DEFAULT_FIG_FILE = 'package.fig' input = nil if options[:input] == :none # ignore elsif options[:input] == '-' input = $stdin.read elsif options[:input].nil? input = os.read(DEFAULT_FIG_FILE) if os.exist?(DEFAULT_FIG_FILE) else if os.exist?(options[:input]) input = os.read(options[:input]) else $stderr.puts "File not found: #{options[:input]}" exit 1 end end options[:cleans].each do |descriptor| package_name, version_name = descriptor.split('/') repos.clean(package_name, version_name) end if options[:list] repos.list_packages.sort.each do |item| puts item end exit 0 end if options[:list_remote] repos.list_remote_packages.sort.each do |item| puts item end exit 0 end if not options[:list_configs].empty? options[:list_configs].each do |descriptor| package_name, version_name = descriptor.split('/') repos.read_local_package(package_name, version_name).configs.each do |config| puts config.name end end exit 0 end if input package = Parser.new.parse_package(nil, nil, ".", input) direct_retrieves=[] if options[:retrieve] package.retrieves.each do |var, path| if var =~ /^@([^\/]+)(.*)/ direct_retrieves << [$1, $2, path] else env.add_retrieve(var, path) end end end unless options[:publish] || options[:list] || options[:publish_local] env.register_package(package) env.apply_config(package, options[:config]) direct_retrieves.each do |info| env.direct_retrieve(info[0], info[1], info[2]) end end else package = Package.new(nil, nil, ".", []) end if options[:publish] || options[:publish_local] raise "Unexpected arguments: #{argv.join(' ')}" if !argv.empty? package_name, config_name, version_name = parse_descriptor(options[:publish] || options[:publish_local]) if package_name.nil? || version_name.nil? raise "Please specify a package name and a version name" end if not options[:modifiers].empty? publish_statements = options[:resources] + options[:archives] + [Configuration.new("default", options[:modifiers])] publish_statements << Publish.new("default","default") elsif not package.statements.empty? publish_statements = package.statements else fail "Nothing to publish" end if options[:publish] puts "Checking status of #{package_name}/#{version_name}..." if repos.list_remote_packages.include?("#{package_name}/#{version_name}") puts "#{package_name}/#{version_name} has already been published" if not options[:force] puts "Use the --force option if you really want to overwrite, or us --publish-local for testing" exit 1 else puts "Overwriting..." end end end puts "Publishing #{package_name}/#{version_name}" repos.publish_package(publish_statements, package_name, version_name, options[:publish_local]) elsif options[:echo] puts env[options[:echo]] elsif shell_command argv.shift env.execute_shell(shell_command) { |cmd| os.shell_exec cmd } elsif argv[0] package_name, config_name, version_name = parse_descriptor(argv.shift) env.include_config(package, package_name, config_name, version_name) env.execute_config(package, package_name, config_name, nil, argv) { |cmd| os.shell_exec cmd } elsif input env.execute_config(package, nil, options[:config], nil, argv) { |cmd| os.shell_exec cmd } end