Module: Maml

Defined in:
lib/maml/maml.rb

Class Method Summary collapse

Class Method Details

.build_model_args(maml) ⇒ Object

build script/generate model arguments from yaml info



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
# File 'lib/maml/maml.rb', line 77

def build_model_args maml

  model_args={}  # of field:type format as used by script/generate model etc.
  model_options={}  # e.g 50, NULL, etc. 
  # note: may not have had to separate them, but did in the case of future additional processing
  
  maml.each do |app|
    puts "\napplication:#{app[0]}"
    print "models: \n" 
    app[1].each do |model|
      current_model_args=[]
      current_model_options=[]
      model_name=model[0]
      print "model: #{model_name} "
      model[1].each do |field|
        arg, options=extract_arg field
        $logger.debug "Extract Arg #{field} ===> #{arg}"
        $logger.debug "Extract Options #{field} ===> #{options}"
        current_model_args << arg
        field_name, field_type=arg.split(":")
        puts "field_name=#{field_name}, field_type=#{field_type}"
        current_model_options << [field_name, options]
      end
      
      model_args[model_name]=current_model_args
      model_options[model_name]=current_model_options
      
    end
    puts
  end
  return model_args, model_options
end

.create_sampleObject



42
43
44
45
46
# File 'lib/maml/maml.rb', line 42

def create_sample
  pwd=Dir.pwd
  sample= File.join File.dirname(__FILE__), "maml.yml"
  File.copy sample, pwd, true
end

.extract_arg(maml_field) ⇒ Object



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
# File 'lib/maml/maml.rb', line 48

def extract_arg maml_field
  maml_arg=nil
  field=nil
  type=nil

  $logger.info this_method + "=> maml_field=#{maml_field}"
  type=case maml_field
      when /^::(\w*)\b[,]?(.*)/; "text"
      when /^:(\w*)\b[,]?(.*)/; "string"
      when /^\.\.(\w*)\b[,]?(.*)/; "float"
      when /^\.(\w*)\b[,]?(.*)/; "integer"
      when /(\w*_id)\b[,]?(.*)/; "integer"
      when /^=(\w*)\b[,]?(.*)/; "boolean"
      when /^%%(\w*)\b[,]?(.*)/; "datetime"
      when /^%(\w*)\b[,]?(.*)/; "date"
      when /^@@(\w*)\b[,]?(.*)/; "timestamp"
      when /^@(\w*)\b[,]?(.*)/; "time"
      when /^&(\w*)\b[,]?(.*)/; "binary"
      when /(\w*)\b[,]?(.*)/; "string"
      else raise "Invalid field type";
   end
   field=Regexp.last_match(1)
   options=Regexp.last_match(2)
   maml_arg= "#{field}:#{type}"
   maml_options= "#{options}"
   return maml_arg, maml_options
end

.mainObject

main function



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/maml/maml.rb', line 157

def main
  puts "\nMAML=Migration Apathy Markup Language"
  puts "======================================"
  puts "Visit http://lazymaml.org for more details"
  puts "Copyright 2009 Zigelo and Nick Van Weerdenburg, Licensed under MIT License\n\n"
  puts "usage: maml.rb [--file <filename>] [your script/generate options] [--go]"
  puts ""
  puts "MAML automates multiple script/generate calls by collecting your model and field names in a DSL."
  puts "Command-line usage is the ame as script/generate with addition of --file and --go"
  puts "--file specifies a yaml file with the models and fields (maml.yml by default)"
  puts "(run maml create_sample to generate a sample file)"
  puts "--go runs script/generate for real. Running without --go is a trial"
  puts "EXAMPLES:"
  puts "e.g. maml.rb --file=blog.yml scaffold"
  puts "e.g. maml.rb --file=blog.yml scaffold --go"
  puts "e.g. maml.rb scaffold --go"
  puts "The last example looks for maml.yml by default\n\n"
  puts "Additional arguments are passed straight through to script/generate, such as --haml and --rspec"
  puts " e.g. maml nifty_scaffold --haml --rspec"
  # puts "Generated files are in <rails_root>/maml"
  puts "Run 'maml.rb create_sample' to generate a sample maml.yml file for a starting point. Note: this will overwrite any existing maml.yml."
  puts "\nSpecify field type by symbol prefix as follows:"
  puts "no prefix=string ; no prefix and _id suffix = integer ; override _id by adding prefix"
  puts "examples: string, integer_id, .integer, ..float,  %date, %%datetime, @time, @@timestamp, :string, ::text, =boolean, &binary"
  puts "------------------------------------------------------------------------\n"
  puts ""

  generate_command, file, go = process_args ARGV
  $logger.info "\ngenerate_command=#{generate_command}, file=#{file}"
    
  file="maml.yml" unless file
  
  unless generate_command
    generate_command="model" 
    puts "WARNING: using default 'model' for script/generate"
    $logger.info "WARNING: using default 'model' for script/generate"
  end
  puts "generate_command=#{generate_command}, file=#{file}"
  
  # allow copy_sample via file (e.g. maml.rb copy_sample) or via command (e.g. maml.rb -copy_sample)
  case file
    when "create_sample"
      create_sample
      exit
    else
      # continue on
  end

  case generate_command
    when "create_sample"
      create_sample
      exit
    else
      # continue on
  end

  maml=nil
  begin 
    maml=YAML::load(File.open(file))
    $logger.info "YAML loaded file"
  rescue
    $logger.debug "Unable to load #{file}"
    puts "Unable to load file #{file}. Check if it exists."
    exit
  end

  model_args, model_options = build_model_args maml
  
  # todo: extract default args
  default_fields=nil
  default_model = model_args["default"]
  default_fields= default_model[1].join(" ") if default_model
  puts ":default fields: #{default_fields}"
  $logger.info ":default fields: #{default_fields}"
  model_args.delete "default" # so not to generate

  puts

  # now execute each model
  model_args.each do |model|
    model_name=model[0]
    puts "model_name: #{model_name}"
    model_fields=model[1].join " "
    # File.open("maml.log", "a") { |file| file.write "---- model: #{model_name} (#{model_fields}) ---\n\n" }
    # File.open("maml.log", "a") { |file| file.write 
    $logger.info "---- model: #{model_name} \n\t\t\t#{model_fields.split(" ").join("\n\t\t\t")}\n---\n\n"
    # command="ruby script/generate #{generate_command} #{model_name} #{model_fields} >> maml.log"
    command="ruby script/generate #{generate_command} #{model_name} #{model_fields}"
    puts "command: #{command}\n\n"

    if go
      puts "=== calling system command ==="
      result=%x[#{command}]
      puts "RESULT ====>\n #{result}"
      post_process_migrations model, result
    
      $logger.info "\n\n#{result}\n\n"
    else
      puts "=== trial run...run with '--go' to generated files ==="
    end
  end

  model_options.each do |model|
    model_name=model[0]
    puts "options model_name: #{model_name}"
  
    model[1].each do |options|
      field_name=options[0]
      option_list=options[1]
      options_list_message=""
      if option_list !=nil && option_list.size > 0
        options_list_message="| options => #{option_list}"
      end
      puts "options logic: field_name=#{field_name} #{options_list_message}"
    end
  end
  # todo: parse generated migrations and add options
  # todo: add index logic
  
  puts "\n\nDONE! This was a trial run unless '--go' provided. 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)"
ensure
  puts "Thanks for being a Lazy MAML!"
  return 0
end

.post_process_migrations(model, result) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/maml/maml.rb', line 136

def post_process_migrations model, result
  model_name=model[0]
  puts "model_name: #{model_name}"
  model_fields=model[1]  # join(" ") to create args for generator ..e.g. fieldname1:string fieldname2:integer

  # create  app/models/user_group.rb
  model_file_regex="create  app/models/(.*).rb"
  # model_file_name=user_group

  # 20090731211953_create_user_groups.rb
  # find migration with same but plural
  # migration_file=

  # open file
  # find text field
  # append options
  # 
end

.process_args(args) ⇒ Object

todo: add support for multiple files todo: add generate command override options to maml.yml for model specific generations.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/maml/maml.rb', line 112

def process_args args
  file, go=nil
  generate_command=[]

  args.each do |arg|
    if arg == "--go" then
        go=true
    elsif arg[0,7] == "--file="
        file=arg[8,100] # 100 is arbitrary
        $logger.info "file=#{file} from --file= argument"
    else 
      generate_command << command
    end
  end
  
  if generate_command.empty?
    generate_command=nil  # will use default
  else
    generate_command = generate_command.join(" ")
  end
  puts "**** script/generate command=#{generate_command}"
  return generate_command,file, go
end

.testObject

simple test fixture



38
39
40
# File 'lib/maml/maml.rb', line 38

def test
  string_arg
end