zassets 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,3 +1,11 @@
1
1
  source :rubygems
2
2
 
3
3
  gemspec
4
+
5
+ group :development, :test do
6
+ gem 'guard', '~> 1.4'
7
+ gem 'guard-rspec', '~> 2.0'
8
+ if RbConfig::CONFIG['target_os'] =~ /linux/i
9
+ gem 'rb-inotify', '~> 0.8'
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ guard 'rspec' do
2
+ watch(%r{\Aspec/.+_spec\.rb\z})
3
+ watch(%r{\Alib/(.+)\.rb\z}) { |m| "spec/#{m[1]}_spec.rb" }
4
+
5
+ watch(%r{\Aspec/support/.+\.rb\z}) { 'spec' }
6
+ end
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- z'assets
2
- ========
1
+ z'assets - standalone asset pipeline
2
+ ====================================
3
3
 
4
4
  z'assets is a tool based on [Sprockets][] for serving and compiling
5
5
  web assets. When serving over HTTP (intended for a development
@@ -57,20 +57,3 @@ Installation
57
57
  Assuming you have a working rubygems installation:
58
58
 
59
59
  gem install zassets
60
-
61
-
62
- Similar projects
63
- ----------------
64
-
65
- FIXME
66
-
67
-
68
- TODO
69
- ----
70
-
71
- * *Test* all that code!
72
-
73
- * Replace compile argument (config?) with build?
74
-
75
- * Verify how sprockets-helpers is useful and either add usages
76
- in documentation or remove it.
@@ -2,14 +2,17 @@ require 'optparse'
2
2
 
3
3
  module ZAssets
4
4
  class CLI
5
- def initialize(args)
5
+ attr_reader :options
6
+
7
+ def initialize(args, stdout = $stdout)
8
+ @stdout = stdout
6
9
  @options = args_parse! args
7
10
  end
8
11
 
9
12
  def args_parse!(args)
10
13
  options = {}
11
14
  parser = OptionParser.new do |o|
12
- o.banner = "Usage: #{File.basename $0} [options] [compile|serve]"
15
+ o.banner = "Usage: #{File.basename $0} [options] compile|serve"
13
16
 
14
17
  o.on '-v', '--verbose', 'Enable verbose mode' do |v|
15
18
  options[:verbose] = v
@@ -32,12 +35,12 @@ module ZAssets
32
35
  end
33
36
 
34
37
  o.on '-h', '--help', 'Show this message' do
35
- puts o
38
+ @stdout.puts o
36
39
  exit
37
40
  end
38
41
 
39
42
  o.on '-V', '--version', 'Show version' do
40
- puts VERSION
43
+ @stdout.puts VERSION
41
44
  exit
42
45
  end
43
46
  end
@@ -46,14 +49,14 @@ module ZAssets
46
49
  parser.parse! args
47
50
  rescue OptionParser::InvalidOption => e
48
51
  warn e.message
49
- puts parser
52
+ @stdout.puts parser
50
53
  exit 64
51
54
  end
52
55
 
53
56
  if args.last && %w(compile serve).include?(args.last)
54
57
  options[:action] = args.last.to_sym
55
58
  else
56
- puts parser
59
+ @stdout.puts parser
57
60
  exit 64
58
61
  end
59
62
 
@@ -2,16 +2,14 @@ require 'sprockets'
2
2
 
3
3
  module ZAssets
4
4
  class Compiler
5
+ attr_writer :manifest
6
+
5
7
  def initialize(config)
6
8
  @config = config
7
9
  end
8
10
 
9
11
  def compile
10
- if @config[:compile]
11
- manifest.compile(@config[:compile])
12
- else
13
- manifest.compile
14
- end
12
+ manifest.compile(@config[:compile])
15
13
  end
16
14
 
17
15
  def manifest
@@ -2,14 +2,14 @@ require 'yaml'
2
2
 
3
3
  module ZAssets
4
4
  class Config
5
- def initialize(options)
6
- o = defaults_options
5
+ def initialize(options = {})
6
+ o = default_options
7
7
  o.merge! load_options(options[:config_file]) if options[:config_file]
8
8
  o.merge! options
9
9
  @options = o
10
10
  end
11
11
 
12
- def defaults_options
12
+ def default_options
13
13
  {
14
14
  :verbose => false,
15
15
  :host => '::1',
@@ -1,5 +1,4 @@
1
1
  require 'sprockets'
2
- require 'sprockets-helpers'
3
2
 
4
3
  module Sprockets
5
4
  autoload :LessTemplate, 'sprockets/less_template'
@@ -19,13 +18,6 @@ module ZAssets
19
18
  require 'handlebars_assets'
20
19
  append_path HandlebarsAssets.path
21
20
  self.register_engine '.hbs', ::HandlebarsAssets::TiltHandlebars
22
-
23
- Sprockets::Helpers.configure do |c|
24
- c.environment = self
25
- c.prefix = config[:base_url]
26
- c.digest = true
27
- c.public_path = config[:public_path]
28
- end
29
21
  end
30
22
  end
31
23
  end
@@ -1,3 +1,3 @@
1
1
  module ZAssets
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -0,0 +1 @@
1
+ console.log('hello!');
@@ -0,0 +1 @@
1
+ file_option: :file_value
@@ -0,0 +1 @@
1
+ hello!
@@ -0,0 +1,3 @@
1
+ require 'zassets'
2
+
3
+ require 'support/fixtures_helpers'
@@ -0,0 +1,13 @@
1
+ module FixturesHelpers
2
+ FIXTURE_PATH = File.join(File.dirname(__FILE__), '..', 'fixtures')
3
+
4
+ def fixture_path_for(path)
5
+ File.join(FIXTURE_PATH, path)
6
+ end
7
+
8
+ def within_fixture_path
9
+ Dir.chdir FIXTURE_PATH do
10
+ yield
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ module ZAssets
4
+ describe CLI do
5
+ let(:args) { ['serve'] }
6
+ subject(:cli) { CLI.new(args) }
7
+
8
+ context 'action arguments parsing' do
9
+ it 'parses the action' do
10
+ cli.options[:action].should == :serve
11
+ end
12
+ end
13
+
14
+ context 'option arguments parsing ' do
15
+ def with_option(option, value = nil)
16
+ if value
17
+ CLI.new [option, value, *args]
18
+ else
19
+ CLI.new [option, *args]
20
+ end
21
+ end
22
+
23
+ it 'parses the -v option' do
24
+ with_option('-v').options[:verbose].should be_true
25
+ end
26
+
27
+ it 'parses the -c option' do
28
+ with_option('-c', 'config').options[:config_file].should == 'config'
29
+ end
30
+
31
+ it 'parses the -o option' do
32
+ with_option('-o', '::0').options[:host].should == '::0'
33
+ end
34
+
35
+ it 'parses the -p option' do
36
+ with_option('-p', '9393').options[:port].should == '9393'
37
+ end
38
+
39
+ it 'parses the -s option' do
40
+ with_option('-s', 'thin').options[:server].should == 'thin'
41
+ end
42
+
43
+ context '-h option' do
44
+ let(:args) { ['-h'] }
45
+ let(:output) { StringIO.new }
46
+ subject(:cli) { CLI.new(args, output) }
47
+
48
+ it 'prints the usage help' do
49
+ begin
50
+ cli
51
+ rescue SystemExit
52
+ end
53
+ output.string.should =~ /\AUsage: /
54
+ end
55
+
56
+ it 'exits' do
57
+ lambda { cli }.should raise_error SystemExit
58
+ end
59
+ end
60
+
61
+ context '-V option' do
62
+ let(:args) { ['-V'] }
63
+ let(:output) { StringIO.new }
64
+ subject(:cli) { CLI.new(args, output) }
65
+
66
+ it 'prints the version' do
67
+ begin
68
+ cli
69
+ rescue SystemExit
70
+ end
71
+ output.string.chomp.should == VERSION
72
+ end
73
+
74
+ it 'exits' do
75
+ lambda { cli }.should raise_error SystemExit
76
+ end
77
+ end
78
+ end
79
+
80
+ describe '#config' do
81
+ it 'builds a config' do
82
+ Config.should_receive(:new).with(cli.options)
83
+ cli.config
84
+ end
85
+
86
+ it 'returns the config' do
87
+ config = double('config')
88
+ Config.stub(:new) { config }
89
+ cli.config.should == config
90
+ end
91
+ end
92
+
93
+ describe '#run' do
94
+ context 'serve action' do
95
+ it 'runs the server' do
96
+ server = double('server')
97
+ cli.stub(:server) { server }
98
+ server.should_receive :run
99
+ cli.run
100
+ end
101
+ end
102
+
103
+ context 'compiler action' do
104
+ let(:args) { ['compile'] }
105
+
106
+ it 'runs the compiler' do
107
+ compiler = double('compiler')
108
+ cli.stub(:compiler) { compiler }
109
+ compiler.should_receive :compile
110
+ cli.run
111
+ end
112
+ end
113
+ end
114
+
115
+ describe '#compiler' do
116
+ it 'builds a compiler' do
117
+ Compiler.should_receive(:new).with(cli.config)
118
+ cli.compiler
119
+ end
120
+
121
+ it 'returns the compiler' do
122
+ compiler = double('compiler')
123
+ Compiler.stub(:new) { compiler }
124
+ cli.compiler.should == compiler
125
+ end
126
+ end
127
+
128
+ describe '#server' do
129
+ it 'builds a server' do
130
+ Server.should_receive(:new).with(cli.config)
131
+ cli.server
132
+ end
133
+
134
+ it 'returns the server' do
135
+ server = double('server')
136
+ Server.stub(:new) { server }
137
+ cli.server.should == server
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ module ZAssets
4
+ describe Compiler do
5
+ let(:config) { Config.new }
6
+ subject(:compiler) { Compiler.new(config) }
7
+
8
+ describe '#compile' do
9
+ it 'compiles the manifest' do
10
+ compiler.manifest = double('manifest')
11
+ compiler.manifest.should_receive :compile
12
+ compiler.compile
13
+ end
14
+ end
15
+
16
+ describe '#manifest' do
17
+ it 'builds a sprockets manifest' do
18
+ Sprockets::Manifest.should_receive(:new).with(
19
+ compiler.environment,
20
+ config[:compile_path]
21
+ )
22
+ compiler.manifest
23
+ end
24
+
25
+ it 'returns the sprockets manifest'do
26
+ manifest = double('manifest')
27
+ Sprockets::Manifest.stub(:new) { manifest }
28
+ compiler.manifest.should == manifest
29
+ end
30
+ end
31
+
32
+ describe '#environment' do
33
+ it 'builds a sprockets env' do
34
+ SprocketsEnv.should_receive(:new).with(config)
35
+ compiler.environment
36
+ end
37
+
38
+ it 'returns the sprockets env' do
39
+ environment = double('environment')
40
+ SprocketsEnv.stub(:new) { environment }
41
+ compiler.environment.should == environment
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+ module ZAssets
4
+ describe Config do
5
+ include FixturesHelpers
6
+
7
+ subject(:config)
8
+
9
+ let(:config_file) { fixture_path_for 'config/zassets.yaml' }
10
+
11
+ describe '#initialize' do
12
+ context 'with an empty option hash' do
13
+ it 'assigns #default_options to @options' do
14
+ config.instance_eval { @options }.should == config.default_options
15
+ end
16
+ end
17
+
18
+ context 'with a non-empty option hash' do
19
+ subject(:config) { Config.new(some_option: :some_value)}
20
+
21
+ it 'merges the option hash' do
22
+ config[:some_option].should == :some_value
23
+ end
24
+
25
+ it 'merges the option hash after default options' do
26
+ Config.any_instance.stub(:default_options) {
27
+ { some_option: :default_value }
28
+ }
29
+ config = Config.new(some_option: :argument_value)
30
+ config[:some_option].should == :argument_value
31
+ end
32
+ end
33
+
34
+ context 'with config_file option' do
35
+ subject(:config) { Config.new(config_file: config_file) }
36
+
37
+ it 'merges the config file options in @options' do
38
+ config[:file_option].should == :file_value
39
+ end
40
+
41
+ it 'merges the config file options before argument options' do
42
+ config = Config.new(file_option: :argument_value)
43
+ config[:file_option].should == :argument_value
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '#default_options' do
49
+ it 'sets verbose to false' do
50
+ config.default_options[:verbose].should be_false
51
+ end
52
+
53
+ it 'sets host to ::1' do
54
+ config.default_options[:host].should == '::1'
55
+ end
56
+
57
+ it 'sets port to 9292' do
58
+ config.default_options[:port].should == 9292
59
+ end
60
+
61
+ it 'sets server to puma' do
62
+ config.default_options[:server].should == :puma
63
+ end
64
+
65
+ it 'sets base_url to /assets' do
66
+ config.default_options[:base_url].should == '/assets'
67
+ end
68
+
69
+ it 'sets paths empty' do
70
+ config.default_options[:paths].should == []
71
+ end
72
+
73
+ it 'sets public_path to public directory' do
74
+ config.default_options[:public_path].should == 'public'
75
+ end
76
+
77
+ it 'sets compile_path to public/assets directory' do
78
+ config.default_options[:compile_path].should == 'public/assets'
79
+ end
80
+
81
+ it 'sets compile empty' do
82
+ config.default_options[:compile].should == []
83
+ end
84
+ end
85
+
86
+ describe '#load_options' do
87
+ it 'loads symbolized options from YAML file' do
88
+ config.load_options(config_file).should == {
89
+ file_option: :file_value
90
+ }
91
+ end
92
+ end
93
+
94
+ describe '#[]' do
95
+ it 'returns an @options value from its key' do
96
+ config.instance_eval { @options[:foo] = :bar }
97
+ config[:foo].should == :bar
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,93 @@
1
+ require 'spec_helper'
2
+
3
+ module ZAssets
4
+ describe Server do
5
+ let(:config) { Config.new }
6
+ subject(:server) { Server.new(config) }
7
+
8
+ describe '#run' do
9
+ let(:handler) { double 'handler' }
10
+
11
+ it 'runs the rack app' do
12
+ server.stub(:handler) { handler }
13
+ handler.should_receive(:run).with(server.app, server.options)
14
+ server.run
15
+ end
16
+ end
17
+
18
+ describe '#handler' do
19
+ it 'returns the configured rack handler' do
20
+ server.handler.should == Rack::Handler::Puma
21
+ end
22
+ end
23
+
24
+ describe '#options' do
25
+ it 'sets rack environment to development' do
26
+ server.options[:environment].should == :development
27
+ end
28
+
29
+ it 'sets rack handler host to configured host' do
30
+ server.options[:Host].should == config[:host]
31
+ end
32
+
33
+ it 'sets rack handler port to configured port' do
34
+ server.options[:Port].should == config[:port]
35
+ end
36
+ end
37
+
38
+ describe '#app' do
39
+ it 'returns a rack application' do
40
+ server.app.should respond_to(:call)
41
+ end
42
+
43
+ it 'builds the rack app once' do
44
+ # When building a rack app with Rack::Builder.new, the app will be
45
+ # built on each request. We dont want that so we need to either build
46
+ # using Rack::Builder.app or call #to_app on the returned instance. We
47
+ # can test if it was done by checking #to_app method absence.
48
+ server.app.should_not respond_to(:to_app)
49
+ end
50
+
51
+ context 'Rack application' do
52
+ include FixturesHelpers
53
+
54
+ let(:app) { Rack::MockRequest.new(server.app) }
55
+
56
+ it 'logs queries' do
57
+ app.get('/').errors.should =~ /GET \/.+404.+/
58
+ end
59
+
60
+ it 'shows exceptions' do
61
+ SprocketsEnv.any_instance.stub(:call) { raise RuntimeError }
62
+ response = app.get(config[:base_url])
63
+ response.should be_server_error
64
+ response.should =~ /RuntimeError/
65
+ end
66
+
67
+ context 'assets mount point' do
68
+ let(:config) { Config.new(paths: [fixture_path_for('assets')]) }
69
+
70
+ it 'maps the sprockets env' do
71
+ within_fixture_path do
72
+ response = app.get([config[:base_url], 'app.js'].join('/'))
73
+ response.should be_ok
74
+ response.content_type.should == 'application/javascript'
75
+ response.body.should == "console.log('hello!');\n"
76
+ end
77
+ end
78
+ end
79
+
80
+ context 'root mount point' do
81
+ it 'maps the static file handler' do
82
+ within_fixture_path do
83
+ response = app.get('/hello.txt')
84
+ response.should be_ok
85
+ response.content_type.should == 'text/plain'
86
+ response.body.should == "hello!\n"
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ module ZAssets
4
+ describe SprocketsEnv do
5
+ let(:config) { Config.new }
6
+ subject(:env) { SprocketsEnv.new(config) }
7
+
8
+ describe 'working directory' do
9
+ it 'initializes with current working directory' do
10
+ env.root.should == Dir.pwd
11
+ end
12
+ end
13
+
14
+ describe 'logger level' do
15
+ context 'by default' do
16
+ it 'defaults to fatal' do
17
+ env.logger.level.should == Logger::FATAL
18
+ end
19
+ end
20
+
21
+ context 'in verbose mode' do
22
+ let(:config) { Config.new(verbose: true)}
23
+
24
+ it 'is set to debug' do
25
+ env.logger.level.should == Logger::DEBUG
26
+ end
27
+ end
28
+ end
29
+
30
+ describe 'search paths' do
31
+ let(:paths) { ['assets/scripts', 'assets/styles'] }
32
+ let(:config) { Config.new(paths: paths) }
33
+
34
+ it 'registers the configured search paths' do
35
+ env.paths.should include(File.join(env.root, paths[0]))
36
+ env.paths.should include(File.join(env.root, paths[1]))
37
+ end
38
+ end
39
+ end
40
+ end
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.version = ZAssets::VERSION
7
7
  s.summary = "zassets-#{ZAssets::VERSION}"
8
8
  s.description = <<-eoh.gsub(/^ +/, '')
9
- Standalone asset manager based on sprockets and sprockets-helpers.
9
+ Standalone asset pipeline based on sprockets.
10
10
  eoh
11
11
  s.homepage = 'https://rubygems.org/gems/zassets'
12
12
 
@@ -26,4 +26,6 @@ Gem::Specification.new do |s|
26
26
  s.add_dependency 'coffee-script', '~> 2.2'
27
27
  s.add_dependency 'less', '~> 2.2'
28
28
  s.add_dependency 'handlebars_assets', '~> 0.6'
29
+
30
+ s.add_development_dependency 'rspec', '~> 2.11'
29
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zassets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-19 00:00:00.000000000 Z
12
+ date: 2012-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sprockets
@@ -139,7 +139,23 @@ dependencies:
139
139
  - - ~>
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0.6'
142
- description: ! 'Standalone asset manager based on sprockets and sprockets-helpers.
142
+ - !ruby/object:Gem::Dependency
143
+ name: rspec
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: '2.11'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: '2.11'
158
+ description: ! 'Standalone asset pipeline based on sprockets.
143
159
 
144
160
  '
145
161
  email: tj@a13.fr
@@ -150,6 +166,7 @@ extra_rdoc_files: []
150
166
  files:
151
167
  - .gitignore
152
168
  - Gemfile
169
+ - Guardfile
153
170
  - README.md
154
171
  - bin/zassets
155
172
  - lib/sprockets/less_template.rb
@@ -161,6 +178,16 @@ files:
161
178
  - lib/zassets/server.rb
162
179
  - lib/zassets/sprockets_env.rb
163
180
  - lib/zassets/version.rb
181
+ - spec/fixtures/assets/app.js
182
+ - spec/fixtures/config/zassets.yaml
183
+ - spec/fixtures/public/hello.txt
184
+ - spec/spec_helper.rb
185
+ - spec/support/fixtures_helpers.rb
186
+ - spec/zassets/cli_spec.rb
187
+ - spec/zassets/compiler_spec.rb
188
+ - spec/zassets/config_spec.rb
189
+ - spec/zassets/server_spec.rb
190
+ - spec/zassets/sprockets_env_spec.rb
164
191
  - zassets.gemspec
165
192
  homepage: https://rubygems.org/gems/zassets
166
193
  licenses: []
@@ -185,6 +212,6 @@ rubyforge_project:
185
212
  rubygems_version: 1.8.23
186
213
  signing_key:
187
214
  specification_version: 3
188
- summary: zassets-0.1.4
215
+ summary: zassets-0.1.5
189
216
  test_files: []
190
217
  has_rdoc: