rjs-rails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dff38938652a0701fef1f2104811298da7d5060a
4
- data.tar.gz: bc0792ad64df754893be31ebfd62e7a769350aa9
3
+ metadata.gz: d03357dc42a524d14a96a92d0558bb0bbf90a1f5
4
+ data.tar.gz: e4b4b038dfb70351d7cc5f0745b48221dcf6b6cc
5
5
  SHA512:
6
- metadata.gz: 7a7d56035c5930620b2e96dd3057f9ceedef9c188ce605fe5717e7a28a7d774fcffb77d9f8ad98f1466e0ee21433233f62ff02937d85ad7e1573c724ee657f10
7
- data.tar.gz: a5c343914c6528fa5cceb2f2b893c9f262ea9f5d8c967068b9495cc56b349228201c7a22deee15f9d87df744adf414175744185b6ea4b31498bcdc09766df61d
6
+ metadata.gz: 2a0f556e170e39ca84162a108768c53e965beabde97a7a20cfd62324f856bb2e2633d7106aa505fb0e744175e2b6b3d6ef1d6aef79452d38e20a36054f84a748
7
+ data.tar.gz: 499cea07c02aa931cc19cf975e92e0743bbffd59dc62d6233b19be1005596f1c280f3e129ebdef1b7b8e7ac58aac8781fb7ec5ad9690b3d4688908e6ddbf5d39
data/lib/rjs-rails.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require 'rjs-rails/env'
2
- require 'rjs-rails/build_env'
3
- require 'rjs-rails/runtime_env'
4
- require 'rjs-rails/rjs_helpers'
2
+ require 'rjs-rails/build/api'
5
3
 
6
4
  module RjsRails
7
5
  end
@@ -1,4 +1,4 @@
1
- var baseConfig = <%= JSON.pretty_generate(build_env.config) %>
1
+ var baseConfig = <%= JSON.pretty_generate(config_hash) %>
2
2
  baseConfig["onModuleBundleComplete"] = function(data) {
3
3
  console.log("Module Finished:", data.path);
4
4
  for (var i=0; i<data.included.length; i++) {
@@ -7,7 +7,7 @@ baseConfig["onModuleBundleComplete"] = function(data) {
7
7
  console.log("total files:", data.included.length);
8
8
  };
9
9
 
10
- var requirejs = require("<%= config.rjs_path %>");
10
+ var requirejs = require("<%= env.rjs_path %>");
11
11
 
12
12
  requirejs.optimize(baseConfig);
13
13
 
@@ -0,0 +1,140 @@
1
+ require 'rjs-rails/env'
2
+ require 'rjs-rails/sources'
3
+ require 'rjs-rails/sprockets'
4
+ require 'rjs-rails/build/config'
5
+
6
+ module RjsRails
7
+ module Build
8
+ class Api
9
+
10
+ def env
11
+ @env ||= ::RjsRails::Env.new
12
+ end
13
+
14
+ def precompile
15
+ copy_sources
16
+ generate_build_js
17
+ optimize
18
+ prepend_path
19
+ add_assets_for_precompile
20
+ end
21
+
22
+ def copy_sources
23
+ empty_dirs
24
+ puts "=> copying sources"
25
+ asset_paths.each_with_index do |logical_path, i|
26
+ cr = "\r"
27
+ clear = "\e[0K"
28
+ print cr
29
+ print clear
30
+ print "#{i+1}/#{num_assets} #{logical_path}"
31
+ write_asset logical_path
32
+ end
33
+ puts
34
+ end
35
+
36
+
37
+ def generate_build_js
38
+ puts "=> generating build.js"
39
+ template = env.template
40
+ output_file = env.build_js
41
+ File.open(template) do |f|
42
+ erb = ERB.new(f.read)
43
+ File.open(output_file , 'w') do |of|
44
+ of.write erb.result(binding)
45
+ end
46
+ end
47
+ end
48
+
49
+ def optimize
50
+ puts "=> optimizing "
51
+ command = "node #{env.build_js}"
52
+ IO.popen(command) do |f|
53
+ f.each { |line| puts line }
54
+ end
55
+ end
56
+
57
+ def prepend_path
58
+ puts "=> prepend_path"
59
+ assets_env = sprockets.rails_assets_env
60
+ assets_env.prepend_path env.build_dir
61
+ case Rails.env
62
+ when "development"
63
+ Rails.application.assets = assets_env
64
+ else
65
+ Rails.application.assets = assets_env.index
66
+ end
67
+ end
68
+
69
+ def add_assets_for_precompile
70
+ puts "=> auto adding assets for precompile"
71
+ assets_array = config.assets_for_precompile
72
+ Rails.application.config.assets.precompile.concat(assets_array)
73
+ end
74
+
75
+
76
+ def config
77
+ @config ||= Config.new(env)
78
+ end
79
+
80
+ def config_hash
81
+ config.to_h
82
+ end
83
+
84
+ def assets
85
+ @assets ||= sprockets.index
86
+ end
87
+
88
+ def sources
89
+ @sources || ::RjsRails::Sources.new(env)
90
+ end
91
+
92
+ def reload!
93
+ @env = nil
94
+ @sources = nil
95
+ @assets = nil
96
+ @config = nil
97
+ @asset_paths = nil
98
+ @num_assets = nil
99
+ end
100
+
101
+ private
102
+
103
+ def empty_dirs
104
+ puts "Emptying #{env.sources_dir}"
105
+ FileUtils.remove_entry_secure(env.sources_dir, true)
106
+ FileUtils.mkpath env.sources_dir
107
+
108
+ puts "Emptying #{env.build_dir}"
109
+ FileUtils.remove_entry_secure(env.build_dir, true)
110
+ FileUtils.mkpath env.build_dir
111
+ end
112
+
113
+ def logical_paths
114
+ sources.paths + config.sources
115
+ end
116
+
117
+ def write_asset(logical_path)
118
+ if asset = assets.find_asset(logical_path)
119
+ filename = "#{env.sources_dir}/#{logical_path}"
120
+ asset.write_to(filename)
121
+ else
122
+ puts "WARNING could not find #{logical_path}"
123
+ end
124
+ end
125
+
126
+ def asset_paths
127
+ @asset_paths ||= assets.each_logical_path(logical_paths)
128
+ end
129
+
130
+ def num_assets
131
+ @num_assets ||= asset_paths.count
132
+ end
133
+
134
+ def sprockets
135
+ @sprockets ||= ::RjsRails::Sprockets.new
136
+ end
137
+
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,99 @@
1
+ module RjsRails
2
+ module Build
3
+ class Config
4
+
5
+ attr_accessor :env
6
+
7
+ def initialize(env = nil)
8
+ self.env = env
9
+ end
10
+
11
+ def config
12
+ @config ||= base_config.merge(user_config)
13
+ end
14
+
15
+ def to_h
16
+ config
17
+ end
18
+
19
+ def modules
20
+ config.fetch "modules", []
21
+ end
22
+
23
+ def module_names
24
+ modules.collect { |m| m["name"] }
25
+ end
26
+
27
+ def shims
28
+ config.fetch "shim", {}
29
+ end
30
+
31
+ def paths
32
+ config.fetch "paths", {}
33
+ end
34
+
35
+ def sources
36
+ sources = []
37
+ module_names.each { |m| sources << "#{m}.js" }
38
+ paths_hash = paths
39
+ shims.each do |key, _|
40
+ if paths_hash.has_key? key
41
+ sources << "#{paths_hash[key]}.js"
42
+ else
43
+ sources << "#{key}.js"
44
+ end
45
+ end
46
+ return sources
47
+ end
48
+
49
+ def assets_for_precompile
50
+ js = []
51
+ js << 'require.js'
52
+ module_names.each { |m| js << "#{m}.js" }
53
+ js
54
+ end
55
+
56
+ def precompile_required?
57
+ ! user_config.empty?
58
+ end
59
+
60
+ private
61
+
62
+ def env
63
+ @env ||= ::RjsRails::Env.new
64
+ end
65
+
66
+ def config_path
67
+ @config_path ||= Pathname.new(env.build_yml)
68
+ end
69
+
70
+ def readable?
71
+ config_path.readable?
72
+ end
73
+
74
+
75
+ def load_config
76
+ return {} if not readable?
77
+ File.open config_path do |f|
78
+ YAML.load f.read
79
+ end
80
+ end
81
+
82
+ def base_config
83
+ base_config = ({
84
+ "baseUrl" => env.sources_dir,
85
+ "dir" => env.build_dir,
86
+ "optimize" => "none",
87
+ "skipDirOptimize" => true,
88
+ "keepBuildDir" => false,
89
+ "normalizeDirDefines" => "skip"
90
+ })
91
+ end
92
+
93
+ def user_config
94
+ @user_config ||= load_config
95
+ end
96
+
97
+ end
98
+ end
99
+ end
data/lib/rjs-rails/env.rb CHANGED
@@ -1,46 +1,30 @@
1
1
  module RjsRails
2
2
  class Env
3
3
 
4
- def config
5
- return @conf if @conf
6
- c = ::ActiveSupport::OrderedOptions.new
7
- c.extensions = [/.js$/]
8
- c.source_dir = "#{Rails.root}/tmp/assets_source_dir"
9
- c.build_dir = "#{Rails.root}/tmp/assets_build_dir"
10
- c.rjs_build_yml = "#{Rails.root}/config/rjs_build.yml"
11
- c.rjs_runtime_yml = "#{Rails.root}/config/rjs_runtime.yml"
12
- c.rjs_path = File.expand_path("../../vendor/assets/javascripts/r.js", File.dirname(__FILE__))
13
- c.template = File.expand_path("./build.js.erb", File.dirname(__FILE__))
14
- c.build_js = "#{Rails.root}/tmp/build.js"
15
- @conf = c
16
- end
4
+ attr_accessor :sources_dir, :build_dir, :build_js
5
+ attr_accessor :build_yml, :sources_yml, :rjs_path
6
+ attr_accessor :template, :runtime_yml
17
7
 
18
- def build_env
19
- @build_env ||= RjsRails::BuildEnv.new(config)
20
- end
21
-
22
- def runtime_env
23
- @runtime_env ||= RjsRails::RuntimeEnv.new(config)
24
- end
25
8
 
26
- def reload!
27
- @build_env = nil
28
- @runtime_env = nil
29
- build_env
30
- runtime_env
9
+ def initialize
10
+ self.sources_dir = rails_path "tmp/assets_source_dir"
11
+ self.build_dir = rails_path "tmp/assets_build_dir"
12
+ self.build_js = rails_path "tmp/build.js"
13
+ self.build_yml = rails_path "config/rjs/build.yml"
14
+ self.sources_yml = rails_path "config/rjs/sources.yml"
15
+ self.runtime_yml = rails_path "config/rjs/runtime.yml"
16
+ self.rjs_path = relative_path "../../vendor/assets/javascripts/r.js"
17
+ self.template = relative_path "./build.js.erb"
31
18
  end
32
19
 
33
- def get_binding()
34
- binding()
35
- end
36
-
37
- def precompile_required?
38
- build_env.yml_exists?
20
+ def relative_path(path)
21
+ dir = File.dirname(__FILE__)
22
+ File.expand_path path, dir
39
23
  end
40
24
 
41
- def assets_for_precompile
42
- build_env.assets_for_precompile
43
- end
25
+ def rails_path(path)
26
+ "#{Rails.root}/#{path}"
27
+ end
44
28
 
45
29
  end
46
- end
30
+ end
@@ -64,7 +64,7 @@ module RjsRails
64
64
  def load_yml_config
65
65
  return {} if not File.exists? yml
66
66
  File.open(yml) { |f|
67
- return YAML.load(f.read)
67
+ YAML.load(f.read)
68
68
  }
69
69
  end
70
70
 
@@ -0,0 +1,46 @@
1
+ module RjsRails
2
+ class Env
3
+
4
+ def config
5
+ return @conf if @conf
6
+ c = ::ActiveSupport::OrderedOptions.new
7
+ c.extensions = [/.js$/]
8
+ c.source_dir = "#{Rails.root}/tmp/assets_source_dir"
9
+ c.build_dir = "#{Rails.root}/tmp/assets_build_dir"
10
+ c.rjs_build_yml = "#{Rails.root}/config/rjs_build.yml"
11
+ c.rjs_runtime_yml = "#{Rails.root}/config/rjs_runtime.yml"
12
+ c.rjs_path = File.expand_path("../../vendor/assets/javascripts/r.js", File.dirname(__FILE__))
13
+ c.template = File.expand_path("./build.js.erb", File.dirname(__FILE__))
14
+ c.build_js = "#{Rails.root}/tmp/build.js"
15
+ @conf = c
16
+ end
17
+
18
+ def build_env
19
+ @build_env ||= RjsRails::BuildEnv.new(config)
20
+ end
21
+
22
+ def runtime_env
23
+ @runtime_env ||= RjsRails::RuntimeEnv.new(config)
24
+ end
25
+
26
+ def reload!
27
+ @build_env = nil
28
+ @runtime_env = nil
29
+ build_env
30
+ runtime_env
31
+ end
32
+
33
+ def get_binding()
34
+ binding()
35
+ end
36
+
37
+ def precompile_required?
38
+ build_env.yml_exists?
39
+ end
40
+
41
+ def assets_for_precompile
42
+ build_env.assets_for_precompile
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,17 @@
1
+ module RjsRails
2
+ class Railtie < Rails::Railtie
3
+ initializer "rjs_rails.configure_rails_initialization" do
4
+ config.rjs = RjsRails::Env.new
5
+ ActionView::Base.send :include, RjsRails::RjsHelpers
6
+ end
7
+
8
+ config.to_prepare do
9
+ Rails.application.config.rjs.reload!
10
+ end
11
+
12
+ rake_tasks do
13
+ load File.expand_path("../tasks/rjs-rails.rake", File.dirname(__FILE__))
14
+ end
15
+
16
+ end
17
+ end
@@ -1,12 +1,15 @@
1
+ require "rjs-rails/runtime/api"
2
+ require "rjs-rails/rjs_helpers"
3
+
1
4
  module RjsRails
2
5
  class Railtie < Rails::Railtie
3
6
  initializer "rjs_rails.configure_rails_initialization" do
4
- config.rjs = RjsRails::Env.new
7
+ config.rjs_runtime = RjsRails::Runtime::Api.new
5
8
  ActionView::Base.send :include, RjsRails::RjsHelpers
6
9
  end
7
10
 
8
11
  config.to_prepare do
9
- Rails.application.config.rjs.reload!
12
+ Rails.application.config.rjs_runtime.reload!
10
13
  end
11
14
 
12
15
  rake_tasks do
@@ -14,4 +17,4 @@ module RjsRails
14
17
  end
15
18
 
16
19
  end
17
- end
20
+ end