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