#!/usr/bin/env ruby # -*- ruby -*- $LOAD_PATH.push(File.expand_path(File.dirname(__FILE__) + "/../lib")) require 'gem2rpm' require 'optparse' require 'fileutils' require 'open-uri' require 'uri' options = Gem2Rpm::Configuration.instance.options rest = options[:args] template = begin Gem2Rpm::Template.find options[:template_file], :gem_file => rest[0] rescue Gem2Rpm::Template::TemplateError => e $stderr.puts e exit(1) end if options[:print_template_file] puts template.read exit 0 end if rest.size != 1 $stderr.puts "Missing GEMFILE\n\n" $stderr.puts opts exit(1) end gemfile = rest[0] if options[:fetch] gem_uri = '' open("https://rubygems.org/api/v1/gems/#{gemfile}.json") do |f| gem_uri = f.read.match(/"gem_uri":\s*"(.*?)",/m)[1] gemfile = URI.parse(gem_uri).path.split('/').last open(gemfile, 'w') do |gf| gf.write(open(gem_uri).read) end end end srpmdir = nil specfile = nil if options[:srpm] gemname = Gem2Rpm::Package.new(gemfile).spec.name srpmdir = `/bin/mktemp -t -d gem2rpm-#{gemname}.XXXXXX`.chomp specfile = File.join(srpmdir, "rubygem-#{gemname}.spec") options[:output_file] ||= specfile end # Produce a specfile if options[:output_file].nil? Gem2Rpm.convert(gemfile, template, $stdout, options[:nongem], options[:local], options[:doc_subpackage]) unless options[:deps] else begin out = open(options[:output_file], "w") Gem2Rpm.convert(gemfile, template, out, options[:nongem], options[:local], options[:doc_subpackage]) ensure out.close end end # Create a source RPM if options[:srpm] FileUtils.copy(options[:output_file], specfile) unless File.exist?(specfile) FileUtils.copy(gemfile, srpmdir) system("rpmbuild -bs --nodeps --define '_sourcedir #{srpmdir}' --define '_srcrpmdir #{Dir.pwd}' #{specfile}") end Gem2Rpm.print_dependencies(gemfile) if options[:deps]