/
Rakefile
206 lines (176 loc) · 5.76 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require "fileutils"
require "jasmine"
require_relative "spec/support/jasmine_config.rb"
load "jasmine/tasks/jasmine.rake"
@debug = true
ROOT_DIR = File.expand_path(File.dirname(__FILE__))
CHROME_DIR = File.join(ROOT_DIR, "chrome")
FIREFOX_DIR = File.join(ROOT_DIR, "firefox")
SOURCE_DIR = File.join(ROOT_DIR, "src")
STATIC_EXTENSIONS = %w(png)
BROWSERS = {
chrome: CHROME_DIR,
firefox: FIREFOX_DIR
}
def verify_directory(path)
FileUtils.mkdir_p(path) unless File.directory?(path)
end
def verify_extension_structure
verify_directory CHROME_DIR
%w(css img js).each do |subdirectory|
verify_directory File.join(CHROME_DIR, subdirectory)
end
end
def manifest
require "json"
@manifest ||= JSON.parser.new(File.read(File.join(CHROME_DIR, "manifest.json"))).parse
end
def compile_sources(input_type, opts = {})
verify_extension_structure
output_dir = File.join(CHROME_DIR, opts[:output_dir] || "")
input_dir = File.join(SOURCE_DIR, opts[:input_dir] || "")
subdir_search = opts[:merge_subdirectories] ? "*" : "**"
puts input_type
Dir[File.join(input_dir, subdir_search, "*.*.#{input_type}")].each do |path|
sub_path = path.sub(input_dir, "").sub(/^[\/.]+/, "")
output_name = File.basename(path, ".*")
relative_path = File.join(File.dirname(sub_path), output_name).sub(/^[\/.]+/, "")
output_path = File.join(output_dir, relative_path)
File.open(output_path, "w") do |handle|
puts " #{relative_path}"
handle.puts yield(File.read(path))
end
end
if opts[:merge_subdirectories]
Dir[File.join(input_dir, "*", "*/")].each do |subdir|
files = Dir[File.join(subdir, "**/*.*.#{input_type}")].sort
extension = File.basename(files[0], ".*").split(".").last
result = files.map { |p| File.read(p) }.join("\n")
sub_paths = subdir.sub(input_dir, "").sub(/^[\/.]+/, "").split("/")
output_name = "#{sub_paths.pop}.#{extension}"
sub_path = sub_paths.join("/")
relative_path = File.join(sub_path, output_name).sub(/^[\/.]+/, "")
output_path = File.join(output_dir, relative_path)
File.open(output_path, "w") do |handle|
puts " #{relative_path}"
handle.puts yield(result)
end
files.each { |p| puts " #{p.sub(subdir, "")}" }
end
end
end
namespace :build do
desc "Compile CoffeeScript to JavaScript"
task :coffee do
require "coffee-script"
require "uglifier"
opts = { :merge_subdirectories => true }
compile_sources("coffee", opts) do |content|
js = CoffeeScript.compile(content, :bare => true)
if @debug
js
else
Uglifier.compile js
end
end
end
desc "Compile HAML to HTML"
task :haml do
require "haml"
compile_sources("haml") do |content|
Haml::Engine.new(content).render
end
end
desc "Compile SASS to CSS"
task :sass do
require "sass"
compile_sources("sass") do |content|
Sass::Engine.new(content).render
end
end
desc "Compile YAML to JSON"
task :yml do
require "yaml"
require "json"
compile_sources("yml") do |content|
json = YAML.load(content).to_json
JSON.pretty_generate JSON.parse(json)
end
end
end
namespace :jasmine do
desc "Compile CoffeeScript specs to JavaScript specs"
task :compile do
opts = { input_dir: "../spec", output_dir: "../spec" }
compile_sources("coffee", "js", opts) do |content|
CoffeeScript.compile content, :bare => true
end
end
end
desc "Copy uncompiled assets from src/ to ext/"
task :raw do
verify_extension_structure
STATIC_EXTENSIONS.each do |ext|
puts ext
Dir[File.join(SOURCE_DIR, "**", "*.#{ext}")].each do |path|
relative_path = path.sub(SOURCE_DIR, "").sub(/^\//, "")
puts " #{relative_path}"
FileUtils.cp path, File.join(CHROME_DIR, relative_path)
end
end
# Copy over licenses
puts "license"
%w(BEERWARE-LICENSE MIT-LICENSE).each do |license|
puts " #{license}"
FileUtils.cp File.expand_path(license), File.join(CHROME_DIR, license)
end
end
desc "Zip extension"
task :zip do
base_name = "#{manifest["name"]}_#{manifest["version"]}"
full_name = File.join(ROOT_DIR, base_name)
FileUtils.rm Dir["#{full_name}.*"]
# Chrome
Dir.chdir(CHROME_DIR) do
puts %x[zip -T #{full_name}.zip *.* **/*.*]
end
# Firefox
# if ENV["MOZ_TOOLS"].nil?
# puts "Environment variable MOZ_TOOLS doesn't exist"
# puts "Set this variable to point to the location of the Firefox Add-on SDK"
# else
# puts %x[cd $MOZ_TOOLS && source bin/activate && cd #{FIREFOX_DIR} && cfx xpi]
# FileUtils.mv File.join(FIREFOX_DIR, "reasonable.xpi"), "#{full_name}.xpi"
# end
end
desc "Turn debug off"
task :production do
@debug = false
end
desc "Run some file organization tasks for browser-specific implementations"
task :browser do
FileUtils.rm_rf FIREFOX_DIR
FileUtils.cp_r CHROME_DIR, FIREFOX_DIR
# Chrome
%w(package.json).each do |path|
full_path = File.join(CHROME_DIR, path)
File.delete full_path if File.exist?(full_path)
end
# Firefox
# Dir.chdir(FIREFOX_DIR) do
# FileUtils.mv "js", "data"
# FileUtils.mkdir "lib"
# FileUtils.mv "data/background.js", "lib/main.js"
# FileUtils.mv "img/icon_48.png", "data/icon.png"
# FileUtils.rm_rf "img"
# FileUtils.rm "manifest.json"
# end
end
desc "Compile and copy over all extension files"
task :build => [:production, "build:coffee", "build:haml", "build:sass", "build:yml",
:browser, :raw, :zip]
desc "Compile and copy over all extension files but leave human-readable"
task "build:debug" => ["build:coffee", "build:haml", "build:sass", "build:yml",
:browser, :raw, :zip]
desc "Compile spec CoffeeScripts and setup tests"
task :spec => ["jasmine:compile", "jasmine"]