smartgen 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog.md CHANGED
@@ -1,3 +1,8 @@
1
+ 0.3.0
2
+ -----
3
+
4
+ * Added a rake task that regenerates files as they are added, changed or deleted
5
+
1
6
  0.2.0
2
7
  -----
3
8
 
data/Gemfile.lock CHANGED
@@ -1,10 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smartgen (0.1.2)
4
+ smartgen (0.3.0)
5
5
  RedCloth (>= 4.2.3)
6
6
  activesupport (>= 2.3.5)
7
7
  bluecloth (>= 2.0.9)
8
+ directory_watcher (>= 1.3.2)
8
9
  i18n (>= 0.5.0)
9
10
  nokogiri (>= 1.4.4)
10
11
  thor (>= 0.14.6)
@@ -18,6 +19,7 @@ GEM
18
19
  bluecloth (2.0.9)
19
20
  columnize (0.3.2)
20
21
  diff-lcs (1.1.2)
22
+ directory_watcher (1.3.2)
21
23
  i18n (0.5.0)
22
24
  linecache (0.43)
23
25
  linecache19 (0.5.11)
@@ -56,6 +58,7 @@ DEPENDENCIES
56
58
  RedCloth (>= 4.2.3)
57
59
  activesupport (>= 2.3.5)
58
60
  bluecloth (>= 2.0.9)
61
+ directory_watcher (>= 1.3.2)
59
62
  i18n (>= 0.5.0)
60
63
  nokogiri (>= 1.4.4)
61
64
  rake (= 0.8.7)
data/README.md CHANGED
@@ -131,9 +131,39 @@ You may also use a rake task:
131
131
 
132
132
  The yielded config is exactly the same config yielded by Smartgen::Resource#configure method, so you can use any of the above configs.
133
133
 
134
+ ## Watching for changes
135
+
136
+ When you are writing documentation it can be boring to always have to regenerate files manually every time you make some changes. Smartgen provides you with a way of regenerate files once they are saved. Just use `Smartgen::WatcherRakeTask`:
137
+
138
+ require 'smartgen/watcher_rake_task'
139
+
140
+ Smartgen::WatcherRakeTask.new :my_doc do |config|
141
+ config.src_files = ['doc/**/*']
142
+ config.output_folder = 'public/docs'
143
+ end
144
+
145
+ When you run this rake task it will generate files based on this configuration and start watching for changes. Whenever you create, modify or delete a file, it will regenerate all files for you, just reload your browser!
146
+
147
+ If you want to have the ability to generate files instantly and also have a watcher you can do this:
148
+
149
+ require 'smartgen/rake_task'
150
+ require 'smartgen/watcher_rake_task'
151
+
152
+ Smartgen::RakeTask.new :my_doc do |config|
153
+ config.src_files = ['doc/**/*']
154
+ config.output_folder = 'public/docs'
155
+ end
156
+
157
+ Smartgen::WatcherRakeTask.new :watch_my_doc, :my_doc
158
+
159
+ Then you can call both `my_doc` and `watch_my_doc`:
160
+
161
+ rake my_doc # generates files right away
162
+ rake watch_my_doc # generates files and watch for changes
163
+
134
164
  ## Contributors
135
165
 
136
- * [Vicente Mundim](http://github.com/vicentemundim) _author_
166
+ * [Vicente Mundim](http://github.com/vicentemundim) _(author)_
137
167
  * [Emerson Leite](http://github.com/emerleite)
138
168
  * [Rodrigo Lopes](http://github.com/rodvlopes)
139
169
  * [Marcos Silva Pereira](http://github.com/marcospereira)
data/lib/smartgen.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  require File.expand_path(File.join('smartgen', 'object_hash'), File.dirname(__FILE__))
3
3
  require File.expand_path(File.join('smartgen', 'resource'), File.dirname(__FILE__))
4
4
  require File.expand_path(File.join('smartgen', 'configuration'), File.dirname(__FILE__))
5
+ require File.expand_path(File.join('smartgen', 'watcher'), File.dirname(__FILE__))
5
6
  require File.expand_path(File.join('smartgen', 'indexer'), File.dirname(__FILE__))
6
7
  require File.expand_path(File.join('smartgen', 'markup_file'), File.dirname(__FILE__))
7
8
  require File.expand_path(File.join('smartgen', 'renderers'), File.dirname(__FILE__))
@@ -16,7 +16,7 @@ module Smartgen
16
16
 
17
17
  # Yields a Configuration, so that you can configure the generation of files.
18
18
  def configure
19
- yield config
19
+ yield config if block_given?
20
20
  end
21
21
 
22
22
  # Returns the Configuration for this resource.
@@ -1,4 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  module Smartgen
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -0,0 +1,75 @@
1
+ require 'directory_watcher'
2
+
3
+ module Smartgen
4
+ # Watches for changes in configured files and regenerated them as they are changed.
5
+ #
6
+ # Just pass the name of the resource to it and call #start. It will generate
7
+ # the files right away and it will keep watching for changes in those files.
8
+ # Every time a file is added, changed or removed, it will regenerate all
9
+ # files.
10
+ #
11
+ # You can also pass a block when initializing to create and configure the
12
+ # resource.
13
+ #
14
+ # For example:
15
+ #
16
+ # watcher = Smartgen::Watcher.new :my_resource do |config|
17
+ # config.src_files = ['docs/**/*']
18
+ # config.output_folder = 'public_docs'
19
+ # end
20
+ #
21
+ # watcher.start
22
+ class Watcher
23
+ attr_accessor :name
24
+
25
+ def initialize(name, &block)
26
+ @name = name
27
+ configure(&block)
28
+ end
29
+
30
+ # Starts watching files for changes.
31
+ def start
32
+ puts "Watching these files for changes #{glob}..."
33
+ configure_directory_watcher
34
+ setup_graceful_exit
35
+
36
+ directory_watcher.start.join
37
+ end
38
+
39
+ # Generate files.
40
+ def generate(*args)
41
+ puts "Regenerating files, #{args.size} files have been added/changed/removed."
42
+ Smartgen[@name].generate!
43
+ puts "Files generated."
44
+ end
45
+
46
+ private
47
+ def resource
48
+ Smartgen[name]
49
+ end
50
+
51
+ def configure(&block)
52
+ resource.configure(&block)
53
+ end
54
+
55
+ def directory_watcher
56
+ @directory_watcher ||= DirectoryWatcher.new '.', :glob => glob
57
+ end
58
+
59
+ def glob
60
+ resource.config.src_files
61
+ end
62
+
63
+ def configure_directory_watcher
64
+ directory_watcher.interval = 2
65
+ directory_watcher.add_observer self, :generate
66
+ end
67
+
68
+ def setup_graceful_exit
69
+ Kernel.trap 'INT' do
70
+ puts "Stopping watcher..."
71
+ directory_watcher.stop
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rake'
4
+ require 'rake/tasklib'
5
+ require 'smartgen'
6
+
7
+ module Smartgen
8
+ # A rake task that starts a watcher for file changes and generate them on
9
+ # demand, using smartgen.
10
+ #
11
+ # To use it, require this file instead of +smartgen+:
12
+ #
13
+ # require 'smartgen/watcher_rake_task'
14
+ #
15
+ # Then create the task and configuration:
16
+ #
17
+ # Smartgen::WatcherRakeTask.new :generate_doc do |config|
18
+ # config.src_files = ['doc/**/*']
19
+ # config.output_folder = 'public/doc'
20
+ # end
21
+ #
22
+ # It yields a Configuration, in fact it calls Resource#configure, so you
23
+ # can use any of the configurations here.
24
+ #
25
+ # When you run this task it will start the watcher and wait for file changes,
26
+ # just go editing your files, and it will regenerate them as you change files.
27
+ #
28
+ # You may also use an existing configuration, while still having a different
29
+ # task name:
30
+ #
31
+ # Smartgen::WatcherRakeTask.new :watcher, :generate_doc do |config|
32
+ # config.src_files = ['doc/**/*']
33
+ # config.output_folder = 'public/doc'
34
+ # end
35
+ #
36
+ # This will use Smartgen[:generate_doc] config, but you will call it with:
37
+ #
38
+ # rake watcher
39
+ class WatcherRakeTask < ::Rake::TaskLib
40
+ def initialize(resource_or_task_name=nil, resource_name=nil, &block)
41
+ task_name = resource_or_task_name || 'smartgen:watcher'
42
+ resource_name = resource_name || task_name
43
+
44
+ watcher = Smartgen::Watcher.new resource_name, &block
45
+
46
+ desc("Watches for changes in files and generate them using smartgen") unless ::Rake.application.last_comment
47
+ task task_name do
48
+ watcher.start
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Smartgen::Watcher do
4
+ it "should create a resource and yield its configuration" do
5
+ expected_config = nil
6
+ Smartgen::Watcher.new(:my_resource) { |config| expected_config = config }
7
+ expected_config.should == Smartgen[:my_resource].config
8
+ end
9
+
10
+ context "when it will start watching" do
11
+ def src_files
12
+ ['doc/**/*']
13
+ end
14
+
15
+ def directory_watcher
16
+ return @directory_watcher if @directory_watcher
17
+
18
+ @directory_watcher = mock(DirectoryWatcher, :add_observer => 'observer', :interval= => '2')
19
+ @directory_watcher.stub!(:start).and_return(@directory_watcher)
20
+ @directory_watcher.stub!(:join).and_return(@directory_watcher)
21
+ @directory_watcher
22
+ end
23
+
24
+ before do
25
+ DirectoryWatcher.stub!(:new).and_return(directory_watcher)
26
+ end
27
+
28
+ subject do
29
+ Smartgen::Watcher.new(:my_resource) do |config|
30
+ config.src_files = src_files
31
+ config.output_folder = 'public/docs'
32
+ end
33
+ end
34
+
35
+ it "should create a directory watcher, pre loading src_files" do
36
+ DirectoryWatcher.should_receive(:new).with('.', :glob => src_files).and_return(directory_watcher)
37
+ capture(:stdout) { subject.start }
38
+ end
39
+
40
+ it "should add itself as an observer, with :generate method as the callback" do
41
+ directory_watcher.should_receive(:add_observer).with(subject, :generate)
42
+ capture(:stdout) { subject.start }
43
+ end
44
+
45
+ it "should start watching" do
46
+ directory_watcher.should_receive(:start)
47
+ directory_watcher.should_receive(:join)
48
+ capture(:stdout) { subject.start }
49
+ end
50
+
51
+ it "should set interval to 2 seconds" do
52
+ directory_watcher.should_receive(:interval=).with(2)
53
+ capture(:stdout) { subject.start }
54
+ end
55
+
56
+ context "when generating" do
57
+ it "should generate files" do
58
+ Smartgen[:my_resource].should_receive(:generate!)
59
+ capture(:stdout) { subject.generate }
60
+ end
61
+ end
62
+
63
+ context "when user hits ctrl+c" do
64
+ it "should exit gracefully" do
65
+ directory_watcher.should_receive(:stop)
66
+ Kernel.should_receive(:trap).with('INT').and_yield
67
+ capture(:stdout) { subject.start }
68
+ end
69
+ end
70
+ end
71
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartgen
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vicente Mundim
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-27 00:00:00 -02:00
18
+ date: 2011-01-28 00:00:00 -02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -115,9 +115,25 @@ dependencies:
115
115
  type: :runtime
116
116
  version_requirements: *id006
117
117
  - !ruby/object:Gem::Dependency
118
- name: rspec
118
+ name: directory_watcher
119
119
  prerelease: false
120
120
  requirement: &id007 !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 31
126
+ segments:
127
+ - 1
128
+ - 3
129
+ - 2
130
+ version: 1.3.2
131
+ type: :runtime
132
+ version_requirements: *id007
133
+ - !ruby/object:Gem::Dependency
134
+ name: rspec
135
+ prerelease: false
136
+ requirement: &id008 !ruby/object:Gem::Requirement
121
137
  none: false
122
138
  requirements:
123
139
  - - ">="
@@ -129,7 +145,7 @@ dependencies:
129
145
  - 0
130
146
  version: 2.3.0
131
147
  type: :development
132
- version_requirements: *id007
148
+ version_requirements: *id008
133
149
  description: Smartgen generates static HTML files from markup files, using textile or markdown, and ERB to create layout templates
134
150
  email:
135
151
  - vicente.mundim@gmail.com
@@ -154,6 +170,8 @@ files:
154
170
  - lib/smartgen/renderers.rb
155
171
  - lib/smartgen/resource.rb
156
172
  - lib/smartgen/version.rb
173
+ - lib/smartgen/watcher.rb
174
+ - lib/smartgen/watcher_rake_task.rb
157
175
  - lib/smartgen.rb
158
176
  - Gemfile
159
177
  - Gemfile.lock
@@ -193,6 +211,7 @@ files:
193
211
  - spec/lib/smartgen/object_hash_spec.rb
194
212
  - spec/lib/smartgen/renderers/erb_spec.rb
195
213
  - spec/lib/smartgen/resource_spec.rb
214
+ - spec/lib/smartgen/watcher_spec.rb
196
215
  - spec/lib/smartgen_spec.rb
197
216
  - spec/sandbox/.gitkeep
198
217
  - spec/spec_helper.rb
@@ -264,6 +283,7 @@ test_files:
264
283
  - spec/lib/smartgen/object_hash_spec.rb
265
284
  - spec/lib/smartgen/renderers/erb_spec.rb
266
285
  - spec/lib/smartgen/resource_spec.rb
286
+ - spec/lib/smartgen/watcher_spec.rb
267
287
  - spec/lib/smartgen_spec.rb
268
288
  - spec/sandbox/.gitkeep
269
289
  - spec/spec_helper.rb