require 'pathname'
require 'fileutils'
require 'time'
require 'erb'
require 'rake/testtask'
require 'pp'
APP_SHORT_NAME = defined?(MACRUBY_VERSION) ? 'MRLimeChat' : 'LimeChat'
APP_NAME = APP_SHORT_NAME + '.app'
ROOT_PATH = Pathname.new(__FILE__).dirname
APP_BUILD_PATH = ROOT_PATH + 'build/Release' + APP_NAME
DOC_PATH = ROOT_PATH + 'doc'
PACKAGES_PATH = ROOT_PATH + 'Packages'
APPCAST_TEMPLATE_PATH = ROOT_PATH + 'etc/appcast_template.rxml'
WEB_PATH = ROOT_PATH + 'web'
APPCAST_PATH = WEB_PATH + 'limechat_appcast.xml'
TMP_PATH = Pathname.new("/tmp/#{APP_SHORT_NAME}_build_image")
task :default => :build
task :clean do |t|
sh "rm -rf build"
end
task :build do |t|
build('10.5')
end
task :package => [:package_app] do |t|
end
task :package_app => :clean do |t|
sdk = '10.5'
build(sdk)
package
end
task :package_source do |t|
package_source
end
task :appcast do |t|
package_fname = "#{APP_SHORT_NAME}_#{app_version}.tbz"
package_path = PACKAGES_PATH + package_fname
stat = File.stat(package_path)
version = app_version
fsize = stat.size
ftime = stat.mtime.rfc2822
updates = parse_commit_log
APPCAST_PATH.rmtree
e = ERB.new(File.open(APPCAST_TEMPLATE_PATH).read, nil, '-')
s = e.result(binding)
File.open(APPCAST_PATH, 'w') do |f|
f.write(s)
end
sh "mate #{WEB_PATH}"
end
Rake::TestTask.new do |t|
t.test_files = FileList['test/**/*_test.rb']
end
def build(sdk)
sh "xcodebuild -project #{APP_SHORT_NAME}.xcodeproj -target #{APP_SHORT_NAME} -configuration Release -sdk macosx#{sdk} build"
end
def embed_framework(sdk)
sh %Q|/usr/bin/ruby -r etc/package_builder -e "PackageBuilder.build('#{APP_BUILD_PATH}', '#{sdk}')"|
end
def package
package_path = PACKAGES_PATH + "#{APP_SHORT_NAME}_#{app_version}.tbz"
package_path.rmtree
TMP_PATH.rmtree
TMP_PATH.mkpath
APP_BUILD_PATH.cptree(TMP_PATH)
DOC_PATH.cptree(TMP_PATH)
rmglob(TMP_PATH + '**/ChangeLog.txt')
rmglob(TMP_PATH + '**/.DS_Store')
Dir.chdir(TMP_PATH) do
sh "tar jcf #{package_path} *"
end
TMP_PATH.rmtree
end
def package_source
source_package_path = PACKAGES_PATH + "#{APP_SHORT_NAME}_#{app_version}_src.tbz"
source_package_path.rmtree
TMP_PATH.rmtree
ROOT_PATH.cptree(TMP_PATH)
rmglob(TMP_PATH + 'build')
rmglob(TMP_PATH + 'etc')
rmglob(TMP_PATH + 'Packages')
rmglob(TMP_PATH + 'script')
rmglob(TMP_PATH + 'web')
rmglob(TMP_PATH + '*.tmproj')
rmglob(TMP_PATH + 'MRLimeChat.xcodeproj')
rmglob(TMP_PATH + 'LimeChat.xcodeproj/*.mode1*')
rmglob(TMP_PATH + 'LimeChat.xcodeproj/*.pbxuser')
rmglob(TMP_PATH + '**/*.tm_build_errors')
rmglob(TMP_PATH + '**/.gitignore')
rmglob(TMP_PATH + '**/.DS_Store')
rmglob(TMP_PATH + '**/*~.nib')
rmglob(TMP_PATH + '**/._*')
Dir.chdir(TMP_PATH) do
sh "tar jcf #{source_package_path} *"
end
TMP_PATH.rmtree
end
class CommitLog
attr_accessor :hash, :merge, :author, :date
attr_reader :lines
def initialize
@lines = []
end
def add_line(line)
@lines << line
end
def release_version
ary = @lines.select {|e| e =~ /^released (\d+\.\d+)$/i }
if ary
$1
else
nil
end
end
def one_line
s = ''
@lines.each do |e|
s << e
s << ' '
end
s.chop
end
def inspect
"<CommitLog #{hash[0...6]} #{author} #{date}>"
end
end
def parse_commit_log
updates = []
commit = nil
log = `git log`
log.each_line do |s|
s.chomp!
if s =~ /^commit\s+/
if commit
updates << commit
end
commit = CommitLog.new
commit.hash = $~.post_match
elsif s =~ /^Author:\s*/
commit.author = $~.post_match
elsif s =~ /^Date:\s*/
commit.date = $~.post_match
elsif s =~ /^Merge:\s*/
commit.merge = $~.post_match
elsif s =~ /^\s*$/
;
elsif s =~ /^\s+/
commit.add_line($~.post_match)
end
end
updates << commit
ver = app_version
first = 0
last = 0
updates.each_with_index do |e,i|
rel = e.release_version
if rel
if rel == ver
first = i + 1
else
last = i
break
end
end
end
updates = updates[first...last]
updates.map {|e| e.one_line }
end
module Util
def app_version
file = ROOT_PATH + 'Info.plist'
file.open do |f|
next_line = false
while s = f.gets
if next_line
next_line = false
if s =~ /<string>(.+)<\/string>/
return $1
end
elsif s =~ /<key>CFBundleShortVersionString<\/key>/
next_line = true
end
end
end
nil
end
def rmglob(path)
FileUtils.rm_rf(Dir.glob(path.to_s))
end
end
include Util
class Pathname
def rmtree
FileUtils.rm_rf(to_s)
end
def cptree(to)
FileUtils.cp_r(to_s, to.to_s)
end
end