forked from binaryage/firequery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rakefile
191 lines (168 loc) · 5.16 KB
/
rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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')
SUPPORT = File.join(ROOT, 'support')
TEST = File.join(ROOT, 'test')
JQUERY_DIR = File.join(ROOT, 'jquery')
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("firequery not found!\n expected to be in #{ADDON}") unless File.exists?(ADDON)
end
def sys(cmd)
puts blue("> "+cmd)
system(cmd)
end
################################################################################
desc "Prepare XPI"
task :default 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)
firequery_js_path = File.join(TMP, "chrome", "content", "firequery.js")
patch(firequery_js_path, /(dbg\(.*\);)/, "/*\\1*/")
res = "#{DST}/firequery-#{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 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")
firequery_js_path = File.join(FIREFOX, 'chrome', 'content', 'firequery.js')
if not patch(firequery_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
desc "Updates jquery in test project"
task :update_jquery do
if File.exists? JQUERY_DIR then
Dir.chdir JQUERY_DIR do
sys("svn up")
end
else
Dir.chdir ROOT do
sys("svn co http://jqueryjs.googlecode.com/svn/trunk jquery")
end
end
Dir.chdir File.join(JQUERY_DIR, 'jquery') do
sys("make jquery")
end
js = File.join(JQUERY_DIR, 'jquery', 'dist', 'jquery.js')
test = File.join(TEST, 'jquery.js')
content = File.join(FIREFOX, 'chrome', 'resources', 'jquery.js')
sys("cp \"#{js}\" \"#{test}\"")
sys("cp \"#{js}\" \"#{content}\"")
end