rad_assets 0.2.0 → 0.2.1

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.
Files changed (33) hide show
  1. data/Rakefile +1 -1
  2. data/lib/components/assets.rb +3 -4
  3. data/lib/components/assets.yml +1 -0
  4. data/lib/rad/assets/assets.rb +45 -0
  5. data/lib/{assets → rad/assets}/compressor.rb +5 -5
  6. data/lib/{assets → rad/assets}/gems.rb +0 -0
  7. data/lib/{assets → rad/assets}/packaged_resource.rb +15 -10
  8. data/lib/rad/assets/require.rb +18 -0
  9. data/lib/{assets → rad/assets}/resource.rb +4 -4
  10. data/lib/rad/assets/static_files.rb +24 -0
  11. data/lib/rad/assets/tasks.rb +6 -0
  12. data/lib/rad/assets/view_helper.rb +21 -0
  13. data/lib/{assets → rad}/vendor/jsmin.rb +0 -0
  14. data/spec/assets_spec/app/lib/app/init.rb +2 -0
  15. data/spec/assets_spec/app/runtime/emptygit +0 -0
  16. data/spec/assets_spec/app/runtime/public/emptygit +0 -0
  17. data/spec/assets_spec/plugin_a/lib/plugin_a/init.rb +0 -0
  18. data/spec/assets_spec/plugin_b/lib/plugin_b/init.rb +3 -0
  19. data/spec/assets_spec/plugin_b/static/app.js +0 -0
  20. data/spec/assets_spec/plugin_b/static/lib/vendor.js +0 -0
  21. data/spec/assets_spec.rb +13 -36
  22. data/spec/{assets_spec → resources_spec/development}/app.js +0 -0
  23. data/spec/{assets_spec → resources_spec/development}/lib/tools.js +0 -0
  24. data/spec/{assets_spec → resources_spec/development}/vendor/jquery.js +0 -0
  25. data/spec/resources_spec/production/public/static/app.js +3 -0
  26. data/spec/resources_spec/production/public/static/lib/tools.js +2 -0
  27. data/spec/resources_spec/production/public/static/vendor/jquery.js +1 -0
  28. data/spec/resources_spec.rb +47 -0
  29. data/spec/spec_helper.rb +1 -1
  30. metadata +28 -36
  31. data/lib/assets/assets.rb +0 -18
  32. data/lib/assets/require.rb +0 -16
  33. data/lib/assets/view_helper.rb +0 -9
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rake_ext'
3
3
  project(
4
4
  name: "assets",
5
5
  official_name: "rad_assets",
6
- version: "0.2.0",
6
+ gem: true,
7
7
  summary: "Assets for Rad Framework",
8
8
 
9
9
  author: "Alexey Petrushin",
@@ -1,5 +1,4 @@
1
- rad.register :assets do
2
- require 'assets/require'
3
-
4
- Assets.new
1
+ rad.register :assets, depends_on: :environment do
2
+ require 'rad/assets/require'
3
+ Rad::Assets.new
5
4
  end
@@ -0,0 +1 @@
1
+ static_path_prefix: '/static'
@@ -0,0 +1,45 @@
1
+ class Rad::Assets
2
+ inject logger: :logger, environment: :environment
3
+
4
+ #
5
+ # Config
6
+ #
7
+ attr_accessor :static_path_prefix
8
+ require_attr :static_path_prefix
9
+
10
+
11
+ def resolve_http_paths http_path
12
+ resource = pack? ? Rad::Assets::PackagedResource.new(http_path) : Rad::Assets::Resource.new(http_path)
13
+ resource.resolved_http_paths
14
+ end
15
+ cache_method_with_params_in_production :resolve_http_paths
16
+
17
+ def paths
18
+ @paths ||= []
19
+ end
20
+
21
+ def copy_to_public!
22
+ paths.each do |path|
23
+ path = path.to_dir
24
+ raise "asset path should exist (#{path})!" unless path.exist?
25
+ raise "asset path should be a directory (#{path})!" unless path.dir?
26
+ p path, "#{rad.http.public_path}#{static_path_prefix}"
27
+ path.copy_to! "#{rad.http.public_path}#{static_path_prefix}".to_dir
28
+ end
29
+ end
30
+
31
+ def fs_path http_path
32
+ http_path.must =~ /^\//
33
+ environment.find_file http_path, paths
34
+ end
35
+
36
+ def asset? http_path
37
+ !!((http_path =~ /^\//) and fs_path(http_path))
38
+ end
39
+ cache_method_with_params_in_production :asset?
40
+
41
+ protected
42
+ def pack?
43
+ rad.production?
44
+ end
45
+ end
@@ -1,9 +1,9 @@
1
- class Assets::Compressor
1
+ class Rad::Assets::Compressor
2
2
  COMPRESSORS = {
3
- js: -> path, packaged_path {
4
- jsmin = "#{__FILE__.dirname}/vendor/jsmin.rb"
5
- `ruby #{jsmin} <#{path} >#{packaged_path} \n`
6
- },
3
+ # js: -> path, packaged_path {
4
+ # jsmin = "#{__FILE__.dirname}/../vendor/jsmin.rb"
5
+ # `ruby #{jsmin} <#{path} >#{packaged_path} \n`
6
+ # },
7
7
  css: -> path, packaged_path {
8
8
  data = path.to_file.read
9
9
  data.gsub!(/\s+/, " ") # collapse space
File without changes
@@ -1,18 +1,19 @@
1
- class Assets::PackagedResource < Assets::Resource
1
+ class Rad::Assets::PackagedResource < Rad::Assets::Resource
2
2
  PACKAGED_POSTFIX = 'packaged'
3
- inject config: :config
4
3
 
5
4
  def resolved_http_paths
6
- fs_path = assets.fs_path http_path
5
+ http_path = "#{assets.static_path_prefix}#{self.http_path}"
6
+
7
+ fs_path = self.fs_path http_path
7
8
  http_paths = []
8
9
  fs_path.to_file.read.scan ASSET_REQUIRE_RE do |dependency_http_path|
9
- res = Assets::PackagedResource.new(dependency_http_path.first)
10
+ res = Rad::Assets::PackagedResource.new(dependency_http_path.first)
10
11
  http_paths.push *res.resolved_http_paths
11
12
  end
12
- http_paths << http_path
13
+ http_paths << http_path
13
14
 
14
- fs_paths = http_paths.collect{|path| "#{config.public_dir!}#{path}"}
15
- packaged_file = "#{config.public_dir!}#{packaged_http_path}".to_file
15
+ fs_paths = http_paths.collect{|path| self.fs_path path}
16
+ packaged_file = self.fs_path(packaged_http_path(http_path)).to_file
16
17
 
17
18
  rebuild = (
18
19
  !packaged_file.exist? or
@@ -20,11 +21,15 @@ class Assets::PackagedResource < Assets::Resource
20
21
  )
21
22
  rebuild! packaged_file, fs_paths if rebuild
22
23
 
23
- [packaged_http_path]
24
+ [packaged_http_path(http_path)]
24
25
  end
25
26
 
26
27
  protected
27
- def packaged_http_path
28
+ def fs_path http_path
29
+ "#{rad.http.public_path}#{http_path}"
30
+ end
31
+
32
+ def packaged_http_path http_path
28
33
  extension = File.extname(http_path)
29
34
  base = http_path[0..(http_path.size - extension.size - 1)]
30
35
  "#{base}.packaged#{extension}"
@@ -41,6 +46,6 @@ class Assets::PackagedResource < Assets::Resource
41
46
  end
42
47
 
43
48
  # packing
44
- Assets::Compressor.pack! packaged_file.path
49
+ Rad::Assets::Compressor.pack! packaged_file.path
45
50
  end
46
51
  end
@@ -0,0 +1,18 @@
1
+ require 'rad/assets/gems'
2
+
3
+ require 'vfs'
4
+ require 'rad'
5
+
6
+ class Rad::Assets
7
+ autoload :StaticFiles, 'rad/assets/static_files'
8
+ end
9
+
10
+ %w(
11
+ compressor
12
+ assets
13
+ resource
14
+ packaged_resource
15
+ view_helper
16
+ ).each{|f| require "rad/assets/#{f}"}
17
+
18
+ Rad::Template::Context.include Rad::Assets::ViewHelper
@@ -1,4 +1,4 @@
1
- class Assets::Resource
1
+ class Rad::Assets::Resource
2
2
  attr_reader :http_path
3
3
  inject assets: :assets
4
4
 
@@ -10,13 +10,13 @@ class Assets::Resource
10
10
  end
11
11
 
12
12
  def resolved_http_paths
13
- fs_path = assets.fs_path http_path
13
+ fs_path = assets.fs_path(http_path) || raise("no asset #{http_path}!")
14
14
  paths = []
15
15
  fs_path.to_file.read.scan ASSET_REQUIRE_RE do |dependency_http_path|
16
- res = Assets::Resource.new(dependency_http_path.first)
16
+ res = Rad::Assets::Resource.new(dependency_http_path.first)
17
17
  paths.push *res.resolved_http_paths
18
18
  end
19
- paths << http_path
19
+ paths << "#{assets.static_path_prefix}#{http_path}"
20
20
  paths.uniq
21
21
  end
22
22
  end
@@ -0,0 +1,24 @@
1
+ class Rad::Assets::StaticFiles < Rack::File
2
+ def initialize(app, filter = nil)
3
+ @app, @filter = app, filter
4
+ end
5
+
6
+ def call(env)
7
+ path = env["PATH_INFO"]
8
+ if path != '/' and (!@filter or (@filter and @filter =~ path)) and (resolved_path = find_file(path))
9
+ @path = resolved_path
10
+ serving(env)
11
+ else
12
+ @app.call(env)
13
+ end
14
+ end
15
+
16
+ protected
17
+ inject assets: :assets
18
+
19
+ def find_file http_path
20
+ http_path = http_path.sub rad.assets.static_path_prefix, ''
21
+ fs_path = assets.fs_path http_path
22
+ fs_path.sub rad.http.public_path, '' if fs_path
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ namespace :assets do
2
+ desc "Copy assets to public folder"
3
+ task copy_to_public: :environment do
4
+ rad.assets.copy_to_public!
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ module Rad::Assets::ViewHelper
2
+ def packaged_assets *http_paths
3
+ resolved_http_paths = []
4
+ http_paths.each do |http_path|
5
+ if rad.assets.asset? http_path
6
+ resolved_http_paths.push *rad.assets.resolve_http_paths(http_path)
7
+ else
8
+ resolved_http_paths << http_path
9
+ end
10
+ end
11
+ resolved_http_paths
12
+ end
13
+
14
+ def stylesheet_link_tag *a
15
+ super(*packaged_assets(*a))
16
+ end
17
+
18
+ def javascript_include_tag *a
19
+ super(*packaged_assets(*a))
20
+ end
21
+ end
File without changes
@@ -0,0 +1,2 @@
1
+ load 'plugin_a/init.rb'
2
+ load 'plugin_b/init.rb'
File without changes
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ plugin_dir = File.expand_path "#{__FILE__}/../../.."
2
+
3
+ rad.assets.paths << "#{plugin_dir}/static"
File without changes
File without changes
data/spec/assets_spec.rb CHANGED
@@ -1,46 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'Assets' do
4
- with_tmp_spec_dir
5
- isolate :config
4
+ the_spec_dir = with_tmp_spec_dir
5
+ with_load_path(
6
+ "#{the_spec_dir}/app/lib",
7
+ "#{the_spec_dir}/plugin_a/lib",
8
+ "#{the_spec_dir}/plugin_b/lib",
9
+ )
10
+
6
11
  inject assets: :assets
7
12
 
8
13
  before do
9
- config.merge!({public_dir: spec_dir}, override: true)
10
- assets.stub!(:fs_path){|http_path| "#{spec_dir}#{http_path}"}
14
+ rad.stub!(:runtime_path).and_return("#{spec_dir}/app/runtime")
15
+ load "app/init.rb"
11
16
  end
12
17
 
13
- describe "development" do
14
- before{assets.stub!(:pack?).and_return(false)}
15
-
16
- it "resolved_http_paths" do
17
- assets.resolved_http_paths('/app.js').should == %w(/vendor/jquery.js /lib/tools.js /app.js)
18
- end
19
- end
20
-
21
- describe "production" do
22
- before{assets.stub!(:pack?).and_return(true)}
23
-
24
- it "resolved_http_paths" do
25
- assets.resolved_http_paths('/app.js').should == %w(/app.packaged.js)
26
-
27
- packaged_file = "#{spec_dir}/app.packaged.js".to_file
28
- packaged_file.should exist
29
- packaged_file.read.should =~ /jQuery.*Tools.*App/m
30
-
31
- "#{spec_dir}/lib/tools.packaged.js".to_file.should exist
32
- "#{spec_dir}/vendor/jquery.packaged.js".to_file.should exist
33
- end
34
-
35
- it "should create new packed version if sources are updated" do
36
- assets.resolved_http_paths('/app.js').should == %w(/app.packaged.js)
37
- packaged_file = "#{spec_dir}/app.packaged.js".to_file
38
- packaged_file.read.should =~ /jQuery.*Tools.*App/m
39
-
40
- sleep 1.1 # file system can't notice smaller difference in file update time
41
- "#{spec_dir}/vendor/jquery.js".to_file.write "newQuery"
42
- assets.resolved_http_paths('/app.js').should == %w(/app.packaged.js)
43
- packaged_file.read.should =~ /newQuery.*Tools.*App/m
44
- end
18
+ it "should copy assets" do
19
+ rad.assets.copy_to_public!
20
+ "#{spec_dir}/app/runtime/public/static/app.js".to_file.should exist
21
+ "#{spec_dir}/app/runtime/public/static/lib/vendor.js".to_file.should exist
45
22
  end
46
23
  end
@@ -0,0 +1,3 @@
1
+ // rad.assets.require '/vendor/jquery.js'
2
+ // rad.assets.require '/lib/tools.js'
3
+ App
@@ -0,0 +1,2 @@
1
+ // rad.assets.require '/vendor/jquery.js'
2
+ Tools
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Resources' do
4
+ with_tmp_spec_dir
5
+ inject assets: :assets
6
+
7
+ describe "development" do
8
+ before do
9
+ assets.stub!(:fs_path){|http_path| "#{spec_dir}/development#{http_path}"}
10
+ assets.stub!(:pack?).and_return(false)
11
+ end
12
+
13
+ it "resolve_http_paths" do
14
+ assets.resolve_http_paths('/app.js').should == %w(/static/vendor/jquery.js /static/lib/tools.js /static/app.js)
15
+ end
16
+ end
17
+
18
+ describe "production" do
19
+ before do
20
+ rad.http.stub!(:public_path).and_return("#{spec_dir}/production/public")
21
+ assets.stub!(:pack?).and_return(true)
22
+ @static_dir = "#{spec_dir}/production/public/static"
23
+ end
24
+
25
+ it "resolve_http_paths" do
26
+ assets.resolve_http_paths('/app.js').should == %w(/static/app.packaged.js)
27
+
28
+ packaged_file = "#{@static_dir}/app.packaged.js".to_file
29
+ packaged_file.should exist
30
+ packaged_file.read.should =~ /jQuery.*Tools.*App/m
31
+
32
+ "#{@static_dir}/lib/tools.packaged.js".to_file.should exist
33
+ "#{@static_dir}/vendor/jquery.packaged.js".to_file.should exist
34
+ end
35
+
36
+ it "should create new packed version if sources are updated" do
37
+ assets.resolve_http_paths('/app.js').should == %w(/static/app.packaged.js)
38
+ packaged_file = "#{@static_dir}/app.packaged.js".to_file
39
+ packaged_file.read.should =~ /jQuery.*Tools.*App/m
40
+
41
+ sleep 1.1 # file system can't notice smaller difference in file update time
42
+ "#{@static_dir}/vendor/jquery.js".to_file.write "newQuery"
43
+ assets.resolve_http_paths('/app.js').should == %w(/static/app.packaged.js)
44
+ packaged_file.read.should =~ /newQuery.*Tools.*App/m
45
+ end
46
+ end
47
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'assets/require'
1
+ require 'rad'
2
2
 
3
3
  require 'rspec_ext'
4
4
  require 'rad/spec'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rad_assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,31 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-17 00:00:00.000000000 +04:00
12
+ date: 2011-07-04 00:00:00.000000000 +04:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rad_core
17
- requirement: &2763970 !ruby/object:Gem::Requirement
18
- none: false
19
- requirements:
20
- - - ! '>='
21
- - !ruby/object:Gem::Version
22
- version: '0'
23
- type: :runtime
24
- prerelease: false
25
- version_requirements: *2763970
26
- - !ruby/object:Gem::Dependency
27
- name: vfs
28
- requirement: &2763660 !ruby/object:Gem::Requirement
29
- none: false
30
- requirements:
31
- - - ! '>='
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: *2763660
14
+ dependencies: []
37
15
  description:
38
16
  email:
39
17
  executables: []
@@ -42,19 +20,33 @@ extra_rdoc_files: []
42
20
  files:
43
21
  - Rakefile
44
22
  - readme.md
45
- - lib/assets/assets.rb
46
- - lib/assets/compressor.rb
47
- - lib/assets/gems.rb
48
- - lib/assets/packaged_resource.rb
49
- - lib/assets/require.rb
50
- - lib/assets/resource.rb
51
- - lib/assets/vendor/jsmin.rb
52
- - lib/assets/view_helper.rb
53
23
  - lib/components/assets.rb
54
- - spec/assets_spec/app.js
55
- - spec/assets_spec/lib/tools.js
56
- - spec/assets_spec/vendor/jquery.js
24
+ - lib/components/assets.yml
25
+ - lib/rad/assets/assets.rb
26
+ - lib/rad/assets/compressor.rb
27
+ - lib/rad/assets/gems.rb
28
+ - lib/rad/assets/packaged_resource.rb
29
+ - lib/rad/assets/require.rb
30
+ - lib/rad/assets/resource.rb
31
+ - lib/rad/assets/static_files.rb
32
+ - lib/rad/assets/tasks.rb
33
+ - lib/rad/assets/view_helper.rb
34
+ - lib/rad/vendor/jsmin.rb
35
+ - spec/assets_spec/app/lib/app/init.rb
36
+ - spec/assets_spec/app/runtime/emptygit
37
+ - spec/assets_spec/app/runtime/public/emptygit
38
+ - spec/assets_spec/plugin_a/lib/plugin_a/init.rb
39
+ - spec/assets_spec/plugin_b/lib/plugin_b/init.rb
40
+ - spec/assets_spec/plugin_b/static/app.js
41
+ - spec/assets_spec/plugin_b/static/lib/vendor.js
57
42
  - spec/assets_spec.rb
43
+ - spec/resources_spec/development/app.js
44
+ - spec/resources_spec/development/lib/tools.js
45
+ - spec/resources_spec/development/vendor/jquery.js
46
+ - spec/resources_spec/production/public/static/app.js
47
+ - spec/resources_spec/production/public/static/lib/tools.js
48
+ - spec/resources_spec/production/public/static/vendor/jquery.js
49
+ - spec/resources_spec.rb
58
50
  - spec/spec_helper.rb
59
51
  has_rdoc: true
60
52
  homepage: http://github.com/alexeypetrushin/rad_assets
data/lib/assets/assets.rb DELETED
@@ -1,18 +0,0 @@
1
- class Assets
2
- inject logger: :logger, config: :config
3
-
4
- def resolved_http_paths http_path
5
- resource = pack? ? PackagedResource.new(http_path) : Resource.new(http_path)
6
- resource.resolved_http_paths
7
- end
8
- cache_method_with_params_in_production :resolved_http_paths
9
-
10
- def fs_path http_path
11
- http.fs_path http_path
12
- end
13
-
14
- protected
15
- def pack?
16
- rad.production?
17
- end
18
- end
@@ -1,16 +0,0 @@
1
- require 'assets/gems'
2
-
3
- require 'vfs'
4
- require 'rad'
5
-
6
- class Assets; end
7
-
8
- %w(
9
- compressor
10
- assets
11
- resource
12
- packaged_resource
13
- view_helper
14
- ).each{|f| require "assets/#{f}"}
15
-
16
- Rad::Template::Context.include Assets::ViewHelper
@@ -1,9 +0,0 @@
1
- module Assets::ViewHelper
2
- def packaged_assets *http_paths
3
- resolved_http_paths = []
4
- http_paths.each do |http_path|
5
- resolved_http_paths.push *rad.assets.resolved_http_paths(http_path)
6
- end
7
- resolved_http_paths
8
- end
9
- end