Skip to content

Commit

Permalink
Merge pull request #24 from SergeyKishenin/patch-1
Browse files Browse the repository at this point in the history
A bunch of fixes and modifications added
  • Loading branch information
rharriso committed Jul 29, 2013
2 parents de12e71 + 3966a3a commit 92737a8
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 45 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,47 @@ The bower.json file is two seperate bower [component.js](https://github.com/twit

##Ruby DSL configuration

The Ruby DSL configuration is a Jsfile with DSL syntax similar to Bundler

The Ruby DSL configuration is a Jsfile with DSL syntax similar to Bundler.

**Example Jsfile**

By default assets are put to `./vendor/assets/bower_components` directory:

``` ruby

# Puts to ./vendor/assets/bower_components
js "backbone"
js "moment"
```

But the default value can be overridden by `assets_path` method:

``` ruby
assets_path "assets/my_javascripts"

# Puts to ./vendor/assets/my_javascripts/bower_components
js "backbone"
js "moment"
```

And finally, the `assets_path` method can be overridden by an option in a `group` call:

``` ruby
assets_path "assets/javascript"

# Puts files under ./vendor/assets/js
# Puts files under ./vendor/assets/js/bower_components
group :vendor, :assets_path => "assets/js" do
js "jquery" # Assummes it's latests
js "backbone", "1.2"
end

# Puts files under ./lib/assets/javascript
# Puts files under ./lib/assets/javascript/bower_components
group :lib do
js "jquery"
js "backbone", "1.2"
end
```
NOTE: All the assets should be stored in `/assets` subdirectory so putting it under `./vendor/js` directory is unavailable

**Available commands with a Jsfile**

Expand Down
83 changes: 48 additions & 35 deletions lib/bower-rails/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,51 @@
require "json"
require 'json'
require 'fileutils'

module BowerRails
class Dsl

def self.config=(conf)
@config = conf
end

def self.config
@config
end
cattr_accessor :config
attr_reader :dependencies

def self.evalute(file)
inst = new
inst.eval_file(file)
inst
instance = new
instance.eval_file(file)
instance
end

def initialize
@dependencies = {}
@groups = []
@root_path = BowerRails::Dsl.config[:root_path] || File.expand_path("./")
@assets_path = BowerRails::Dsl.config[:assets_path] || "/assets/javascripts"
@assets_path = BowerRails::Dsl.config[:assets_path] || "assets"
end

def eval_file(file)
contents = File.open(file,"r").read
instance_eval(contents, file.to_s, 1)
instance_eval(File.open(file, "rb") { |f| f.read }, file.to_s)
end

def directories
@dependencies.keys
end

def dependencies
@dependencies
end

def group(*args, &blk)
@groups.concat [args]
if args[1]
custom_assets_path = args[1][:assets_path]
raise ArgumentError, "Assets should be stored in /assets directory, try :assets_path => 'assets/#{custom_assets_path}' instead" if custom_assets_path.match(/assets/).nil?
new_group = [args[0], args[1]]
else
new_group = [args[0]]
end

@groups << new_group
yield
ensure
args.each { @groups.pop }
end

def js(name, *args)
options = Hash === args.last ? args.pop : {}
version = args.first || "latest"

@groups = ["lib"] if @groups.empty?
@groups = [[:vendor, { assets_path: @assets_path }]] if @groups.empty?

@groups.each do |g|
g_options = Hash === g.last ? g.pop : {}
assets_path = g_options[:assets_path] || @assets_path

g_norm = normalize_location_path(g.first,assets_path)
g_norm = normalize_location_path(g.first.to_s, group_assets_path(g))
@dependencies[g_norm] ||= {}
@dependencies[g_norm][name] = version
end
Expand All @@ -68,26 +58,49 @@ def to_json(location)
def write_bower_json
@dependencies.each do |dir,data|
FileUtils.mkdir_p dir unless File.directory? dir
File.open(File.join(dir,"bower.json"),"w") do |f|
File.open(File.join(dir,"bower.json"), "w") do |f|
f.write(dependencies_to_json(data))
end
end
end

def write_dotbowerrc
@groups = [[:vendor, { assets_path: @assets_path }]] if @groups.empty?
@groups.map do |g|
File.open(File.join(g.first.to_s, group_assets_path(g), ".bowerrc"), "w") do |f|
f.write(JSON.pretty_generate({:directory => "bower_components"}))
end
end
end

def final_assets_path
@groups = [[:vendor, { assets_path: @assets_path }]] if @groups.empty?
@groups.map do |g|
[g.first.to_s, group_assets_path(g)]
end
end

def group_assets_path group
group_options = Hash === group.last ? group.last : {:assets_path => @assets_path}
group_options[:assets_path]
end

private

def dependencies_to_json(data)
JSON.pretty_generate({
:dependencies => data
})
JSON.pretty_generate({
:name => "dsl-generated dependencies",
:dependencies => data
})
end

def assets_path(assets_path)
raise ArgumentError, "Assets should be stored in /assets directory, try assets_path 'assets/#{assets_path}' instead" if assets_path.match(/assets/).nil?
@assets_path = assets_path
end

def normalize_location_path(loc,assets_path)
File.join(@root_path,loc.to_s,assets_path)
def normalize_location_path(loc, assets_path)
File.join(@root_path, loc.to_s, assets_path)
end

end
Expand Down
19 changes: 16 additions & 3 deletions lib/bower-rails/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
require 'bower-rails'
require 'bower-rails/dsl'
require 'rails'

module BowerRails
class Railtie < Rails::Railtie
railtie_name :bower

config.after_initialize do |app|
["lib", "vendor"].each do |dir|
app.config.assets.paths << Rails.root.join(dir, 'assets', 'bower_components')
if File.exist?(File.join("Jsfile"))
BowerRails::Dsl.config = {:root_path => Rails.root}
dsl = BowerRails::Dsl.evalute(File.join("Jsfile"))

config.before_initialize do |app|
dsl.final_assets_path.map do |assets_root, assets_path|
app.config.assets.paths << Rails.root.join(assets_root, assets_path, "bower_components")
end
end
else
config.before_initialize do |app|
["lib", "vendor"].each do |dir|
app.config.assets.paths << Rails.root.join(dir, 'assets', 'bower_components')
end
end
end

Expand Down
13 changes: 10 additions & 3 deletions lib/tasks/bower.rake
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def dsl_perform_command remove_components = true

if remove_components
dsl.write_bower_json
dsl.write_dotbowerrc
puts "bower.js files generated"
end

Expand Down Expand Up @@ -91,17 +92,23 @@ def perform_command remove_components = true
FileUtils.rm_rf("bower_components") if remove_components

#create bower json
File.open("bower.json","w") do |f|
File.open("bower.json", "w") do |f|
f.write(data.to_json)
end

#create .bowerrc
File.open(".bowerrc", "w") do |f|
f.write(JSON.pretty_generate({:directory => "bower_components"}))
end

#run command
yield

#remove bower file
FileUtils.rm("bower.json")

end if data

#remove .bowerrc
FileUtils.rm(".bowerrc")
end if data && !data["dependencies"].empty?
end
end

0 comments on commit 92737a8

Please sign in to comment.