Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
163 lines (142 sloc) 4.42 KB
ROOT = File.expand_path('.')
ADDON = File.expand_path(File.join(ROOT))
FIREFOX = File.join(ADDON, 'firefox')
DST = File.join(ROOT, 'build')
TMP = File.join(ROOT, 'tmp')
unless defined? OSX then
OSX = PLATFORM =~ /darwin/
WIN = PLATFORM =~ /win32/
NIX = !(OSX || WIN)
end
begin
require 'term/ansicolor'
include Term::ANSIColor
rescue LoadError
raise 'Run "gem install term-ansicolor"'
end
# http://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
if WIN then
begin
require 'win32console'
include Win32::Console::ANSI
rescue LoadError
raise 'Run "gem install win32console" to use terminal colors on Windows'
end
end
def file_color(text); yellow(text); end
def dir_color(text); blue(text); end
def cmd_color(text); green(text); end
#
# you can use FileUtils: http://corelib.rubyonrails.org/classes/FileUtils.html
#
require 'find'
# copies directory tree without .svn, .git and other temporary files
def cp_dir(src, dst)
puts "#{cmd_color('copying')} #{dir_color(src)}"
puts " -> #{dir_color(dst)}"
Find.find(src) do |fn|
next if fn =~ /\/\./
next if fn =~ /Thumbs\.db/
r = fn[src.size..-1]
if File.directory? fn
mkdir(File.join(dst, r), {:verbose => false}) unless File.exist? File.join(dst,r)
else
cp(fn, File.join(dst, r), {:verbose => false})
end
end
end
def cp_file(src, dst)
puts "#{cmd_color('copying')} #{file_color(src)}"
puts " -> #{file_color(dst)}"
cp(src, dst, {:verbose => false})
end
def dep(src)
s = File.expand_path src
rs = s[FIREFOX.size..-1]
d = File.join(TMP, rs)
puts "#{cmd_color('copying')} #{file_color(s)}"
puts " -> #{file_color(d)}"
cp(s, d, {:verbose => false})
end
def dep2(src, add='')
s = File.expand_path src
rs = s[ROOT.size..-1]
d = File.join(TMP, add, rs)
puts "#{cmd_color('copying')} #{file_color(s)}"
puts " -> #{file_color(d)}"
cp(s, d, {:verbose => false})
end
def my_mkdir(dir)
puts "#{cmd_color('creating directory')} #{dir_color(dir)}"
mkdir(dir, {:verbose => false})
end
def parse_version()
f = File.new(File.join(FIREFOX, 'install.rdf'))
text = f.read
unless text=~/<em:version>([^<]*)<\/em:version>/
puts "#{red('Version not found')}"
exit
end
$1
end
def die(s)
puts(red(s))
exit(1)
end
def patch(filepath, matcher, replacer)
puts "Patching #{blue(filepath[ROOT.size+1..-1])} with #{yellow(replacer.to_s)}"
applied = false
lines = []
File.open(filepath, 'r') do |f|
f.each do |line|
lines << line.gsub(matcher, replacer)
applied ||= lines[-1]!=line
end
end
File.open(filepath, "w") do |f|
f << lines
end
applied
end
def check_if_addon_exists()
die("firelogger addon not found!\n expected to be in #{ADDON}") unless File.exists?(ADDON)
end
################################################################################
desc "Prepare XPI"
task :xpi do
check_if_addon_exists()
version = parse_version()
remove_dir(TMP) if File.exists?(TMP) # recursive!
mkdir(TMP, {:verbose => false})
cp_dir(File.join(FIREFOX, 'chrome'), File.join(TMP, "chrome"))
cp_dir(File.join(FIREFOX, 'defaults'), File.join(TMP, "defaults"))
dep(File.join(FIREFOX, 'chrome.manifest'))
dep(File.join(FIREFOX, 'install.rdf'))
dep(File.join(FIREFOX, 'license.txt'))
my_mkdir(DST) unless File.exist?(DST)
firelogger_js_path = File.join(TMP, "chrome", "content", "firelogger.js")
patch(firelogger_js_path, /(dbg\(.*\);)/, "/*\\1*/")
res = "#{DST}/firelogger-#{version}.xpi"
File.unlink(res) if File.exists?(res)
puts "#{cmd_color('zipping')} #{file_color(res)}"
Dir.chdir(TMP) do
puts red('need zip on command line (download http://www.info-zip.org/Zip.html)') unless system("zip -r \"#{res}\" *");
end
remove_dir(TMP) if File.exist?(TMP) # recursive!
puts "\nInstaller is prepared in #{yellow(res)}, enjoy!"
end
desc "Resets version in all relevant sources"
task :version do
check_if_addon_exists()
version = ARGV[1] or die("Please specify a version as first parameter")
firelogger_js_path = File.join(FIREFOX, 'chrome', 'content', 'firelogger.js')
if not patch(firelogger_js_path, /version: '([0-9\.])+'/, "version: '#{version}'")
puts " #{red("patching had no effect")}"
end
install_rdf_path = File.join(FIREFOX, 'install.rdf')
if not patch(install_rdf_path, /<em:version>([0-9\.])+<\/em:version>/, "<em:version>#{version}</em:version>")
puts " #{red("patching had no effect")}"
end
exit(0)
end
task :default => :xpi