-
Notifications
You must be signed in to change notification settings - Fork 56
/
Rakefile
188 lines (140 loc) · 4.98 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
require 'rake/clean'
LIVERELOAD_VERSION = '1.6.1'
GEM_SRC = FileList["server/lib/*.rb", "server/*.gemspec", "server/bin/*"]
GEM_DIST = "server/livereload-#{LIVERELOAD_VERSION}.gem"
class FileGroup
attr_reader :sources, :mapping
def initialize sources, &mapping
@sources = sources
@mapping = mapping
end
def targets
@targets ||= @sources.map { |source| @mapping.call(source) }
end
def each
@sources.each do |source|
yield source, @mapping.call(source)
end
end
def rule
each do |source, target|
file target => [source] do |t|
yield source, target
end
end
end
def self.[] *args
self.new(*args)
end
end
def copy_source dest, *sources
File.open(dest, 'w') do |df|
df.write sources.map { |fn| File.read(fn) }.join("\n")
end
end
file 'LiveReload.chromeextension/LiveReload-content.js' => ['src/content.js', 'src/chrome/content.js'] do |t|
copy_source t.name, *t.prerequisites
end
file 'LiveReload.chromeextension/LiveReload-background.js' => ['src/background.js', 'src/chrome/background.js'] do |t|
copy_source t.name, *t.prerequisites
end
desc "Prepare the Chome extension"
task :chrome => ['LiveReload.chromeextension/LiveReload-content.js', 'LiveReload.chromeextension/LiveReload-background.js']
file 'LiveReload.safariextension/LiveReload-injected.js' => ['src/content.js', 'src/safari/injected.js'] do |t|
copy_source t.name, *t.prerequisites
end
file 'LiveReload.safariextension/LiveReload-global.js' => ['src/background.js', 'src/safari/global.js'] do |t|
copy_source t.name, *t.prerequisites
end
desc "Prepare the Safari extension"
task :safari => ['LiveReload.safariextension/LiveReload-injected.js', 'LiveReload.safariextension/LiveReload-global.js']
FIREFOX_INTERIM = FileGroup.new(%w[src/background.js src/content.js]) { |f| File.join('Firefox/content', File.basename(f)) }
FIREFOX_BASIC = FileList['Firefox/**/*.{js,xul,manifest,rdf}'] - FIREFOX_INTERIM.targets
FIREFOX_ALL = FIREFOX_BASIC + FIREFOX_INTERIM.targets
FIREFOX_INTERIM.rule do |source, target|
copy_source target, source
end
file 'LiveReload.xpi' => FIREFOX_ALL do |t|
full_dst = File.expand_path(t.name)
Dir.chdir 'Firefox' do
sh 'zip', full_dst, *t.prerequisites.map { |f| f.sub(%r!^Firefox/!, '') }
end
end
desc "Build the Firefox extension"
task :firefox => 'LiveReload.xpi'
file 'livereload-xbrowser.js' => %w(src/background.js src/content.js src/xbrowser/livereload.js) do |t|
src = %w(src/background.js src/content.js src/xbrowser/livereload.js).collect { |f| File.read(f).strip }.join("\n") + "\n"
src.gsub! "host: (navigator.appVersion.indexOf('Linux') >= 0 ? '0.0.0.0' : 'localhost'),", "host: (location.host || 'localhost').split(':')[0],"
File.open(t.name, 'w') { |f| f.write(src) }
end
file '../LiveReload/livereload.js' => ['livereload-xbrowser.js'] do |t|
File.open(t.name, 'w') { |f| f.write(File.read(t.prerequisites.first)) }
end
desc "Build the cross-browser version"
task :xbrowser => 'livereload-xbrowser.js'
desc "Update the file bundled with LiveReload 2"
task :lr2 => ['../LiveReload/livereload.js']
desc "Process all browser extensions"
task :all => [:safari, :chrome, :firefox, :xbrowser]
task :default => :all
namespace :gem do
file GEM_DIST => GEM_SRC do
cd 'server' do
sh 'gem', 'build', 'livereload.gemspec'
end
end
desc "Build the livereload gem"
task :build => GEM_DIST
desc "Install the livereload gem/command"
task :install => :build do
sh 'sudo', 'gem', 'install', GEM_DIST
end
desc "Uninstall the livereload gem/command"
task :uninstall do
sh 'sudo', 'gem', 'uninstall', 'livereload'
end
desc "Publish the gem on gemcutter"
task :publish => :build do
sh 'gem', 'push', GEM_DIST
end
end
task :test do
#`python -m SimpleHTTPServer`
require 'webrick'
# http://tobyho.com/HTTP_Server_in_5_Lines_With_Webrick
class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler
def prevent_caching(res)
res['ETag'] = nil
res['Last-Modified'] = Time.now + 100**4
res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
end
def do_GET(req, res)
super
prevent_caching(res)
end
end
serv = Thread.new() {
server = WEBrick::HTTPServer.new :Port => 8000
server.mount('/', NonCachingFileHandler, './')
puts 'http://0.0.0.0:8000 started'
server.start
}
serv2 = Thread.new() {
server = WEBrick::HTTPServer.new :Port => 8001
server.mount('/', NonCachingFileHandler, './')
puts 'http://0.0.0.0:8001 started'
server.start
}
`open http://0.0.0.0:8000/test` unless `which open`.empty?
serv.join
serv2.join
end
CLEAN.include('LiveReload.xpi')
CLOBBER.include(%w(
LiveReload.chromeextension/LiveReload-content.js
LiveReload.chromeextension/LiveReload-background.js
LiveReload.safariextension/LiveReload-injected.js
LiveReload.safariextension/LiveReload-global.js
Firefox/content/background.js
Firefox/content/content.js
))