guard-reloader 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010-2011 Fajar Firdaus
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.
data/README ADDED
@@ -0,0 +1,15 @@
1
+ To use:
2
+
3
+ Install guard gem
4
+ gem install guard
5
+ or add to Gemfile
6
+ gem 'guard'
7
+
8
+ Install dependencies
9
+ gem install test-unit json
10
+ or add to Gemfile
11
+ gem 'test-unit'
12
+ gem 'json'
13
+
14
+ copy guard folder and Guardfile sample to your Rails application
15
+ execute guard from the rails folder
@@ -0,0 +1,6 @@
1
+ guard 'reloader' do
2
+ watch(%r{^app/models/(.+)\.rb$})
3
+ watch(%r{^test/unit/(.+)\.rb$})
4
+ watch(%r{^app/controllers/(.+)\.rb$})
5
+ watch(%r{^test/functional/(.+)\.rb$})
6
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ module Guard
3
+ module ReloaderVersion
4
+ VERSION = "0.1.0" unless defined? ReloaderVersion::VERSION
5
+ end
6
+ end
@@ -0,0 +1,140 @@
1
+ # encoding: utf-8
2
+ require 'json'
3
+ require 'guard'
4
+ require 'guard/guard'
5
+ require 'guard/reloader/version'
6
+ require 'test/unit/version' unless defined?(::Test::Unit::VERSION)
7
+
8
+ module Guard
9
+ class Reloader < Guard
10
+
11
+ def initialize(watchers=[], options={})
12
+ super
13
+ @options = {
14
+ :all_on_start => true,
15
+ :all_after_pass => true,
16
+ :keep_failed => true
17
+ }.update(options)
18
+ ::Guard::UI.info("Guard::Reloader #{ReloaderVersion::VERSION} setting up test environment... this may take some time", :reset => true)
19
+ require 'test/test_helper'
20
+ require 'test/unit/ui/console/testrunner'
21
+
22
+ # @runner = Runner.new(options)
23
+ end
24
+
25
+ def start
26
+ ::Guard::UI.info("Guard::Reloader #{ReloaderVersion::VERSION} is running, with Test::Unit #{::Test::Unit::VERSION}!", :reset => true)
27
+ run_all if @options[:all_on_start]
28
+ end
29
+
30
+ #taken from rails source code ==> http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-camelize
31
+ def camelize(lower_case_and_underscored_word)
32
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
33
+ end
34
+ def get_model_name(p)
35
+ m = %r{^app/models/(.+)\.rb$}.match(p)
36
+ camelize(m[1]) if m[1]
37
+ end
38
+ def get_controller_name(p)
39
+ m = %r{^app/controllers/(.+)\.rb$}.match(p)
40
+ camelize(m[1]) if m[1]
41
+ end
42
+ def get_unit_test_path(p)
43
+ m = %r{^app/models/(.+)\.rb$}.match(p)
44
+ "test/unit/#{m[1]}_test.rb"
45
+ end
46
+ def get_functional_test_path(p)
47
+ m = %r{^app/controllers/(.+)\.rb$}.match(p)
48
+ "test/functional/#{m[1]}_test.rb"
49
+ end
50
+ def get_test_name(p)
51
+ m = %r{^test/(unit|functional)/(.+)\.rb$}.match(p)
52
+ if m[2]
53
+ camelize(m[2])
54
+ end
55
+ end
56
+ def unit_test?(p)
57
+ !!%r{^test/unit/(.+)\.rb$}.match(p)
58
+ end
59
+ def functional_test?(p)
60
+ !!%r{^test/functional/(.+)\.rb$}.match(p)
61
+ end
62
+ def controller?(p)
63
+ !!%r{^app/controllers/(.+)\.rb$}.match(p)
64
+ end
65
+ def model?(p)
66
+ !!%r{^app/models/(.+)\.rb$}.match(p)
67
+ end
68
+ def json?(p)
69
+ begin
70
+ map = JSON.parse(p)
71
+ !!map["path"]
72
+ rescue
73
+ false
74
+ end
75
+ end
76
+ def smart_test(path)
77
+ if model?(path)
78
+ ::Guard::UI.info("Detected #{path} as model!", :reset => true)
79
+ model_class = reload(get_model_name(path), path)
80
+ test_path = get_unit_test_path(path)
81
+ test_class = reload(get_test_name(test_path),test_path)
82
+ execute_test(test_class) if test_class
83
+ elsif controller?(path)
84
+ ::Guard::UI.info("Detected #{path} as controller!", :reset => true)
85
+ controller_class = reload(get_controller_name(path), path)
86
+ test_path = get_functional_test_path(path)
87
+ test_class = reload(get_test_name(test_path),test_path)
88
+ execute_test(test_class) if test_class
89
+ elsif unit_test?(path)
90
+ ::Guard::UI.info("Detected #{path} as unit test!", :reset => true)
91
+ test_class = reload(get_test_name(path),path)
92
+ execute_test(test_class) if test_class
93
+ elsif functional_test?(path)
94
+ ::Guard::UI.info("Detected #{path} as functional test!", :reset => true)
95
+ test_class = reload(get_test_name(path),path)
96
+ execute_test(test_class) if test_class
97
+ else
98
+ ::Guard::UI.info("Give up on #{path}", :reset => true)
99
+ end
100
+ ::Guard::UI.info("Process for #{path} is done!\n\n", :reset => true)
101
+ end
102
+ def run_on_change(paths)
103
+ paths.each {|p| smart_test(p) }
104
+ # paths = Inspector.clean(paths)
105
+ # passed = @runner.run(paths)
106
+ end
107
+ def execute_test(klass)
108
+ begin
109
+ ::Guard::UI.info("run test for #{klass.name}")
110
+ Test::Unit::UI::Console::TestRunner.run(klass) if klass
111
+ # rescue Exception => ex find what kind of exception to handle first
112
+ end
113
+ end
114
+ def to_class(class_name)
115
+ begin
116
+ k = class_name.split('::').inject(Object) {|p, x| p.const_get(x.to_sym) }
117
+ rescue NameError => ex
118
+ puts "#{class_name} class doesn't exists"
119
+ end
120
+ end
121
+ def class_symbol(class_name)
122
+ class_name.split('::')[-1].to_sym
123
+ end
124
+ def reload(klass_name, path)
125
+ begin
126
+ klass = to_class(klass_name)
127
+ if klass
128
+ ::Guard::UI.info("Killing #{klass_name} class", :reset => true)
129
+ klass.parent.send(:remove_const, class_symbol(klass_name))
130
+ end
131
+ load(path)
132
+ ::Guard::UI.info("#{klass_name} on #{path} reloader process is done!", :reset => true)
133
+ to_class(klass_name)
134
+ rescue Exception => ex
135
+ puts ex
136
+ end
137
+ end
138
+
139
+ end
140
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-reloader
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Fajar Firdaus
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-07 00:00:00 +07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: guard
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 0
32
+ - 2
33
+ - 2
34
+ version: 0.2.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: test-unit
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 2
48
+ - 2
49
+ version: "2.2"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: Guard::Reloader automatically load class definition then run your tests on file modification.
53
+ email:
54
+ - fajarmf@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - lib/guard/reloader/template/Guardfile
63
+ - lib/guard/reloader/version.rb
64
+ - lib/guard/reloader.rb
65
+ - LICENSE
66
+ - README
67
+ has_rdoc: true
68
+ homepage: https://github.com/fajarmf/Rails-Autotester
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 23
91
+ segments:
92
+ - 1
93
+ - 3
94
+ - 6
95
+ version: 1.3.6
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.5.2
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Guard gem that reload file then run test
103
+ test_files: []
104
+