bower-rails-pt 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Ross Harrison
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,110 @@
1
+ bower-rails
2
+ ===========
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/bower-rails.png)](http://badge.fury.io/rb/bower-rails)
5
+ [![Code Climate](https://codeclimate.com/github/42dev/bower-rails.png)](https://codeclimate.com/github/42dev/bower-rails)
6
+ [![Dependency Status](https://gemnasium.com/SergeyKishenin/bower-rails.png)](https://gemnasium.com/SergeyKishenin/bower-rails)
7
+ [![Build Status](https://travis-ci.org/42dev/bower-rails.png?branch=master)](https://travis-ci.org/42dev/bower-rails)
8
+ [![Coverage Status](https://coveralls.io/repos/42dev/bower-rails/badge.png)](https://coveralls.io/r/42dev/bower-rails)
9
+
10
+ Bower support for Rails projects. Dependency file is bower.json in Rails root dir or Bowerfile if you use DSL.
11
+ Check out Changelog.md for the latest changes and releases.
12
+
13
+ **Requirements**
14
+
15
+ * [node](http://nodejs.org) ([on github](https://github.com/joyent/node))
16
+ * [bower](https://github.com/bower/bower) (>= 0.10.0) installed with npm
17
+
18
+ **Install**
19
+
20
+ in Gemfile
21
+
22
+ ``` Ruby
23
+ gem "bower-rails", "~> 0.5.0"
24
+ ```
25
+
26
+ **Initialize**
27
+
28
+ To add an empty bower.json file to the project root.
29
+
30
+ ``` Bash
31
+ rails g bower_rails:initialize
32
+ ```
33
+
34
+ ##JSON configuration
35
+
36
+ The bower.json file is two seperate bower [component.js](https://github.com/twitter/bower#defining-a-package) files. Defining a package in lib and vendor will install those packages to the corresponding directories.
37
+
38
+ **example bower.json file**
39
+
40
+ ``` javascript
41
+ {
42
+ "lib": {
43
+ "name": "bower-rails generated lib assets",
44
+ "dependencies": {
45
+ "threex" : "git@github.com:rharriso/threex.git",
46
+ "gsvpano.js" : "https://github.com/rharriso/GSVPano.js/blob/master/src/GSVPano.js"
47
+ }
48
+ },
49
+ "vendor": {
50
+ "name": "bower-rails generated vendor assets",
51
+ "dependencies": {
52
+ "three.js" : "https://raw.github.com/mrdoob/three.js/master/build/three.js"
53
+ }
54
+ }
55
+ }
56
+ ```
57
+
58
+ ##Ruby DSL configuration
59
+
60
+ The Ruby DSL configuration is a Bowerfile at the project's root with DSL syntax similar to Bundler.
61
+
62
+ **Example Bowerfile**
63
+
64
+ By default assets are put to `./vendor/assets/bower_components` directory:
65
+
66
+ ``` ruby
67
+
68
+ # Puts to ./vendor/assets/bower_components
69
+ asset "backbone"
70
+ asset "moment"
71
+ ```
72
+
73
+ But the default value can be overridden by `assets_path` method:
74
+
75
+ ``` ruby
76
+ assets_path "assets/my_javascripts"
77
+
78
+ # Puts to ./vendor/assets/my_javascripts/bower_components
79
+ asset "backbone"
80
+ asset "moment"
81
+ ```
82
+
83
+ And finally, the `assets_path` method can be overridden by an option in a `group` call:
84
+
85
+ ``` ruby
86
+ assets_path "assets/javascript"
87
+
88
+ # Puts files under ./vendor/assets/js/bower_components
89
+ group :vendor, :assets_path => "assets/js" do
90
+ asset "jquery" # Assummes it's latests
91
+ asset "backbone", "1.2"
92
+ end
93
+
94
+ # Puts files under ./lib/assets/javascript/bower_components
95
+ group :lib do
96
+ asset "jquery"
97
+ asset "backbone", "1.2"
98
+ end
99
+ ```
100
+ NOTE: All the assets should be stored in `/assets` subdirectory so putting it under `./vendor/js` directory is unavailable
101
+
102
+ ##Rake tasks
103
+
104
+ Once you are done with `bower.json` or `Bowerfile` you can run
105
+
106
+ * `rake bower:install` to install js components
107
+ * `rake bower:install:force` to install with force option
108
+ * `rake bower:update` to update js components
109
+ * `rake bower:update:prune` to update components and uninstall extraneous packages
110
+ * `rake bower:list` to list all packages
@@ -0,0 +1,4 @@
1
+ module BowerRails
2
+ require 'bower-rails/railtie' if defined?(Rails)
3
+ require "bower-rails/dsl"
4
+ end
@@ -0,0 +1,108 @@
1
+ require 'json'
2
+ require 'fileutils'
3
+
4
+ module BowerRails
5
+ class Dsl
6
+
7
+ def self.evalute(filename)
8
+ new.tap { |dsl| dsl.eval_file(File.join(dsl.root_path, filename)) }
9
+ end
10
+
11
+ attr_reader :dependencies, :root_path
12
+
13
+ def initialize
14
+ @dependencies = {}
15
+ @root_path ||= defined?(Rails) ? Rails.root : Dir.pwd
16
+ @assets_path ||= "assets"
17
+ end
18
+
19
+ def eval_file(file)
20
+ instance_eval(File.open(file, "rb") { |f| f.read }, file.to_s)
21
+ end
22
+
23
+ def directories
24
+ @dependencies.keys
25
+ end
26
+
27
+ def group(name, options = {}, &block)
28
+ if custom_assets_path = options[:assets_path]
29
+ assert_asset_path custom_assets_path
30
+ end
31
+ add_group(name, options)
32
+ yield if block_given?
33
+ end
34
+
35
+ def asset(name, version = "latest")
36
+ groups.each do |g|
37
+ g_norm = normalize_location_path(g.first, group_assets_path(g))
38
+ @dependencies[g_norm] ||= {}
39
+ @dependencies[g_norm][name] = version
40
+ end
41
+ end
42
+
43
+ def to_json(location)
44
+ dependencies_to_json @dependencies[normalize_location_path(location)]
45
+ end
46
+
47
+ def write_bower_json
48
+ @dependencies.each do |dir,data|
49
+ FileUtils.mkdir_p dir unless File.directory? dir
50
+ File.open(File.join(dir,"bower.json"), "w") do |f|
51
+ f.write(dependencies_to_json(data))
52
+ end
53
+ end
54
+ end
55
+
56
+ def write_dotbowerrc
57
+ groups.map do |g|
58
+ g_norm = normalize_location_path(g.first, group_assets_path(g))
59
+ File.open(File.join(g_norm, ".bowerrc"), "w") do |f|
60
+ f.write(JSON.pretty_generate({:directory => "bower_components/pt"}))
61
+ end
62
+ end
63
+ end
64
+
65
+ def final_assets_path
66
+ groups.map do |g|
67
+ [g.first.to_s, group_assets_path(g)]
68
+ end
69
+ end
70
+
71
+ def group_assets_path group
72
+ group_options = Hash === group.last ? group.last : {:assets_path => @assets_path}
73
+ group_options[:assets_path]
74
+ end
75
+
76
+ private
77
+
78
+ def add_group(*group)
79
+ @groups = (groups << group)
80
+ end
81
+
82
+ def groups
83
+ @groups ||= [[:vendor, { :assets_path => @assets_path }]]
84
+ end
85
+
86
+ def dependencies_to_json(data)
87
+ JSON.pretty_generate({
88
+ :name => "dsl-generated dependencies",
89
+ :dependencies => data
90
+ })
91
+ end
92
+
93
+ def assets_path(assets_path)
94
+ assert_asset_path assets_path
95
+ @assets_path = assets_path
96
+ end
97
+
98
+ def assert_asset_path(path)
99
+ if !path.start_with?('assets', '/assets')
100
+ raise ArgumentError, "Assets should be stored in /assets directory, try assets_path 'assets/#{path}' instead"
101
+ end
102
+ end
103
+
104
+ def normalize_location_path(loc, assets_path)
105
+ File.join(@root_path, loc.to_s, assets_path)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,30 @@
1
+ require 'bower-rails'
2
+ require 'bower-rails/dsl'
3
+ require 'rails'
4
+
5
+ module BowerRails
6
+ class Railtie < Rails::Railtie
7
+ railtie_name :bower
8
+ @@bowerfile = File.join("Bowerfile")
9
+
10
+ if File.exist?(@@bowerfile)
11
+ config.before_initialize do |app|
12
+ @dsl = BowerRails::Dsl.evalute(@@bowerfile)
13
+
14
+ @dsl.final_assets_path.map do |assets_root, assets_path|
15
+ app.config.assets.paths << Rails.root.join(assets_root, assets_path, "bower_components")
16
+ end
17
+ end
18
+ else
19
+ config.before_initialize do |app|
20
+ ["lib", "vendor"].each do |dir|
21
+ app.config.assets.paths << Rails.root.join(dir, 'assets', 'bower_components')
22
+ end
23
+ end
24
+ end
25
+
26
+ rake_tasks do
27
+ load "tasks/bower.rake"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ module BowerRails
2
+ module Generators
3
+ class InitializeGenerator < Rails::Generators::Base
4
+ desc "Adds a boilerplate component.json file to root of rails project"
5
+
6
+ def self.source_root
7
+ @_bower_rails_source_root ||= File.expand_path("../templates", __FILE__)
8
+ end
9
+
10
+ def create_initializer_file
11
+ template "bower.json", 'bower.json'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ {
2
+ "lib": {
3
+ "name": "bower-rails generated lib assets",
4
+ "dependencies": {
5
+ }
6
+ },
7
+ "vendor": {
8
+ "name": "bower-rails generated vendor assets",
9
+ "dependencies": {
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,123 @@
1
+ require 'json'
2
+ require 'pp'
3
+
4
+ namespace :bower do
5
+ desc "Install components from bower"
6
+ task :install do
7
+ perform do
8
+ sh 'bower install'
9
+ end
10
+ end
11
+
12
+ namespace :install do
13
+ desc "Install components with -F option"
14
+ task :force do
15
+ perform do
16
+ sh 'bower install -F'
17
+ end
18
+ end
19
+ end
20
+
21
+ desc "Update bower components"
22
+ task :update do
23
+ perform do
24
+ sh 'bower update'
25
+ end
26
+ end
27
+
28
+ desc "List bower components"
29
+ task :list do
30
+ perform false do
31
+ sh 'bower list'
32
+ end
33
+ end
34
+
35
+ namespace :update do
36
+ desc "Update existing components and uninstalls extraneous components"
37
+ task :prune do
38
+ perform do
39
+ sh 'bower update && bower prune'
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def perform remove_components = true, &block
46
+ entries = Dir.entries(get_bower_root_path)
47
+
48
+ if entries.include?('Bowerfile')
49
+ dsl_perform_command remove_components, &block
50
+ elsif entries.include?('bower.json')
51
+ perform_command remove_components, &block
52
+ else
53
+ raise LoadError, "No Bowerfile or bower.json file found. Make sure you have it at the root of your project"
54
+ end
55
+ end
56
+
57
+ def get_bower_root_path
58
+ if defined?(Rails)
59
+ return Rails.root
60
+ else
61
+ return Dir.pwd
62
+ end
63
+ end
64
+
65
+ def dsl_perform_command remove_components = true, &block
66
+ dsl = BowerRails::Dsl.evalute("Bowerfile")
67
+
68
+ if remove_components
69
+ dsl.write_bower_json
70
+ dsl.write_dotbowerrc
71
+ puts "bower.js files generated"
72
+ end
73
+
74
+ if block_given?
75
+ dsl.directories.each do |dir|
76
+ Dir.chdir(dir) do
77
+ yield
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ #run the passed bower block in appropriate folders
84
+ def perform_command remove_components = true, &block
85
+ bower_root = get_bower_root_path
86
+ #load in bower json file
87
+ txt = File.read(File.join(bower_root, "bower.json"))
88
+ json = JSON.parse(txt)
89
+
90
+ ["lib", "vendor"].each do |dir|
91
+
92
+ data = json[dir]
93
+
94
+ #check folder existence and create?
95
+ dir = File.join(bower_root, dir, "assets")
96
+ FileUtils.mkdir_p dir unless File.directory? dir
97
+ #go in to dir to act
98
+ Dir.chdir(dir) do
99
+
100
+ #remove old components
101
+ FileUtils.rm_rf("bower_components") if remove_components
102
+
103
+ #create bower json
104
+ File.open("bower.json", "w") do |f|
105
+ f.write(data.to_json)
106
+ end
107
+
108
+ #create .bowerrc
109
+ File.open(".bowerrc", "w") do |f|
110
+ f.write(JSON.pretty_generate({:directory => "bower_components"}))
111
+ end
112
+
113
+ #run command
114
+ yield if block_given?
115
+
116
+ #remove bower file
117
+ FileUtils.rm("bower.json")
118
+
119
+ #remove .bowerrc
120
+ FileUtils.rm(".bowerrc")
121
+ end if data && !data["dependencies"].empty?
122
+ end
123
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bower-rails-pt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ross Harrison
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Rails integration for bower.
47
+ email: rtharrison86@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/generators/bower_rails/initialize/templates/bower.json
53
+ - lib/generators/bower_rails/initialize/initialize_generator.rb
54
+ - lib/bower-rails.rb
55
+ - lib/tasks/bower.rake
56
+ - lib/bower-rails/dsl.rb
57
+ - lib/bower-rails/railtie.rb
58
+ - MIT-LICENSE
59
+ - README.md
60
+ homepage: https://github.com/cj/bower-rails-pt
61
+ licenses:
62
+ - MIT
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.25
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Bower for Rails
85
+ test_files: []
86
+ has_rdoc: