maml 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/lib/maml.rb +1 -244
  3. data/lib/maml/maml.rb +244 -0
  4. data/maml.gemspec +2 -1
  5. metadata +2 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/maml.rb CHANGED
@@ -1,244 +1 @@
1
- #!/usr/bin/env ruby
2
- # maml.rb - Model and Migration Apathy Markup Language
3
- # there is only 1 m in maml because I'm lazy.
4
- # Copyright 2009 Zigleo, Inc.
5
- # Author: Nick Van Weerdenburg
6
- # todo: switch all puts to logger
7
- # todo: remove some excess logging
8
- # todo: create a gem
9
- # todo:
10
- require "yaml"
11
- require "logger"
12
-
13
- # I love logging
14
- $logger=Logger.new('maml.log')
15
- $logger.level=Logger::INFO
16
- $logger.info "PLATFORM=#{PLATFORM}"
17
-
18
- # look user and project configuration file
19
- user_config_file=ENV['HOME'] + "/.mamlrc"
20
- project_config_file=".mamlrc"
21
- $logger.info("user .mamlrc #{File.exist? user_config_file ? 'found' : 'not found'}")
22
- $logger.info("project .mamlrc #{File.exist? project_config_file ? 'found' : 'not found'}")
23
-
24
- module CurrentMethodName
25
- def this_method
26
- caller[0]=~/`(.*?)'/
27
- $1
28
- end
29
- end
30
-
31
- include CurrentMethodName
32
-
33
- # simple test fixture
34
- def test
35
- string_arg
36
- end
37
-
38
- def extract_arg maml_field
39
- maml_arg=nil
40
- field=nil
41
- type=nil
42
-
43
- $logger.info this_method + "=> maml_field=#{maml_field}"
44
- type=case maml_field
45
- when /^::(\w*)\b[,]?(.*)/; "text"
46
- when /^:(\w*)\b[,]?(.*)/; "string"
47
- when /^\.\.(\w*)\b[,]?(.*)/; "float"
48
- when /^\.(\w*)\b[,]?(.*)/; "integer"
49
- when /(\w*_id)\b[,]?(.*)/; "integer"
50
- when /^=(\w*)\b[,]?(.*)/; "boolean"
51
- when /^%%(\w*)\b[,]?(.*)/; "datetime"
52
- when /^%(\w*)\b[,]?(.*)/; "date"
53
- when /^@@(\w*)\b[,]?(.*)/; "timestamp"
54
- when /^@(\w*)\b[,]?(.*)/; "time"
55
- when /^&(\w*)\b[,]?(.*)/; "binary"
56
- when /(\w*)\b[,]?(.*)/; "string"
57
- else raise "Invalid field type";
58
- end
59
- field=Regexp.last_match(1)
60
- options=Regexp.last_match(2)
61
- maml_arg= "#{field}:#{type}"
62
- maml_options= "#{options}"
63
- return maml_arg, maml_options
64
- end
65
-
66
- # build script/generate model arguments from yaml info
67
- def build_model_args maml
68
- model_args={}
69
- model_options={}
70
- maml.each do |app|
71
- puts "\napplication:#{app[0]}"
72
- print "models: "
73
- app[1].each do |model|
74
- current_model_args=[]
75
- current_model_options=[]
76
- model_name=model[0]
77
- print "#{model_name} "
78
- model[1].each do |field|
79
- arg, options=extract_arg field
80
- $logger.debug "Extract Arg #{field} ===> #{arg}"
81
- $logger.debug "Extract Options #{field} ===> #{options}"
82
- current_model_args << arg
83
- field_name, field_type=arg.split(":")
84
- puts "field_name=#{field_name}, field_type=#{field_type}"
85
- current_model_options << [field_name, options]
86
- end
87
- model_args[model_name]=current_model_args
88
- model_options[model_name]=current_model_options
89
- end
90
- puts
91
- end
92
- return model_args, model_options
93
- end
94
-
95
- # todo: add support for multiple files
96
- # todo: add generate command override options to maml.yml for model specific generations.
97
- def process_args args
98
- file, go=nil
99
- generate_command=[]
100
-
101
- args.each do |arg|
102
- if arg[0,1] == "-" then
103
- command=arg[1,arg.length]
104
- if command == "go"
105
- go=true
106
- else
107
- generate_command << command
108
- end
109
- else
110
- file=arg
111
- end
112
- end
113
- if generate_command.empty?
114
- generate_command=nil
115
- else
116
- generate_command = generate_command.join(" ")
117
- end
118
- puts "**** generate_command=#{generate_command}"
119
- return generate_command,file, go
120
- end
121
-
122
- def post_process_migrations model, result
123
- model_name=model[0]
124
- puts "model_name: #{model_name}"
125
- model_fields=model[1] # join(" ") to create args for generator ..e.g. fieldname1:string fieldname2:integer
126
-
127
- # create app/models/user_group.rb
128
- model_file_regex="create app/models/(.*).rb"
129
- # model_file_name=user_group
130
-
131
- # 20090731211953_create_user_groups.rb
132
- # find migration with same but plural
133
- # migration_file=
134
-
135
- # open file
136
- # find text field
137
- # append options
138
- #
139
- end
140
-
141
-
142
- # main function
143
- def main
144
- puts "\nMAML=Migration Apathy Markup Language"
145
- puts "======================================"
146
- puts "Visit http://lazymaml.org for more details"
147
- puts "Copyright 2009 Zigelo and Nick Van Weerdenburg, Licensed under MIT License\n\n"
148
- puts "usage: maml.rb <filename> [-generate_command]"
149
- puts "runs trial by default, add -go for rails generation"
150
- puts "e.g. maml.rb blog.yml -scaffold"
151
- puts "OR"
152
- puts "e.g. maml.rb blog.yml -scaffold -go"
153
- puts "OR"
154
- puts "usage: maml.rb"
155
- puts "(defaults to 'maml.yml' file and generating the model)\n\n"
156
- puts "use ---haml or similar for adding extra commands. -<anything> is passed to the command-line minus the -"
157
- puts "maml supports one file at time"
158
- puts "generated files are in <rails_root>/maml"
159
- puts "\nSpecify field type by symbol prefix as follows:"
160
- puts "no prefix=string ; no prefix and _id suffix = integer ; override _id by adding prefix"
161
- puts "examples: string, integer_id, .integer, ..float, %date, %%datetime, @time, @@timestamp, :string, ::text, =boolean, &binary"
162
- puts "------------------------------------------------------------------------\n"
163
- puts ""
164
-
165
- generate_command, file, go = process_args ARGV
166
- $logger.info "\ngenerate_command=#{generate_command}, file=#{file}"
167
- @file_provided=true if file
168
- $logger.info "@file_provided=#{@file_provided}"
169
-
170
- file="maml.yml" unless file
171
- generate_command="model" unless generate_command
172
- puts "generate_command=#{generate_command}, file=#{file}"
173
-
174
- maml=nil
175
- begin
176
- maml=YAML::load(File.open(file))
177
- $logger.info "YAML loaded file"
178
- rescue
179
- $logger.debug "Unable to load #{file}"
180
- puts "Unable to load file #{file}"
181
- exit
182
- end
183
-
184
- model_args, model_options = build_model_args maml
185
-
186
- puts
187
-
188
- # now execute each model
189
- model_args.each do |model|
190
- model_name=model[0]
191
- puts "model_name: #{model_name}"
192
- model_fields=model[1].join " "
193
- # File.open("maml.log", "a") { |file| file.write "---- model: #{model_name} (#{model_fields}) ---\n\n" }
194
- # File.open("maml.log", "a") { |file| file.write
195
- $logger.info "---- model: #{model_name} \n\t\t\t#{model_fields.split(" ").join("\n\t\t\t")}\n---\n\n"
196
- # command="ruby script/generate #{generate_command} #{model_name} #{model_fields} >> maml.log"
197
- command="ruby script/generate #{generate_command} #{model_name} #{model_fields}"
198
- puts "command: #{command}\n\n"
199
- if @file_provided == true
200
- if go
201
- puts "=== calling system command ==="
202
- result=%x[#{command}]
203
- puts "RESULT ====>\n #{result}"
204
- post_process_migrations model, result
205
-
206
- $logger.info "\n\n#{result}\n\n"
207
- else
208
- puts "=== trial run...run with '-go' to generated files ==="
209
- end
210
- else
211
- puts "=== no file provided ==="
212
- end
213
- end
214
-
215
- model_options.each do |model|
216
- model_name=model[0]
217
- puts "options model_name: #{model_name}"
218
-
219
- model[1].each do |options|
220
- field_name=options[0]
221
- option_list=options[1]
222
- options_list_message=""
223
- if option_list !=nil && option_list.size > 0
224
- options_list_message="| options => #{option_list}"
225
- end
226
- puts "options logic: field_name=#{field_name} #{options_list_message}"
227
- end
228
- end
229
- # todo: parse generated migrations and add options
230
- # todo: add index logic
231
-
232
- puts "\n\nDONE! Look at maml.log for script results, and in app/models, db/migrations, test/fixtures and test/unit for generated code (if you ran maml.rb with a command line arg)"
233
- unless ARGV[0]
234
- puts "\n\nUse 'maml.rb maml.yml' (or other file arg) to actuallly run generators. Running with default maml.yml does test run only."
235
- end
236
- end
237
-
238
-
239
- # only run main if run standalone (e.g. not via ruby require)
240
- if __FILE__ == $0
241
- # puts "***** #{File.basename($0)} ran from file *****"
242
-
243
- main
244
- end
1
+ require "maml/maml.rb"
data/lib/maml/maml.rb ADDED
@@ -0,0 +1,244 @@
1
+ #!/usr/bin/env ruby
2
+ # maml.rb - Model and Migration Apathy Markup Language
3
+ # there is only 1 m in maml because I'm lazy.
4
+ # Copyright 2009 Zigleo, Inc.
5
+ # Author: Nick Van Weerdenburg
6
+ # todo: switch all puts to logger
7
+ # todo: remove some excess logging
8
+ # todo: create a gem
9
+ # todo:
10
+ require "yaml"
11
+ require "logger"
12
+
13
+ # I love logging
14
+ $logger=Logger.new('maml.log')
15
+ $logger.level=Logger::INFO
16
+ $logger.info "PLATFORM=#{PLATFORM}"
17
+
18
+ # look user and project configuration file
19
+ user_config_file=ENV['HOME'] + "/.mamlrc"
20
+ project_config_file=".mamlrc"
21
+ $logger.info("user .mamlrc #{File.exist? user_config_file ? 'found' : 'not found'}")
22
+ $logger.info("project .mamlrc #{File.exist? project_config_file ? 'found' : 'not found'}")
23
+
24
+ module CurrentMethodName
25
+ def this_method
26
+ caller[0]=~/`(.*?)'/
27
+ $1
28
+ end
29
+ end
30
+
31
+ include CurrentMethodName
32
+
33
+ # simple test fixture
34
+ def test
35
+ string_arg
36
+ end
37
+
38
+ def extract_arg maml_field
39
+ maml_arg=nil
40
+ field=nil
41
+ type=nil
42
+
43
+ $logger.info this_method + "=> maml_field=#{maml_field}"
44
+ type=case maml_field
45
+ when /^::(\w*)\b[,]?(.*)/; "text"
46
+ when /^:(\w*)\b[,]?(.*)/; "string"
47
+ when /^\.\.(\w*)\b[,]?(.*)/; "float"
48
+ when /^\.(\w*)\b[,]?(.*)/; "integer"
49
+ when /(\w*_id)\b[,]?(.*)/; "integer"
50
+ when /^=(\w*)\b[,]?(.*)/; "boolean"
51
+ when /^%%(\w*)\b[,]?(.*)/; "datetime"
52
+ when /^%(\w*)\b[,]?(.*)/; "date"
53
+ when /^@@(\w*)\b[,]?(.*)/; "timestamp"
54
+ when /^@(\w*)\b[,]?(.*)/; "time"
55
+ when /^&(\w*)\b[,]?(.*)/; "binary"
56
+ when /(\w*)\b[,]?(.*)/; "string"
57
+ else raise "Invalid field type";
58
+ end
59
+ field=Regexp.last_match(1)
60
+ options=Regexp.last_match(2)
61
+ maml_arg= "#{field}:#{type}"
62
+ maml_options= "#{options}"
63
+ return maml_arg, maml_options
64
+ end
65
+
66
+ # build script/generate model arguments from yaml info
67
+ def build_model_args maml
68
+ model_args={}
69
+ model_options={}
70
+ maml.each do |app|
71
+ puts "\napplication:#{app[0]}"
72
+ print "models: "
73
+ app[1].each do |model|
74
+ current_model_args=[]
75
+ current_model_options=[]
76
+ model_name=model[0]
77
+ print "#{model_name} "
78
+ model[1].each do |field|
79
+ arg, options=extract_arg field
80
+ $logger.debug "Extract Arg #{field} ===> #{arg}"
81
+ $logger.debug "Extract Options #{field} ===> #{options}"
82
+ current_model_args << arg
83
+ field_name, field_type=arg.split(":")
84
+ puts "field_name=#{field_name}, field_type=#{field_type}"
85
+ current_model_options << [field_name, options]
86
+ end
87
+ model_args[model_name]=current_model_args
88
+ model_options[model_name]=current_model_options
89
+ end
90
+ puts
91
+ end
92
+ return model_args, model_options
93
+ end
94
+
95
+ # todo: add support for multiple files
96
+ # todo: add generate command override options to maml.yml for model specific generations.
97
+ def process_args args
98
+ file, go=nil
99
+ generate_command=[]
100
+
101
+ args.each do |arg|
102
+ if arg[0,1] == "-" then
103
+ command=arg[1,arg.length]
104
+ if command == "go"
105
+ go=true
106
+ else
107
+ generate_command << command
108
+ end
109
+ else
110
+ file=arg
111
+ end
112
+ end
113
+ if generate_command.empty?
114
+ generate_command=nil
115
+ else
116
+ generate_command = generate_command.join(" ")
117
+ end
118
+ puts "**** generate_command=#{generate_command}"
119
+ return generate_command,file, go
120
+ end
121
+
122
+ def post_process_migrations model, result
123
+ model_name=model[0]
124
+ puts "model_name: #{model_name}"
125
+ model_fields=model[1] # join(" ") to create args for generator ..e.g. fieldname1:string fieldname2:integer
126
+
127
+ # create app/models/user_group.rb
128
+ model_file_regex="create app/models/(.*).rb"
129
+ # model_file_name=user_group
130
+
131
+ # 20090731211953_create_user_groups.rb
132
+ # find migration with same but plural
133
+ # migration_file=
134
+
135
+ # open file
136
+ # find text field
137
+ # append options
138
+ #
139
+ end
140
+
141
+
142
+ # main function
143
+ def main
144
+ puts "\nMAML=Migration Apathy Markup Language"
145
+ puts "======================================"
146
+ puts "Visit http://lazymaml.org for more details"
147
+ puts "Copyright 2009 Zigelo and Nick Van Weerdenburg, Licensed under MIT License\n\n"
148
+ puts "usage: maml.rb <filename> [-generate_command]"
149
+ puts "runs trial by default, add -go for rails generation"
150
+ puts "e.g. maml.rb blog.yml -scaffold"
151
+ puts "OR"
152
+ puts "e.g. maml.rb blog.yml -scaffold -go"
153
+ puts "OR"
154
+ puts "usage: maml.rb"
155
+ puts "(defaults to 'maml.yml' file and generating the model)\n\n"
156
+ puts "use ---haml or similar for adding extra commands. -<anything> is passed to the command-line minus the -"
157
+ puts "maml supports one file at time"
158
+ puts "generated files are in <rails_root>/maml"
159
+ puts "\nSpecify field type by symbol prefix as follows:"
160
+ puts "no prefix=string ; no prefix and _id suffix = integer ; override _id by adding prefix"
161
+ puts "examples: string, integer_id, .integer, ..float, %date, %%datetime, @time, @@timestamp, :string, ::text, =boolean, &binary"
162
+ puts "------------------------------------------------------------------------\n"
163
+ puts ""
164
+
165
+ generate_command, file, go = process_args ARGV
166
+ $logger.info "\ngenerate_command=#{generate_command}, file=#{file}"
167
+ @file_provided=true if file
168
+ $logger.info "@file_provided=#{@file_provided}"
169
+
170
+ file="maml.yml" unless file
171
+ generate_command="model" unless generate_command
172
+ puts "generate_command=#{generate_command}, file=#{file}"
173
+
174
+ maml=nil
175
+ begin
176
+ maml=YAML::load(File.open(file))
177
+ $logger.info "YAML loaded file"
178
+ rescue
179
+ $logger.debug "Unable to load #{file}"
180
+ puts "Unable to load file #{file}"
181
+ exit
182
+ end
183
+
184
+ model_args, model_options = build_model_args maml
185
+
186
+ puts
187
+
188
+ # now execute each model
189
+ model_args.each do |model|
190
+ model_name=model[0]
191
+ puts "model_name: #{model_name}"
192
+ model_fields=model[1].join " "
193
+ # File.open("maml.log", "a") { |file| file.write "---- model: #{model_name} (#{model_fields}) ---\n\n" }
194
+ # File.open("maml.log", "a") { |file| file.write
195
+ $logger.info "---- model: #{model_name} \n\t\t\t#{model_fields.split(" ").join("\n\t\t\t")}\n---\n\n"
196
+ # command="ruby script/generate #{generate_command} #{model_name} #{model_fields} >> maml.log"
197
+ command="ruby script/generate #{generate_command} #{model_name} #{model_fields}"
198
+ puts "command: #{command}\n\n"
199
+ if @file_provided == true
200
+ if go
201
+ puts "=== calling system command ==="
202
+ result=%x[#{command}]
203
+ puts "RESULT ====>\n #{result}"
204
+ post_process_migrations model, result
205
+
206
+ $logger.info "\n\n#{result}\n\n"
207
+ else
208
+ puts "=== trial run...run with '-go' to generated files ==="
209
+ end
210
+ else
211
+ puts "=== no file provided ==="
212
+ end
213
+ end
214
+
215
+ model_options.each do |model|
216
+ model_name=model[0]
217
+ puts "options model_name: #{model_name}"
218
+
219
+ model[1].each do |options|
220
+ field_name=options[0]
221
+ option_list=options[1]
222
+ options_list_message=""
223
+ if option_list !=nil && option_list.size > 0
224
+ options_list_message="| options => #{option_list}"
225
+ end
226
+ puts "options logic: field_name=#{field_name} #{options_list_message}"
227
+ end
228
+ end
229
+ # todo: parse generated migrations and add options
230
+ # todo: add index logic
231
+
232
+ puts "\n\nDONE! Look at maml.log for script results, and in app/models, db/migrations, test/fixtures and test/unit for generated code (if you ran maml.rb with a command line arg)"
233
+ unless ARGV[0]
234
+ puts "\n\nUse 'maml.rb maml.yml' (or other file arg) to actuallly run generators. Running with default maml.yml does test run only."
235
+ end
236
+ end
237
+
238
+
239
+ # only run main if run standalone (e.g. not via ruby require)
240
+ if __FILE__ == $0
241
+ # puts "***** #{File.basename($0)} ran from file *****"
242
+
243
+ main
244
+ end
data/maml.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{maml}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nick Van Weerdenburg"]
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "gem_notes",
30
30
  "init.rb",
31
31
  "lib/maml.rb",
32
+ "lib/maml/maml.rb",
32
33
  "maml-0.1.1.gem",
33
34
  "maml-0.1.2.gem",
34
35
  "maml.gemspec",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Van Weerdenburg
@@ -33,6 +33,7 @@ files:
33
33
  - gem_notes
34
34
  - init.rb
35
35
  - lib/maml.rb
36
+ - lib/maml/maml.rb
36
37
  - maml-0.1.1.gem
37
38
  - maml-0.1.2.gem
38
39
  - maml.gemspec