-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
bake.rb
219 lines (163 loc) · 4.69 KB
/
bake.rb
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
207
208
209
210
211
212
213
214
215
216
217
218
219
# This file is part of the "jQuery.Syntax" project, and is distributed under the MIT License.
# See <jquery.syntax.js> for licensing details.
# Copyright (c) 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
require 'stringio'
require 'fileutils'
require 'tmpdir'
require 'pathname'
require 'yaml'
require 'closure-compiler'
require_relative 'ext/theme'
CACHE_FILE = "jquery.syntax.cache.js"
MINIFIED_FILE = "jquery.syntax.min.js"
LICENSE = <<EOF
// This file is part of the "jQuery.Syntax" project, and is distributed under the MIT License.
EOF
BASE_PATH = Pathname(Dir.getwd)
DIST_PATH = 'dist'
def initialize(*)
super
@config = nil
end
# if ENV['PREFIX']
# Dir.chdir(ENV['PREFIX'])
# end
def update_aliases
code = StringIO.new
code.puts "// This file is automatically generated. Any changes may be lost."
code.puts "// The following declarations describes all resources that might be loaded dynamically."
code.puts
code.puts "// Brush Aliases"
Dir["jquery.syntax.brush.*.js"].sort.each do |path|
File.open(path, "r") do |f|
first_line = f.readline rescue ""
if first_line.match(/^\/\/ brush: (.+?) aliases: (.+)$/)
code.puts "Syntax.alias(#{$1}, #{$2});"
end
end
end
styles = {}
Dir["**/jquery.syntax.*.css"].sort.each do |path|
basename = File.basename(path, ".css")
styles[basename] ||= []
styles[basename] << path
end
code.puts
code.puts "// CSS Extensions"
styles.each do |basename, paths|
code.puts "Syntax.styles[#{basename.inspect}] = #{paths.inspect};"
end
code.puts
code.puts "// Theme Configuration"
Dir["**/theme.js"].sort.each do |path|
code.write File.read(path)
end
File.open(CACHE_FILE, "w") do |f|
f.write(code.string)
end
puts "*** Written updated cache file #{CACHE_FILE} ***"
# puts code.string
end
def generate_stylesheets(theme: "base")
output = File.expand_path(theme)
FileUtils.mkdir_p(output)
cache_path = File.expand_path(".sass-cache", __dir__)
aggregate_theme = Theme.new(output, File.join(File.dirname(__FILE__), "themes"))
aggregate_theme.load_theme(theme)
Dir.glob(File.join(output, "jquery.syntax.*.{sass,scss}")) do |sass|
output_name = File.basename(sass).sub(/\.(sass|scss)$/, ".css")
output_path = File.join(output, output_name)
command = ["sass", "--sourcemap=none", "-I", output, "--stdin", output_path, "--cache-location", cache_path]
IO.popen(command, "w") do |io|
aggregate_theme.includes_for(sass, :prepend).each do |incl|
io.puts("@import #{incl}")
end
io.puts("@import #{File.basename(sass)}")
aggregate_theme.includes_for(sass, :append).each do |incl|
io.puts("@import #{incl}")
end
io.close_write
end
end
Dir.glob(File.join(output, "*.{sass,scss}")) do |path|
FileUtils.rm path
end
Dir.glob(File.join(output, "_*")) do |path|
FileUtils.rm path
end
end
# This builds a combined jquery.syntax.js and jquery.syntax.cache.js and minifies the result
def build_combined
self.update_aliases
compiler = Closure::Compiler.new
output = compiler.compile_files(
["jquery.syntax.js", "jquery.syntax.cache.js"]
)
File.open(MINIFIED_FILE, "w") do |file|
file.write(LICENSE)
file.write(output)
end
end
# Note... this is one way !
def compress_all
self.build_combined
files = Dir["jquery.syntax*.js"]
compiler = Closure::Compiler.new
puts "Minifying JavaScript..."
files.each do |path|
puts "Minifying #{File.basename(path)}..."
output = compiler.compile_files([path])
File.open(path, "w") do |file|
file.write(LICENSE)
file.write(output)
end
end
end
def setup_prefix
if ENV['CONFIG']
config_path = BASE_PATH + ENV['CONFIG']
else
config_path = BASE_PATH + "site.yaml"
unless File.exist? config_path
config_path = BASE_PATH + "install.yaml"
end
end
puts "Using configuration #{config_path}"
@config = YAML::load_file(config_path)
if @config['prefix'] && !ENV['PREFIX']
prefix = config_path.dirname + (@config['prefix'] || DIST_PATH)
elsif ENV['PREFIX']
prefix = BASE_PATH + ENV['PREFIX']
else
prefix = BASE_PATH + DIST_PATH
end
prefix.mkpath
Dir.chdir(prefix)
ENV['PREFIX'] = prefix.to_s
puts "Working in #{Dir.getwd}..."
end
def install
self.setup_prefix
self.clean
@config['themes'].each do |theme|
self.generate_stylesheets(theme: theme)
end
js_files = Dir[BASE_PATH + "source/jquery.syntax*.js"]
js_files.each do |path|
$stderr.puts "Copying #{File.basename(path)}..."
output_path = File.basename(path)
FileUtils.cp(path, output_path)
end
self.build_combined
self.compress_all if @config['minify']
puts "Install into #{Dir.getwd} finished."
end
def clean
self.setup_prefix
Dir.glob("*") do |path|
FileUtils.rm_r path
end
end
def default
self.install
end