Skip to content

Rakeify rake tasks #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
327 changes: 59 additions & 268 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,322 +1,113 @@
require 'fileutils'

MRUBY_VERSION="1.2.0"

file :mruby do
APP_NAME = ENV.fetch "APP_NAME", "mruby-cli"
APP_ROOT = ENV.fetch "APP_ROOT", Dir.pwd

def expand_and_set(env_name, default)
unexpanded = ENV.fetch env_name, default

expanded = File.expand_path unexpanded

ENV[env_name] = expanded
end

# avoid redefining constants in mruby Rakefile
mruby_root = expand_and_set "MRUBY_ROOT", "#{APP_ROOT}/mruby"
mruby_config = expand_and_set "MRUBY_CONFIG", "build_config.rb"

directory mruby_root 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")
mv "mruby-#{MRUBY_VERSION}", mruby_root
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)
Dir.chdir(mruby_root)
load "#{mruby_root}/Rakefile"
task :mruby => mruby_root

load File.join(File.expand_path(File.dirname(__FILE__)), "mrbgem.rake")
mruby_rakefile = "#{mruby_root}/Rakefile"

current_gem = MRuby::Gem.current
app_version = MRuby::Gem.current.version
APP_VERSION = (app_version.nil? || app_version.empty?) ? "unknown" : app_version
file mruby_rakefile => mruby_root
import mruby_rakefile

desc "compile all the binaries"
task :compile => [:all] 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
test_rakefile = "tasks/test.rake"
file test_rakefile => mruby_rakefile
import test_rakefile

namespace :test do
desc "run mruby & unit tests"
# only build mtest for host
task :mtest => :compile 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
task :app_version do
load "mrbgem.rake"

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
current_gem = MRuby::Gem.current
app_version = MRuby::Gem.current.version
APP_VERSION = (app_version.nil? || app_version.empty?) ? "unknown" : app_version
end

desc "run integration tests"
task :bintest => :compile do
MRuby.each_target do |target|
clean_env(%w(MRUBY_ROOT MRUBY_CONFIG)) do
run_bintest if target.bintest_enabled?
end
end
desc "compile all the binaries"
task :compile => [:all] do
%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

desc "run all tests"
Rake::Task['test'].clear
task :test => ['test:bintest', 'test:mtest']

desc "cleanup"
task :clean do
sh "rake deep_clean"
end
task :clean

desc "generate a release tarball"
task :release => :compile do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

considering what you did with other long tasks, I think I'll extract that one too. For now, we can keep as it, there are already enough changed.

require 'tmpdir'

# since we're in the mruby/
release_dir = "releases/v#{APP_VERSION}"
release_path = Dir.pwd + "/../#{release_dir}"
release_path = File.expand_path "releases/v#{APP_VERSION}"
Copy link
Collaborator

@toch toch Jul 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another good consequence :)
no more headache due to the surprising working directory

app_name = "#{APP_NAME}-#{APP_VERSION}"
FileUtils.mkdir_p(release_path)
mkdir_p release_path

Dir.mktmpdir do |tmp_dir|
Dir.chdir(tmp_dir) do
cd 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)
mkdir_p name
cp bin, name

Dir.chdir(arch) do
cd arch do
arch_release = "#{app_name}-#{arch}"
puts "Writing #{release_dir}/#{arch_release}.tgz"
puts "Writing #{release_path}/#{arch_release}.tgz"
`tar czf #{release_path}/#{arch_release}.tgz *`
end
end

puts "Writing #{release_dir}/#{app_name}.tgz"
puts "Writing #{release_path}/#{app_name}.tgz"
`tar czf #{release_path}/#{app_name}.tgz *`
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
namespace :local do
desc "show version"
task :version do
puts "#{APP_NAME} #{APP_VERSION}"
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}`
task :ensure_in_docker do
unless is_in_a_docker_container? then
abort 'Not running in docker, you should type "docker-compose run <task>".'
end
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
Rake.application.tasks.each do |task|
next if task.name.start_with?("local:")
next if Rake::FileTask === task

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
task task.name => "local:ensure_in_docker"
end unless ENV["MRUBY_CLI_LOCAL"]

end
file "tasks/package.rake" => :app_version

desc "create all packages"
task :package => ["package:deb", "package:rpm", "package:msi", "package:dmg"]
import "tasks/package.rake"

Loading