-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Rakefile
92 lines (76 loc) · 1.75 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
require "bundler/setup"
require "webrick"
require "ghost"
task :default => :serve
IP_ADDRESS = "172.16.88.88"
HOSTNAME = "ajax.googleapis.com"
desc "[run as sudo] Virtual IP, remap ajax.googleapis.com, starts a web server to serve local mirror of ajax.googleapis.com"
task :serve do
prepare_for_exit
start_virtual_server
end
desc "Downloads all libraries from ajax.googleapis.com that are listed in libraries.txt"
task :sync do
File.foreach("libraries.txt") do |url|
download(url)
end
end
def prepare_for_exit
trap("INT") { cleanup_and_exit }
trap("TERM") { cleanup_and_exit }
puts "\nPress Ctrl-C at any time to shutdown\n"
end
def start_virtual_server
create_virtual_ip
map_hostname
start_web_server
end
def stop_virtual_server
stop_web_server
unmap_hostname
delete_virtual_ip
end
def cleanup_and_exit
return if @cleaned
puts
stop_virtual_server
puts "Bye."
@cleaned = true
end
def create_virtual_ip
sh("ifconfig lo0 alias #{IP_ADDRESS}")
end
def delete_virtual_ip
sh("ifconfig lo0 -alias #{IP_ADDRESS}")
end
def map_hostname
unmap_hostname # to be sure
sh("ghost add #{HOSTNAME} #{IP_ADDRESS}")
end
def unmap_hostname
sh("ghost delete #{HOSTNAME}")
end
def start_web_server
@server = WEBrick::HTTPServer.new(:BindAddress => IP_ADDRESS, :Port => 80, :DocumentRoot => Dir.pwd)
@server.start
end
def stop_web_server
@server.shutdown if @server
end
def download(url)
url, path = url_and_path(url)
return unless url
dir = File.dirname(path)
FileUtils.mkdir_p(dir)
if File.exists?(path)
puts "#{path} already exists"
else
sh("curl '#{url}' > '#{path}'")
end
end
def url_and_path(url)
return if url.nil? || url.strip.empty?
url.strip!
path = url.gsub("http://#{HOSTNAME}/", "")
[url, path]
end