]>
Commit | Line | Data |
---|---|---|
63194730 ER |
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 | ||
10 | if ARGV[0] == "build" or ARGV[0] == "install" | |
11 | require 'yaml' | |
12 | require 'zlib' | |
13 | ||
14 | filter = nil | |
15 | opts = nil | |
16 | keepcache = false | |
17 | fixperms = false | |
18 | gemdir = nil | |
19 | dry_run = false | |
20 | files = [] | |
21 | argv = ARGV[1..-1] | |
22 | # Push this into some environment variables as the modified classes doesn't | |
23 | # seem to be able to access our global variables.. </lameworkaround> | |
24 | ENV['GEM_MODE'] = ARGV[0] | |
25 | if ARGV[0] == "build" | |
26 | opts = OptionParser.new("#{$0} <--filter PATTERN>") | |
27 | opts.on("-f", "--filter PATTERN", "Filter pattern to use for gem files") do |val| | |
28 | filter = val | |
29 | end | |
30 | opts.on("-j", "--jobs JOBS", "Number of jobs to run simultaneously.") do |val| | |
31 | ENV['jobs'] = "-j"+val | |
32 | end | |
33 | opts.on("--dry-run", "Only show the files the gem will include") do | |
34 | ARGV.delete("--dry-run") | |
35 | dry_run = true | |
36 | end | |
37 | elsif ARGV[0] == "install" | |
38 | opts = OptionParser.new("#{$0} <--keep-cache>") | |
39 | opts.on("--keep-cache", "Don't delete gem copy from cache") do | |
40 | ARGV.delete("--keep-cache") | |
41 | keepcache = true | |
42 | end | |
43 | opts.on("--fix-permissions", "Force standard permissions for files installed") do | |
44 | ARGV.delete("--fix-permissions") | |
45 | fixperms = true | |
46 | end | |
47 | opts.on("-i", "--install-dir GEMDIR", "Gem repository directory") do |val| | |
48 | gemdir = val | |
49 | end | |
50 | end | |
51 | while argv.length > 0 | |
52 | begin | |
53 | opts.parse!(argv) | |
54 | rescue OptionParser::InvalidOption => e | |
55 | e.recover(argv) | |
56 | end | |
57 | argv.delete_at(0) | |
58 | end | |
59 | ||
60 | file_data = Zlib::GzipReader.open("metadata.gz") | |
61 | header = YAML::load(file_data) | |
62 | file_data.close() | |
63 | body = header.instance_variable_get :@ivars | |
64 | ||
65 | require 'rubygems' | |
66 | spec = Gem::Specification.from_yaml(YAML.dump(header)) | |
67 | ||
68 | if ARGV[0] == "install" | |
69 | system("gem %s %s.gem" % [ARGV.join(' '), spec.full_name]) | |
70 | if !keepcache | |
71 | require 'fileutils' | |
72 | FileUtils.rm_rf("%s/cache" % gemdir) | |
73 | end | |
74 | if fixperms | |
75 | chmod = "chmod u+r,u+w,g-w,g+r,o+r -R %s" % gemdir | |
76 | print "\nFixing permissions:\n\n%s\n" % chmod | |
77 | system("%s" % chmod) | |
78 | print "\n" | |
79 | end | |
80 | end | |
81 | ||
82 | if body['extensions'].size > 0 | |
83 | require 'rubygems/ext' | |
84 | module Gem::Ext | |
85 | class Builder | |
86 | def self.make(dest_path, results) | |
87 | make_program = ENV['make'] | |
88 | unless make_program then | |
89 | make_program = (/mswin/ =~ RUBY_PLATFORM) ? 'nmake' : 'make' | |
90 | end | |
91 | cmd = make_program | |
92 | if ENV['GEM_MODE'] == "build" | |
93 | cmd += " %s" % ENV['jobs'] | |
94 | elsif ENV['GEM_MODE'] == "install" | |
95 | cmd += " DESTDIR='%s' install" % ENV['DESTDIR'] | |
96 | end | |
97 | results << cmd | |
98 | results << `#{cmd} #{redirector}` | |
99 | ||
100 | raise Gem::ExtensionBuildError, "make failed:\n\n#{results}" unless | |
101 | $?.success? | |
102 | end | |
103 | end | |
104 | end | |
105 | ||
106 | require 'rubygems/installer' | |
107 | module Gem | |
108 | class Installer | |
109 | def initialize(spec, options={}) | |
110 | @gem_dir = Dir.pwd | |
111 | @spec = spec | |
112 | end | |
113 | end | |
114 | class ConfigFile | |
115 | def really_verbose | |
116 | true | |
117 | end | |
118 | end | |
119 | end | |
120 | ||
121 | unless dry_run | |
122 | Gem::Installer.new(spec).build_extensions | |
123 | else | |
124 | for ext in body['extensions'] | |
125 | files.push(ext[0..ext.rindex("/")-1]+".so") | |
126 | end | |
127 | end | |
128 | ||
129 | body['extensions'].clear() | |
130 | end | |
131 | if ARGV[0] == "build" | |
132 | body['test_files'].clear() | |
133 | ||
134 | # We don't want ext/ in require_paths, it will only contain content for | |
135 | # building extensions which needs to be installed in sitearchdir anyways.. | |
136 | idx = 0 | |
137 | for i in 0..body['require_paths'].size()-1 | |
138 | if body['require_paths'][idx].match("^ext(/|$)") | |
139 | body['require_paths'].delete_at(idx) | |
140 | else | |
141 | idx += 1 | |
142 | end | |
143 | end | |
144 | ||
145 | # We'll get rid of all the files we don't really need to install | |
146 | idx = 0 | |
147 | for i in 0..body['files'].size()-1 | |
148 | if filter and body['files'][idx].match(filter) | |
149 | match = true | |
150 | else | |
151 | match = false | |
152 | for path in body['require_paths'] | |
153 | if body['files'][idx].match("^%s/" % path) | |
154 | match = true | |
155 | end | |
156 | end | |
157 | end | |
158 | if !match | |
159 | body['files'].delete_at(idx) | |
160 | else | |
161 | idx += 1 | |
162 | end | |
163 | end | |
164 | ||
165 | spec = Gem::Specification.from_yaml(YAML.dump(header)) | |
166 | unless dry_run | |
167 | Gem::Builder.new(spec).build | |
168 | else | |
169 | files.concat(spec.files) | |
170 | print "%s\n" % files.join("\n") | |
171 | end | |
172 | end | |
173 | end |