3 # Copyright 2010 Per Øyvind Karlsen <peroyvind@mandriva.org>
4 # This program is free software. It may be redistributed and/or modified under
5 # the terms of the LGPL version 2.1 (or later).
11 if ARGV[0] == "build" or ARGV[0] == "install" or ARGV[0] == "spec"
23 # Push this into some environment variables as the modified classes doesn't
24 # seem to be able to access our global variables.. </lameworkaround>
25 ENV['GEM_MODE'] = ARGV[0]
27 opts = OptionParser.new("#{$0} <--filter PATTERN>")
28 opts.on("-f", "--filter PATTERN", "Filter pattern to use for gem files") do |val|
31 opts.on("-j", "--jobs JOBS", "Number of jobs to run simultaneously.") do |val|
32 ENV['jobs'] = "-j"+val
34 opts.on("--dry-run", "Only show the files the gem will include") do
35 ARGV.delete("--dry-run")
38 elsif ARGV[0] == "install"
39 opts = OptionParser.new("#{$0} <--keep-cache>")
40 opts.on("--keep-cache", "Don't delete gem copy from cache") do
41 ARGV.delete("--keep-cache")
44 opts.on("--fix-permissions", "Force standard permissions for files installed") do
45 ARGV.delete("--fix-permissions")
48 opts.on("-i", "--install-dir GEMDIR", "Gem repository directory") do |val|
55 rescue OptionParser::InvalidOption => e
61 file_data = Zlib::GzipReader.open("metadata.gz") {|io| io.read}
62 header = YAML::load(file_data)
64 # I don't know any better.. :/
65 header.instance_variables.each do |iv|
66 body[iv.to_s.sub(/^@/,'')] = header.instance_variable_get(iv)
69 spec = Gem::Specification.from_yaml(YAML.dump(header))
72 # Write the .gemspec specification (in Ruby)
73 file_name = spec.full_name.untaint + '.gemspec'
74 File.open(file_name, "w") do |file|
75 file.puts spec.to_ruby_for_cache
77 print "Wrote: %s\n" % file_name
81 if ARGV[0] == "install"
82 system("gem %s %s.gem" % [ARGV.join(' '), spec.full_name])
85 FileUtils.rm_rf("%s/cache" % gemdir)
88 chmod = "chmod u+r,u+w,g-w,g+r,o+r -R %s" % gemdir
89 print "\nFixing permissions:\n\n%s\n" % chmod
95 if body['extensions'].size > 0
96 require 'rubygems/ext'
99 def self.make(dest_path, results)
100 make_program = ENV['make']
101 unless make_program then
102 make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
105 if ENV['GEM_MODE'] == "build"
106 cmd += " %s" % ENV['jobs']
107 elsif ENV['GEM_MODE'] == "install"
108 cmd += " DESTDIR='%s' install" % ENV['DESTDIR']
111 results << `#{cmd} #{redirector}`
113 raise Gem::ExtensionBuildError, "make failed:\n\n#{results}" unless
119 require 'rubygems/installer'
122 def initialize(spec, options={})
135 Gem::Installer.new(spec).build_extensions
137 for ext in body['extensions']
138 files.push(ext[0..ext.rindex("/")-1]+".so")
142 body['extensions'].clear()
144 if ARGV[0] == "build"
145 body['test_files'].clear()
147 # We don't want ext/ in require_paths, it will only contain content for
148 # building extensions which needs to be installed in sitearchdir anyways..
150 for i in 0..body['require_paths'].size()-1
151 if body['require_paths'][idx].match("^ext(/|$)")
152 body['require_paths'].delete_at(idx)
158 # We'll get rid of all the files we don't really need to install
160 for i in 0..body['files'].size()-1
161 if filter and body['files'][idx].match(filter)
165 for path in body['require_paths']
166 if body['files'][idx].match("^%s/" % path)
172 body['files'].delete_at(idx)
178 spec = Gem::Specification.from_yaml(YAML.dump(header))
180 Gem::Builder.new(spec).build
182 files.concat(spec.files)
183 print "%s\n" % files.join("\n")