public
Description: A simple and easy to use generator system for Ruby
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/genosaurus.git
Search Repo:
Click here to lend your support to: genosaurus and make a donation at www.pledgie.com !
Release 1.1.1: Better RDoc, README has examples, plus a little clean up.
markbates (author)
Mon Apr 21 14:20:26 -0700 2008
commit  91789f734b505884f1f5e87ed23781b8781a8d82
tree    e167946326c529ff5bb2f29995adce8284c8adb1
parent  cd032a83eea270bf2fda7d6031acb52a375ba66e
...
3
4
5
6
7
 
 
8
...
3
4
5
 
6
7
8
9
0
@@ -3,4 +3,5 @@ tmp
0
 *.log
0
 *.tmp
0
 pkg
0
-*.gem
0
\ No newline at end of file
0
+*.gem
0
+doc
0
\ No newline at end of file
0
...
1
2
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
...
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
0
@@ -1,3 +1,117 @@
0
-README
0
-========================================================================
0
-genosaurus was developed by: markbates
0
+=Hello, and welcome to Genosaurus!
0
+
0
+Genosaurus is meant to be a very, very easy to use generation system for Ruby. It's simple and straightforward, yet offers plenty of flexibility in it's use. People use 'generators' all the time, and you either have to roll your own, or use some really cumbersome generator that some one else wrote, that is just way too difficult to use. Enter Genosaurus!
0
+
0
+==Installation
0
+ $ sudo gem install genosaurus
0
+
0
+==Getting Started
0
+
0
+===Implied Manifests
0
+The easiest way to use Genosaurus is to let it do the work for you. Let's looked at what's called an 'implied' manifest:
0
+
0
+ dir:
0
+ simple_generator.rb
0
+ templates:
0
+ hello_world.txt.template
0
+
0
+That's our folder structure. Now let's look at simple_generator.rb:
0
+
0
+ require 'rubygems'
0
+ require 'genosaurus'
0
+
0
+ class SimpleGenerator < Genosaurus
0
+ end
0
+
0
+Now if we run that generator:
0
+
0
+ $irb: SimpleGenerator.run
0
+
0
+We should get a file called hello_world.txt generated in the current directory. Yes, it truly is that simple!
0
+
0
+With implied manifests our directory structure under 'templates' tells the whole story, and Genosaurus is smart enough to figure it out. All the file names, and the same goes for folders, need to end in .template, and Genosaurus will do the rest.
0
+
0
+All the files will go through ERB before they generated, so you can put all your lovely little dynamic goodies in there. File, and folder, names also get run through ERB so you can even make the file name dynamic too!
0
+
0
+Let's look at a more complex example:
0
+
0
+ dir:
0
+ complex_generator.rb
0
+ templates:
0
+ app:
0
+ views:
0
+ <%=param(:name).plural%>.template:
0
+ hello_world.html.erb
0
+ models:
0
+ <%=param(:name)%>.rb.template
0
+
0
+Let's run our complex_generator.rb file:
0
+
0
+ require 'rubygems'
0
+ require 'genosaurus'
0
+
0
+ class ComplexGenerator < Genosaurus
0
+ require_param: name
0
+ end
0
+
0
+Now if we run that generator:
0
+
0
+ $irb: ComplexGenerator.run("name" => "user")
0
+
0
+Now you should end up with the following:
0
+
0
+ app:
0
+ views:
0
+ users:
0
+ hello_world.html.erb
0
+ models:
0
+ user.rb.template
0
+
0
+In the ComplexGenerator we told Genosaurus that we are requiring that the parameter, name, be passed into it. We are then using that parameter to generate the names of some files and folders. Pretty cool, eh? See how simple that is.
0
+
0
+Let's look at the manifest.yml file for our simple_generator example:
0
+
0
+ template_1:
0
+ type: file
0
+ template_path: <%= File.join(templates_directory_path, "templates", "hello_world.txt.template")
0
+ output_path: hello_world.txt
0
+
0
+Pretty simple. We give the template a name, template_1, it really doesn't matter what it is, but Hash objects need keys. The 'type' parameter is either file or directory. The template_path is the path to the template. Finally, the output_path is the where you want the file to be generated.
0
+
0
+Let's look at our more complex example. We can change the directory structure a bit, since we really don't need ERB in the file names now:
0
+
0
+ dir:
0
+ complex_generator.rb
0
+ templates:
0
+ hello_world.html.erb.template
0
+ model.rb.template
0
+
0
+Our manifest.yml file would look like this:
0
+
0
+ hello_world_template:
0
+ type: file
0
+ template_path: <%= File.join(templates_directory_path, "templates", "hello_world.html.erb")
0
+ output_path: <%= File.join("app", "views", param(:name).plural, "hello_world.html.erb") %>
0
+ model_template:
0
+ type: file
0
+ template_path: <%= File.join(templates_directory_path, "templates", "model.html.erb")
0
+ output_path: <%= File.join("app", "models", "#{param(:name)}.rb") %>
0
+
0
+This will generate the exact same thing as our implied manifest.
0
+
0
+===Explicit Manifests
0
+Explicit manifests are used when there is a manifest.yml supplied at the same level as the generator. If there is a manifest.yml file then implied manifests are not used. This means you have to define the entire generation process. This is great if you have a pretty complicated generator, as the manifest.yml is also sent through ERB before being loaded.
0
+
0
+==Contact
0
+Please mail bugs, suggestions and patches to <bugs@mackframework.com>.
0
+
0
+On the web at: http://www.mackframework.com
0
+
0
+==License and Copyright
0
+Copyright (C) 2008 Mark Bates, http://www.mackframework.com
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
\ No newline at end of file
...
8
9
10
11
 
12
13
14
...
8
9
10
 
11
12
13
14
0
@@ -8,7 +8,7 @@ require 'rubyforge'
0
 require 'rubygems'
0
 require 'rubygems/gem_runner'
0
 
0
-GEM_VERSION = "1.1.0"
0
+GEM_VERSION = "1.1.1"
0
 GEM_NAME = "genosaurus"
0
 GEM_RUBYFORGE_PROJECT = "genosaurus"
0
 
...
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
23
24
25
...
28
29
30
 
 
31
32
33
...
35
36
37
38
39
 
 
40
41
42
43
 
 
44
45
46
47
48
49
50
 
 
 
 
 
 
51
52
53
54
 
 
 
 
 
 
55
56
 
57
58
59
60
 
61
62
63
 
 
64
65
66
 
 
 
 
67
68
69
70
 
71
72
 
73
74
75
 
76
77
78
 
79
80
81
...
141
142
143
144
 
 
145
146
147
...
4
5
6
 
 
 
 
 
 
 
 
 
 
7
8
9
10
11
12
13
14
15
16
...
19
20
21
22
23
24
25
26
...
28
29
30
 
 
31
32
33
34
 
 
35
36
37
38
39
40
41
 
 
42
43
44
45
46
47
48
49
 
 
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
 
78
79
 
80
81
82
 
83
84
85
 
86
87
88
89
...
149
150
151
 
152
153
154
155
156
0
@@ -4,22 +4,13 @@ require 'fileutils'
0
 require 'erb'
0
 require 'yaml'
0
 
0
-
0
-# All generator classes should extend this class if they're expected to be used by the rake generate:<generator_name> task.
0
-# A generator must by name in the following style: <name>Generator.
0
-#
0
-# Example:
0
-# class MyCoolGenerator < Genosaurus
0
-# require_param :name, :foo
0
-# end
0
-#
0
-# rake generate:my_cool # => calls MyCoolGenerator
0
 class Genosaurus
0
 
0
   include FileUtils
0
   
0
   class << self
0
     
0
+ # Instantiates a new Genosaurus, passing the ENV hash as options into it, runs the generate method, and returns the Genosaurus object.
0
     def run(options = ENV.to_hash)
0
       gen = self.new(options)
0
       gen.generate
0
@@ -28,6 +19,8 @@ class Genosaurus
0
     
0
   end
0
   
0
+ # Takes any options needed for this generator. If the generator requires any parameters an ArgumentError exception will be
0
+ # raised if those parameters are found in the options Hash. The setup method is called at the end of the initialization.
0
   def initialize(options = {})
0
     @options = options
0
     self.class.required_params.each do |p|
0
@@ -35,47 +28,62 @@ class Genosaurus
0
     end
0
     @generator_name = self.class.name
0
     @generator_name_underscore = @generator_name.underscore
0
- @generator_file_path = nil
0
- @generator_directory_path = nil
0
+ @templates_directory_path = nil
0
+ @manifest_path = nil
0
     $".each do |f|
0
       if f.match(/#{@generator_name_underscore}\.rb$/)
0
- @generator_file_path = f
0
- @generator_directory_path = File.dirname(@generator_file_path)
0
+ @templates_directory_path = File.join(File.dirname(f), "templates")
0
+ @manifest_path = File.join(File.dirname(f), "manifest.yml")
0
       end
0
     end
0
     setup
0
   end
0
   
0
- def generator_file_path
0
- @generator_file_path
0
+ # Returns the path to the templates directory.
0
+ # IMPORTANT: The location of the templates_directory_path is VERY important! Genosaurus will attempt to find this location
0
+ # automatically, HOWEVER, if there is a problem, or you want to be special, you can override this method in your generator
0
+ # and have it return the correct path.
0
+ def templates_directory_path
0
+ @templates_directory_path
0
   end
0
   
0
- def generator_directory_path
0
- @generator_directory_path
0
+ # Returns the path to the manifest.yml. This is only used if you have a manifest.yml file, if there is no file, or this
0
+ # method returns nil, then an implied manifest is used based on the templates_directory_path contents.
0
+ # IMPORTANT: Genosaurus will attempt to find this location automatically, HOWEVER, if there is a problem, or you want to
0
+ # be special, you can override this method in your generator and have it return the correct path.
0
+ def manifest_path
0
+ @manifest_path
0
   end
0
   
0
+ # To be overridden in subclasses to do any setup work needed by the generator.
0
   def setup
0
     # does nothing, unless overridden in subclass.
0
   end
0
   
0
+ # To be overridden in subclasses to do work before the generate method is run.
0
   def before_generate
0
   end
0
   
0
+ # To be overridden in subclasses to do work after the generate method is run.
0
+ # This is a simple way to call other generators.
0
   def after_generate
0
   end
0
   
0
+ # Returns the manifest for this generator, which is used by the generate method to do the dirty work.
0
+ # If there is a manifest.yml, defined by the manifest_path method, then the contents of that file are processed
0
+ # with ERB and returned. If there is not manifest.yml then an implied manifest is generated from the contents
0
+ # of the templates_directory_path.
0
   def manifest
0
     ivar_cache do
0
- yml = File.join(generator_directory_path, "manifest.yml")
0
- if File.exists?(yml)
0
+ if File.exists?(manifest_path)
0
         # run using the yml file
0
- template = ERB.new(File.open(yml).read, nil, "->")
0
+ template = ERB.new(File.open(manifest_path).read, nil, "->")
0
         man = YAML.load(template.result(binding))
0
       else
0
- files = Dir.glob(File.join(generator_directory_path, "**/*.template"))
0
+ files = Dir.glob(File.join(templates_directory_path, "**/*.template"))
0
         man = {}
0
         files.each_with_index do |f, i|
0
- output_path = f.gsub(File.join(generator_directory_path, "templates"), "")
0
+ output_path = f.gsub(templates_directory_path, "")
0
           output_path.gsub!(".template", "")
0
           output_path.gsub!(/^\//, "")
0
           man["template_#{i+1}"] = {
0
@@ -141,7 +149,8 @@ class Genosaurus
0
     mkdir_p(output_dir)
0
     puts "Created: #{output_dir}"
0
   end
0
-
0
+
0
+ # This does the dirty work of generation.
0
   def generate
0
     generate_callbacks do
0
       manifest.each_value do |info|
...
3
4
5
6
 
7
8
...
3
4
5
 
6
7
8
0
@@ -3,5 +3,5 @@ directory_1:
0
   output_path: beatles/albums/magical_mystery_tour
0
 template_1:
0
   type: file
0
- template_path: <%= File.join(generator_directory_path, "templates", "fields.txt") %>
0
+ template_path: <%= File.join(templates_directory_path, "fields.txt") %>
0
   output_path: beatles/albums/magical_mystery_tour/lyrics/strawberry_fields_forever.lyrics
0
\ No newline at end of file

Comments

    No one has commented yet.