/
Rakefile
151 lines (125 loc) · 4.12 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
# based on: http://gravelog.blogspot.com/2007/03/using-rake-to-build-firefox-extensions_04.html
require 'rexml/document'
require 'digest/sha1'
include REXML
include FileUtils::Verbose
EXTENSION_NAME="multi-user-hatena"
BUILD_DIR="build/#{EXTENSION_NAME}"
UPDATE_LINK="" # URL for updated xpi
directory "#{BUILD_DIR}/chrome"
task :create_buildchrome_dir => ["#{BUILD_DIR}/chrome"]
desc "prepare the chrome.manifest file"
file "#{BUILD_DIR}/chrome.manifest" => [:create_buildchrome_dir] do
open("#{BUILD_DIR}/chrome.manifest",'w') do |infile|
open("chrome.manifest", "r") do |outfile|
while line = outfile.gets
infile.puts line.gsub(/chrome\//, "jar:chrome/#{EXTENSION_NAME}.jar!/")
end
end
end
end
task :create_chrome_manifest => ["#{BUILD_DIR}/chrome.manifest"]
desc "prepare the install.rdf file"
file "#{BUILD_DIR}/install.rdf" => [:create_buildchrome_dir] do
cp 'install.rdf', "#{BUILD_DIR}/install.rdf"
end
task :create_install_rdf => ["#{BUILD_DIR}/install.rdf"]
task :create_extra_directories => []
%w(defaults components modules).each do |dir|
if File.directory?(dir)
dst_dir = "#{BUILD_DIR}/#{dir}"
desc "prepare the #{dir} directory"
file "#{BUILD_DIR}/#{dir}" => [:create_buildchrome_dir] do
cp_r dir, dst_dir
end
task :create_extra_directories => dst_dir
end
end
desc "create the chrome jar file"
task :create_chrome_jar => [:create_buildchrome_dir] do
sh "cd chrome && zip -qr -0 ../#{BUILD_DIR}/chrome/#{EXTENSION_NAME}.jar * -x \*.svn\*"
end
desc "create the xpi file and use the version number in the file name"
task :create_extension_xpi => [
:create_install_rdf,
:create_chrome_manifest,
:create_extra_directories,
:create_chrome_jar] do
install_rdf_file = File.new('install.rdf','r')
install_rdf_xmldoc = Document.new(install_rdf_file)
version_number = ""
install_rdf_xmldoc.elements.each('RDF/Description/em:version') do |element|
version_number = element.text
end
sh "cd #{BUILD_DIR} && zip -qr -9 ../../xpi/#{EXTENSION_NAME}-#{version_number}-fx.xpi *"
rm_rf "build"
end
desc "update update.rdf"
task :update_update_manifest => [:create_extension_xpi] do
update_rdf_xmldoc = nil
File.open('update.rdf','r') do |f|
update_rdf_xmldoc = Document.new(f.read)
update_rdf_xmldoc.elements.each('//RDF:Description') do |element|
if element.attributes['em:version']
element.attributes['em:version'] = version_number
end
if element.attributes['em:updateHash']
element.attributes['em:updateHash'] = 'sha1:' + Digest::SHA1.hexdigest(open(xpi_filename).read)
end
if element.attributes['em:updateLink']
element.attributes['em:updateLink'] = UPDATE_LINK
end
end
File.open('update.rdf','w') do |f|
update_rdf_xmldoc.write(f)
end
end
end
desc "install to local profile directory"
task :install do
File.open("#{firefox_profile_dir(ENV['NAME'])}/extensions/#{extension_id}", 'w') do |f|
f.puts(Dir.pwd)
end
end
desc "uninstall from local profile directory"
task :uninstall do
File.unlink("#{firefox_profile_dir}/extensions/#{extension_id}")
end
task :default => :create_extension_xpi
def firefox_profile_dir(name = nil)
name ||= 'default'
base =
case RUBY_PLATFORM
when /darwin/
'~/Library/Application Support/Firefox/profiles'
when /win/
'~/Application Data/Mozilla/Firefox/Profiles'
else
'~/.mozilla/firefox'
end
path = Dir.glob("#{File.expand_path(base)}/*.#{name}")
if path.length == 1
path.first
else
raise
end
end
def extension_id
open('install.rdf','r') do |file|
install_rdf_xmldoc = Document.new(file)
install_rdf_xmldoc.elements.each('RDF/Description/em:id') do |element|
return element.text
end
end
end
def version_number
open('install.rdf') do |f|
install_rdf_xmldoc = Document.new(f.read)
install_rdf_xmldoc.elements.each('RDF/Description/em:version') do |element|
return element.text
end
end
end
def xpi_filename
"#{EXTENSION_NAME}-#{version_number}-fx.xpi"
end