padrino-gen 0.10.2 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.document +3 -3
  2. data/.yardopts +1 -0
  3. data/{LICENSE → LICENSE.txt} +0 -0
  4. data/README.rdoc +1 -1
  5. data/lib/padrino-gen/command.rb +7 -0
  6. data/lib/padrino-gen/generators/actions.rb +264 -24
  7. data/lib/padrino-gen/generators/app/app.rb.tt +11 -11
  8. data/lib/padrino-gen/generators/app.rb +10 -9
  9. data/lib/padrino-gen/generators/cli.rb +3 -0
  10. data/lib/padrino-gen/generators/components/actions.rb +86 -11
  11. data/lib/padrino-gen/generators/components/mocks/rr.rb +3 -1
  12. data/lib/padrino-gen/generators/components/orms/mongoid.rb +1 -1
  13. data/lib/padrino-gen/generators/components/orms/mongomapper.rb +1 -1
  14. data/lib/padrino-gen/generators/components/orms/mongomatic.rb +1 -1
  15. data/lib/padrino-gen/generators/components/stylesheets/less.rb +1 -1
  16. data/lib/padrino-gen/generators/components/tests/minitest.rb +78 -0
  17. data/lib/padrino-gen/generators/controller.rb +3 -0
  18. data/lib/padrino-gen/generators/mailer.rb +3 -0
  19. data/lib/padrino-gen/generators/model.rb +3 -0
  20. data/lib/padrino-gen/generators/plugin.rb +2 -0
  21. data/lib/padrino-gen/generators/project.rb +11 -3
  22. data/lib/padrino-gen/generators/runner.rb +85 -34
  23. data/lib/padrino-gen/generators/templates/Gemfile.tt +12 -21
  24. data/lib/padrino-gen/padrino-tasks/activerecord.rb +0 -194
  25. data/lib/padrino-gen.rb +21 -0
  26. data/test/helper.rb +2 -20
  27. data/test/test_app_generator.rb +17 -17
  28. data/test/test_cli.rb +6 -5
  29. data/test/test_controller_generator.rb +52 -44
  30. data/test/test_generator.rb +1 -1
  31. data/test/test_mailer_generator.rb +18 -18
  32. data/test/test_migration_generator.rb +44 -44
  33. data/test/test_model_generator.rb +140 -123
  34. data/test/test_plugin_generator.rb +21 -33
  35. data/test/test_project_generator.rb +125 -103
  36. metadata +10 -8
@@ -5,7 +5,14 @@ module Padrino
5
5
  module Runner
6
6
 
7
7
  # Generates project scaffold based on a given template file
8
- # project :test => :shoulda, :orm => :activerecord, :renderer => "haml"
8
+ #
9
+ # @param [Hash] options
10
+ # Options to use to generate the project
11
+ #
12
+ # @example
13
+ # project :test => :shoulda, :orm => :activerecord, :renderer => "haml"
14
+ #
15
+ # @api public
9
16
  def project(options={})
10
17
  components = options.sort_by { |k, v| k.to_s }.map { |component, value| "--#{component}=#{value}" }
11
18
  params = [name, *components].push("-r=#{destination_root("../")}")
@@ -14,9 +21,18 @@ module Padrino
14
21
  end
15
22
 
16
23
  # Executes generator command for specified type with given arguments
17
- # generate :model, "post title:string body:text"
18
- # generate :controller, "posts get:index get:new post:new"
19
- # generate :migration, "AddEmailToUser email:string"
24
+ #
25
+ # @param [Symbol] type
26
+ # Type of component module
27
+ # @param [String] arguments
28
+ # Arguments to send to component generator
29
+ #
30
+ # @example
31
+ # generate :model, "post title:string body:text"
32
+ # generate :controller, "posts get:index get:new post:new"
33
+ # generate :migration, "AddEmailToUser email:string"
34
+ #
35
+ # @api public
20
36
  def generate(type, arguments="")
21
37
  params = arguments.split(" ").push("-r=#{destination_root}")
22
38
  params.push("--app=#{@_app_name}") if @_app_name
@@ -25,16 +41,32 @@ module Padrino
25
41
  end
26
42
 
27
43
  # Executes rake command with given arguments
28
- # rake "custom task1 task2"
44
+ #
45
+ # @param [String] command
46
+ # Rake tasks to execute
47
+ #
48
+ # @example
49
+ # rake "custom task1 task2"
50
+ #
51
+ # @api public
29
52
  def rake(command)
30
53
  Padrino.bin("rake", command, "-c=#{destination_root}")
31
54
  end
32
55
 
33
56
  # Executes App generator. Accepts an optional block allowing generation inside subapp.
34
- # app :name
35
- # app :name do
36
- # generate :model, "posts title:string"
37
- # end
57
+ #
58
+ # @param [Symbol] name
59
+ # name of (sub)application to generate
60
+ # @param [Proc] block
61
+ # commands to execute in context of (sub)appliation directory
62
+ #
63
+ # @example
64
+ # app :name
65
+ # app :name do
66
+ # generate :model, "posts title:string" # generate a model inside of subapp
67
+ # end
68
+ #
69
+ # @api public
38
70
  def app(name, &block)
39
71
  say "=> Executing: padrino-gen app #{name} -r=#{destination_root}", :magenta
40
72
  Padrino.bin_gen(:app, name.to_s, "-r=#{destination_root}")
@@ -46,9 +78,18 @@ module Padrino
46
78
  end
47
79
 
48
80
  # Executes git commmands in project using Grit
49
- # git :init
50
- # git :add, "."
51
- # git :commit, "hello world"
81
+ #
82
+ # @param [Symbol] action
83
+ # Git command to execute
84
+ # @param [String] arguments
85
+ # Arguments to invoke on git command
86
+ #
87
+ # @example
88
+ # git :init
89
+ # git :add, "."
90
+ # git :commit, "hello world"
91
+ #
92
+ # @api public
52
93
  def git(action, arguments=nil)
53
94
  FileUtils.cd(destination_root) do
54
95
  require 'grit' unless defined?(::Grit)
@@ -63,28 +104,38 @@ module Padrino
63
104
  end
64
105
 
65
106
  private
66
- # Resolves the path to the plugin template
67
- # given the project_name and the template_file
68
- # execute_runner(:plugin, 'path/to/local/file')
69
- # execute_runner(:plugin, 'hoptoad')
70
- # execute_runner(:template, 'sampleblog')
71
- # execute_runner(:template, 'http://gist.github.com/357045')
72
- def execute_runner(kind, template_file)
73
- # Determine resolved template path
74
- template_file = template_file.to_s
75
- template_path = case
76
- when template_file =~ %r{^http://} && template_file !~ /gist/
77
- template_file
78
- when template_file =~ /gist/ && template_file !~ /raw/
79
- raw_link, _ = *open(template_file).read.scan(/<a\s+href\s?\=\"(.*?)\"\>raw/)
80
- raw_link ? "https://gist.github.com#{raw_link[0]}" : template_file
81
- when File.extname(template_file).blank? # referencing official plugin (i.e hoptoad)
82
- "https://github.com/padrino/padrino-recipes/raw/master/#{kind.to_s.pluralize}/#{template_file}_#{kind}.rb"
83
- else # local file on system
84
- File.expand_path(template_file)
85
- end
86
- self.apply(template_path)
87
- end
107
+
108
+ # Resolves the path to the plugin template
109
+ # given the project_name and the template_file
110
+ #
111
+ # @param [Symbol] kind
112
+ # Context of template file to run, i.e :plugin, :template
113
+ # @param [String] template_file
114
+ # Path to template file
115
+ #
116
+ # @example
117
+ # execute_runner(:plugin, 'path/to/local/file')
118
+ # execute_runner(:plugin, 'hoptoad')
119
+ # execute_runner(:template, 'sampleblog')
120
+ # execute_runner(:template, 'https://gist.github.com/357045')
121
+ #
122
+ # @api private
123
+ def execute_runner(kind, template_file)
124
+ # Determine resolved template path
125
+ template_file = template_file.to_s
126
+ template_path = case
127
+ when template_file =~ %r{^https?://} && template_file !~ /gist/
128
+ template_file
129
+ when template_file =~ /gist/ && template_file !~ /raw/
130
+ raw_link, _ = *open(template_file).read.scan(/<a\s+href\s?\=\"(.*?)\"\>raw/)
131
+ raw_link ? "https://gist.github.com#{raw_link[0]}" : template_file
132
+ when File.extname(template_file).blank? # referencing official plugin (i.e hoptoad)
133
+ "https://github.com/padrino/padrino-recipes/raw/master/#{kind.to_s.pluralize}/#{template_file}_#{kind}.rb"
134
+ else # local file on system
135
+ File.expand_path(template_file)
136
+ end
137
+ self.apply(template_path)
138
+ end
88
139
  end # Runner
89
140
  end # Generators
90
141
  end # Padrino
@@ -1,33 +1,24 @@
1
1
  source :rubygems
2
2
 
3
- # Server requirements
4
- # gem 'thin' or mongrel
3
+ # Server requirements (defaults to WEBrick)
4
+ # gem 'thin'
5
+ # gem 'mongrel'
5
6
 
6
7
  # Project requirements
7
8
  gem 'rake'
8
- gem 'rack-flash'
9
+ gem 'sinatra-flash'
9
10
 
10
11
  # Component requirements
11
12
 
12
13
  # Test requirements
13
14
 
15
+ # Padrino Stable Gem
16
+ <% if options.dev? %># <% end %>gem 'padrino', '<%= Padrino.version %>'
14
17
 
15
- <%- if options.dev? -%>
16
- # Padrino
17
- %w(core gen helpers cache mailer admin).each do |gem|
18
- gem 'padrino-' + gem, :path => '<%= Padrino::Generators::DEV_PATH %>/padrino-' + gem
19
- end
20
- <%- else -%>
21
- # Padrino
22
- gem 'padrino', '<%= Padrino.version %>'
23
- <%- end -%>
24
- # Padrino EDGE
18
+ # Or Padrino Edge
25
19
  # gem 'padrino', :git => 'git://github.com/padrino/padrino-framework.git'
26
- #
27
- # Individual GEMS
28
- # gem 'padrino-core', '<%= Padrino.version %>'
29
- # gem 'padrino-admin', '<%= Padrino.version %>'
30
- # gem 'padrino-cache', '<%= Padrino.version %>'
31
- # gem 'padrino-gen', '<%= Padrino.version %>'
32
- # gem 'padrino-helpers', '<%= Padrino.version %>'
33
- # gem 'padrino-mailer', '<%= Padrino.version %>'
20
+
21
+ # Or Individual Gems
22
+ <% unless options.dev? %># <% end %>%w(core gen helpers cache mailer admin).each do |g|
23
+ <% unless options.dev? %># <% end %> gem 'padrino-' + g, <% if options.dev? %>:path => '<%= Padrino::Generators::DEV_PATH %>/padrino-' + g<% else %>'<%= Padrino.version %>'<% end %>
24
+ <% unless options.dev? %># <% end %>end
@@ -305,13 +305,6 @@ if defined?(ActiveRecord)
305
305
  end
306
306
  end
307
307
 
308
- namespace :auto do
309
- desc "Use schema.rb to auto-upgrade"
310
- task :upgrade => :environment do
311
- AutoMigrations.run
312
- end
313
- end
314
-
315
308
  if defined?(I18n)
316
309
  desc "Generates .yml files for I18n translations"
317
310
  task :translate => :environment do
@@ -380,191 +373,4 @@ if defined?(ActiveRecord)
380
373
  def firebird_db_string(config)
381
374
  FireRuby::Database.db_string_for(config.symbolize_keys)
382
375
  end
383
-
384
- ##
385
- # Padrino plugin for automating migrations
386
- #
387
- # Thanks to:: PJ Hyett
388
- # Original Repo:: http://github.com/pjhyett/auto_migrations
389
- #
390
- module AutoMigrations
391
-
392
- def self.run
393
- # Turn off schema_info code for auto-migration
394
- class << ActiveRecord::Schema
395
- alias :old_define :define
396
- attr_accessor :version
397
- def define(info={}, &block) @version = Time.now.utc.strftime("%Y%m%d%H%M%S"); instance_eval(&block) end
398
- end
399
-
400
- load(Padrino.root('db', 'schema.rb'))
401
- ActiveRecord::Migration.drop_unused_tables
402
- ActiveRecord::Migration.drop_unused_indexes
403
- ActiveRecord::Migration.update_schema_version(ActiveRecord::Schema.version) if ActiveRecord::Schema.version
404
-
405
- class << ActiveRecord::Schema
406
- alias :define :old_define
407
- end
408
- end
409
-
410
- def self.schema_to_migration(with_reset = false)
411
- schema_in = File.read(Padrino.root("db", "schema.rb"))
412
- schema_in.gsub!(/#(.)+\n/, '')
413
- schema_in.sub!(/ActiveRecord::Schema.define(.+)do[ ]?\n/, '')
414
- schema_in.gsub!(/^/, ' ')
415
- schema = "class InitialSchema < ActiveRecord::Migration\n def self.up\n"
416
- schema += " # We're resetting the migrations database...\n" +
417
- " drop_table :schema_migrations\n" +
418
- " initialize_schema_migrations_table\n\n" if with_reset
419
- schema += schema_in
420
- schema << "\n def self.down\n"
421
- schema << (ActiveRecord::Base.connection.tables - %w(schema_info schema_migrations)).map do |table|
422
- " drop_table :#{table}\n"
423
- end.join
424
- schema << " end\nend\n"
425
- migration_file = Padrino.root("db", "migrate", "001_initial_schema.rb")
426
- File.open(migration_file, "w") { |f| f << schema }
427
- puts "Migration created at db/migrate/001_initial_schema.rb"
428
- end
429
-
430
- def self.included(base)
431
- base.extend ClassMethods
432
- class << base
433
- cattr_accessor :tables_in_schema, :indexes_in_schema
434
- self.tables_in_schema, self.indexes_in_schema = [], []
435
- alias_method_chain :method_missing, :auto_migration
436
- end
437
- end
438
-
439
- module ClassMethods
440
-
441
- def method_missing_with_auto_migration(method, *args, &block)
442
- case method
443
- when :create_table
444
- auto_create_table(method, *args, &block)
445
- when :add_index
446
- auto_add_index(method, *args, &block)
447
- else
448
- method_missing_without_auto_migration(method, *args, &block)
449
- end
450
- end
451
-
452
- def auto_create_table(method, *args, &block)
453
- table_name = args.shift.to_s
454
- options = args.pop || {}
455
-
456
- (self.tables_in_schema ||= []) << table_name
457
-
458
- # Table doesn't exist, create it
459
- unless ActiveRecord::Base.connection.tables.include?(table_name)
460
- return method_missing_without_auto_migration(method, *[table_name, options], &block)
461
- end
462
-
463
- # Grab database columns
464
- fields_in_db = ActiveRecord::Base.connection.columns(table_name).inject({}) do |hash, column|
465
- hash[column.name] = column
466
- hash
467
- end
468
-
469
- # Grab schema columns (lifted from active_record/connection_adapters/abstract/schema_statements.rb)
470
- table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(ActiveRecord::Base.connection)
471
- primary_key = options[:primary_key] || "id"
472
- table_definition.primary_key(primary_key) unless options[:id] == false
473
- yield table_definition
474
- fields_in_schema = table_definition.columns.inject({}) do |hash, column|
475
- hash[column.name.to_s] = column
476
- hash
477
- end
478
-
479
- # Add fields to db new to schema
480
- (fields_in_schema.keys - fields_in_db.keys).each do |field|
481
- column = fields_in_schema[field]
482
- options = {:limit => column.limit, :precision => column.precision, :scale => column.scale}
483
- options[:default] = column.default if !column.default.nil?
484
- options[:null] = column.null if !column.null.nil?
485
- add_column table_name, column.name, column.type.to_sym, options
486
- end
487
-
488
- # Remove fields from db no longer in schema
489
- (fields_in_db.keys - fields_in_schema.keys & fields_in_db.keys).each do |field|
490
- column = fields_in_db[field]
491
- remove_column table_name, column.name
492
- end
493
-
494
- (fields_in_schema.keys & fields_in_db.keys).each do |field|
495
- if field != primary_key #ActiveRecord::Base.get_primary_key(table_name)
496
- changed = false # flag
497
- new_type = fields_in_schema[field].type.to_sym
498
- new_attr = {}
499
-
500
- # First, check if the field type changed
501
- if fields_in_schema[field].type.to_sym != fields_in_db[field].type.to_sym
502
- changed = true
503
- end
504
-
505
- # Special catch for precision/scale, since *both* must be specified together
506
- # Always include them in the attr struct, but they'll only get applied if changed = true
507
- new_attr[:precision] = fields_in_schema[field][:precision]
508
- new_attr[:scale] = fields_in_schema[field][:scale]
509
-
510
- # Next, iterate through our extended attributes, looking for any differences
511
- # This catches stuff like :null, :precision, etc
512
- fields_in_schema[field].each_pair do |att,value|
513
- next if att == :type or att == :base or att == :name # special cases
514
- if !value.nil? && value != fields_in_db[field].send(att)
515
- new_attr[att] = value
516
- changed = true
517
- end
518
- end
519
-
520
- # Change the column if applicable
521
- change_column table_name, field, new_type, new_attr if changed
522
- end
523
- end
524
- end
525
-
526
- def auto_add_index(method, *args, &block)
527
- table_name = args.shift.to_s
528
- fields = Array(args.shift).map(&:to_s)
529
- options = args.shift
530
-
531
- index_name = options[:name].to_s if options
532
- index_name ||= ActiveRecord::Base.connection.index_name(table_name, :column => fields)
533
-
534
- (self.indexes_in_schema ||= []) << index_name
535
-
536
- unless ActiveRecord::Base.connection.indexes(table_name).detect { |i| i.name == index_name }
537
- method_missing_without_auto_migration(method, *[table_name, fields, options], &block)
538
- end
539
- end
540
-
541
- def drop_unused_tables
542
- (ActiveRecord::Base.connection.tables - tables_in_schema - %w(schema_info schema_migrations)).each do |table|
543
- drop_table table
544
- end
545
- end
546
-
547
- def drop_unused_indexes
548
- tables_in_schema.each do |table_name|
549
- indexes_in_db = ActiveRecord::Base.connection.indexes(table_name).map(&:name)
550
- (indexes_in_db - indexes_in_schema & indexes_in_db).each do |index_name|
551
- remove_index table_name, :name => index_name
552
- end
553
- end
554
- end
555
-
556
- def update_schema_version(version)
557
- ActiveRecord::Base.connection.update("INSERT INTO schema_migrations VALUES ('#{version}')")
558
-
559
- schema_file = Padrino.root("db", "schema.rb")
560
- schema = File.read(schema_file)
561
- schema.sub!(/:version => \d+/, ":version => #{version}")
562
- File.open(schema_file, "w") { |f| f << schema }
563
- end
564
-
565
- end
566
-
567
- end
568
-
569
- ActiveRecord::Migration.send :include, AutoMigrations
570
376
  end
data/lib/padrino-gen.rb CHANGED
@@ -8,6 +8,13 @@ module Padrino
8
8
  # This method return the correct location of padrino-gen bin or
9
9
  # exec it using Kernel#system with the given args
10
10
  #
11
+ # @param [Array<String>] args
12
+ # Splat of arguments to pass to padrino-gen
13
+ #
14
+ # @example
15
+ # Padrino.bin_gen(:app, name.to_s, "-r=#{destination_root}")
16
+ #
17
+ # @api semipublic
11
18
  def self.bin_gen(*args)
12
19
  @_padrino_gen_bin ||= [Padrino.ruby_command, File.expand_path("../../bin/padrino-gen", __FILE__)]
13
20
  args.empty? ? @_padrino_gen_bin : system(args.unshift(@_padrino_gen_bin).join(" "))
@@ -35,6 +42,7 @@ module Padrino
35
42
  ##
36
43
  # Here we store our generators paths
37
44
  #
45
+ # @api semipublic
38
46
  def load_paths
39
47
  @_files ||= []
40
48
  end
@@ -42,6 +50,7 @@ module Padrino
42
50
  ##
43
51
  # Return a ordered list of task with their class
44
52
  #
53
+ # @api semipublic
45
54
  def mappings
46
55
  @_mappings ||= ActiveSupport::OrderedHash.new
47
56
  end
@@ -49,6 +58,17 @@ module Padrino
49
58
  ##
50
59
  # Gloabl add a new generator class to +padrino-gen+
51
60
  #
61
+ # @param [Symbol] name
62
+ # key name for generator mapping
63
+ # @param [Class] klass
64
+ # class of generator
65
+ #
66
+ # @return [Hash] generator mappings
67
+ #
68
+ # @example
69
+ # Padrino::Generators.add_generator(:controller, Controller)
70
+ #
71
+ # @api semipublic
52
72
  def add_generator(name, klass)
53
73
  mappings[name] = klass
54
74
  end
@@ -56,6 +76,7 @@ module Padrino
56
76
  ##
57
77
  # Load Global Actions and Component Actions then all files in +load_path+.
58
78
  #
79
+ # @api private
59
80
  def load_components!
60
81
  require 'padrino-gen/generators/actions'
61
82
  require 'padrino-gen/generators/components/actions'