From 4fa80b4bc1c6d02138e84c4344c1f20b7a951f3d Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Tue, 21 Aug 2018 22:34:39 +0100 Subject: [PATCH] sass -> sassc Fixes #1156 --- Gemfile | 2 +- Rakefile | 18 ++++++++++-------- assets/stylesheets/bootstrap/_theme.scss | 4 ++-- bootstrap-sass.gemspec | 8 ++++---- lib/bootstrap-sass.rb | 16 +++++----------- tasks/converter/less_conversion.rb | 2 ++ tasks/converter/network.rb | 4 ++-- test/compilation_test.rb | 4 ++-- test/dummy_rails/config/boot.rb | 2 +- test/dummy_sass_only/Gemfile | 2 +- test/dummy_sass_only/compile.rb | 21 ++++++++++++++------- test/dummy_sass_only/import_all.sass | 2 -- test/dummy_sass_only/import_all.scss | 2 ++ test/sass_test.rb | 16 +++++++++------- test/sprockets_rails_test.rb | 20 ++++++++++++-------- test/test_helper.rb | 3 ++- 16 files changed, 69 insertions(+), 57 deletions(-) delete mode 100644 test/dummy_sass_only/import_all.sass create mode 100644 test/dummy_sass_only/import_all.scss diff --git a/Gemfile b/Gemfile index 38da900afb..a441504c17 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,5 @@ gemspec gem 'compass', require: false group :development do - gem 'byebug', platforms: [:mri_21, :mri_22], require: false + gem 'byebug', platform: :mri, require: false end diff --git a/Rakefile b/Rakefile index 3e88526d68..ae42c2e2d7 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,4 @@ -lib_path = File.join(File.dirname(__FILE__), 'lib') +lib_path = File.join(__dir__, 'lib') $:.unshift(lib_path) unless $:.include?(lib_path) load './tasks/bower.rake' @@ -42,11 +42,12 @@ end desc 'Dumps output to a CSS file for testing' task :debug do - require 'sass' + require 'sassc' + require 'bootstrap-sass' path = Bootstrap.stylesheets_path - %w(bootstrap).each do |file| - engine = Sass::Engine.for_file("#{path}/#{file}.scss", syntax: :scss, load_paths: [path]) - File.open("./#{file}.css", 'w') { |f| f.write(engine.render) } + %w(_bootstrap).each do |file| + engine = SassC::Engine.new(File.read("#{path}/#{file}.scss"), syntax: :scss, load_paths: [path]) + File.open("tmp/#{file}.css", 'w') { |f| f.write(engine.render) } end end @@ -64,7 +65,8 @@ end desc 'Compile bootstrap-sass to tmp/ (or first arg)' task :compile, :css_path do |t, args| - require 'sass' + require 'sassc' + require 'bootstrap-sass' require 'term/ansicolor' path = 'assets/stylesheets' @@ -74,8 +76,8 @@ task :compile, :css_path do |t, args| %w(_bootstrap bootstrap/_theme).each do |file| save_path = "#{css_path}/#{file.sub(/(^|\/)?_+/, '\1').sub('/', '-')}.css" puts Term::ANSIColor.cyan(" #{save_path}") + '...' - engine = Sass::Engine.for_file("#{path}/#{file}.scss", syntax: :scss, load_paths: [path]) - css = engine.render + engine = SassC::Engine.new(File.read("#{path}/#{file}.scss"), syntax: :scss, load_paths: [path]) + css = engine.render File.open(save_path, 'w') { |f| f.write css } end end diff --git a/assets/stylesheets/bootstrap/_theme.scss b/assets/stylesheets/bootstrap/_theme.scss index eb3a8f5713..9c2a76cd52 100644 --- a/assets/stylesheets/bootstrap/_theme.scss +++ b/assets/stylesheets/bootstrap/_theme.scss @@ -8,8 +8,8 @@ // Load core variables and mixins // -------------------------------------------------- -@import "variables"; -@import "mixins"; +@import "bootstrap/variables"; +@import "bootstrap/mixins"; // diff --git a/bootstrap-sass.gemspec b/bootstrap-sass.gemspec index c8f833d935..1ab1786ff2 100644 --- a/bootstrap-sass.gemspec +++ b/bootstrap-sass.gemspec @@ -11,14 +11,14 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/twbs/bootstrap-sass' s.license = 'MIT' - s.add_runtime_dependency 'sass', '>= 3.3.4' + s.add_runtime_dependency 'sassc', '>= 1.12.1' s.add_runtime_dependency 'autoprefixer-rails', '>= 5.2.1' # Testing dependencies - s.add_development_dependency 'minitest', '~> 5.8' - s.add_development_dependency 'minitest-reporters', '~> 1.1' + s.add_development_dependency 'minitest', '~> 5.11' + s.add_development_dependency 'minitest-reporters', '~> 1.3' # Integration testing - s.add_development_dependency 'capybara', '>= 2.5.0' + s.add_development_dependency 'capybara', '~> 3.6' s.add_development_dependency 'poltergeist' # Dummy Rails app dependencies s.add_development_dependency 'actionpack', '>= 4.1.5' diff --git a/lib/bootstrap-sass.rb b/lib/bootstrap-sass.rb index bde132d458..a58e81cf33 100644 --- a/lib/bootstrap-sass.rb +++ b/lib/bootstrap-sass.rb @@ -11,9 +11,12 @@ def load! register_lotus elsif sprockets? register_sprockets + elsif defined?(::Sass) && ::Sass.respond_to?(:load_paths) + # The deprecated `sass` gem: + ::Sass.load_paths << stylesheets_path + # bootstrap requires minimum precision of 8, see https://github.com/twbs/bootstrap-sass/issues/409 + ::Sass::Script::Number.precision = [8, ::Sass::Script::Number.precision].max end - - configure_sass end # Paths @@ -56,15 +59,6 @@ def lotus? private - def configure_sass - require 'sass' - - ::Sass.load_paths << stylesheets_path - - # bootstrap requires minimum precision of 8, see https://github.com/twbs/bootstrap-sass/issues/409 - ::Sass::Script::Number.precision = [8, ::Sass::Script::Number.precision].max - end - def register_compass_extension ::Compass::Frameworks.register( 'bootstrap', diff --git a/tasks/converter/less_conversion.rb b/tasks/converter/less_conversion.rb index c4eaed813b..3ce692e4b6 100644 --- a/tasks/converter/less_conversion.rb +++ b/tasks/converter/less_conversion.rb @@ -124,6 +124,8 @@ def process_stylesheet_assets file = replace_all file, /(\s*)\.navbar-(right|left)\s*\{\s*@extend\s*\.pull-(right|left);\s*/, "\\1.navbar-\\2 {\\1 float: \\2 !important;\\1" when 'tables.less' file = replace_all file, /(@include\s*table-row-variant\()(\w+)/, "\\1'\\2'" + when 'theme.less' + file = replace_all file, /@import "/, '\0bootstrap/' when 'thumbnails.less', 'labels.less', 'badges.less', 'buttons.less' file = extract_nested_rule file, 'a&' when 'glyphicons.less' diff --git a/tasks/converter/network.rb b/tasks/converter/network.rb index cc60276069..aa62be5e3e 100644 --- a/tasks/converter/network.rb +++ b/tasks/converter/network.rb @@ -32,7 +32,7 @@ def read_cached_files(path, files) if File.directory?(full_path) files.each do |name| path = "#{full_path}/#{name}" - contents[name] = File.read(path, mode: 'rb') if File.exists?(path) + contents[name] = File.read(path, mode: 'rb') if File.exist?(path) end end contents @@ -51,7 +51,7 @@ def get_file(url) uri = URI(url) cache_path = "./#@cache_path#{uri.path}#{uri.query.tr('?&=', '-') if uri.query}" FileUtils.mkdir_p File.dirname(cache_path) - if File.exists?(cache_path) + if File.exist?(cache_path) log_http_get_file url, true File.read(cache_path, mode: 'rb') else diff --git a/test/compilation_test.rb b/test/compilation_test.rb index 6808813a27..c44a97ac9f 100644 --- a/test/compilation_test.rb +++ b/test/compilation_test.rb @@ -1,13 +1,13 @@ require 'test_helper' require 'fileutils' -require 'sass' +require 'sassc' class CompilationTest < Minitest::Test def test_compilation path = 'assets/stylesheets' %w(_bootstrap bootstrap/_theme).each do |file| FileUtils.rm_rf('.sass-cache', secure: true) - engine = Sass::Engine.for_file("#{path}/#{file}.scss", syntax: :scss, load_paths: [path]) + engine = SassC::Engine.new(File.read("#{path}/#{file}.scss"), syntax: :scss, load_paths: [path]) FileUtils.mkdir_p("tmp/#{File.dirname(file)}") File.open("tmp/#{file}.css", 'w') { |f| f.write engine.render diff --git a/test/dummy_rails/config/boot.rb b/test/dummy_rails/config/boot.rb index ef360470a3..6266cfc509 100644 --- a/test/dummy_rails/config/boot.rb +++ b/test/dummy_rails/config/boot.rb @@ -1,5 +1,5 @@ # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) -require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) diff --git a/test/dummy_sass_only/Gemfile b/test/dummy_sass_only/Gemfile index bfde6fa157..784b2761e4 100644 --- a/test/dummy_sass_only/Gemfile +++ b/test/dummy_sass_only/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' -gem 'sass', '~> 3.3' +gem 'sassc', '>= 1.12.1' gem 'bootstrap-sass', path: '../..' diff --git a/test/dummy_sass_only/compile.rb b/test/dummy_sass_only/compile.rb index 09e6785572..6253af119d 100644 --- a/test/dummy_sass_only/compile.rb +++ b/test/dummy_sass_only/compile.rb @@ -1,13 +1,20 @@ -require 'sass' +# frozen_string_literal: true + +require 'sassc' require 'bootstrap-sass' require 'fileutils' -scss_path = File.expand_path('./import_all.sass', File.dirname(__FILE__)) -css = Sass.compile File.read(scss_path), syntax: 'sass' +load_path = ARGV[0] +out_path = ARGV[1] + +output = SassC::Engine.new( + File.read(File.expand_path('./import_all.scss', __dir__)), + syntax: :scss, load_paths: [load_path] +).render -if ARGV[0] - FileUtils.mkdir_p File.dirname(ARGV[0]) - File.open(ARGV[0], 'w') { |f| f.write css } +if out_path + FileUtils.mkdir_p(File.dirname(out_path)) + File.write(out_path, output) else - puts css + puts output end diff --git a/test/dummy_sass_only/import_all.sass b/test/dummy_sass_only/import_all.sass deleted file mode 100644 index a44a6e3bff..0000000000 --- a/test/dummy_sass_only/import_all.sass +++ /dev/null @@ -1,2 +0,0 @@ -@import 'bootstrap' -@import 'bootstrap/theme' \ No newline at end of file diff --git a/test/dummy_sass_only/import_all.scss b/test/dummy_sass_only/import_all.scss new file mode 100644 index 0000000000..f12e223030 --- /dev/null +++ b/test/dummy_sass_only/import_all.scss @@ -0,0 +1,2 @@ +@import 'bootstrap'; +@import 'bootstrap/theme'; diff --git a/test/sass_test.rb b/test/sass_test.rb index fc54c77b98..0cb435334e 100644 --- a/test/sass_test.rb +++ b/test/sass_test.rb @@ -1,23 +1,25 @@ +# frozen_string_literal: true + require 'test_helper' require 'shellwords' +require 'fileutils' class SassTest < Minitest::Test DUMMY_PATH = 'test/dummy_sass_only' def test_font_helper - assert_match %r(url\(['"]?.*eot['"]?\)), @css + assert_match %r{url\(['"]?.*eot['"]?\)}, @css end def setup - Dir.chdir DUMMY_PATH do - %x[rm -rf .sass-cache/] - %x[bundle] - end + FileUtils.rm_rf(File.join(DUMMY_PATH, '.sass-cache'), secure: true) css_path = File.join GEM_PATH, 'tmp/bootstrap-sass-only.css' - command = "bundle exec ruby compile.rb #{Shellwords.escape css_path}" success = Dir.chdir DUMMY_PATH do silence_stdout_if !ENV['VERBOSE'] do - system(command) + Bundler.with_original_env do + system('bundle') && system('bundle', 'exec', 'ruby', 'compile.rb', + Bootstrap.stylesheets_path, css_path) + end end end assert success, 'Sass-only compilation failed' diff --git a/test/sprockets_rails_test.rb b/test/sprockets_rails_test.rb index 38707ca84b..4969f28912 100644 --- a/test/sprockets_rails_test.rb +++ b/test/sprockets_rails_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' require 'fileutils' require 'find' @@ -6,22 +8,24 @@ class SprocketsRailsTest < Minitest::Test def test_sprockets_digest_asset_refs - root = 'test/dummy_rails' - command = "bundle exec rake assets:precompile GEMFILE=#{GEM_PATH}/Gemfile RAILS_ENV=production" + root = 'test/dummy_rails' compiled = Dir.chdir root do silence_stderr_if !ENV['VERBOSE'] do - system(command) + Bundler.with_original_env do + system({ 'BUNDLE_GEMFILE' => File.join(GEM_PATH, 'Gemfile'), + 'RAILS_ENV' => 'production' }, + 'bundle exec rake assets:precompile') + end end end assert compiled, 'Could not precompile assets' Dir.glob(File.join(root, 'public', 'assets', 'app*.{css,js}')) do |path| - File.open(path, 'r') do |f| - f.read.scan /url\("?[^"]+\.(?:jpg|png|eot|woff2?|ttf|svg)[^"]*"?\)/ do |m| - assert_match /-[0-9a-f]{12,}\./, m - end + File.read(path) + .scan(/url\("?[^"]+\.(?:jpg|png|eot|woff2?|ttf|svg)[^"]*"?\)/) do |m| + assert_match(/-[0-9a-f]{12,}\./, m) end end ensure - FileUtils.rm_rf %W(#{root}/public/assets/ #{root}/tmp/cache/), secure: true + FileUtils.rm_rf %W[#{root}/public/assets/ #{root}/tmp/cache/], secure: true end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 304b2e0c20..1e995a8939 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,7 +10,7 @@ require_relative File.join('.', file) end -GEM_PATH = File.expand_path('../', File.dirname(__FILE__)) +GEM_PATH = File.expand_path('../', __dir__) #= Capybara + Poltergeist require 'capybara/poltergeist' @@ -26,6 +26,7 @@ end Capybara.configure do |config| + config.server = :webrick config.app_host = 'http://localhost:7000' config.default_driver = :poltergeist config.javascript_driver = :poltergeist