Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/bootstrap-rubygem.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGleb Mazovetskiy <glex.spb@gmail.com>2013-12-21 02:16:03 +0400
committerGleb Mazovetskiy <glex.spb@gmail.com>2013-12-21 02:20:01 +0400
commitf076f64a4a3524f6791a1d04e63a9ed107ea919a (patch)
tree94803263ee1ddfc9f62ceded4c0763cace1ac395
parentf12cfb1a7088e4321875397ca53dd8674ddc2ca1 (diff)
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) }
-rw-r--r--Rakefile9
-rw-r--r--tasks/converter.rb36
-rw-r--r--tasks/converter/fonts_conversion.rb10
-rw-r--r--tasks/converter/js_conversion.rb23
-rw-r--r--tasks/converter/less_conversion.rb30
-rw-r--r--tasks/converter/logger.rb14
-rw-r--r--tasks/converter/network.rb36
7 files changed, 86 insertions, 72 deletions
diff --git a/Rakefile b/Rakefile
index 90158b2..e12012d 100644
--- a/Rakefile
+++ b/Rakefile
@@ -19,8 +19,13 @@ end
desc 'Convert bootstrap to bootstrap-sass'
task :convert, :branch do |t, args|
require './tasks/converter'
- branch = args[:branch]
- Converter.new(branch).process
+ Converter.new(branch: args[:branch]).process_bootstrap
+end
+
+desc 'LESS to stdin -> Sass to stdout'
+task :less_to_scss, :branch do |t, args|
+ require './tasks/converter'
+ puts Converter.new(branch: args[:branch]).convert_less(STDIN.read)
end
desc 'Compile bootstrap-sass to tmp/ (or first arg)'
diff --git a/tasks/converter.rb b/tasks/converter.rb
index e60ee26..9b3f380 100644
--- a/tasks/converter.rb
+++ b/tasks/converter.rb
@@ -34,25 +34,31 @@ class Converter
include JsConversion
include FontsConversion
- def initialize(branch)
- @git_data_api_host = 'https://api.github.com/repos'
- @git_raw_host = 'https://raw.github.com'
-
- @repo = 'twbs/bootstrap'
- @repo_url = "https://github.com/#@repo"
+ def initialize(repo: 'twbs/bootstrap', branch: 'master', save_to: {}, cache_path: 'tmp/converter-cache-bootstrap')
+ @logger = Logger.new
+ @repo = repo
@branch = branch || 'master'
@branch_sha = get_branch_sha
- @save_at = { js: 'vendor/assets/javascripts/bootstrap',
- scss: 'vendor/assets/stylesheets/bootstrap',
- fonts: 'vendor/assets/fonts/bootstrap' }
- @save_at.each { |_,v| FileUtils.mkdir_p(v) }
- @cache_path = 'tmp/converter-cache'
- @logger = Logger.new(repo: @repo_url, branch: @branch, branch_sha: @branch_sha, save_at: @save_at, cache_path: @cache_path)
+ @cache_path = cache_path
+ @repo_url = "https://github.com/#@repo"
+ @save_to = {
+ js: 'vendor/assets/javascripts/bootstrap',
+ scss: 'vendor/assets/stylesheets/bootstrap',
+ fonts: 'vendor/assets/fonts/bootstrap'}.merge(save_to)
end
- def_delegators :@logger, :log_status, :log_processing, :log_transform, :log_file_info, :log_processed, :log_http_get_file, :log_http_get_files, :silence_log
+ 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
+
+ def process_bootstrap
+ log_status "Convert Bootstrap LESS to SASS"
+ puts " repo : #@repo_url"
+ puts " branch : #@branch_sha #@repo_url/tree/#@branch"
+ puts " save to: #{@save_to.to_json}"
+ puts " twbs cache: #{@cache_path}"
+ puts '-' * 60
+
+ @save_to.each { |_, v| FileUtils.mkdir_p(v) }
- def process
process_stylesheet_assets
process_javascript_assets
process_font_assets
@@ -65,7 +71,7 @@ class Converter
# Update version.rb file with BOOTSTRAP_SHA
def store_version
- path = 'lib/bootstrap-sass/version.rb'
+ path = 'lib/bootstrap-sass/version.rb'
content = File.read(path).sub(/BOOTSTRAP_SHA\s*=\s*['"][\w]+['"]/, "BOOTSTRAP_SHA = '#@branch_sha'")
File.open(path, 'w') { |f| f.write(content) }
end
diff --git a/tasks/converter/fonts_conversion.rb b/tasks/converter/fonts_conversion.rb
index 04e04ec..86d809a 100644
--- a/tasks/converter/fonts_conversion.rb
+++ b/tasks/converter/fonts_conversion.rb
@@ -3,10 +3,14 @@ class Converter
def process_font_assets
log_status 'Processing fonts...'
files = read_files('fonts', bootstrap_font_files)
- save_at = @save_at[:fonts]
+ save_to = @save_to[:fonts]
files.each do |name, content|
- save_file "#{save_at}/#{name}", content
+ save_file "#{save_to}/#{name}", content
end
end
+
+ def bootstrap_font_files
+ @bootstrap_font_files ||= get_paths_by_type('fonts', /\.(eot|svg|ttf|woff)$/)
+ end
end
-end \ No newline at end of file
+end
diff --git a/tasks/converter/js_conversion.rb b/tasks/converter/js_conversion.rb
index f3e86f1..2ab5edd 100644
--- a/tasks/converter/js_conversion.rb
+++ b/tasks/converter/js_conversion.rb
@@ -2,9 +2,9 @@ class Converter
module JsConversion
def process_javascript_assets
log_status 'Processing javascripts...'
- save_at = @save_at[:js]
+ save_to = @save_to[:js]
read_files('js', bootstrap_js_files).each do |name, file|
- save_file("#{save_at}/#{name}", file)
+ save_file("#{save_to}/#{name}", file)
end
log_processed "#{bootstrap_js_files * ' '}"
@@ -18,5 +18,22 @@ class Converter
save_file(path, content)
log_processed path
end
+
+ def bootstrap_js_files
+ @bootstrap_js_files ||= begin
+ files = get_paths_by_type 'js', /\.js$/
+ files.sort_by { |f|
+ case f
+ # tooltip depends on popover and must be loaded earlier
+ when /tooltip/ then
+ 1
+ when /popover/ then
+ 2
+ else
+ 0
+ end
+ }
+ end
+ end
end
-end \ No newline at end of file
+end
diff --git a/tasks/converter/less_conversion.rb b/tasks/converter/less_conversion.rb
index 4d77020..599a415 100644
--- a/tasks/converter/less_conversion.rb
+++ b/tasks/converter/less_conversion.rb
@@ -36,18 +36,30 @@ class Converter
transition transition-duration transition-property transition-transform box-shadow
)
+ # Convert a snippet of bootstrap LESS to Scss
+ def convert_less(less)
+ load_shared
+ less = convert_to_scss(less)
+ less = yield(less) if block_given?
+ less
+ end
+
+ def load_shared
+ @shared_mixins ||= begin
+ log_status ' Reading shared mixins from mixins.less'
+ read_mixins read_files('less', ['mixins.less'])['mixins.less'], nested: NESTED_MIXINS
+ end
+ end
+
def process_stylesheet_assets
log_status 'Processing stylesheets...'
files = read_files('less', bootstrap_less_files)
- log_status ' Reading shared mixins from mixins.less'
- @shared_mixins = read_mixins files['mixins.less'], nested: NESTED_MIXINS
-
log_status ' Converting LESS files to Scss:'
files.each do |name, file|
log_processing name
# apply common conversions
- file = convert_to_scss(file)
+ file = convert_less(file)
case name
when 'mixins.less'
NESTED_MIXINS.each do |selector, prefix|
@@ -102,15 +114,19 @@ $bootstrap-sass-asset-helper: (twbs-font-path('') != unquote("twbs-font-path('')
end
name = name.sub(/\.less$/, '.scss')
- save_at = @save_at[:scss]
- path = "#{save_at}/#{'_' unless name == 'bootstrap.scss'}#{name}"
+ save_to = @save_to[:scss]
+ path = "#{save_to}/#{'_' unless name == 'bootstrap.scss'}#{name}"
save_file(path, file)
log_processed File.basename(path)
end
end
+ def bootstrap_less_files
+ @bootstrap_less_files ||= get_paths_by_type('less', /\.less$/)
+ end
+
# apply general less to scss conversion
- def convert_to_scss(file)
+ def convert_to_scss(file)
# mixins may also be defined in the file. get mixin names before doing any processing
mixin_names = (@shared_mixins + read_mixins(file)).uniq
file = replace_vars(file)
diff --git a/tasks/converter/logger.rb b/tasks/converter/logger.rb
index 9c1ea2b..f3b1ee1 100644
--- a/tasks/converter/logger.rb
+++ b/tasks/converter/logger.rb
@@ -2,16 +2,6 @@ class Converter
class Logger
include Term::ANSIColor
- def initialize(env)
- @env = env
- puts bold "Convert Bootstrap LESS to SASS"
- puts " repo : #{env[:repo]}"
- puts " branch : #{env[:branch]} #{dark "#{env[:repo]}/tree/#{env[:branch_sha]}"}"
- puts " save to: #{@env[:save_at].to_json}"
- puts " twbs cache: #{@env[:cache_path]}"
- puts dark "-" * 60
- end
-
def log_status(status)
puts bold status
end
@@ -51,9 +41,11 @@ class Converter
end
def puts(*args)
- STDOUT.puts *args unless @silence
+ STDERR.puts *args unless @silence
end
+ alias log puts
+
def silence_log
@silence = true
yield
diff --git a/tasks/converter/network.rb b/tasks/converter/network.rb
index 25c6ecb..b1b683e 100644
--- a/tasks/converter/network.rb
+++ b/tasks/converter/network.rb
@@ -1,40 +1,14 @@
class Converter
module Network
-
- def bootstrap_font_files
- @bootstrap_font_files ||= get_paths_by_type('fonts', /\.(eot|svg|ttf|woff)$/)
- end
-
- def bootstrap_less_files
- @bootstrap_less_files ||= get_paths_by_type('less', /\.less$/)
- end
-
- def bootstrap_js_files
- @bootstrap_js_files ||= begin
- files = get_paths_by_type 'js', /\.js$/
- files.sort_by { |f|
- case f
- # tooltip depends on popover and must be loaded earlier
- when /tooltip/ then
- 1
- when /popover/ then
- 2
- else
- 0
- end
- }
- end
- end
-
protected
def get_paths_by_type(dir, file_re)
- files = get_json "#{@git_data_api_host}/#@repo/git/trees/#{get_tree_sha(dir)}"
+ files = get_json "https://api.github.com/repos/#@repo/git/trees/#{get_tree_sha(dir)}"
files['tree'].select { |f| f['type'] == 'blob' && f['path'] =~ file_re }.map { |f| f['path'] }
end
def read_files(path, files)
- full_path = "#{@git_raw_host}/#@repo/#@branch_sha/#{path}"
+ full_path = "https://raw.github.com/#@repo/#@branch_sha/#{path}"
if (contents = read_cached_files(path, files))
log_http_get_files files, full_path, true
else
@@ -88,8 +62,8 @@ class Converter
# get sha of the branch (= the latest commit)
def get_branch_sha
return @branch if @branch =~ /\A[0-9a-f]+\z/
- cmd = "git ls-remote '#@repo_url' | awk '/#@branch/ {print $1}'"
- puts cmd
+ cmd = "git ls-remote 'https://github.com/#@repo' | awk '/#@branch/ {print $1}'"
+ log cmd
@branch_sha ||= %x[#{cmd}].chomp
raise 'Could not get branch sha!' unless $?.success?
@branch_sha
@@ -101,7 +75,7 @@ class Converter
end
def get_trees
- @trees ||= get_json("#{@git_data_api_host}/#@repo/git/trees/#@branch_sha")
+ @trees ||= get_json("https://api.github.com/repos/#@repo/git/trees/#@branch_sha")
end
def get_json(url)