From 6676f467ce7cf37d7d36ad37e1be347bfb6ef3ad Mon Sep 17 00:00:00 2001 From: Sergey Kishenin Date: Thu, 17 Jul 2014 20:08:30 +0600 Subject: [PATCH] make root_path configurable #94 --- CHANGELOG.md | 1 + README.md | 15 +++++++++++---- lib/bower-rails.rb | 7 +++++-- lib/bower-rails/dsl.rb | 9 ++++----- .../initialize/templates/bower_rails.rb | 11 +++++++---- lib/tasks/bower.rake | 10 +++------- spec/bower-rails/bower-rails_spec.rb | 11 +++++++++++ 7 files changed, 42 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04950dd..95c2cd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Edge version +* Add configurable `root_path` option for `bower-rails` * Add `rake bower:install:deployment` which installs from generated bower.json without generating it first, keeping any additions (like dependency conflict resolutions) intact [#89][] and [#92][] [#89]: https://github.com/42dev/bower-rails/pull/89 diff --git a/README.md b/README.md index 27ba847..d900a17 100644 --- a/README.md +++ b/README.md @@ -130,10 +130,17 @@ Change options in your `config/initializers/bower_rails.rb`: ``` ruby BowerRails.configure do |bower_rails| - # By default options are false - bower_rails.install_before_precompile = true # invokes rake bower:install before precompilation - bower_rails.resolve_before_precompile = true # invokes rake bower:resolve before precompilation - bower_rails.clean_before_precompile = true # invokes rake bower:clean before precompilation + # Tell bower-rails what path should be considered as root. Defaults to Dir.pwd + bower_rails.root_path = Dir.pwd + + # Invokes rake bower:install before precompilation. Defaults to false + bower_rails.install_before_precompile = true + + # Invokes rake bower:resolve before precompilation. Defaults to false + bower_rails.resolve_before_precompile = true + + # Invokes rake bower:clean before precompilation. Defaults to false + bower_rails.clean_before_precompile = true end ``` diff --git a/lib/bower-rails.rb b/lib/bower-rails.rb index 19eab93..8928261 100644 --- a/lib/bower-rails.rb +++ b/lib/bower-rails.rb @@ -5,6 +5,9 @@ module BowerRails extend self class << self + # The root path of the project + attr_accessor :root_path + # An array of tasks to enhance `rake assets:precompile` attr_reader :tasks @@ -35,10 +38,10 @@ def collect_tasks end end - # By default tasks are empty - @tasks = [] # Set default values for options + @root_path = Dir.pwd + @tasks = [] @install_before_precompile = false @resolve_before_precompile = false @clean_before_precompile = false diff --git a/lib/bower-rails/dsl.rb b/lib/bower-rails/dsl.rb index c69fe41..807fc16 100644 --- a/lib/bower-rails/dsl.rb +++ b/lib/bower-rails/dsl.rb @@ -5,14 +5,13 @@ module BowerRails class Dsl def self.evalute(filename) - new.tap { |dsl| dsl.eval_file(File.join(dsl.root_path, filename)) } + new.tap { |dsl| dsl.eval_file(File.join(BowerRails.root_path, filename)) } end - attr_reader :dependencies, :root_path + attr_reader :dependencies def initialize @dependencies = {} - @root_path ||= Dir.pwd @assets_path ||= "assets" end @@ -66,7 +65,7 @@ def write_bower_json end def generate_dotbowerrc - contents = JSON.parse(File.read(File.join(@root_path, '.bowerrc'))) rescue {} + contents = JSON.parse(File.read(File.join(BowerRails.root_path, '.bowerrc'))) rescue {} contents["directory"] = "bower_components" JSON.pretty_generate(contents) end @@ -127,7 +126,7 @@ def assert_group_name name end def normalize_location_path(loc, assets_path) - File.join(@root_path, loc.to_s, assets_path) + File.join(BowerRails.root_path, loc.to_s, assets_path) end end end diff --git a/lib/generators/bower_rails/initialize/templates/bower_rails.rb b/lib/generators/bower_rails/initialize/templates/bower_rails.rb index f641437..79d2ecb 100644 --- a/lib/generators/bower_rails/initialize/templates/bower_rails.rb +++ b/lib/generators/bower_rails/initialize/templates/bower_rails.rb @@ -1,10 +1,13 @@ BowerRails.configure do |bower_rails| - # Invoke `rake bower:install` automatically before the `rake assets:precompile` task. + # Tell bower-rails what path should be considered as root. Defaults to Dir.pwd + # bower_rails.root_path = Dir.pwd + + # Invokes rake bower:install before precompilation. Defaults to false # bower_rails.install_before_precompile = true - # Invoke `rake bower:resolve` automatically before the `rake assets:precompile` task. + # Invokes rake bower:resolve before precompilation. Defaults to false # bower_rails.resolve_before_precompile = true - # Invoke `rake bower:clean` automatically before the `rake assets:precompile` task. + # Invokes rake bower:clean before precompilation. Defaults to false # bower_rails.clean_before_precompile = true -end +end \ No newline at end of file diff --git a/lib/tasks/bower.rake b/lib/tasks/bower.rake index 37ebeb1..cced024 100644 --- a/lib/tasks/bower.rake +++ b/lib/tasks/bower.rake @@ -70,9 +70,9 @@ before 'assets:precompile' do end def perform remove_components = true, &block - entries = Dir.entries(get_bower_root_path) + entries = Dir.entries(BowerRails.root_path) - npm_path = File.join(get_bower_root_path, 'node_modules', '.bin') + npm_path = File.join(BowerRails.root_path, 'node_modules', '.bin') bower = find_command('bower', [npm_path]) if bower.nil? @@ -97,10 +97,6 @@ EOS end end -def get_bower_root_path - Dir.pwd -end - def dsl_perform_command remove_components = true, &block dsl = BowerRails::Dsl.evalute("Bowerfile") @@ -121,7 +117,7 @@ end #run the passed bower block in appropriate folders def perform_command remove_components = true, &block - bower_root = get_bower_root_path + bower_root = BowerRails.root_path #load in bower json file txt = File.read(File.join(bower_root, "bower.json")) json = JSON.parse(txt) diff --git a/spec/bower-rails/bower-rails_spec.rb b/spec/bower-rails/bower-rails_spec.rb index aa91d11..855146e 100644 --- a/spec/bower-rails/bower-rails_spec.rb +++ b/spec/bower-rails/bower-rails_spec.rb @@ -1,6 +1,10 @@ require 'spec_helper' describe BowerRails do + it 'should set default value for root_path option' do + expect(BowerRails.root_path).to eq(Dir.pwd) + end + it 'should set default value for install_before_precompile option' do expect(BowerRails.install_before_precompile).to eq(false) end @@ -21,12 +25,19 @@ before :each do BowerRails.instance_variable_set(:@tasks, []) BowerRails.configure do |bower_rails| + bower_rails.root_path = '/home/username/dirname' bower_rails.install_before_precompile = false bower_rails.resolve_before_precompile = false bower_rails.clean_before_precompile = false end end + describe '#root_path' do + it 'should set root_path option' do + expect(BowerRails.root_path).to eq('/home/username/dirname') + end + end + describe '#install_before_precompile' do before do BowerRails.configure do |bower_rails|