Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 163 lines (134 sloc) 5.73 KB
#!/usr/bin/ruby
module Fusuma
require 'time'
require 'fileutils'
APP_NAME = 'fusuma'
VERSION = '0.1.0'
class Build
BUILD_TIME = Time.now.iso8601
############################################################################
# the project and build heirarchy
PROJECT_ROOT = File.dirname(__FILE__)
DEFAULT_CONFIGURATION = File.join(PROJECT_ROOT, 'default_configuration.json')
PROJECT_LICENSE = File.join(PROJECT_ROOT, 'LICENSE.txt')
BUILD_ROOT = File.join(PROJECT_ROOT, 'build')
BUILD_DIR = File.join(BUILD_ROOT, BUILD_TIME)
OSX_APP_DIR = File.join(BUILD_DIR, "#{APP_NAME}.app")
CONTENTS_DIR = File.join(OSX_APP_DIR, 'Contents')
BUILD_STAMP_FILE = File.join(CONTENTS_DIR, 'build')
FRAMEWORKS_DIR = File.join(CONTENTS_DIR, 'Frameworks')
# MACRUBY_DIR = File.join(FRAMEWORKS_DIR, 'MacRuby.framework') # TODO: embed MacRuby
INFO_PLIST_FILE = File.join(CONTENTS_DIR, 'Info.plist')
MACOS_DIR = File.join(CONTENTS_DIR, 'MacOS')
EXECUTABLE_FILE = File.join(MACOS_DIR, APP_NAME)
PROFILE_DIR = File.join(CONTENTS_DIR, 'Profile')
RESOURCES_DIR = File.join(CONTENTS_DIR, 'Resources')
LICENSE_FILE = File.join(PROJECT_ROOT, 'LICENSE.txt')
APP_BIN_DIR = File.join(RESOURCES_DIR, 'bin')
CONF_DIR = File.join(RESOURCES_DIR, 'conf')
CONFIGURATION = File.join(CONF_DIR, 'configuration.json')
# ICON_FILE = File.join(RESOURCES_DIR, "#{APP_NAME}.icns") # TODO: make icon
APP_LIB_DIR = File.join(RESOURCES_DIR, 'lib')
VERSION_FILE = File.join(APP_LIB_DIR, 'version.rb')
LOG_DIR = File.join(RESOURCES_DIR, 'log')
APP_VENDOR_DIR = File.join(RESOURCES_DIR, 'vendor')
VERSION_PLIST_FILE = File.join(CONTENTS_DIR, 'version.plist')
# ICON_CHECK_FILE = File.join(OSX_APP_DIR, 'Icon?')
PROJECT_SOURCE_DIR = File.join(PROJECT_ROOT, 'source')
SOURCE_BIN_DIR = File.join(PROJECT_SOURCE_DIR, 'bin')
SOURCE_CONF_DIR = File.join(PROJECT_SOURCE_DIR, 'conf')
SOURCE_LIB_DIR = File.join(PROJECT_SOURCE_DIR, 'lib')
SOURCE_VENDOR_DIR = File.join(PROJECT_SOURCE_DIR, 'vendor')
############################################################################
BUILD_STAMP_CONTENTS = BUILD_TIME
LICENSE_CONTENTS = File.open(PROJECT_LICENSE).read
INFO_PLIST_CONTENTS = <<-TEXT
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>#{APP_NAME}</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSUIElement</key>
<true/>
</dict>
</plist>
TEXT
EXECUTABLE_CONTENTS = <<-TEXT
#!/bin/sh
cd "`dirname $0`/../Resources"
/usr/local/bin/macruby bin/#{APP_NAME}
TEXT
VERSION_PLIST_CONTENTS = <<-TEXT
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleShortVersionString</key>
<string>#{VERSION}</string>
<key>CFBundleVersion</key>
<string>#{VERSION}</string>
<key>ProjectName</key>
<string>#{APP_NAME}</string>
</dict>
</plist>
TEXT
VERSION_CONTENTS = <<-TEXT
module #{APP_NAME.capitalize}
VERSION = "#{VERSION}"
end
TEXT
def create_directories
# FileUtils.makedirs will create all parent directories needed
directories = [FRAMEWORKS_DIR, MACOS_DIR, PROFILE_DIR, CONF_DIR, LOG_DIR]
directories.each do |directory|
FileUtils.makedirs directory
end
end
def copy_source
# each source directory constant is paired with a target app directory
# ex. SOURCE_BIN_DIR is paired with APP_BIN_DIR
source = %w(BIN LIB VENDOR)
source.each do |directory|
values = %w(SOURCE APP).each
copy_dir { Build.const_get "#{values.next}_#{directory}_DIR" }
end
end
def copy_dir
FileUtils.copy_entry(yield, yield)
end
def create_files
# each file name constant is paired with a file contents constant.
# ex. EXECUTABLE_FILE is paired with EXECUTABLE_CONTENTS
files = %w(BUILD_STAMP INFO_PLIST EXECUTABLE LICENSE VERSION_PLIST VERSION)
files.each do |file|
values = %w(FILE CONTENTS).each
write_file { Build.const_get "#{file}_#{values.next}" }
end
File.chmod(0766, EXECUTABLE_FILE) # make app launcher executable
end
def write_file
File.open(yield, 'w') { |f| f.write yield }
end
def configure_with(configuration)
File.open(CONFIGURATION, 'w') do |file|
file.write File.open(configuration).read
end
end
def initialize(configuration)
create_directories
copy_source
create_files
configure_with(configuration)
puts "#{APP_NAME} has been created and saved to #{OSX_APP_DIR}."
rescue => error
puts error.message
puts error.backtrace.join("\n")
end
end
end
configuration = ARGV.shift
configuration ||= Fusuma::Build::DEFAULT_CONFIGURATION # use the default configuration if none given
Fusuma::Build.new(configuration)