Skip to content

Commit f076f64

Browse files
committedDec 20, 2013
Minor refactorings and rake less_to_scss
less_to_scss: $ echo '.p { #gradient > .horizontal(red,blue) }' | rake less_to_scss[master] .p { @include gradient-horizontal(red,blue) }
1 parent f12cfb1 commit f076f64

File tree

7 files changed

+86
-72
lines changed

7 files changed

+86
-72
lines changed
 

‎Rakefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ end
1919
desc 'Convert bootstrap to bootstrap-sass'
2020
task :convert, :branch do |t, args|
2121
require './tasks/converter'
22-
branch = args[:branch]
23-
Converter.new(branch).process
22+
Converter.new(branch: args[:branch]).process_bootstrap
23+
end
24+
25+
desc 'LESS to stdin -> Sass to stdout'
26+
task :less_to_scss, :branch do |t, args|
27+
require './tasks/converter'
28+
puts Converter.new(branch: args[:branch]).convert_less(STDIN.read)
2429
end
2530

2631
desc 'Compile bootstrap-sass to tmp/ (or first arg)'

‎tasks/converter.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,31 @@ class Converter
3434
include JsConversion
3535
include FontsConversion
3636

37-
def initialize(branch)
38-
@git_data_api_host = 'https://api.github.com/repos'
39-
@git_raw_host = 'https://raw.github.com'
40-
41-
@repo = 'twbs/bootstrap'
42-
@repo_url = "https://github.com/#@repo"
37+
def initialize(repo: 'twbs/bootstrap', branch: 'master', save_to: {}, cache_path: 'tmp/converter-cache-bootstrap')
38+
@logger = Logger.new
39+
@repo = repo
4340
@branch = branch || 'master'
4441
@branch_sha = get_branch_sha
45-
@save_at = { js: 'vendor/assets/javascripts/bootstrap',
46-
scss: 'vendor/assets/stylesheets/bootstrap',
47-
fonts: 'vendor/assets/fonts/bootstrap' }
48-
@save_at.each { |_,v| FileUtils.mkdir_p(v) }
49-
@cache_path = 'tmp/converter-cache'
50-
@logger = Logger.new(repo: @repo_url, branch: @branch, branch_sha: @branch_sha, save_at: @save_at, cache_path: @cache_path)
42+
@cache_path = cache_path
43+
@repo_url = "https://github.com/#@repo"
44+
@save_to = {
45+
js: 'vendor/assets/javascripts/bootstrap',
46+
scss: 'vendor/assets/stylesheets/bootstrap',
47+
fonts: 'vendor/assets/fonts/bootstrap'}.merge(save_to)
5148
end
5249

53-
def_delegators :@logger, :log_status, :log_processing, :log_transform, :log_file_info, :log_processed, :log_http_get_file, :log_http_get_files, :silence_log
50+
def_delegators :@logger, :log, :log_status, :log_processing, :log_transform, :log_file_info, :log_processed, :log_http_get_file, :log_http_get_files, :silence_log
51+
52+
def process_bootstrap
53+
log_status "Convert Bootstrap LESS to SASS"
54+
puts " repo : #@repo_url"
55+
puts " branch : #@branch_sha #@repo_url/tree/#@branch"
56+
puts " save to: #{@save_to.to_json}"
57+
puts " twbs cache: #{@cache_path}"
58+
puts '-' * 60
59+
60+
@save_to.each { |_, v| FileUtils.mkdir_p(v) }
5461

55-
def process
5662
process_stylesheet_assets
5763
process_javascript_assets
5864
process_font_assets
@@ -65,7 +71,7 @@ def save_file(path, content, mode='w')
6571

6672
# Update version.rb file with BOOTSTRAP_SHA
6773
def store_version
68-
path = 'lib/bootstrap-sass/version.rb'
74+
path = 'lib/bootstrap-sass/version.rb'
6975
content = File.read(path).sub(/BOOTSTRAP_SHA\s*=\s*['"][\w]+['"]/, "BOOTSTRAP_SHA = '#@branch_sha'")
7076
File.open(path, 'w') { |f| f.write(content) }
7177
end

‎tasks/converter/fonts_conversion.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ module FontsConversion
33
def process_font_assets
44
log_status 'Processing fonts...'
55
files = read_files('fonts', bootstrap_font_files)
6-
save_at = @save_at[:fonts]
6+
save_to = @save_to[:fonts]
77
files.each do |name, content|
8-
save_file "#{save_at}/#{name}", content
8+
save_file "#{save_to}/#{name}", content
99
end
1010
end
11+
12+
def bootstrap_font_files
13+
@bootstrap_font_files ||= get_paths_by_type('fonts', /\.(eot|svg|ttf|woff)$/)
14+
end
1115
end
12-
end
16+
end

‎tasks/converter/js_conversion.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ class Converter
22
module JsConversion
33
def process_javascript_assets
44
log_status 'Processing javascripts...'
5-
save_at = @save_at[:js]
5+
save_to = @save_to[:js]
66
read_files('js', bootstrap_js_files).each do |name, file|
7-
save_file("#{save_at}/#{name}", file)
7+
save_file("#{save_to}/#{name}", file)
88
end
99
log_processed "#{bootstrap_js_files * ' '}"
1010

@@ -18,5 +18,22 @@ def process_javascript_assets
1818
save_file(path, content)
1919
log_processed path
2020
end
21+
22+
def bootstrap_js_files
23+
@bootstrap_js_files ||= begin
24+
files = get_paths_by_type 'js', /\.js$/
25+
files.sort_by { |f|
26+
case f
27+
# tooltip depends on popover and must be loaded earlier
28+
when /tooltip/ then
29+
1
30+
when /popover/ then
31+
2
32+
else
33+
0
34+
end
35+
}
36+
end
37+
end
2138
end
22-
end
39+
end

‎tasks/converter/less_conversion.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,30 @@ module LessConversion
3636
transition transition-duration transition-property transition-transform box-shadow
3737
)
3838

39+
# Convert a snippet of bootstrap LESS to Scss
40+
def convert_less(less)
41+
load_shared
42+
less = convert_to_scss(less)
43+
less = yield(less) if block_given?
44+
less
45+
end
46+
47+
def load_shared
48+
@shared_mixins ||= begin
49+
log_status ' Reading shared mixins from mixins.less'
50+
read_mixins read_files('less', ['mixins.less'])['mixins.less'], nested: NESTED_MIXINS
51+
end
52+
end
53+
3954
def process_stylesheet_assets
4055
log_status 'Processing stylesheets...'
4156
files = read_files('less', bootstrap_less_files)
4257

43-
log_status ' Reading shared mixins from mixins.less'
44-
@shared_mixins = read_mixins files['mixins.less'], nested: NESTED_MIXINS
45-
4658
log_status ' Converting LESS files to Scss:'
4759
files.each do |name, file|
4860
log_processing name
4961
# apply common conversions
50-
file = convert_to_scss(file)
62+
file = convert_less(file)
5163
case name
5264
when 'mixins.less'
5365
NESTED_MIXINS.each do |selector, prefix|
@@ -102,15 +114,19 @@ def process_stylesheet_assets
102114
end
103115

104116
name = name.sub(/\.less$/, '.scss')
105-
save_at = @save_at[:scss]
106-
path = "#{save_at}/#{'_' unless name == 'bootstrap.scss'}#{name}"
117+
save_to = @save_to[:scss]
118+
path = "#{save_to}/#{'_' unless name == 'bootstrap.scss'}#{name}"
107119
save_file(path, file)
108120
log_processed File.basename(path)
109121
end
110122
end
111123

124+
def bootstrap_less_files
125+
@bootstrap_less_files ||= get_paths_by_type('less', /\.less$/)
126+
end
127+
112128
# apply general less to scss conversion
113-
def convert_to_scss(file)
129+
def convert_to_scss(file)
114130
# mixins may also be defined in the file. get mixin names before doing any processing
115131
mixin_names = (@shared_mixins + read_mixins(file)).uniq
116132
file = replace_vars(file)

‎tasks/converter/logger.rb

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@ class Converter
22
class Logger
33
include Term::ANSIColor
44

5-
def initialize(env)
6-
@env = env
7-
puts bold "Convert Bootstrap LESS to SASS"
8-
puts " repo : #{env[:repo]}"
9-
puts " branch : #{env[:branch]} #{dark "#{env[:repo]}/tree/#{env[:branch_sha]}"}"
10-
puts " save to: #{@env[:save_at].to_json}"
11-
puts " twbs cache: #{@env[:cache_path]}"
12-
puts dark "-" * 60
13-
end
14-
155
def log_status(status)
166
puts bold status
177
end
@@ -51,9 +41,11 @@ def log_http_get_files(files, from, cached = false)
5141
end
5242

5343
def puts(*args)
54-
STDOUT.puts *args unless @silence
44+
STDERR.puts *args unless @silence
5545
end
5646

47+
alias log puts
48+
5749
def silence_log
5850
@silence = true
5951
yield

‎tasks/converter/network.rb

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
11
class Converter
22
module Network
3-
4-
def bootstrap_font_files
5-
@bootstrap_font_files ||= get_paths_by_type('fonts', /\.(eot|svg|ttf|woff)$/)
6-
end
7-
8-
def bootstrap_less_files
9-
@bootstrap_less_files ||= get_paths_by_type('less', /\.less$/)
10-
end
11-
12-
def bootstrap_js_files
13-
@bootstrap_js_files ||= begin
14-
files = get_paths_by_type 'js', /\.js$/
15-
files.sort_by { |f|
16-
case f
17-
# tooltip depends on popover and must be loaded earlier
18-
when /tooltip/ then
19-
1
20-
when /popover/ then
21-
2
22-
else
23-
0
24-
end
25-
}
26-
end
27-
end
28-
293
protected
304

315
def get_paths_by_type(dir, file_re)
32-
files = get_json "#{@git_data_api_host}/#@repo/git/trees/#{get_tree_sha(dir)}"
6+
files = get_json "https://api.github.com/repos/#@repo/git/trees/#{get_tree_sha(dir)}"
337
files['tree'].select { |f| f['type'] == 'blob' && f['path'] =~ file_re }.map { |f| f['path'] }
348
end
359

3610
def read_files(path, files)
37-
full_path = "#{@git_raw_host}/#@repo/#@branch_sha/#{path}"
11+
full_path = "https://raw.github.com/#@repo/#@branch_sha/#{path}"
3812
if (contents = read_cached_files(path, files))
3913
log_http_get_files files, full_path, true
4014
else
@@ -88,8 +62,8 @@ def get_file(url)
8862
# get sha of the branch (= the latest commit)
8963
def get_branch_sha
9064
return @branch if @branch =~ /\A[0-9a-f]+\z/
91-
cmd = "git ls-remote '#@repo_url' | awk '/#@branch/ {print $1}'"
92-
puts cmd
65+
cmd = "git ls-remote 'https://github.com/#@repo' | awk '/#@branch/ {print $1}'"
66+
log cmd
9367
@branch_sha ||= %x[#{cmd}].chomp
9468
raise 'Could not get branch sha!' unless $?.success?
9569
@branch_sha
@@ -101,7 +75,7 @@ def get_tree_sha(dir)
10175
end
10276

10377
def get_trees
104-
@trees ||= get_json("#{@git_data_api_host}/#@repo/git/trees/#@branch_sha")
78+
@trees ||= get_json("https://api.github.com/repos/#@repo/git/trees/#@branch_sha")
10579
end
10680

10781
def get_json(url)

0 commit comments

Comments
 (0)
Please sign in to comment.