handlebars_precompiler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in handlebars-precompiler-rails.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Felipe Elias Philipp
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # Handlebars::Precompiler::Rails
2
+
3
+ ## Installation
4
+
5
+ You'll need handlebars node precompiler. Install it with:
6
+
7
+ npm install handlebars -g
8
+
9
+ More info about handlebars precompiler: [http://handlebarsjs.com/precompilation.html](http://handlebarsjs.com/precompilation.html)
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ group :development do
14
+ gem 'handlebars-precompiler', git: 'https://github.com/felipeelias/handlebars-precompiler'
15
+ end
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/handlebars_precompiler/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Felipe Elias Philipp"]
6
+ gem.email = ["felipeelias@gmail.com"]
7
+ gem.description = "Precompile handlebars templates"
8
+ gem.summary = "Precompile handlebars templates"
9
+ gem.homepage = "https://github.com/felipeelias/handlebars-precompiler"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "handlebars_precompiler"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = HandlebarsPrecompiler::VERSION
17
+
18
+ gem.add_development_dependency 'rspec'
19
+ end
@@ -0,0 +1,16 @@
1
+ require 'handlebars_precompiler/version'
2
+ require 'handlebars_precompiler/config'
3
+ require 'handlebars_precompiler/middleware'
4
+ require 'handlebars_precompiler/command'
5
+ require 'handlebars_precompiler/runner'
6
+ require 'handlebars_precompiler/engine' if defined? Rails
7
+
8
+ module HandlebarsPrecompiler
9
+ def self.config
10
+ @config ||= Config.new
11
+ end
12
+
13
+ def self.configure(&block)
14
+ block.call(self.config)
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module HandlebarsPrecompiler
2
+ class Command
3
+ def initialize(template_root, output_file_path)
4
+ @template_root = template_root
5
+ @output_file_path = output_file_path
6
+ end
7
+
8
+ def call
9
+ Rails.logger.info "Running Handlebars::Precompiler with #{command_text}"
10
+ system command_text
11
+ end
12
+
13
+ private
14
+
15
+ def command_text
16
+ "handlebars #{@template_root} --min --output #{@output_file_path}"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ module HandlebarsPrecompiler
2
+ class Config
3
+ attr_writer :template_root
4
+
5
+ def template_root
6
+ if defined? Rails
7
+ @template_root ||= Rails.root.join('app/assets/javascripts/templates')
8
+ else
9
+ @template_root
10
+ end
11
+ end
12
+
13
+ attr_writer :output_file
14
+
15
+ def output_file
16
+ if defined? Rails
17
+ @output_file ||= Rails.root.join('app/assets/javascripts/templates.js')
18
+ else
19
+ @output_file
20
+ end
21
+ end
22
+
23
+ attr_writer :extension
24
+
25
+ def extension
26
+ @extension ||= :handlebars
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ module HandlebarsPrecompiler
2
+ class Engine < ::Rails::Engine
3
+ initializer 'handlebars_precompiler.middleware' do |app|
4
+ app.middleware.use Middleware, Runner.new, ::HandlebarsPrecompiler.config
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module HandlebarsPrecompiler
2
+ class Middleware
3
+ attr_accessor :runner
4
+
5
+ def initialize(app, runner, config)
6
+ @app = app
7
+ @runner = runner
8
+
9
+ runner.config = config
10
+ end
11
+
12
+ def call(env)
13
+ runner.run
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,52 @@
1
+ module HandlebarsPrecompiler
2
+ class Runner
3
+ attr_accessor :config, :command
4
+
5
+ def initialize(config = nil)
6
+ @config = config
7
+ @last_files = []
8
+ @last_update = Time.at(0).to_i
9
+ end
10
+
11
+ def updated?
12
+ return true unless output_file_exists?
13
+
14
+ new_files = list_files
15
+ new_update = last_update_time list_files
16
+
17
+ if new_update != @last_update || file_list_changed?(list_files)
18
+ @last_files = new_files
19
+ @last_update = new_update
20
+ true
21
+ else
22
+ false
23
+ end
24
+ end
25
+
26
+ def run
27
+ command.call if updated?
28
+ end
29
+
30
+ def command
31
+ @command ||= Command.new(config.template_root, config.output_file)
32
+ end
33
+
34
+ private
35
+
36
+ def output_file_exists?
37
+ File.exists? config.output_file
38
+ end
39
+
40
+ def list_files
41
+ Dir["#{config.template_root}/**/*.#{config.extension}"]
42
+ end
43
+
44
+ def file_list_changed?(new_files)
45
+ @last_files.size != new_files.size
46
+ end
47
+
48
+ def last_update_time(files)
49
+ files.map { |path| File.mtime(path).to_i }.max
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module HandlebarsPrecompiler
2
+ VERSION = "0.0.1"
3
+ end
File without changes
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+ require 'command'
3
+ require 'runner'
4
+
5
+ module HandlebarsPrecompiler
6
+ describe Runner do
7
+ include FileUtils
8
+
9
+ let :config do
10
+ stub :config, {
11
+ template_root: Pathname.new(File.dirname(__FILE__)).join('../fixtures'),
12
+ output_file: Pathname.new(File.dirname(__FILE__)).join('../fixtures/output.js'),
13
+ extension: :handlebars
14
+ }
15
+ end
16
+
17
+ let :command do
18
+ lambda { }
19
+ end
20
+
21
+ subject :reloader do
22
+ Runner.new
23
+ end
24
+
25
+ before do
26
+ reloader.config = config
27
+ reloader.command = command
28
+
29
+ rm Dir[config.template_root.join('*.handlebars')]
30
+ touch config.output_file
31
+ end
32
+
33
+ describe :updated? do
34
+ it 'initial state is updated' do
35
+ expect(reloader).to be_updated
36
+ end
37
+
38
+ it 'is updated when a file is added' do
39
+ expect(reloader).to be_updated
40
+
41
+ expect do
42
+ touch config.template_root.join('added.handlebars')
43
+ end.to change {
44
+ reloader.updated?
45
+ }.from(false).to(true)
46
+ end
47
+
48
+ it 'is updated when a file is removed' do
49
+ touch config.template_root.join('added.handlebars')
50
+ expect(reloader).to be_updated
51
+
52
+ expect do
53
+ rm config.template_root.join('added.handlebars')
54
+ end.to change {
55
+ reloader.updated?
56
+ }.from(false).to(true)
57
+ end
58
+
59
+ it 'is updated when a file is updated' do
60
+ touch config.template_root.join('added.handlebars')
61
+ expect(reloader).to be_updated
62
+
63
+ expect do
64
+ sleep 1
65
+ touch config.template_root.join('added.handlebars')
66
+ end.to change {
67
+ reloader.updated?
68
+ }.from(false).to(true)
69
+ end
70
+
71
+ it 'is not updated if no files were changed' do
72
+ expect(reloader).to be_updated
73
+ expect(reloader).to_not be_updated
74
+ end
75
+
76
+ it 'is updated if the output file is not there' do
77
+ rm config.output_file
78
+ expect(reloader).to be_updated
79
+ end
80
+ end
81
+
82
+ describe :run do
83
+ it 'calls the block when updated' do
84
+ subject.should_receive(:updated?).and_return(true)
85
+ command.should_receive(:call)
86
+
87
+ reloader.run
88
+ end
89
+
90
+ it 'does not call the block if not updated' do
91
+ subject.should_receive(:updated?).and_return(false)
92
+ command.should_not_receive(:call)
93
+
94
+ reloader.run
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,2 @@
1
+ require 'pry'
2
+ $LOAD_PATH.unshift File.join(File.dirname(File.expand_path(__FILE__)), '../lib/handlebars_precompiler')
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handlebars_precompiler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Felipe Elias Philipp
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-25 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
+ description: Precompile handlebars templates
31
+ email:
32
+ - felipeelias@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - handlebars-precompiler.gemspec
44
+ - lib/handlebars_precompiler.rb
45
+ - lib/handlebars_precompiler/command.rb
46
+ - lib/handlebars_precompiler/config.rb
47
+ - lib/handlebars_precompiler/engine.rb
48
+ - lib/handlebars_precompiler/middleware.rb
49
+ - lib/handlebars_precompiler/runner.rb
50
+ - lib/handlebars_precompiler/version.rb
51
+ - spec/fixtures/output.js
52
+ - spec/handlebars_precompiler/runner_spec.rb
53
+ - spec/spec_helper.rb
54
+ homepage: https://github.com/felipeelias/handlebars-precompiler
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.25
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Precompile handlebars templates
78
+ test_files:
79
+ - spec/fixtures/output.js
80
+ - spec/handlebars_precompiler/runner_spec.rb
81
+ - spec/spec_helper.rb
82
+ has_rdoc: