]> git.pld-linux.org Git - packages/rpm.git/blob - gem_helper.rb
apply gem_helper-spec-arg.patch to gem_helper.rb
[packages/rpm.git] / gem_helper.rb
1 #!/usr/bin/env ruby
2 #--
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).
6 #++
7
8 require 'optparse'
9 require 'rubygems'
10
11 if ARGV[0] == "build" or ARGV[0] == "install" or ARGV[0] == "spec"
12   require 'yaml'
13   require 'zlib'
14
15   filter = nil
16   opts = nil
17   keepcache = false
18   fixperms = false
19   gemdir = nil
20   dry_run = false
21   files = []
22   argv = ARGV[1..-1]
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]
26   if ARGV[0] == "build"
27     opts = OptionParser.new("#{$0} <--filter PATTERN>")
28     opts.on("-f", "--filter PATTERN", "Filter pattern to use for gem files") do |val|
29       filter = val
30     end
31     opts.on("-j", "--jobs JOBS", "Number  of  jobs to run simultaneously.") do |val|
32       ENV['jobs'] = "-j"+val
33     end
34     opts.on("--dry-run", "Only show the files the gem will include") do
35       ARGV.delete("--dry-run")
36       dry_run = true
37     end
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")
42       keepcache = true
43     end
44     opts.on("--fix-permissions", "Force standard permissions for files installed") do
45       ARGV.delete("--fix-permissions")
46       fixperms = true
47     end    
48     opts.on("-i", "--install-dir GEMDIR", "Gem repository directory") do |val|
49       gemdir = val
50     end
51   end
52   while argv.length > 0
53     begin
54       opts.parse!(argv)
55     rescue OptionParser::InvalidOption => e
56       e.recover(argv)
57     end
58     argv.delete_at(0)
59   end
60
61   file_data = Zlib::GzipReader.open("metadata.gz")
62   header = YAML::load(file_data)
63   file_data.close()
64   body = header.instance_variable_get :@ivars
65
66   spec = Gem::Specification.from_yaml(YAML.dump(header))
67
68   if ARGV[0] == "spec"
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
73     end
74     print "Wrote: %s\n" % file_name
75     exit(0)
76   end
77
78   if ARGV[0] == "install"
79     system("gem %s %s.gem" % [ARGV.join(' '), spec.full_name])
80     if !keepcache
81       require 'fileutils'
82       FileUtils.rm_rf("%s/cache" % gemdir)
83     end
84     if fixperms
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
87       system("%s" % chmod)
88       print "\n"
89     end
90   end
91
92   if body['extensions'].size > 0
93     require 'rubygems/ext'
94     module Gem::Ext
95       class Builder
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'
100           end
101           cmd = make_program
102           if ENV['GEM_MODE'] == "build"
103             cmd += " %s" % ENV['jobs']
104           elsif ENV['GEM_MODE'] == "install"
105             cmd += " DESTDIR='%s' install" % ENV['DESTDIR']
106           end
107           results << cmd
108           results << `#{cmd} #{redirector}`
109
110           raise Gem::ExtensionBuildError, "make failed:\n\n#{results}" unless
111           $?.success?
112         end
113       end
114     end
115
116     require 'rubygems/installer'
117     module Gem
118       class Installer
119         def initialize(spec, options={})
120           @gem_dir = Dir.pwd
121           @spec = spec
122         end
123       end
124       class ConfigFile
125         def really_verbose
126           true
127         end
128       end
129     end
130
131     unless dry_run
132       Gem::Installer.new(spec).build_extensions
133     else
134       for ext in body['extensions']
135         files.push(ext[0..ext.rindex("/")-1]+".so")
136       end
137     end
138
139     body['extensions'].clear()
140   end
141   if ARGV[0] == "build"
142     body['test_files'].clear()
143
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..
146     idx = 0
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)
150       else
151         idx += 1
152       end
153     end
154
155     # We'll get rid of all the files we don't really need to install
156     idx = 0
157     for i in 0..body['files'].size()-1
158       if filter and body['files'][idx].match(filter)
159         match = true
160       else
161         match = false
162         for path in body['require_paths']
163           if body['files'][idx].match("^%s/" % path)
164             match = true
165           end
166         end
167       end
168       if !match
169         body['files'].delete_at(idx)
170       else
171         idx += 1
172       end
173     end
174
175     spec = Gem::Specification.from_yaml(YAML.dump(header))
176     unless dry_run
177       Gem::Builder.new(spec).build
178     else
179       files.concat(spec.files)
180       print "%s\n" % files.join("\n")
181     end
182   end
183 end
This page took 0.0782 seconds and 4 git commands to generate.