From e1be6243fd0d9f8341f1f5db979015cfa5337705 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Wed, 14 Jan 2015 21:32:44 -0700 Subject: [PATCH] Add deployment packaging task --- .gitignore | 1 + .travis.yml | 2 + Rakefile | 2 + packaging/wrapper.sh | 13 ++++ tasks/package.rake | 168 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 186 insertions(+) create mode 100755 packaging/wrapper.sh create mode 100644 tasks/package.rake diff --git a/.gitignore b/.gitignore index ae3fdc2..a49d624 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ *.o *.a mkmf.log +/distro diff --git a/.travis.yml b/.travis.yml index b05af60..8b4bf1e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,6 @@ language: ruby +env: + - CI=true sudo: true before_install: - gem update --system diff --git a/Rakefile b/Rakefile index dbc399c..e34a365 100644 --- a/Rakefile +++ b/Rakefile @@ -1,3 +1,5 @@ +Dir[File.join(__dir__, 'tasks', '*.rake')].each { |f| load f } + require 'bundler/gem_tasks' require 'open-uri' require 'fileutils' diff --git a/packaging/wrapper.sh b/packaging/wrapper.sh new file mode 100755 index 0000000..43c6307 --- /dev/null +++ b/packaging/wrapper.sh @@ -0,0 +1,13 @@ +#!/bin/bash +set -e + +# Figure out where this script is located. +SELFDIR="`dirname \"$0\"`" +SELFDIR="`cd \"$SELFDIR\" && pwd`" + +# Tell Bundler where the Gemfile and gems are. +export BUNDLE_GEMFILE="$SELFDIR/lib/app/Gemfile" +unset BUNDLE_IGNORE_CONFIG + +# Run the actual app using the bundled Ruby interpreter. +exec "$SELFDIR/lib/ruby/bin/ruby" -rbundler/setup "$SELFDIR/lib/app/bin/octodown" "$@" diff --git a/tasks/package.rake b/tasks/package.rake new file mode 100644 index 0000000..ea1da27 --- /dev/null +++ b/tasks/package.rake @@ -0,0 +1,168 @@ +require 'octodown' +require 'fileutils' + +module PackageHelpers + def curl(file) + system "curl -L -O --fail --silent #{file} > /dev/null" + end + + def print_to_console(msg) + puts "[#{arch}]:" + ' ' * (16 - arch.size) + '=>' + ' ' + msg + end +end + +class Package + PACKAGE_NAME = 'octodown' + VERSION = Octodown::VERSION + RB_VERSION = '20141215-2.1.5' + PACKAGING_DIR = "#{Octodown.root}/packaging" + + include ::PackageHelpers + + attr_reader :arch, :dir, :tar + + def initialize(arch) + abort 'Ruby 2.1.x required' if RUBY_VERSION !~ /^2\.1\./ + + @arch = arch + @dir = "#{PACKAGE_NAME}-#{VERSION}-#{arch}" + @tar = "v#{VERSION}.tar.gz" + + build + end + + singleton_class.send :alias_method, :create, :new + + def build + initialize_install_dir + download_octodown + bundle_install + remove_unneccesary_files + install_ruby + create_executable + post_cleanup + + create_tarball unless ENV['DIR_ONLY'] + end + + private + + def post_cleanup + print_to_console 'Cleaning up...' + + files = ["packaging/traveling-ruby-#{RB_VERSION}-#{arch}.tar.gz"] + files.each do |file| + FileUtils.rm file if File.exist? file + end + end + + def create_tarball + print_to_console 'Creating tarball...' + + FileUtils.mkdir_p 'distro' + system "tar -czf distro/#{dir}.tar.gz #{dir} > /dev/null" + FileUtils.remove_dir "#{dir}", true + end + + def create_executable + print_to_console 'Creating exexutable...' + + FileUtils.cp 'packaging/wrapper.sh', "#{dir}/#{PACKAGE_NAME}" + end + + def install_ruby + print_to_console 'Installing Ruby...' + + download_runtime + FileUtils.mkdir "#{dir}/lib/ruby" + system( + "tar -xzf packaging/traveling-ruby-#{RB_VERSION}-#{arch}.tar.gz " \ + "-C #{dir}/lib/ruby " \ + '> /dev/null' + ) + end + + def remove_unneccesary_files + print_to_console 'Removing unneccesary files...' + + FileUtils.cd "#{dir}/lib/app" do + FileUtils.remove_dir '.git', true + FileUtils.remove_dir 'spec', true + end + end + + def initialize_install_dir + print_to_console 'Initializing install directory...' + + FileUtils.cd Octodown.root do + FileUtils.remove_dir(dir, true) if File.exist? dir + FileUtils.mkdir_p "#{dir}/lib/app" + end + end + + def bundle_install + print_to_console 'Running `bundle install`...' + + Bundler.with_clean_env do + FileUtils.cd "#{dir}/lib/app" do + system( + 'BUNDLE_IGNORE_CONFIG=1 bundle install ' \ + '--path vendor --without development --jobs 2 ' \ + ' > /dev/null' + ) + end + end + end + + def download_octodown + print_to_console 'Downloading octodown...' + + FileUtils.cd "#{dir}/lib/app" do + curl "https://github.com/ianks/octodown/archive/#{tar}" + system "tar --strip-components=1 -xzf #{tar} > /dev/null" + FileUtils.rm tar if File.exist? tar + end + end + + def download_runtime + print_to_console 'Downloading Travelling Ruby...' + ruby = "traveling-ruby-#{RB_VERSION}-#{arch}.tar.gz" + + FileUtils.cd PACKAGING_DIR do + unless File.exist? ruby + curl "http://d6r77u77i8pq3.cloudfront.net/releases/#{ruby}" + end + end + end +end + +desc 'Package octodown into self-contained programs' +task :package do + ['package:linux:x86', 'package:linux:x86_64', 'package:osx'].each do |task| + fork do + Rake::Task[task].invoke + exit + end + end + + Process.waitall +end + +namespace :package do + namespace :linux do + desc 'Package for Linux x86' + task :x86 do + Package.create 'linux-x86' + end + + desc 'Package for Linux x86_64' + task :x86_64 do + Package.create 'linux-x86_64' + end + end + + desc 'Package for OS X' + task :osx do + Package.create 'osx' + end +end