-
Notifications
You must be signed in to change notification settings - Fork 0
/
gemgem.rb
340 lines (281 loc) · 8.14 KB
/
gemgem.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
module Gemgem
class << self
attr_accessor :dir, :spec, :submodules, :spec_create
end
module_function
def gem_tag ; "#{spec.name}-#{spec.version}" ; end
def gem_path ; "#{pkg_dir}/#{gem_tag}.gem" ; end
def spec_path ; "#{dir}/#{spec.name}.gemspec" ; end
def pkg_dir ; "#{dir}/pkg" ; end
def escaped_dir; @escaped_dir ||= Regexp.escape(dir); end
def init dir, options={}, &block
self.dir = dir
ENV['RUBYLIB'] = "#{dir}/lib:#{ENV['RUBYLIB']}"
ENV['PATH'] = "#{dir}/bin:#{ENV['PATH']}"
self.submodules = options[:submodules] || []
self.spec_create = block
$LOAD_PATH.unshift("#{dir}/lib", *submodules_libs)
end
def create
spec = Gem::Specification.new do |s|
s.authors = ['Lin Jen-Shin (godfat)']
s.email = ['godfat (XD) godfat.org']
s.description = description.join
s.summary = description.first
s.license = license
s.date = Time.now.strftime('%Y-%m-%d')
s.files = gem_files
s.test_files = test_files
s.executables = bin_files
end
spec_create.call(spec)
spec.homepage ||= "https://github.com/godfat/#{spec.name}"
self.spec = spec
end
def gem_install
require 'rubygems/commands/install_command'
require 'rubygems/package'
# read ~/.gemrc
Gem.use_paths(Gem.configuration[:gemhome], Gem.configuration[:gempath])
Gem::Command.extra_args = Gem.configuration[:gem]
# setup install options
cmd = Gem::Commands::InstallCommand.new
cmd.handle_options([])
# install
gem_package = Gem::Package.new(gem_path)
install = Gem::Installer.new(gem_package, cmd.options)
install.install
puts "\e[35mGem installed: \e[33m#{strip_path(install.gem_dir)}\e[0m"
end
def gem_spec
create
write
end
def gem_build
require 'fileutils'
require 'rubygems/package'
gem = nil
Dir.chdir(dir) do
gem = Gem::Package.build(Gem::Specification.load(spec_path))
FileUtils.mkdir_p(pkg_dir)
FileUtils.mv(gem, pkg_dir) # gem is relative path, but might be ok
end
puts "\e[35mGem built: \e[33m#{strip_path("#{pkg_dir}/#{gem}")}\e[0m"
end
def gem_release
sh_git('tag', gem_tag)
sh_git('push')
sh_git('push', '--tags')
sh_gem('push', gem_path)
end
def gem_check
unless git('status', '--porcelain').empty?
puts("\e[35mWorking copy is not clean.\e[0m")
exit(3)
end
ver = spec.version.to_s
if ENV['VERSION'].nil?
puts("\e[35mExpected " \
"\e[33mVERSION\e[35m=\e[33m#{ver}\e[0m")
exit(1)
elsif ENV['VERSION'] != ver
puts("\e[35mExpected \e[33mVERSION\e[35m=\e[33m#{ver} " \
"\e[35mbut got\n " \
"\e[33mVERSION\e[35m=\e[33m#{ENV['VERSION']}\e[0m")
exit(2)
end
end
def test
return if test_files.empty?
if ENV['COV'] || ENV['CI']
require 'simplecov'
if ENV['CI']
begin
require 'coveralls'
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
rescue LoadError => e
puts "Cannot load coveralls, skip: #{e}"
end
end
SimpleCov.start do
add_filter('test/')
add_filter('test.rb')
submodules_libs.each(&method(:add_filter))
end
end
test_files.each{ |file| require "#{dir}/#{file[0..-4]}" }
end
def clean
return if ignored_files.empty?
require 'fileutils'
trash = File.expand_path("~/.Trash/#{spec.name}")
puts "Move the following files into: \e[35m#{strip_path(trash)}\e[33m"
ignored_files.each do |file|
from = "#{dir}/#{file}"
to = "#{trash}/#{File.dirname(file)}"
puts strip_path(from)
FileUtils.mkdir_p(to)
FileUtils.mv(from, to)
end
print "\e[0m"
end
def write
File.open(spec_path, 'w'){ |f| f << split_lines(spec.to_ruby) }
end
def split_lines ruby
ruby.gsub(/(.+?)\s*=\s*\[(.+?)\]/){ |s|
if $2.index(',')
"#{$1} = [\n #{$2.split(',').map(&:strip).join(",\n ")}]"
else
s
end
}
end
def strip_path path
strip_home_path(strip_cwd_path(path))
end
def strip_home_path path
path.sub(/\A#{Regexp.escape(ENV['HOME'])}\//, '~/')
end
def strip_cwd_path path
path.sub(/\A#{Regexp.escape(Dir.pwd)}\//, '')
end
def submodules_libs
submodules.map{ |path| "#{dir}/#{path}/lib" }
end
def git *args
`git --git-dir=#{dir}/.git #{args.join(' ')}`
end
def sh_git *args
Rake.sh('git', "--git-dir=#{dir}/.git", *args)
end
def sh_gem *args
Rake.sh(Gem.ruby, '-S', 'gem', *args)
end
def glob path=dir
Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)
end
def readme
@readme ||=
if (path = "#{Gemgem.dir}/README.md") && File.exist?(path)
ps = "##{File.read(path)}".
scan(/((#+)[^\n]+\n\n.+?(?=(\n\n\2[^#\n]+\n)|\Z))/m).map(&:first)
ps.inject('HEADER' => ps.first){ |r, s, i|
r[s[/\w+/]] = s
r
}
else
{}
end
end
def description
# JRuby String#lines is returning an enumerator
@description ||= (readme['DESCRIPTION']||'').sub(/.+\n\n/, '').lines.to_a
end
def license
readme['LICENSE'].sub(/.+\n\n/, '').lines.first.
split(/[()]/).map(&:strip).reject(&:empty?).last
end
def all_files
@all_files ||= fold_files(glob).sort
end
def fold_files files
files.inject([]){ |r, path|
if File.file?(path) && path !~ %r{/\.git(/|$)} &&
(rpath = path[%r{^#{escaped_dir}/(.*$)}, 1])
r << rpath
elsif File.symlink?(path) # walk into symlinks...
r.concat(fold_files(glob(File.expand_path(path,
File.readlink(path)))))
else
r
end
}
end
def gem_files
@gem_files ||= all_files.reject{ |f|
f =~ submodules_pattern ||
(f =~ ignored_pattern && !git_files.include?(f))
}
end
def test_files
@test_files ||= gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
end
def bin_files
@bin_files ||= gem_files.grep(%r{^bin/}).map{ |f| File.basename(f) }
end
def git_files
@git_files ||= if File.exist?("#{dir}/.git")
git('ls-files').split("\n")
else
[]
end
end
def ignored_files
@ignored_files ||= all_files.grep(ignored_pattern)
end
def ignored_pattern
@ignored_pattern ||= if gitignore.empty?
/^$/
else
Regexp.new(expand_patterns(gitignore).join('|'))
end
end
def submodules_pattern
@submodules_pattern ||= if submodules.empty?
/^$/
else
Regexp.new(submodules.map{ |path|
"^#{Regexp.escape(path)}/" }.join('|'))
end
end
def expand_patterns pathes
# http://git-scm.com/docs/gitignore
pathes.flat_map{ |path|
# we didn't implement negative pattern for now
Regexp.escape(path).sub(%r{^/}, '^').gsub(/\\\*/, '[^/]*')
}
end
def gitignore
@gitignore ||= if File.exist?(path = "#{dir}/.gitignore")
File.read(path).lines.
reject{ |l| l == /^\s*(#|\s+$)/ }.map(&:strip)
else
[]
end
end
end
namespace :gem do
desc 'Install gem'
task :install => [:build] do
Gemgem.gem_install
end
desc 'Build gem'
task :build => [:spec] do
Gemgem.gem_build
end
desc 'Generate gemspec'
task :spec do
Gemgem.gem_spec
end
desc 'Release gem'
task :release => [:spec, :check, :build] do
Gemgem.gem_release
end
task :check do
Gemgem.gem_check
end
end # of gem namespace
desc 'Run tests'
task :test do
Gemgem.test
end
desc 'Trash ignored files'
task :clean => ['gem:spec'] do
Gemgem.clean
end
task :default do
# Is there a reliable way to do this in the current process?
# It failed miserably before between Rake versions...
exec "#{Gem.ruby} -S #{$PROGRAM_NAME} -f #{Rake.application.rakefile} -T"
end