-
Notifications
You must be signed in to change notification settings - Fork 29
/
Rakefile
331 lines (286 loc) · 9.86 KB
/
Rakefile
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
require 'fileutils'
MRUBY_VERSION="1.2.0"
file :mruby do
#sh "git clone --depth=1 https://github.com/mruby/mruby"
sh "curl -L --fail --retry 3 --retry-delay 1 https://github.com/mruby/mruby/archive/#{MRUBY_VERSION}.tar.gz -s -o - | tar zxf -"
FileUtils.mv("mruby-#{MRUBY_VERSION}", "mruby")
end
APP_NAME=ENV["APP_NAME"] || "mruby-cli"
APP_ROOT=ENV["APP_ROOT"] || Dir.pwd
# avoid redefining constants in mruby Rakefile
mruby_root=File.expand_path(ENV["MRUBY_ROOT"] || "#{APP_ROOT}/mruby")
mruby_config=File.expand_path(ENV["MRUBY_CONFIG"] || "build_config.rb")
ENV['MRUBY_ROOT'] = mruby_root
ENV['MRUBY_CONFIG'] = mruby_config
Rake::Task[:mruby].invoke unless Dir.exist?(mruby_root)
load "#{mruby_root}/Rakefile"
load File.join(File.expand_path(File.dirname(__FILE__)), "mrbgem.rake")
current_gem = MRuby::Gem.current
app_version = MRuby::Gem.current.version
APP_VERSION = (app_version.nil? || app_version.empty?) ? "unknown" : app_version
desc "compile all the binaries"
task :compile => [:all] do
Dir.chdir(mruby_root) do
MRuby.each_target do |target|
`#{target.cc.command} --version`
abort("Command #{target.cc.command} for #{target.name} is missing.") unless $?.success?
end
%W(#{mruby_root}/build/x86_64-pc-linux-gnu/bin/#{APP_NAME} #{mruby_root}/build/i686-pc-linux-gnu/#{APP_NAME}).each do |bin|
sh "strip --strip-unneeded #{bin}" if File.exist?(bin)
end
end
end
namespace :test do
desc "run mruby & unit tests"
# only build mtest for host
task :mtest => :compile do
Dir.chdir(mruby_root) do
# in order to get mruby/test/t/synatx.rb __FILE__ to pass,
# we need to make sure the tests are built relative from mruby_root
MRuby.each_target do |target|
# only run unit tests here
target.enable_bintest = false
run_test if target.test_enabled?
end
end
end
def clean_env(envs)
old_env = {}
envs.each do |key|
old_env[key] = ENV[key]
ENV[key] = nil
end
yield
envs.each do |key|
ENV[key] = old_env[key]
end
end
desc "run integration tests"
task :bintest => :compile do
Dir.chdir(mruby_root) do
MRuby.each_target do |target|
clean_env(%w(MRUBY_ROOT MRUBY_CONFIG)) do
run_bintest if target.bintest_enabled?
end
end
end
end
end
desc "run all tests"
Rake::Task['test'].clear
task :test => ['test:bintest', 'test:mtest']
desc "cleanup"
task :clean do
Dir.chdir(mruby_root) do
sh "rake deep_clean"
end
end
desc "generate a release tarball"
task :release => :compile do
require 'tmpdir'
Dir.chdir(mruby_root) do
# since we're in the mruby/
release_dir = "releases/v#{APP_VERSION}"
release_path = Dir.pwd + "/../#{release_dir}"
app_name = "#{APP_NAME}-#{APP_VERSION}"
FileUtils.mkdir_p(release_path)
Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir) do
MRuby.each_target do |target|
next if name == "host"
arch = name
bin = "#{build_dir}/bin/#{exefile(APP_NAME)}"
FileUtils.mkdir_p(name)
FileUtils.cp(bin, name)
Dir.chdir(arch) do
arch_release = "#{app_name}-#{arch}"
puts "Writing #{release_dir}/#{arch_release}.tgz"
`tar czf #{release_path}/#{arch_release}.tgz *`
end
end
puts "Writing #{release_dir}/#{app_name}.tgz"
`tar czf #{release_path}/#{app_name}.tgz *`
end
end
end
end
namespace :local do
desc "show version"
task :version do
puts "#{APP_NAME} #{APP_VERSION}"
end
end
def is_in_a_docker_container?
`grep -q docker /proc/self/cgroup`
$?.success?
end
Rake.application.tasks.each do |task|
next if ENV["MRUBY_CLI_LOCAL"]
unless task.name.start_with?("local:")
# Inspired by rake-hooks
# https://github.com/guillermo/rake-hooks
old_task = Rake.application.instance_variable_get('@tasks').delete(task.name)
desc old_task.full_comment
task old_task.name => old_task.prerequisites do
abort("Not running in docker, you should type \"docker-compose run <task>\".") \
unless is_in_a_docker_container?
old_task.invoke
end
end
end
namespace :package do
require 'fileutils'
require 'tmpdir'
version = APP_VERSION
release_dir = "releases/v#{version}"
package_dir = "packages/v#{version}"
release_path = Dir.pwd + "/../#{release_dir}"
package_path = Dir.pwd + "/../#{package_dir}"
FileUtils.mkdir_p(package_path)
def check_fpm_installed?
`gem list -i fpm`.chomp == "true"
end
def check_msi_installed?
`wixl --version`
$?.success?
end
def check_dmg_installed?
`genisoimage --version`
$?.success?
end
def wxs_content(version, arch)
arch_wxs = case arch
when "x86_64"
{
string: "64-bit",
program_files_folder: "ProgramFiles64Folder",
define: "<?define Win64 = \"yes\"?>"
}
else
{
string: "32-bit",
program_files_folder: "ProgramFilesFolder",
define: "<?define Win64 = \"no\"?>"
}
end
<<-EOF
<?xml version='1.0' encoding='utf-8'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
#{arch_wxs[:define]}
<Product
Name='mruby-cli #{arch_wxs[:string]}'
Id='F43E56B6-5FF2-450C-B7B7-0B12BF066ABD'
Version='#{version}'
Language='1033'
Manufacturer='mruby-cli'
UpgradeCode='12268671-59a0-42d3-b1f2-79e52b5657a6'
>
<Package InstallerVersion="200" Compressed="yes" Comments="comments" InstallScope="perMachine"/>
<Media Id="1" Cabinet="cabinet.cab" EmbedCab="yes"/>
<Directory Id='TARGETDIR' Name='SourceDir'>
<Directory Id='#{arch_wxs[:program_files_folder]}' Name='PFiles'>
<Directory Id='INSTALLDIR' Name='mruby-cli'>
<Component Id='MainExecutable' Guid='3DCA4C4D-205C-4FA4-8BB1-C0BF41CA5EFA'>
<File Id='mruby-cliEXE' Name='mruby-cli.exe' DiskId='1' Source='mruby-cli.exe' KeyPath='yes'/>
</Component>
</Directory>
</Directory>
</Directory>
<Feature Id='Complete' Level='1'>
<ComponentRef Id='MainExecutable' />
</Feature>
</Product>
</Wix>
EOF
end
def info_plist_content(version, arch)
<<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>mruby-cli</string>
<key>CFBundleGetInfoString</key>
<string>mruby-cli #{version} #{arch}</string>
<key>CFBundleName</key>
<string>mruby-cli</string>
<key>CFBundleIdentifier</key>
<string>mruby-cli</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>#{version}</string>
<key>CFBundleSignature</key>
<string>mrbc</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
</dict>
</plist>
EOF
end
def osx_setup_bash_path_script
<<-EOF
#!/bin/bash
echo "export PATH=$PATH:/Applications/mruby-cli.app/Contents/MacOs" >> $HOME/.bash_profile
source $HOME/.bash_profile
EOF
end
def log(package_dir, version, package)
puts "Writing packages #{package_dir}/#{version}/#{package}"
end
desc "create deb package"
task :deb => [:release] do
abort("fpm is not installed. Please check your docker install.") unless check_fpm_installed?
["x86_64", "i686"].each do |arch|
release_tar_file = "mruby-cli-#{version}-#{arch}-pc-linux-gnu.tgz"
arch_name = (arch == "x86_64" ? "amd64" : arch)
log(package_dir, version, "mruby-cli_#{version}_#{arch_name}.deb")
`fpm -s tar -t deb -a #{arch} -n mruby-cli -v #{version} --prefix /usr/bin -p #{package_path} #{release_path}/#{release_tar_file}`
end
end
desc "create rpm package"
task :rpm => [:release] do
abort("fpm is not installed. Please check your docker install.") unless check_fpm_installed?
["x86_64", "i686"].each do |arch|
release_tar_file = "mruby-cli-#{version}-#{arch}-pc-linux-gnu.tgz"
log(package_dir, version, "mruby-cli-#{version}-1.#{arch}.rpm")
`fpm -s tar -t rpm -a #{arch} -n mruby-cli -v #{version} --prefix /usr/bin -p #{package_path} #{release_path}/#{release_tar_file}`
end
end
desc "create msi package"
task :msi => [:release] do
abort("msitools is not installed. Please check your docker install.") unless check_msi_installed?
["x86_64", "i686"].each do |arch|
log(package_dir, version, "mruby-cli-#{version}-#{arch}.msi")
release_tar_file = "mruby-cli-#{version}-#{arch}-w64-mingw32.tgz"
Dir.mktmpdir do |dest_dir|
Dir.chdir dest_dir
`tar -zxf #{release_path}/#{release_tar_file}`
File.write("mruby-cli-#{version}-#{arch}.wxs", wxs_content(version, arch))
`wixl -v mruby-cli-#{version}-#{arch}.wxs && mv mruby-cli-#{version}-#{arch}.msi #{package_path}`
end
end
end
desc "create dmg package"
task :dmg => [:release] do
abort("dmg tools are not installed. Please check your docker install.") unless check_dmg_installed?
["x86_64", "i386"].each do |arch|
log(package_dir, version, "mruby-cli-#{version}-#{arch}.dmg")
release_tar_file = "mruby-cli-#{version}-#{arch}-apple-darwin14.tgz"
Dir.mktmpdir do |dest_dir|
Dir.chdir dest_dir
`tar -zxf #{release_path}/#{release_tar_file}`
FileUtils.chmod 0755, "mruby-cli"
FileUtils.mkdir_p "mruby-cli.app/Contents/MacOs"
FileUtils.mv "mruby-cli", "mruby-cli.app/Contents/MacOs"
File.write("mruby-cli.app/Contents/Info.plist", info_plist_content(version, arch))
File.write("add-mruby-cli-to-my-path.sh", osx_setup_bash_path_script)
FileUtils.chmod 0755, "add-mruby-cli-to-my-path.sh"
`genisoimage -V mruby-cli -D -r -apple -no-pad -o #{package_path}/mruby-cli-#{version}-#{arch}.dmg #{dest_dir}`
end
end
end
end
desc "create all packages"
task :package => ["package:deb", "package:rpm", "package:msi", "package:dmg"]