guard-treetop 0.1.0

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.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Zeh Rizzatti
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,58 @@
1
+ # Guard::Treetop [![Build Status][build-status]][travis]
2
+
3
+ Guard::Treetop compiles your treetop grammars to ruby files using guard.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ group :development do
11
+ gem 'guard-treetop'
12
+ end
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```bash
24
+ $ gem install guard-treetop
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```bash
30
+ $ bundle exec guard init treetop
31
+ ```
32
+
33
+ And edit your `Guardfile`.
34
+
35
+ ## Options
36
+
37
+ ```ruby
38
+ :input => 'grammars' # Relative path to the root directory.
39
+ # Will compile .tt and .treetop files inside
40
+ # default: nil
41
+
42
+ :output => 'lib/parsers' # Relative path to the root directory.
43
+ # default: the same as :input
44
+
45
+ :all_on_start => false # Run compilation when guard starts
46
+ # default: false
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
56
+
57
+ [build-status]: https://travis-ci.org/zehrizzatti/guard-treetop.png
58
+ [travis]: https://travis-ci.org/zehrizzatti/guard-treetop
@@ -0,0 +1,5 @@
1
+ module Guard
2
+ module TreetopVersion
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,51 @@
1
+ require 'guard'
2
+ require 'guard/guard'
3
+ require 'guard/treetop/version'
4
+ require 'treetop'
5
+
6
+ module Guard
7
+ class Treetop < Guard
8
+ include TreetopVersion
9
+
10
+ DEFAULT_OPTIONS = {
11
+ :all_on_start => false
12
+ }
13
+
14
+ attr_reader :compiler
15
+
16
+ def initialize(watchers=[], options={})
17
+ watchers ||= []
18
+ defaults = DEFAULT_OPTIONS.clone
19
+
20
+ if options[:input]
21
+ input = options.delete(:input)
22
+ defaults.merge!({:output => input})
23
+ watchers << ::Guard::Watcher.new(%r{#{input}/(.+)(\.treetop|\.tt)})
24
+ end
25
+
26
+ super(watchers, defaults.merge!(options))
27
+
28
+ @compiler = ::Treetop::Compiler::GrammarCompiler.new
29
+ end
30
+
31
+ def start
32
+ run_all if options[:all_on_start]
33
+ end
34
+
35
+ def run_all
36
+ run_on_changes(Watcher.match_files(self, Dir.glob('**/*.{tt,treetop}')))
37
+ end
38
+
39
+ def run_on_changes(paths)
40
+ tuples = paths.map do |path|
41
+ basename = File.basename(path).gsub(/\.(tt|treetop)\z/, '.rb')
42
+ dirname = options[:output] || File.dirname(path)
43
+ output = File.join(dirname, basename)
44
+ [path, output]
45
+ end
46
+ tuples.each { |e| compiler.compile(*e) }
47
+ rescue StandardError
48
+ throw :task_has_failed
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+ require 'guard/treetop'
3
+
4
+ module Guard
5
+ describe Treetop do
6
+ describe 'the class' do
7
+ it 'has a version number' do
8
+ subject.class::VERSION.should == TreetopVersion::VERSION
9
+ end
10
+ end
11
+
12
+ describe '#initialize' do
13
+ context 'when no options are provided' do
14
+ it 'sets a default :all_on_start option' do
15
+ subject.options[:all_on_start].should be_false
16
+ end
17
+
18
+ it 'has no :input option' do
19
+ subject.options[:input].should be_nil
20
+ end
21
+
22
+ it 'has no :output option' do
23
+ subject.options[:output].should be_nil
24
+ end
25
+ end
26
+
27
+ context 'with options provided' do
28
+ subject { Treetop.new(nil, {:all_on_start => true}) }
29
+
30
+ it 'sets the provided :all_on_start option' do
31
+ subject.options[:all_on_start].should be_true
32
+ end
33
+ end
34
+
35
+ context 'with :input provided' do
36
+ subject { Treetop.new(nil, {:input => 'lib'}) }
37
+
38
+ it 'removes the provided :input' do
39
+ subject.options[:input].should be_nil
40
+ end
41
+
42
+ it 'has one watcher' do
43
+ subject.should have(1).watchers
44
+ end
45
+
46
+ it 'watchers *.treetop and *.tt files' do
47
+ subject.watchers.first.pattern.should == %r{lib/(.+)(\.treetop|\.tt)}
48
+ end
49
+
50
+ context 'and no :output provided' do
51
+ it 'outputs to the same directory as input' do
52
+ subject.options[:output].should == 'lib'
53
+ end
54
+ end
55
+
56
+ context 'and :output is provided' do
57
+ let(:options) do
58
+ {:input => 'lib/inputs', :output => 'lib/outputs'}
59
+ end
60
+ subject { Treetop.new(nil, options) }
61
+
62
+ it 'respects the provided :output' do
63
+ subject.options[:output].should == 'lib/outputs'
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ describe '#start' do
70
+ context 'without all_on_start' do
71
+ it 'should not call run_all' do
72
+ subject.should_not_receive(:run_all)
73
+ subject.start
74
+ end
75
+ end
76
+
77
+ context 'with all_on_start' do
78
+ subject { Treetop.new(nil, :all_on_start => true) }
79
+
80
+ it 'should call run_all' do
81
+ subject.should_receive(:run_all)
82
+ subject.start
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'running without output' do
88
+ subject { Treetop.new(nil, :input => 'inputs') }
89
+ let(:inputs) { ['inputs/1.tt', 'inputs/2.treetop'] }
90
+ let(:outputs) { ['inputs/1.rb', 'inputs/2.rb'] }
91
+ before(:all) { Dir.stub(:glob).and_return(inputs) }
92
+
93
+ it 'should call run_on_changes' do
94
+ subject.should_receive(:run_on_changes).with(inputs)
95
+ subject.run_all
96
+ end
97
+
98
+ it 'throws :task_has_failed on error' do
99
+ expect { subject.run_on_changes(['/tmp/non_existent']) }.to throw_symbol :task_has_failed
100
+ end
101
+
102
+ it 'calls run_on_changes' do
103
+ files = [inputs, outputs].transpose
104
+ subject.compiler.should_receive(:compile).with(*files.first)
105
+ subject.compiler.should_receive(:compile).with(*files.last)
106
+ subject.run_on_changes(inputs)
107
+ end
108
+ end
109
+
110
+ context 'running with output' do
111
+ subject { Treetop.new(nil, :input => 'inputs', :output => 'outputs') }
112
+ let(:inputs) { ['inputs/1.tt', 'inputs/2.treetop'] }
113
+ let(:outputs) { ['outputs/1.rb', 'outputs/2.rb'] }
114
+ before(:all) { Dir.stub(:glob).and_return(inputs) }
115
+
116
+ it 'calls run_on_changes' do
117
+ files = [inputs, outputs].transpose
118
+ subject.compiler.should_receive(:compile).with(*files.first)
119
+ subject.compiler.should_receive(:compile).with(*files.last)
120
+ subject.run_on_changes(inputs)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems' unless defined? Gem
2
+ require 'bundler/setup'
3
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-treetop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zeh Rizzatti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: treetop
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.4.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.4.0
46
+ description: Guard::Treetop automatically compiles your treetop grammars
47
+ email:
48
+ - zehrizzatti@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/guard/treetop/version.rb
54
+ - lib/guard/treetop.rb
55
+ - spec/lib/guard/treetop_spec.rb
56
+ - spec/spec_helper.rb
57
+ - README.md
58
+ - LICENSE.txt
59
+ homepage: https://github.org/zehrizzatti/guard-treetop
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.23
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Guard gem for Treetop
83
+ test_files:
84
+ - spec/lib/guard/treetop_spec.rb
85
+ - spec/spec_helper.rb
86
+ has_rdoc: