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")
62 header = YAML::load(file_data)
64 body = header.instance_variable_get :@ivars
66 spec = Gem::Specification.from_yaml(YAML.dump(header))
69 # Write the .gemspec specification (in Ruby)
70 file_name = spec.full_name.untaint + '.gemspec'
71 File.open(file_name, "w") do |file|
72 file.puts spec.to_ruby_for_cache
74 print "Wrote: %s\n" % file_name
78 if ARGV[0] == "install"
79 system("gem %s %s.gem" % [ARGV.join(' '), spec.full_name])
82 FileUtils.rm_rf("%s/cache" % gemdir)
85 chmod = "chmod u+r,u+w,g-w,g+r,o+r -R %s" % gemdir
86 print "\nFixing permissions:\n\n%s\n" % chmod
92 if body['extensions'].size > 0
93 require 'rubygems/ext'
96 def self.make(dest_path, results)
97 make_program = ENV['make']
98 unless make_program then
99 make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make'
102 if ENV['GEM_MODE'] == "build"
103 cmd += " %s" % ENV['jobs']
104 elsif ENV['GEM_MODE'] == "install"
105 cmd += " DESTDIR='%s' install" % ENV['DESTDIR']
108 results << `#{cmd} #{redirector}`
110 raise Gem::ExtensionBuildError, "make failed:\n\n#{results}" unless
116 require 'rubygems/installer'
119 def initialize(spec, options={})
132 Gem::Installer.new(spec).build_extensions
134 for ext in body['extensions']
135 files.push(ext[0..ext.rindex("/")-1]+".so")
139 body['extensions'].clear()
141 if ARGV[0] == "build"
142 body['test_files'].clear()
144 # We don't want ext/ in require_paths, it will only contain content for
145 # building extensions which needs to be installed in sitearchdir anyways..
147 for i in 0..body['require_paths'].size()-1
148 if body['require_paths'][idx].match("^ext(/|$)")
149 body['require_paths'].delete_at(idx)
155 # We'll get rid of all the files we don't really need to install
157 for i in 0..body['files'].size()-1
158 if filter and body['files'][idx].match(filter)
162 for path in body['require_paths']
163 if body['files'][idx].match("^%s/" % path)
169 body['files'].delete_at(idx)
175 spec = Gem::Specification.from_yaml(YAML.dump(header))
177 Gem::Builder.new(spec).build
179 files.concat(spec.files)
180 print "%s\n" % files.join("\n")