Skip to content

Commit

Permalink
make root_path configurable #94
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKishenin committed Jul 17, 2014
1 parent 0c298d9 commit 6676f46
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
7 changes: 5 additions & 2 deletions lib/bower-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/bower-rails/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
11 changes: 7 additions & 4 deletions lib/generators/bower_rails/initialize/templates/bower_rails.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 3 additions & 7 deletions lib/tasks/bower.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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")

Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions spec/bower-rails/bower-rails_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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|
Expand Down

0 comments on commit 6676f46

Please sign in to comment.