genosaurus 1.1.1

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.
@@ -0,0 +1,208 @@
1
+
2
+ body {
3
+ font-family: Verdana,Arial,Helvetica,sans-serif;
4
+ font-size: 90%;
5
+ margin: 0;
6
+ margin-left: 40px;
7
+ padding: 0;
8
+ background: white;
9
+ }
10
+
11
+ h1,h2,h3,h4 { margin: 0; color: #efefef; background: transparent; }
12
+ h1 { font-size: 150%; }
13
+ h2,h3,h4 { margin-top: 1em; }
14
+
15
+ a { background: #eef; color: #039; text-decoration: none; }
16
+ a:hover { background: #039; color: #eef; }
17
+
18
+ /* Override the base stylesheet's Anchor inside a table cell */
19
+ td > a {
20
+ background: transparent;
21
+ color: #039;
22
+ text-decoration: none;
23
+ }
24
+
25
+ /* and inside a section title */
26
+ .section-title > a {
27
+ background: transparent;
28
+ color: #eee;
29
+ text-decoration: none;
30
+ }
31
+
32
+ /* === Structural elements =================================== */
33
+
34
+ div#index {
35
+ margin: 0;
36
+ margin-left: -40px;
37
+ padding: 0;
38
+ font-size: 90%;
39
+ }
40
+
41
+
42
+ div#index a {
43
+ margin-left: 0.7em;
44
+ }
45
+
46
+ div#index .section-bar {
47
+ margin-left: 0px;
48
+ padding-left: 0.7em;
49
+ background: #ccc;
50
+ font-size: small;
51
+ }
52
+
53
+
54
+ div#classHeader, div#fileHeader {
55
+ width: auto;
56
+ color: white;
57
+ padding: 0.5em 1.5em 0.5em 1.5em;
58
+ margin: 0;
59
+ margin-left: -40px;
60
+ border-bottom: 3px solid #006;
61
+ }
62
+
63
+ div#classHeader a, div#fileHeader a {
64
+ background: inherit;
65
+ color: white;
66
+ }
67
+
68
+ div#classHeader td, div#fileHeader td {
69
+ background: inherit;
70
+ color: white;
71
+ }
72
+
73
+
74
+ div#fileHeader {
75
+ background: #057;
76
+ }
77
+
78
+ div#classHeader {
79
+ background: #048;
80
+ }
81
+
82
+
83
+ .class-name-in-header {
84
+ font-size: 180%;
85
+ font-weight: bold;
86
+ }
87
+
88
+
89
+ div#bodyContent {
90
+ padding: 0 1.5em 0 1.5em;
91
+ }
92
+
93
+ div#description {
94
+ padding: 0.5em 1.5em;
95
+ background: #efefef;
96
+ border: 1px dotted #999;
97
+ }
98
+
99
+ div#description h1,h2,h3,h4,h5,h6 {
100
+ color: #125;;
101
+ background: transparent;
102
+ }
103
+
104
+ div#validator-badges {
105
+ text-align: center;
106
+ }
107
+ div#validator-badges img { border: 0; }
108
+
109
+ div#copyright {
110
+ color: #333;
111
+ background: #efefef;
112
+ font: 0.75em sans-serif;
113
+ margin-top: 5em;
114
+ margin-bottom: 0;
115
+ padding: 0.5em 2em;
116
+ }
117
+
118
+
119
+ /* === Classes =================================== */
120
+
121
+ table.header-table {
122
+ color: white;
123
+ font-size: small;
124
+ }
125
+
126
+ .type-note {
127
+ font-size: small;
128
+ color: #DEDEDE;
129
+ }
130
+
131
+ .xxsection-bar {
132
+ background: #eee;
133
+ color: #333;
134
+ padding: 3px;
135
+ }
136
+
137
+ .section-bar {
138
+ color: #333;
139
+ border-bottom: 1px solid #999;
140
+ margin-left: -20px;
141
+ }
142
+
143
+
144
+ .section-title {
145
+ background: #79a;
146
+ color: #eee;
147
+ padding: 3px;
148
+ margin-top: 2em;
149
+ margin-left: -30px;
150
+ border: 1px solid #999;
151
+ }
152
+
153
+ .top-aligned-row { vertical-align: top }
154
+ .bottom-aligned-row { vertical-align: bottom }
155
+
156
+ /* --- Context section classes ----------------------- */
157
+
158
+ .context-row { }
159
+ .context-item-name { font-family: monospace; font-weight: bold; color: black; }
160
+ .context-item-value { font-size: small; color: #448; }
161
+ .context-item-desc { color: #333; padding-left: 2em; }
162
+
163
+ /* --- Method classes -------------------------- */
164
+ .method-detail {
165
+ background: #efefef;
166
+ padding: 0;
167
+ margin-top: 0.5em;
168
+ margin-bottom: 1em;
169
+ border: 1px dotted #ccc;
170
+ }
171
+ .method-heading {
172
+ color: black;
173
+ background: #ccc;
174
+ border-bottom: 1px solid #666;
175
+ padding: 0.2em 0.5em 0 0.5em;
176
+ }
177
+ .method-signature { color: black; background: inherit; }
178
+ .method-name { font-weight: bold; }
179
+ .method-args { font-style: italic; }
180
+ .method-description { padding: 0 0.5em 0 0.5em; }
181
+
182
+ /* --- Source code sections -------------------- */
183
+
184
+ a.source-toggle { font-size: 90%; }
185
+ div.method-source-code {
186
+ background: #262626;
187
+ color: #ffdead;
188
+ margin: 1em;
189
+ padding: 0.5em;
190
+ border: 1px dashed #999;
191
+ overflow: hidden;
192
+ }
193
+
194
+ div.method-source-code pre { color: #ffdead; overflow: hidden; }
195
+
196
+ /* --- Ruby keyword styles --------------------- */
197
+
198
+ .standalone-code { background: #221111; color: #ffdead; overflow: hidden; }
199
+
200
+ .ruby-constant { color: #7fffd4; background: transparent; }
201
+ .ruby-keyword { color: #00ffff; background: transparent; }
202
+ .ruby-ivar { color: #eedd82; background: transparent; }
203
+ .ruby-operator { color: #00ffee; background: transparent; }
204
+ .ruby-identifier { color: #ffdead; background: transparent; }
205
+ .ruby-node { color: #ffa07a; background: transparent; }
206
+ .ruby-comment { color: #b22222; font-weight: bold; background: transparent; }
207
+ .ruby-regexp { color: #ffa07a; background: transparent; }
208
+ .ruby-value { color: #7fffd4; background: transparent; }
data/lib/genosaurus.rb ADDED
@@ -0,0 +1,176 @@
1
+ require 'rubygems'
2
+ require 'mack_ruby_core_extensions'
3
+ require 'fileutils'
4
+ require 'erb'
5
+ require 'yaml'
6
+
7
+ class Genosaurus
8
+
9
+ include FileUtils
10
+
11
+ class << self
12
+
13
+ # Instantiates a new Genosaurus, passing the ENV hash as options into it, runs the generate method, and returns the Genosaurus object.
14
+ def run(options = ENV.to_hash)
15
+ gen = self.new(options)
16
+ gen.generate
17
+ gen
18
+ end
19
+
20
+ end
21
+
22
+ # Takes any options needed for this generator. If the generator requires any parameters an ArgumentError exception will be
23
+ # raised if those parameters are found in the options Hash. The setup method is called at the end of the initialization.
24
+ def initialize(options = {})
25
+ @options = options
26
+ self.class.required_params.each do |p|
27
+ raise ::ArgumentError.new("The required parameter '#{p.to_s.upcase}' is missing for this generator!") unless param(p)
28
+ end
29
+ @generator_name = self.class.name
30
+ @generator_name_underscore = @generator_name.underscore
31
+ @templates_directory_path = nil
32
+ @manifest_path = nil
33
+ $".each do |f|
34
+ if f.match(/#{@generator_name_underscore}\.rb$/)
35
+ @templates_directory_path = File.join(File.dirname(f), "templates")
36
+ @manifest_path = File.join(File.dirname(f), "manifest.yml")
37
+ end
38
+ end
39
+ setup
40
+ end
41
+
42
+ # Returns the path to the templates directory.
43
+ # IMPORTANT: The location of the templates_directory_path is VERY important! Genosaurus will attempt to find this location
44
+ # automatically, HOWEVER, if there is a problem, or you want to be special, you can override this method in your generator
45
+ # and have it return the correct path.
46
+ def templates_directory_path
47
+ @templates_directory_path
48
+ end
49
+
50
+ # 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
51
+ # method returns nil, then an implied manifest is used based on the templates_directory_path contents.
52
+ # IMPORTANT: Genosaurus will attempt to find this location automatically, HOWEVER, if there is a problem, or you want to
53
+ # be special, you can override this method in your generator and have it return the correct path.
54
+ def manifest_path
55
+ @manifest_path
56
+ end
57
+
58
+ # To be overridden in subclasses to do any setup work needed by the generator.
59
+ def setup
60
+ # does nothing, unless overridden in subclass.
61
+ end
62
+
63
+ # To be overridden in subclasses to do work before the generate method is run.
64
+ def before_generate
65
+ end
66
+
67
+ # To be overridden in subclasses to do work after the generate method is run.
68
+ # This is a simple way to call other generators.
69
+ def after_generate
70
+ end
71
+
72
+ # Returns the manifest for this generator, which is used by the generate method to do the dirty work.
73
+ # If there is a manifest.yml, defined by the manifest_path method, then the contents of that file are processed
74
+ # with ERB and returned. If there is not manifest.yml then an implied manifest is generated from the contents
75
+ # of the templates_directory_path.
76
+ def manifest
77
+ ivar_cache do
78
+ if File.exists?(manifest_path)
79
+ # run using the yml file
80
+ template = ERB.new(File.open(manifest_path).read, nil, "->")
81
+ man = YAML.load(template.result(binding))
82
+ else
83
+ files = Dir.glob(File.join(templates_directory_path, "**/*.template"))
84
+ man = {}
85
+ files.each_with_index do |f, i|
86
+ output_path = f.gsub(templates_directory_path, "")
87
+ output_path.gsub!(".template", "")
88
+ output_path.gsub!(/^\//, "")
89
+ man["template_#{i+1}"] = {
90
+ "type" => File.directory?(f) ? "directory" : "file",
91
+ "template_path" => f,
92
+ "output_path" => ERB.new(output_path, nil, "->").result(binding)
93
+ }
94
+ end
95
+ end
96
+ # puts man.inspect
97
+ man
98
+ end
99
+ end
100
+
101
+
102
+ # Used to define arguments that are required by the generator.
103
+ def self.require_param(*args)
104
+ required_params << args
105
+ required_params.flatten!
106
+ end
107
+
108
+ # Returns the required_params array.
109
+ def self.required_params
110
+ @required_params ||= []
111
+ end
112
+
113
+ # Returns a parameter from the initial Hash of parameters.
114
+ def param(key)
115
+ (@options[key.to_s.downcase] ||= @options[key.to_s.upcase])
116
+ end
117
+
118
+ # Takes an input_file runs it through ERB and
119
+ # saves it to the specified output_file. If the output_file exists it will
120
+ # be skipped. If you would like to force the writing of the file, use the
121
+ # :force => true option.
122
+ def template(input_file, output_file, options = @options)
123
+ if File.exists?(output_file)
124
+ unless options[:force]
125
+ puts "Skipped: #{output_file}"
126
+ return
127
+ end
128
+ end
129
+ # incase the directory doesn't exist, let's create it.
130
+ directory(File.dirname(output_file))
131
+ # puts "input_file: #{input_file}"
132
+ # puts "output_file: #{output_file}"
133
+ if $genosaurus_output_directory
134
+ output_file = File.join($genosaurus_output_directory, output_file)
135
+ end
136
+ File.open(output_file, "w") {|f| f.puts ERB.new(File.open(input_file).read, nil, "->").result(binding)}
137
+ puts "Wrote: #{output_file}"
138
+ end
139
+
140
+ # Creates the specified directory.
141
+ def directory(output_dir, options = @options)
142
+ if $genosaurus_output_directory
143
+ output_dir = File.join($genosaurus_output_directory, output_dir)
144
+ end
145
+ if File.exists?(output_dir)
146
+ puts "Exists: #{output_dir}"
147
+ return
148
+ end
149
+ mkdir_p(output_dir)
150
+ puts "Created: #{output_dir}"
151
+ end
152
+
153
+ # This does the dirty work of generation.
154
+ def generate
155
+ generate_callbacks do
156
+ manifest.each_value do |info|
157
+ case info["type"]
158
+ when "file"
159
+ template(info["template_path"], info["output_path"])
160
+ when "directory"
161
+ directory(info["output_path"])
162
+ else
163
+ raise "Unknown 'type': #{info["type"]}!"
164
+ end
165
+ end
166
+ end
167
+ end
168
+
169
+ private
170
+ def generate_callbacks
171
+ before_generate
172
+ yield
173
+ after_generate
174
+ end
175
+
176
+ end # Genosaurus
@@ -0,0 +1,81 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class GenosaurusBaseTest < Test::Unit::TestCase
4
+
5
+ def test_simple_implied_generator
6
+ hello_file = File.join($genosaurus_output_directory, "hello_world.rb")
7
+ goodbye_file = File.join($genosaurus_output_directory, "goodbye_world.rb")
8
+ assert !File.exists?(hello_file)
9
+ assert !File.exists?(goodbye_file)
10
+ @generator = HelloGoodbyeGenerator.run("name" => "Mark")
11
+ assert File.exists?(hello_file)
12
+ assert File.exists?(goodbye_file)
13
+ File.open(hello_file, "r") do |f|
14
+ assert_equal "Hello Mark\n", f.read
15
+ end
16
+ File.open(goodbye_file, "r") do |f|
17
+ assert_equal "Goodbye cruel world! Love, Mark\n", f.read
18
+ end
19
+ end
20
+
21
+ def test_simple_implied_manifest
22
+ @generator = HelloGoodbyeGenerator.new("name" => "Mark")
23
+ manifest = @generator.manifest
24
+ assert manifest.is_a?(Hash)
25
+ assert_equal 2, manifest.size
26
+ temp1 = manifest["template_1"]
27
+ assert_equal "file", temp1["type"]
28
+ assert_equal File.join(File.dirname(__FILE__), "lib", "hello_goodbye_generator", "templates", "goodbye_world.rb.template"), temp1["template_path"]
29
+ assert_equal "goodbye_world.rb", temp1["output_path"]
30
+ temp2 = manifest["template_2"]
31
+ assert_equal "file", temp2["type"]
32
+ assert_equal File.join(File.dirname(__FILE__), "lib", "hello_goodbye_generator", "templates", "hello_world.rb.template"), temp2["template_path"]
33
+ assert_equal "hello_world.rb", temp2["output_path"]
34
+ end
35
+
36
+ def test_require_param
37
+ assert_raise(ArgumentError) { HelloGoodbyeGenerator.new }
38
+ @generator = HelloGoodbyeGenerator.new("name" => :foo)
39
+ assert_not_nil @generator
40
+ assert_equal :foo, @generator.param(:name)
41
+ end
42
+
43
+ def test_complex_implied_generator
44
+ album_dir = File.join($genosaurus_output_directory, "beatles", "albums")
45
+ lyrics_file = File.join($genosaurus_output_directory, "beatles", "lyrics", "i_am_the_walrus.txt")
46
+ assert !File.exists?(album_dir)
47
+ assert !File.exists?(lyrics_file)
48
+ @generator = IAmTheWalrusGenerator.run("name" => "i_am_the_walrus")
49
+ assert File.exists?(album_dir)
50
+ assert File.exists?(lyrics_file)
51
+ File.open(lyrics_file, "r") do |f|
52
+ assert_equal "Lyrics for: I Am The Walrus\n", f.read
53
+ end
54
+ end
55
+
56
+ def test_simple_yml_manifest
57
+ @generator = StrawberryFieldsGenerator.new
58
+ manifest = @generator.manifest
59
+ assert manifest.is_a?(Hash)
60
+ assert_equal 2, manifest.size
61
+ info = manifest["directory_1"]
62
+ assert_equal "beatles/albums/magical_mystery_tour", info["output_path"]
63
+ assert_equal "directory", info["type"]
64
+ info = manifest["template_1"]
65
+ assert_equal File.join(File.dirname(__FILE__), "lib", "strawberry_fields_generator", "templates", "fields.txt"), info["template_path"]
66
+ assert_equal "beatles/albums/magical_mystery_tour/lyrics/strawberry_fields_forever.lyrics", info["output_path"]
67
+ end
68
+
69
+ def clean_tmp
70
+ FileUtils.rm_rf($genosaurus_output_directory, :verbose => false)
71
+ end
72
+
73
+ def setup
74
+ clean_tmp
75
+ end
76
+
77
+ def teardown
78
+ clean_tmp
79
+ end
80
+
81
+ end
@@ -0,0 +1,3 @@
1
+ class HelloGoodbyeGenerator < Genosaurus
2
+ require_param :name
3
+ end
@@ -0,0 +1 @@
1
+ Goodbye cruel world! Love, <%= param(:name) %>
@@ -0,0 +1 @@
1
+ Hello <%= param(:name) %>
@@ -0,0 +1,2 @@
1
+ class IAmTheWalrusGenerator < Genosaurus
2
+ end
@@ -0,0 +1 @@
1
+ Lyrics for: <%= param(:name).gsub("_", " ").capitalize_all_words %>
@@ -0,0 +1,7 @@
1
+ directory_1:
2
+ type: directory
3
+ output_path: beatles/albums/magical_mystery_tour
4
+ template_1:
5
+ type: file
6
+ template_path: <%= File.join(templates_directory_path, "fields.txt") %>
7
+ output_path: beatles/albums/magical_mystery_tour/lyrics/strawberry_fields_forever.lyrics
@@ -0,0 +1,2 @@
1
+ class StrawberryFieldsGenerator < Genosaurus
2
+ end
@@ -0,0 +1,29 @@
1
+ Let me take you down, 'cause I'm going to Strawberry Fields.
2
+ Nothing is real and nothing to get hung about.
3
+ Strawberry Fields forever.
4
+
5
+ Living is easy with eyes closed, misunderstanding all you see.
6
+ It's getting hard to be someone but it all works out.
7
+ It doesn't matter much to me.
8
+
9
+ Let me take you down, 'cause I'm going to Strawberry Fields.
10
+ Nothing is real and nothing to get hung about.
11
+ Strawberry Fields forever.
12
+
13
+ No one I think is in my tree, I mean it must be high or low.
14
+ That is you can't you know tune in but it's all right.
15
+ That is I think it's not too bad.
16
+
17
+ Let me take you down, 'cause I'm going to Strawberry Fields.
18
+ Nothing is real and nothing to get hung about.
19
+ Strawberry Fields forever.
20
+
21
+ Always no sometimes think it's me, but you know I know when it's a dream.
22
+ I think I know I mean "Yes," but it's all wrong.
23
+ That is I think I disagree.
24
+
25
+ Let me take you down, 'cause I'm going to Strawberry Fields.
26
+ Nothing is real and nothing to get hung about.
27
+ Strawberry Fields forever.
28
+ Strawberry Fields forever.
29
+ Strawberry Fields forever.
@@ -0,0 +1,17 @@
1
+ require "test/unit"
2
+ require 'fileutils'
3
+ require File.join(File.dirname(__FILE__), "..", "lib", "genosaurus")
4
+ # place common methods, assertions, and other type things in this file so
5
+ # other tests will have access to them.
6
+
7
+ if $genosaurus_output_directory.nil?
8
+ $genosaurus_output_directory = File.join(FileUtils.pwd, "tmp")
9
+ end
10
+
11
+ puts "$genosaurus_output_directory: #{$genosaurus_output_directory}"
12
+
13
+ Dir.glob(File.join(File.dirname(__FILE__), "lib", "**/*.rb")).each {|f| require f}
14
+
15
+ class Test::Unit::TestCase
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: genosaurus
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - markbates
8
+ autorequire: genosaurus
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-22 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mack_ruby_core_extensions
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: "genosaurus was developed by: markbates"
25
+ email: mark@markbates.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/genosaurus.rb
34
+ - README
35
+ - doc/classes/Genosaurus.html
36
+ - doc/created.rid
37
+ - doc/files/lib/genosaurus_rb.html
38
+ - doc/files/README.html
39
+ - doc/fr_class_index.html
40
+ - doc/fr_file_index.html
41
+ - doc/fr_method_index.html
42
+ - doc/index.html
43
+ - doc/rdoc-style.css
44
+ has_rdoc: true
45
+ homepage: http://www.mackframework.com
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: magrathea
67
+ rubygems_version: 1.0.1
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: genosaurus
71
+ test_files:
72
+ - test/genosaurus_test.rb
73
+ - test/lib
74
+ - test/lib/directory_generator
75
+ - test/lib/directory_generator/templates
76
+ - test/lib/directory_generator/templates/months
77
+ - test/lib/directory_generator/templates/months/february.template
78
+ - test/lib/directory_generator/templates/months/january.template
79
+ - test/lib/directory_generator/templates/months/march.template
80
+ - test/lib/hello_goodbye_generator
81
+ - test/lib/hello_goodbye_generator/hello_goodbye_generator.rb
82
+ - test/lib/hello_goodbye_generator/templates
83
+ - test/lib/hello_goodbye_generator/templates/goodbye_world.rb.template
84
+ - test/lib/hello_goodbye_generator/templates/hello_world.rb.template
85
+ - test/lib/i_am_the_walrus_generator
86
+ - test/lib/i_am_the_walrus_generator/i_am_the_walrus_generator.rb
87
+ - test/lib/i_am_the_walrus_generator/templates
88
+ - test/lib/i_am_the_walrus_generator/templates/beatles
89
+ - test/lib/i_am_the_walrus_generator/templates/beatles/albums.template
90
+ - test/lib/i_am_the_walrus_generator/templates/beatles/lyrics
91
+ - test/lib/i_am_the_walrus_generator/templates/beatles/lyrics/<%=param(:name)%>.txt.template
92
+ - test/lib/strawberry_fields_generator
93
+ - test/lib/strawberry_fields_generator/manifest.yml
94
+ - test/lib/strawberry_fields_generator/strawberry_fields_generator.rb
95
+ - test/lib/strawberry_fields_generator/templates
96
+ - test/lib/strawberry_fields_generator/templates/fields.txt
97
+ - test/test_helper.rb