10091f7111d01d8970e17117594d91a918899643
[yaffs-website] / vendor / twbs / bootstrap-sass / tasks / converter.rb
1 # coding: utf-8
2 # Based on convert script from vwall/compass-twitter-bootstrap gem.
3 # https://github.com/vwall/compass-twitter-bootstrap/blob/master/build/convert.rb
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this work except in compliance with the License.
7 # You may obtain a copy of the License in the LICENSE file, or at:
8 #
9 #    http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 require 'open-uri'
18 require 'json'
19 require 'strscan'
20 require 'forwardable'
21 require 'term/ansicolor'
22 require 'fileutils'
23
24 require_relative 'converter/fonts_conversion'
25 require_relative 'converter/less_conversion'
26 require_relative 'converter/js_conversion'
27 require_relative 'converter/logger'
28 require_relative 'converter/network'
29
30 class Converter
31   extend Forwardable
32   include Network
33   include LessConversion
34   include JsConversion
35   include FontsConversion
36
37   def initialize(repo: 'twbs/bootstrap', branch: 'master', save_to: {}, cache_path: 'tmp/converter-cache-bootstrap')
38     @logger     = Logger.new
39     @repo       = repo
40     @branch     = branch || 'master'
41     @branch_sha = get_branch_sha
42     @cache_path = cache_path
43     @repo_url   = "https://github.com/#@repo"
44     @save_to    = {
45         js:    'assets/javascripts/bootstrap',
46         scss:  'assets/stylesheets/bootstrap',
47         fonts: 'assets/fonts/bootstrap'}.merge(save_to)
48   end
49
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) }
61
62     process_font_assets
63     process_stylesheet_assets
64     process_javascript_assets
65     store_version
66   end
67
68   def save_file(path, content, mode='w')
69     dir = File.dirname(path)
70     FileUtils.mkdir_p(dir) unless File.directory?(dir)
71     File.open(path, mode) { |file| file.write(content) }
72   end
73
74   # Update version.rb file with BOOTSTRAP_SHA
75   def store_version
76     path    = 'lib/bootstrap-sass/version.rb'
77     content = File.read(path).sub(/BOOTSTRAP_SHA\s*=\s*['"][\w]+['"]/, "BOOTSTRAP_SHA = '#@branch_sha'")
78     File.open(path, 'w') { |f| f.write(content) }
79   end
80 end