This repository has been archived by the owner on Jan 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
converter.rb
353 lines (299 loc) · 11.8 KB
/
converter.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
BOOTSTRAP_GEM_ROOT = Gem::Specification.find_by_name("bootstrap-sass").gem_dir
require "#{BOOTSTRAP_GEM_ROOT}/tasks/converter/less_conversion"
require 'rugged'
class Converter
include Converter::LessConversion
TEST_DIR = File.join('spec', 'html')
TRANSFORMATIONS = [
:replace_vars,
:replace_file_imports,
:replace_mixin_definitions,
:replace_mixins,
:replace_spin,
# :replace_fadein,
:replace_fade,
:replace_image_urls,
:remove_unnecessary_escaping,
:replace_escaping,
:convert_less_ampersand,
:deinterpolate_vararg_mixins,
:replace_calculation_semantics,
:escape_variables_in_calc
]
TOP = <<-VAR.gsub(/^\s*/, '')
// PatternFly SASS
@import "patternfly/variables";
@import "bootstrap/variables";
VAR
def initialize(options={})
@repository = options.fetch(:repository, 'patternfly/patternfly')
@branch = options.fetch(:branch, 'master')
@destination = options.fetch(:destination, 'assets')
@cache_path = options.fetch(:cache_path, 'tmp')
@source = File.join(@cache_path, 'repository')
end
def convert
checkout_upstream
build_less
copy_non_less
process_stylesheets
store_version
FileUtils.rm_rf(@cache_path) # Clean up the repository & the cache
end
private
def sass_read_mixins(file)
file.scan(/@mixin\s+([^\(]+)\(/).flatten.uniq
end
def replace_mixins(file)
mixins = shared_mixins + sass_read_mixins(file)
super(file, mixins)
end
def replace_vars(less)
less = less.dup
# skip header comment
less =~ %r(\A/\*(.*?)\*/)m
from = $~ ? $~.to_s.length : 0
less[from..-1] = less[from..-1].
gsub(/(?!@mixin|@media|@page|@supports|@keyframes|@font-face|@-\w)@/, '$').
# variables that would be ignored by gsub above: e.g. @page-header-border-color
gsub(/@(page[\w-]+)/, '$\1')
less
end
def remove_button_variant(file)
replace_rules(file, /.button-variant(.*?)/) { |_, _| "" }
end
# SASS doesn't require escaping in calc()
def remove_unnecessary_escaping(file)
file.gsub(/calc\(\~\'([^\']+)\'\)/, 'calc(\1)').gsub(/calc\(\~\"([^\"]+)\"\)/, 'calc(\1)')
end
def escape_variables_in_calc(file)
file.scan(/calc\(.*\$.*\)/).uniq.each do |calc|
dest = calc.gsub(/(\$[a-zA-Z0-9_\-]+)/, '#{\1}')
file.gsub!(calc, dest)
end
file
end
# Override
def replace_file_imports(less, target_path='')
less.gsub!(
%r{[@\$]import\s+(?:\(\w+\)\s+)?["|']([\w\-\./]+).less["|'];},
"@import \"#{target_path}\\1\";"
)
less.gsub!(
%r{[@\$]import\s+(?:\(\w+\)\s+)?["|']([\w\-\./]+).(css)["|'];},
"@import \"#{target_path}\\1.\\2\";"
)
less
end
def insert_default_vars(file)
file = super(file)
file = replace_all(file, %r{"../img"}, '"../images"')
file = replace_all(file, %r{(\$icon-font-path): (\s*)"(.*)" (!default);\n}, '')
file = replace_all(file, %r{\$fa-font-path: .* (!default);\n}, '')
file = replace_all(
file,
%r{(\$(font|img)-path):(\s*)"(.*)" (!default);},
'\\1:\\3if($patternfly-sass-asset-helper, "patternfly", "\\4/patternfly") \\5;')
['$patternfly-sass-asset-helper: false !default;', file].join("\n")
end
def flatten_mixins(file)
NESTED_MIXINS.inject(file) { |a, e| super(a, e.first, e.last) }
end
def fix_dropdown_toggle(file)
replacestr = " {\\1}\n .open & { &.dropdown-toggle {\\1} }"
file = replace_all(file, /,\s*\.open\s+\.dropdown-toggle& \{([^\{\}]*?)\}/m, replacestr)
replace_all(file, /,\s*\.open\s+\.dropdown-toggle& \{(.*?\{.*?\}.*?)\}/m, replacestr)
end
def fix_font_paths(file)
replace_rules(file, /\s*@font-face/) { |rule| replace_asset_url(rule, :font) }
end
def fix_top_level(file)
file = replace_all(file, %r{@import\s+"variables";}, "")
file = replace_all(file, /@import "([^\.]{2})/, '@import "patternfly/\1')
file = replace_all(file, "@import \"patternfly/bootstrap/less/bootstrap\";", fetch_bootstrap_top)
file = replace_all(file, "@import \"patternfly/bootstrap/less/variables\";\n", '')
file = replace_all(file, "@import \"patternfly/bootstrap/less/mixins\";\n", '')
file = replace_all(file, "@import \"patternfly/font-awesome/less/variables\";\n", '')
file = replace_all(file, 'patternfly/font-awesome/less/font-awesome', 'font-awesome')
bower_contrib('patternfly-bootstrap-combobox/css/bootstrap-combobox.css', 'patternfly-bootstrap-combobox.scss', false)
file = replace_all(file, 'patternfly//patternfly-bootstrap-combobox/less/combobox', 'patternfly/lib/patternfly-bootstrap-combobox')
bower_contrib('bootstrap-select/dist/css/bootstrap-select.css', 'bootstrap-select.scss', false)
file = replace_all(file, 'patternfly/bootstrap-select/less/bootstrap-select', 'patternfly/lib/bootstrap-select')
bower_contrib('bootstrap-touchspin/dist/jquery.bootstrap-touchspin.css', 'bootstrap-touchspin.scss', false)
file = replace_all(file, 'patternfly/bootstrap-touchspin/dist/jquery.bootstrap-touchspin.css', 'patternfly/lib/bootstrap-touchspin')
bower_contrib('c3/c3.css', 'c3.scss', false)
file = replace_all(file, 'patternfly/c3/c3.css', 'patternfly/lib/c3')
bower_contrib('bootstrap-datepicker/dist/css/bootstrap-datepicker3.css', 'bootstrap-datepicker.scss', false)
file = replace_all(file, 'patternfly/bootstrap-datepicker/less/datepicker3', 'patternfly/lib/bootstrap-datepicker')
bower_contrib('bootstrap-switch/src/less/bootstrap3/bootstrap-switch.less', 'bootstrap-switch.scss')
file = replace_all(file, 'patternfly/bootstrap-switch/src/less/bootstrap3/bootstrap-switch', 'patternfly/lib/bootstrap-switch')
bower_contrib('eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.css', 'bootstrap-datetimepicker.scss', false)
file = replace_all(file, 'patternfly/eonasdan-bootstrap-datetimepicker/src/less/bootstrap-datetimepicker-build', 'patternfly/lib/bootstrap-datetimepicker')
TOP + remove_comments_and_whitespace(file)
end
def bower_contrib(src, dst, convert=true)
base = 'assets/stylesheets/patternfly/lib'
less = File.read(File.join('bower_components', src))
sass = convert ? less_to_sass(nil, less) : less
sass = remove_map_comments(sass)
FileUtils.mkdir_p(base) unless File.exist?(base)
File.open(File.join(base, dst), 'w') { |f| f.write(sass) }
end
def shared_mixins
@shared_mixins ||= begin
mixins = retrieve_files(File.join('bower_components', 'bootstrap', 'less', 'mixins'), /\.less$/)
mixins.unshift File.join(@source, 'src', 'less', 'mixins.less')
read_mixins(mixins.map { |f| File.read(f) }.join("\n"), :nested => NESTED_MIXINS)
end
end
def fetch_bootstrap_top
path = File.join(BOOTSTRAP_GEM_ROOT, 'assets', 'stylesheets', '_bootstrap.scss')
file = replace_all(File.read(path), %r{@import\s+"bootstrap/variables";}, '')
replace_all(file, %r{^(@import\s+"bootstrap/mixins";)}, "\\1\n@import \"patternfly/mixin_overrides\";")
end
def remove_comments_and_whitespace(input)
input = replace_all(input, /\/\*.*?\*\//m, '')
input = replace_all(input, /\s*\/\/.*$/, '')
input.split("\n").reject { |line| line == "" }.join("\n").concat("\n")
end
def process_stylesheets
save_to = File.join(@destination, 'stylesheets', 'patternfly')
FileUtils.mkdir_p(save_to) unless File.exist?(save_to)
patternfly_less_files.each do |path|
file = File.basename(path)
less = File.read(path)
output = File.join(save_to, "_#{file.sub(/\.less$/, '.scss')}")
File.open(output, 'w') do |f|
f.write(less_to_sass(file, less))
end
end
File.open(File.join(save_to, '..', '_patternfly.scss'), 'w') do |f|
f.write(generate_top_level)
end
end
def generate_top_level
less_to_sass('patternfly.less', top_level_files.map { |f| File.read(f) }.join("\n"))
end
def build_less
Dir.chdir(@source) do
system('npm install')
system('grunt build')
end
end
def copy_non_less
copy_config.each do |asset|
FileUtils.rm_rf(asset[:destination])
retrieve_files(asset[:source], asset[:select], asset[:reject]).each do |f|
copy_with_path(f, asset[:source], asset[:destination])
end
end
end
def retrieve_files(folder, select=%r{.*}, reject=nil)
Dir["#{folder}/**/*"].reject { |f| File.directory?(f) || f !~ select || f =~ reject }
end
def copy_with_path(file, src, dst)
dst = file.sub(/^#{src}/, dst)
dir = File.dirname(dst)
FileUtils.mkdir_p(dir) unless File.exist?(dir)
FileUtils.cp(file, dst)
end
def checkout_upstream
unless Dir.exist?(@source)
repo = Rugged::Repository.clone_at("https://github.com/#{@repository}.git", @source)
end
repo ||= Rugged::Repository.new(@source)
repo.checkout(@branch)
@sha = repo.last_commit.oid
FileUtils.cp( # Mixins correction
File.join(@source, 'src', 'less', 'mixins.less'),
File.join(@source, 'src', 'less', 'mixin_overrides.less')
)
end
def replace_fade(less)
less.gsub(/(?![\-$@.])fade\((.*?),\s*(.*?)%\)/) { "rgba(#{$1}, #{$2.to_f / 100.0})" }
end
def replace_escaping(less)
less = less.gsub(/~"([^"]+)"/, '#{\1}')
less.gsub!(/\$\{([\w\-]+)\}/, '#{$\1}')
less.gsub!(/\$\{([^}]+)\}/, '$\1')
less.gsub(/(\W)e\(%\("?([^"]*)"?\)\)/, '\1\2')
end
def remove_map_comments(less)
less.gsub(%r{\s*(#|@) sourceMappingURL=[^\s]+}, '')
end
def less_to_sass(file, input)
transforms = TRANSFORMATIONS.dup
case file
when 'fonts.less'
transforms << :fix_font_paths
when 'icons.less'
transforms << :fix_font_paths
transforms.delete(:replace_spin)
when 'mixins.less',
transforms << :flatten_mixins
transforms << :fix_dropdown_toggle
when 'mixin_overrides.less'
transforms.unshift(:remove_button_variant)
transforms << :flatten_mixins
when 'variables.less'
transforms.delete(:replace_spin)
transforms << :insert_default_vars
when 'patternfly.less'
transforms.delete(:replace_spin)
transforms << :fix_top_level
when 'bootstrap-touchspin.less', 'spinner.less'
transforms.delete(:replace_spin)
end
transforms.inject(input) { |a, e| send(e, a) }
end
def copy_config
[
{
:source => File.join(@source, 'dist', 'img'),
:select => /\.(png|gif|jpe?g|svg|ico)$/,
:reject => nil,
:destination => File.join(@destination, 'images', 'patternfly')
},
{
:source => File.join(@source, 'dist', 'fonts'),
:select => /\.(eot|svg|ttf|woff2?)$/,
:reject => nil,
:destination => File.join(@destination, 'fonts', 'patternfly')
},
{
:source => File.join(@source, 'dist', 'js'),
:select => /\.js$/,
:reject => nil,
:destination => File.join(@destination, 'javascripts')
},
{
:source => File.join(@source, 'dist', 'tests'),
:select => /.*/,
:reject => nil,
:destination => TEST_DIR
},
{
:source => File.join(@source, 'dist', 'css'),
:select => /css/,
:reject => /styles(-additions)?(\.min)?\.css/,
:destination => File.join(TEST_DIR, 'dist', 'css')
}
]
end
def patternfly_less_files
retrieve_files(File.join(@source, 'src', 'less'), /\.less$/, /lib|patternfly/)
end
def top_level_files
retrieve_files(File.join(@source, 'src', 'less'), /patternfly(\-additions)?\.less$/).reverse
end
def store_version
path = 'lib/patternfly-sass/version.rb'
content = File.read(path).sub(/PATTERNFLY_SHA\s*=\s*['"][\w]+['"]/, "PATTERNFLY_SHA = '#{@sha}'")
File.open(path, 'w') { |f| f.write(content) }
end
def log_transform(*_opts)
end
def log_file_info(*_opts)
end
alias_method :bootstrap_less_files, :patternfly_less_files
end