-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
145 lines (119 loc) · 3.87 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
require 'fileutils'
STDOUT.sync = true #ensures console output is realtime, not buffered
desc "Build a new distribution zipfile for this project"
task :build do
Builder.setup_tmp
Builder.build_js
Builder.build_css
Builder.process_html
Builder.zip
end
desc "Decouple this project from the project_template git repository ready for use in a new project"
task :decouple do
ROOT = File.dirname(__FILE__)
`rm -rf #{File.join(ROOT, '.git')}`
`rm -rf #{File.join(ROOT, 'bin-tmp')}`
`rm -rf #{File.join(ROOT, 'bin-dist')}`
FileUtils.mkdir_p(File.join(ROOT, 'bin-tmp'))
FileUtils.mkdir_p(File.join(ROOT, 'bin-dist'))
`cp .gitignore bin-tmp/`
`cp .gitignore bin-dist/`
end
module Builder
ROOT = File.dirname(__FILE__)
COMPILER_VERSION = 964
COMPRESSOR_VERSION = '2.4.6'
JS_REGEXP = /<!-- JS_START -->(.*)<!-- JS_END -->/m
JS_FILES_REGEXP = /src=["|'](.*?)['|"]/
CSS_REGEXP = /<!-- CSS_START -->(.*)<!-- CSS_END -->/m
CSS_FILES_REGEXP = /href=["|'](.*?)["|']/
def self.build_js
js_files = self.get_js_files
unless js_files
puts "WARN: No JS block found in index.html or no JS files found"
return
end
puts "LOG : JS Files: #{js_files.join(', ')}"
js_files = js_files.map do |file|
"--js=#{ROOT}/#{file} "
end.join
output = "--js_output_file=#{ROOT}/bin-tmp/js/project.js"
`java -jar #{ROOT}/support/compiler-#{COMPILER_VERSION}.jar #{js_files} #{output}`
puts "LOG : JS Compiled"
end
def self.get_js_files
js_block = JS_REGEXP.match(self.get_html)
return false if js_block.nil?
js_files = js_block[0].scan(JS_FILES_REGEXP).to_a.flatten
js_files = self.confirm_exist(js_files)
return false unless js_files.any?
js_files
end
def self.get_html
return @@html if defined? @@html
@@html = File.readlines(File.join(ROOT, 'src/index.html')).join(' ')
end
def self.build_css
css_files = self.get_css_files
unless css_files
puts "WARN: No CSS block found in index.html or no CSS files found"
return
end
puts "LOG : CSS Files: #{css_files.join(', ')}"
css = ""
css_files.each do |file|
yui = IO.popen("java -jar #{ROOT}/support/yuicompressor-#{COMPRESSOR_VERSION}.jar --type css ", "w+")
yui.puts IO.read(file)
yui.close_write
css += yui.gets
end
css_file = File.open(File.join(ROOT, 'bin-tmp', 'css', 'project.css'), 'w')
css_file.puts css
css_file.close
puts "LOG : CSS Compiled"
end
def self.get_css_files
css_block = CSS_REGEXP.match(self.get_html)
return false if css_block.nil?
css_files = css_block[0].scan(CSS_FILES_REGEXP).to_a.flatten
css_files = self.confirm_exist(css_files)
return false unless css_files.any?
css_files
end
def self.confirm_exist(files)
files = files.map do |file|
file = "src/" + file
unless File.exists?(file)
puts "WARN: #{file} specified but could not be found"
nil
else
file
end
end.compact
files
end
def self.process_html
html = self.get_html
html.gsub!(JS_REGEXP, "<script type='text/javascript' src='js/project.js'></script>")
html.gsub!(CSS_REGEXP, "<link rel='stylesheet' href='css/project.css' />")
File.open(File.join(ROOT, 'bin-tmp', 'index.html'), 'w') do |file|
file.puts html
end
end
def self.zip
stamp = Time.now.strftime("%d%m%Y-%H%M")
`cd bin-tmp && zip -r ../bin-dist/project-#{stamp}.zip * -x "?*/.?*"`
end
def self.clean_up
`rm -rf #{File.join(ROOT, 'bin-tmp')}`
end
def self.setup_tmp
self.clean_up
FileUtils.mkdir_p(File.join(ROOT, 'bin-tmp'))
`cp -r src/ bin-tmp/`
`rm -rf #{File.join(ROOT, 'bin-tmp', 'js')}`
`rm -rf #{File.join(ROOT, 'bin-tmp', 'css')}`
FileUtils.mkdir_p(File.join(ROOT, 'bin-tmp', 'js'))
FileUtils.mkdir_p(File.join(ROOT, 'bin-tmp', 'css'))
end
end