rjs-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +38 -0
  5. data/lib/rjs-rails.rb +8 -0
  6. data/lib/rjs-rails/build.js.erb +13 -0
  7. data/lib/rjs-rails/build_env.rb +68 -0
  8. data/lib/rjs-rails/env.rb +46 -0
  9. data/lib/rjs-rails/railtie.rb +17 -0
  10. data/lib/rjs-rails/rjs_helpers.rb +32 -0
  11. data/lib/rjs-rails/runtime_env.rb +85 -0
  12. data/lib/rjs-rails/version.rb +3 -0
  13. data/lib/tasks/rjs-rails.rake +123 -0
  14. data/test/dummy/README.rdoc +261 -0
  15. data/test/dummy/Rakefile +7 -0
  16. data/test/dummy/app/assets/javascripts/application.js +15 -0
  17. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  18. data/test/dummy/app/controllers/application_controller.rb +3 -0
  19. data/test/dummy/app/helpers/application_helper.rb +2 -0
  20. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  21. data/test/dummy/config.ru +4 -0
  22. data/test/dummy/config/application.rb +59 -0
  23. data/test/dummy/config/boot.rb +10 -0
  24. data/test/dummy/config/database.yml +25 -0
  25. data/test/dummy/config/environment.rb +5 -0
  26. data/test/dummy/config/environments/development.rb +37 -0
  27. data/test/dummy/config/environments/production.rb +67 -0
  28. data/test/dummy/config/environments/test.rb +37 -0
  29. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  30. data/test/dummy/config/initializers/inflections.rb +15 -0
  31. data/test/dummy/config/initializers/mime_types.rb +5 -0
  32. data/test/dummy/config/initializers/secret_token.rb +7 -0
  33. data/test/dummy/config/initializers/session_store.rb +8 -0
  34. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  35. data/test/dummy/config/locales/en.yml +5 -0
  36. data/test/dummy/config/routes.rb +58 -0
  37. data/test/dummy/public/404.html +26 -0
  38. data/test/dummy/public/422.html +26 -0
  39. data/test/dummy/public/500.html +25 -0
  40. data/test/dummy/public/favicon.ico +0 -0
  41. data/test/dummy/script/rails +6 -0
  42. data/test/rjs-rails_test.rb +7 -0
  43. data/test/test_helper.rb +15 -0
  44. data/vendor/assets/javascripts/r.js +25256 -0
  45. data/vendor/assets/javascripts/require.js +36 -0
  46. metadata +144 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b059627e7c4b6f1e515338d289b4ec37dff7b865
4
+ data.tar.gz: ed3121cd6a6ffb02c1f3329e714645d695df1909
5
+ SHA512:
6
+ metadata.gz: 52f5e01eda1731d3118a3c2e14b4ec47c3790dc1281aca3ccf24afd5eff1199ed668a21a41b0d8def059fb803fcaa2e0da27fee98fad3da704f94b657a59bcf8
7
+ data.tar.gz: c2c39222397c61709591c9a81bc0764659b18ac122cf0902e0371d0404ee2e7bb80ec367d00a1ce00daaead9a7685fbd0259e12f57ade2131d03ceaf0e7b5001
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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,3 @@
1
+ = RjsRails
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'RjsRails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,8 @@
1
+ require 'rjs-rails/env'
2
+ require 'rjs-rails/build_env'
3
+ require 'rjs-rails/runtime_env'
4
+ require 'rjs-rails/rjs_helpers'
5
+
6
+ module RjsRails
7
+ end
8
+ require 'rjs-rails/railtie' if defined?(Rails)
@@ -0,0 +1,13 @@
1
+ var baseConfig = <%= JSON.pretty_generate(build_env.config) %>
2
+ baseConfig["onModuleBundleComplete"] = function(data) {
3
+ console.log("Module Finished:", data.path);
4
+ for (var i=0; i<data.included.length; i++) {
5
+ console.log(' ' + data.included[i]);
6
+ }
7
+ console.log("total files:", data.included.length);
8
+ };
9
+
10
+ var requirejs = require("<%= config.rjs_path %>");
11
+
12
+ requirejs.optimize(baseConfig);
13
+
@@ -0,0 +1,68 @@
1
+ module RjsRails
2
+ class BuildEnv
3
+
4
+ attr_accessor :env
5
+
6
+ def initialize(env)
7
+ @env = env
8
+ config
9
+ end
10
+
11
+ def base_config
12
+ return @base_config if @base_config
13
+ base_config = {
14
+ "baseUrl" => env.source_dir,
15
+ "dir" => env.build_dir,
16
+ "optimize" => "none",
17
+ "skipDirOptimize" => true,
18
+ "keepBuildDir" => false,
19
+ "normalizeDirDefines" => "skip"
20
+ }
21
+ end
22
+
23
+ def user_config
24
+ @user_config ||= load_yml_config
25
+ end
26
+
27
+ def config
28
+ @config ||= base_config.merge(user_config)
29
+ end
30
+
31
+ def modules
32
+ config["modules"] || []
33
+ end
34
+
35
+ def module_names
36
+ modules.collect { |m| m["name"] }
37
+ end
38
+
39
+ def yml_exists?
40
+ File.exists? yml
41
+ end
42
+
43
+ def shims
44
+ config["shim"] || {}
45
+ end
46
+
47
+ def assets_for_precompile
48
+ js = []
49
+ js << 'require.js'
50
+ modules.each { |m| js << "#{m["name"]}.js" }
51
+ js
52
+ end
53
+
54
+ private
55
+
56
+ def yml
57
+ env.rjs_build_yml
58
+ end
59
+
60
+ def load_yml_config
61
+ return {} if not File.exists? yml
62
+ File.open(yml) { |f|
63
+ return YAML.load(f.read)
64
+ }
65
+ end
66
+
67
+ end
68
+ end
@@ -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
@@ -0,0 +1,32 @@
1
+ module RjsRails
2
+ module RjsHelpers
3
+
4
+ def rjs_include_tag(js)
5
+ tag = rjs_static_include + rjs_dynamic_template(js)
6
+ tag.html_safe
7
+ end
8
+
9
+ def rjs_static_include
10
+ rjs_runtime.rjs_static_include
11
+ end
12
+
13
+ def rjs_dynamic_template(js)
14
+ "<script data-main=\"#{rjs_javascript_path(js)}\" src=\"#{javascript_path "require.js"}\"></script>"
15
+ end
16
+
17
+ def rjs_javascript_path(js)
18
+ path = javascript_path(js).sub(/^#{rjs_baseurl}/,'')
19
+ path = path.sub(/.js$/,'')
20
+ path
21
+ end
22
+
23
+ def rjs_baseurl()
24
+ rjs_runtime.baseurl
25
+ end
26
+
27
+ def rjs_runtime
28
+ Rails.application.config.rjs.runtime_env
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,85 @@
1
+ module RjsRails
2
+ class RuntimeEnv
3
+
4
+ attr_accessor :env
5
+
6
+ def initialize(env)
7
+ @env = env
8
+ rjs_static_include
9
+ end
10
+
11
+ def base_config
12
+ return @base_config if @base_config
13
+ base_config = {
14
+ "baseUrl" => baseurl,
15
+ "paths" => module_paths,
16
+ "shim" => shims,
17
+ "waitSeconds" => 0
18
+ }
19
+ end
20
+
21
+ def user_config
22
+ @user_config ||= load_yml_config
23
+ end
24
+
25
+ def config
26
+ @config ||= base_config.merge(user_config)
27
+ end
28
+
29
+ def rjs_static_include
30
+ @static_template ||= build_static_include
31
+ end
32
+
33
+
34
+ def yml
35
+ env.rjs_runtime_yml
36
+ end
37
+
38
+ def baseurl
39
+ "#{Rails.application.config.asset_host}#{Rails.application.config.assets.prefix}/"
40
+ end
41
+
42
+ def module_names
43
+ Rails.application.config.rjs.build_env.module_names
44
+ end
45
+
46
+ def shims
47
+ Rails.application.config.rjs.build_env.shims
48
+ end
49
+
50
+ def helpers
51
+ ActionController::Base.helpers
52
+ end
53
+
54
+ def module_path(m)
55
+ helpers.javascript_path("#{m}.js").sub(/.js$/,"")
56
+ end
57
+
58
+ def module_paths
59
+ paths = {}
60
+ module_names.each { |m| paths[m] = module_path(m) }
61
+ paths
62
+ end
63
+
64
+ def static_template
65
+ erb_template = <<-EOF
66
+ <script>var require = <%= JSON.generate(config).html_safe %>;</script>
67
+ EOF
68
+ end
69
+
70
+ def build_static_include
71
+ erb = ERB.new(static_template)
72
+ p = Proc.new { config }
73
+ binding = Kernel.binding
74
+ erb.result(binding)
75
+ end
76
+
77
+ def load_yml_config
78
+ return {} if not File.exists? yml
79
+ File.open(yml) { |f|
80
+ return YAML.load(f.read)
81
+ }
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module RjsRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,123 @@
1
+ namespace :rjs do
2
+
3
+ rjs = RjsRails::Env.new
4
+ config = rjs.config
5
+ source_dir = config.source_dir
6
+ build_dir = config.build_dir
7
+
8
+ def clean_dir(dir)
9
+ print "Cleaning #{dir} ..."
10
+ FileUtils.remove_entry_secure(dir, true)
11
+ FileUtils.mkpath dir
12
+ puts " done"
13
+ end
14
+
15
+ class NoCompression
16
+ def compress(string)
17
+ string
18
+ end
19
+ end
20
+
21
+ def disable_gc
22
+ GC.disable
23
+ begin
24
+ yield
25
+ ensure
26
+ # GC.enable
27
+ # GC.start
28
+ end
29
+ end
30
+
31
+ task "clean_dirs" do
32
+ clean_dir(source_dir)
33
+ clean_dir(build_dir)
34
+ end
35
+
36
+ task "build_init" => ["assets:cache:clean", "rjs:clean_dirs"]
37
+
38
+ desc "Copy project's all javascript files to #{source_dir}"
39
+ task "copy_js_sources" => [ "assets:environment", "rjs:clean_dirs" ] do
40
+ print "Copying js sources"
41
+ Rails.application.config.assets.js_compressor = NoCompression.new
42
+ assets = Rails.application.assets
43
+ js_assets = assets.each_logical_path(["*.js"])
44
+ num_assets = js_assets.count
45
+ count = 0
46
+ js_assets.each do |logical_path|
47
+ if asset = assets.find_asset(logical_path)
48
+ filename = "#{source_dir}/#{logical_path}"
49
+ asset.write_to(filename)
50
+ end
51
+ count += 1
52
+ percent = count*100/num_assets
53
+ print "\rCopying js sources .. #{percent}% complete"
54
+ end
55
+ puts
56
+ end
57
+
58
+ desc "Generate build.js for r.js"
59
+ task "prepare_build_js" do
60
+ puts "Generating build.js"
61
+ template = config.template
62
+ output_file = config.build_js
63
+ binding = rjs.get_binding()
64
+ File.open(template) { |f|
65
+ erb = ERB.new(f.read)
66
+ File.open(output_file , 'w') { |of|
67
+ of.write(erb.result(binding))
68
+ }
69
+ }
70
+ end
71
+
72
+ desc "Rjs asset build rake task"
73
+ task "optimization" do
74
+ command = "node #{config.build_js}"
75
+ IO.popen(command) { |f|
76
+ f.each { |line|
77
+ puts line
78
+ }
79
+ }
80
+ end
81
+
82
+ task "build_end" => [
83
+ "refresh_sprockets_env",
84
+ "additional_assets_for_precompile",
85
+ "reenable_assets_cache_clean"
86
+ ]
87
+
88
+ desc "Add #{build_dir} to sprockets index "
89
+ task "refresh_sprockets_env" do
90
+ assets_env = Rails.application.assets.instance_variable_get("@environment")
91
+ assets_env.prepend_path(build_dir)
92
+ assets_env.js_compressor = Uglifier.new
93
+ Rails.application.assets = assets_env.index
94
+ end
95
+
96
+ task "additional_assets_for_precompile" do
97
+ assets_array = rjs.assets_for_precompile
98
+ Rails.application.config.assets.precompile.concat(assets_array)
99
+ end
100
+
101
+ task "reenable_assets_cache_clean" do
102
+ Rake::Task["assets:cache:clean"].reenable
103
+ end
104
+
105
+
106
+ desc "Build r.js optimized js files to #{build_dir}"
107
+ task "build" => [
108
+ "rjs:build_init",
109
+ "rjs:copy_js_sources",
110
+ "rjs:prepare_build_js",
111
+ "rjs:optimization",
112
+ "rjs:build_end"
113
+ ]
114
+
115
+ desc "rjs assets precompile"
116
+ task "precompile" do
117
+ if rjs.precompile_required?
118
+ Rake::Task["rjs:build"].invoke
119
+ end
120
+ end
121
+
122
+ end
123
+ task "assets:precompile" => ["rjs:precompile"]