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
@@ -23,17 +23,17 @@ class <%= @app_name %> < Padrino::Application
23
23
  ##
24
24
  # Application configuration options
25
25
  #
26
- # set :raise_errors, true # Raise exceptions (will stop application) (default for test)
27
- # set :dump_errors, true # Exception backtraces are written to STDERR (default for production/development)
28
- # set :show_exceptions, true # Shows a stack trace in browser (default for development)
29
- # set :logging, true # Logging in STDOUT for development and file for production (default only for development)
30
- # set :public, "foo/bar" # Location for static assets (default root/public)
31
- # set :reload, false # Reload application files (default in development)
32
- # set :default_builder, "foo" # Set a custom form builder (default 'StandardFormBuilder')
33
- # set :locale_path, "bar" # Set path for I18n translations (default your_app/locales)
34
- # disable :sessions # Disabled sessions by default (enable if needed)
35
- # disable :flash # Disables rack-flash (enabled by default if Rack::Flash is defined)
36
- # layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
26
+ # set :raise_errors, true # Raise exceptions (will stop application) (default for test)
27
+ # set :dump_errors, true # Exception backtraces are written to STDERR (default for production/development)
28
+ # set :show_exceptions, true # Shows a stack trace in browser (default for development)
29
+ # set :logging, true # Logging in STDOUT for development and file for production (default only for development)
30
+ # set :public_folder, "foo/bar" # Location for static assets (default root/public)
31
+ # set :reload, false # Reload application files (default in development)
32
+ # set :default_builder, "foo" # Set a custom form builder (default 'StandardFormBuilder')
33
+ # set :locale_path, "bar" # Set path for I18n translations (default your_app/locales)
34
+ # disable :sessions # Disabled sessions by default (enable if needed)
35
+ # disable :flash # Disables sinatra-flash (enabled by default if Sinatra::Flash is defined)
36
+ # layout :my_layout # Layout can be in views/layouts/foo.ext or views/foo.ext (default :application)
37
37
  #
38
38
 
39
39
  ##
@@ -26,6 +26,8 @@ module Padrino
26
26
  require_arguments!
27
27
 
28
28
  # Copies over the Padrino base admin application
29
+ #
30
+ # @api private
29
31
  def create_app
30
32
  self.destination_root = options[:root]
31
33
  @app_name = name.gsub(/\W/, "_").underscore.camelize
@@ -36,15 +38,14 @@ module Padrino
36
38
  append_file destination_root("config/apps.rb"), "\nPadrino.mount(\"#{@app_name}\").to(\"/#{@app_name.downcase}\")"
37
39
 
38
40
  return if self.behavior == :revoke
39
- say (<<-TEXT).gsub(/ {10}/,'')
40
-
41
- =================================================================
42
- Your #{@app_name} application has been installed.
43
- This application has been mounted to /#{@app_name.downcase}
44
- You can configure a different path by editing 'config/apps.rb'
45
- =================================================================
46
-
47
- TEXT
41
+ say
42
+ say "="*65, :green
43
+ say "Your #{@app_name} application has been installed."
44
+ say "="*65, :green
45
+ say "This application has been mounted to /#{@app_name.downcase}"
46
+ say "You can configure a different path by editing 'config/apps.rb"
47
+ say "="*65, :green
48
+ say
48
49
  else
49
50
  say "You are not at the root of a Padrino application! (config/boot.rb not found)"
50
51
  end
@@ -15,6 +15,8 @@ module Padrino
15
15
 
16
16
  # We need to TRY to load boot because some of our app dependencies maybe have
17
17
  # custom generators, so is necessary know who are.
18
+ #
19
+ # @api private
18
20
  def load_boot
19
21
  begin
20
22
  ENV['PADRINO_LOG_LEVEL'] ||= "test"
@@ -35,6 +37,7 @@ module Padrino
35
37
  end
36
38
  end
37
39
 
40
+ # @api private
38
41
  def setup
39
42
  Padrino::Generators.load_components!
40
43
 
@@ -4,15 +4,30 @@ module Padrino
4
4
  module Actions
5
5
  # For orm database components
6
6
  # Generates the model migration file created when generating a new model
7
- # options => { :base => "....text...", :up => "..text...",
8
- # :down => "..text...", column_format => "t.column :#{field}, :#{kind}" }
7
+ #
8
+ # @param [String] filename
9
+ # File name of model migration
10
+ # @param [String] name
11
+ # Name of model
12
+ # @param [Array<String>] columns
13
+ # Array of column names and property type
14
+ # @param [Hash] options
15
+ # Additional migration options, e.g { :base => "....text...", :up => "..text...",
16
+ # :down => "..text...", column_format => "t.column :#{field}, :#{kind}" }
17
+ # @example
18
+ # output_model_migration("AddPerson", "person", ["name:string", "age:integer"],
19
+ # :base => AR_MIGRATION,
20
+ # :column_format => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
21
+ # :up => AR_MODEL_UP_MG, :down => AR_MODEL_DOWN_MG)
22
+ #
23
+ # @api private
9
24
  def output_model_migration(filename, name, columns, options={})
10
25
  if behavior == :revoke
11
26
  remove_migration(filename)
12
27
  else
13
28
  return if migration_exist?(filename)
14
29
  model_name = name.to_s.pluralize
15
- field_tuples = fields.map { |value| value.split(":") }
30
+ field_tuples = columns.map { |value| value.split(":") }
16
31
  field_tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } # fix datetime
17
32
  column_declarations = field_tuples.map(&options[:column_format]).join("\n ")
18
33
  contents = options[:base].dup.gsub(/\s{4}!UP!\n/m, options[:up]).gsub(/!DOWN!\n/m, options[:down])
@@ -27,9 +42,25 @@ module Padrino
27
42
 
28
43
  # For orm database components
29
44
  # Generates a standalone migration file based on the given options and columns
30
- # options => { :base "...text...", :change_format => "...text...",
31
- # :add => proc { |field, kind| "add_column :#{table_name}, :#{field}, :#{kind}" },
32
- # :remove => proc { |field, kind| "remove_column :#{table_name}, :#{field}" }
45
+ #
46
+ # @param [String] filename
47
+ # File name of model migration
48
+ # @param [String] name
49
+ # Name of model
50
+ # @param [Array<String>] columns
51
+ # Array of column names and property type
52
+ # @param [Hash] options
53
+ # Additional migration options, e.g { :base "...text...", :change_format => "...text...",
54
+ # :add => proc { |field, kind| "add_column :#{table_name}, :#{field}, :#{kind}" },
55
+ # :remove => proc { |field, kind| "remove_column :#{table_name}, :#{field}" }
56
+ # @example
57
+ # output_migration_file(migration_name, name, columns,
58
+ # :base => AR_MIGRATION, :change_format => AR_CHANGE_MG,
59
+ # :add => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
60
+ # :remove => Proc.new { |field, kind| "t.remove :#{field}" }
61
+ # )
62
+ #
63
+ # @api private
33
64
  def output_migration_file(filename, name, columns, options={})
34
65
  if behavior == :revoke
35
66
  remove_migration(name)
@@ -56,6 +87,8 @@ module Padrino
56
87
 
57
88
  # For migration files
58
89
  # returns the number of the latest(most current) migration file
90
+ #
91
+ # @api private
59
92
  def return_last_migration_number
60
93
  Dir[destination_root('db/migrate/*.rb')].map { |f|
61
94
  File.basename(f).match(/^(\d+)/)[0].to_i
@@ -63,12 +96,24 @@ module Padrino
63
96
  end
64
97
 
65
98
  # Return true if the migration already exist
99
+ #
100
+ # @param [String] filename
101
+ # File name of the migration file
102
+ #
103
+ # @param [Boolean] Boolean if file exists
104
+ #
105
+ # @api private
66
106
  def migration_exist?(filename)
67
107
  Dir[destination_root("db/migrate/*_#{filename.underscore}.rb")].size > 0
68
108
  end
69
109
 
70
110
  # For the removal of migration files
71
111
  # removes the migration file based on the migration name
112
+ #
113
+ # @param [String] name
114
+ # File name of the migration
115
+ #
116
+ # @api private
72
117
  def remove_migration(name)
73
118
  migration_path = Dir[destination_root('db/migrate/*.rb')].find do |f|
74
119
  File.basename(f) =~ /#{name.to_s.underscore}/
@@ -81,8 +126,17 @@ module Padrino
81
126
 
82
127
  # For testing components
83
128
  # Injects the test class text into the test_config file for setting up the test gen
84
- # insert_test_suite_setup('...CLASS_NAME...')
85
- # => inject_into_file("test/test_config.rb", TEST.gsub(/CLASS_NAME/, @app_name), :after => "set :environment, :test")
129
+ #
130
+ # @param [String] suite_text
131
+ # Class name for test suite
132
+ # @param [Hash] options
133
+ # Additional options to pass into injection
134
+ #
135
+ # @example
136
+ # insert_test_suite_setup('...CLASS_NAME...')
137
+ # => inject_into_file("test/test_config.rb", TEST.gsub(/CLASS_NAME/, @app_name), :after => "set :environment, :test")
138
+ #
139
+ # @api private
86
140
  def insert_test_suite_setup(suite_text, options={})
87
141
  options.reverse_merge!(:path => "test/test_config.rb")
88
142
  create_file(options[:path], suite_text.gsub(/CLASS_NAME/, @app_name))
@@ -90,8 +144,17 @@ module Padrino
90
144
 
91
145
  # For mocking components
92
146
  # Injects the mock library include into the test class in test_config for setting up mock gen
93
- # insert_mock_library_include('Mocha::API')
94
- # => inject_into_file("test/test_config.rb", " include Mocha::API\n", :after => /class.*?\n/)
147
+ #
148
+ # @param [String] library_name
149
+ # name of mocking library
150
+ # @param [Hash] options
151
+ # Additional options to pass into injection
152
+ #
153
+ # @example
154
+ # insert_mock_library_include('Mocha::API')
155
+ # => inject_into_file("test/test_config.rb", " include Mocha::API\n", :after => /class.*?\n/)
156
+ #
157
+ # @api private
95
158
  def insert_mocking_include(library_name, options={})
96
159
  options.reverse_merge!(:indent => 2, :after => /class.*?\n/, :path => "test/test_config.rb")
97
160
  return unless File.exist?(destination_root(options[:path]))
@@ -100,13 +163,25 @@ module Padrino
100
163
  end
101
164
 
102
165
  # Returns space characters of given count
103
- # indent_spaces(2)
166
+ #
167
+ # @example
168
+ # indent_spaces(2)
169
+ #
170
+ # @api private
104
171
  def indent_spaces(count)
105
172
  ' ' * count
106
173
  end
107
174
 
108
175
  # For Controller action generation
109
176
  # Takes in fields for routes in the form of get:index post:test delete:yada and such
177
+ #
178
+ # @param [Array<String>] fields
179
+ # Array of controller actions and route name
180
+ #
181
+ # @example
182
+ # controller_actions "get:index", "post:test"o
183
+ #
184
+ # @api private
110
185
  def controller_actions(fields)
111
186
  field_tuples = fields.map { |value| value.split(":") }
112
187
  action_declarations = field_tuples.map do |request, name|
@@ -5,7 +5,9 @@ def setup_mock
5
5
  inject_into_file 'spec/spec_helper.rb', " conf.mock_with :rr\n", :after => "RSpec.configure do |conf|\n"
6
6
  when 'riot'
7
7
  inject_into_file "test/test_config.rb","require 'riot/rr'\n", :after => "\"/../config/boot\")\n"
8
- else
8
+ when 'minitest'
9
+ insert_mocking_include "RR::Adapters::MiniTest", :path => "test/test_config.rb"
10
+ else # default include
9
11
  insert_mocking_include "RR::Adapters::RRMethods", :path => "test/test_config.rb"
10
12
  end
11
13
  end
@@ -29,7 +29,7 @@ MONGO
29
29
  def setup_orm
30
30
  require_dependencies 'bson_ext', :require => 'mongo'
31
31
  require_dependencies 'mongoid'
32
- require_dependencies('SystemTimer', :require => 'system_timer') if RUBY_VERSION =~ /1\.8/
32
+ require_dependencies('SystemTimer', :require => 'system_timer') if RUBY_VERSION =~ /1\.8/ && (!defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby')
33
33
  create_file("config/database.rb", MONGOID.gsub(/!NAME!/, @app_name.underscore))
34
34
  end
35
35
 
@@ -11,7 +11,7 @@ MONGO
11
11
  def setup_orm
12
12
  require_dependencies 'bson_ext', :require => 'mongo'
13
13
  require_dependencies 'mongo_mapper'
14
- require_dependencies('SystemTimer', :require => 'system_timer') if RUBY_VERSION =~ /1\.8/
14
+ require_dependencies('SystemTimer', :require => 'system_timer') if RUBY_VERSION =~ /1\.8/ && (!defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby')
15
15
  create_file("config/database.rb", MONGO.gsub(/!NAME!/, @app_name.underscore))
16
16
  end
17
17
 
@@ -35,7 +35,7 @@ class !NAME! < Mongomatic::Base
35
35
  # String datatypes
36
36
 
37
37
 
38
- # Examples
38
+ # Examples:
39
39
  # def validate
40
40
  # expectations do
41
41
  # be_present self['name'], "Name cannot be blank"
@@ -16,7 +16,7 @@ app.use Rack::Less,
16
16
  LESS
17
17
 
18
18
  def setup_stylesheet
19
- require_dependencies 'less', 'rack-less'
19
+ require_dependencies 'rack-less'
20
20
  initializer :less, LESS_INIT
21
21
  empty_directory destination_root('/app/stylesheets')
22
22
  end
@@ -0,0 +1,78 @@
1
+ MINITEST_SETUP = (<<-TEST).gsub(/^ {10}/, '') unless defined?(MINITEST_SETUP)
2
+ PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
3
+ require File.expand_path('../../config/boot, __FILE__)
4
+
5
+ class MiniTest::Unit::TestCase
6
+ include Rack::Test::Methods
7
+
8
+ def app
9
+ ##
10
+ # You can handle all padrino applications using instead:
11
+ # Padrino.application
12
+ CLASS_NAME.tap { |app| }
13
+ end
14
+ end
15
+ TEST
16
+
17
+ MINITEST_RAKE = (<<-TEST).gsub(/^ {10}/, '') unless defined?(MINITEST_RAKE)
18
+ require 'rake/testtask'
19
+
20
+ test_tasks = Dir['test/*/'].map { |d| File.basename(d) }
21
+
22
+ test_tasks.each do |folder|
23
+ Rake::TestTask.new("test:\#{folder}") do |test|
24
+ test.pattern = "test/\#{folder}/**/*_test.rb"
25
+ test.verbose = true
26
+ end
27
+ end
28
+
29
+ desc "Run application test suite"
30
+ task 'test' => test_tasks.map { |f| "test:\#{f}" }
31
+ TEST
32
+
33
+ MINITEST_CONTROLLER_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(MINITEST_CONTROLLER_TEST)
34
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_config.rb')
35
+
36
+ describe "!NAME!Controller" do
37
+ before do
38
+ get '/'
39
+ end
40
+
41
+ it "should return hello world text" do
42
+ assert_equal "Hello World", last_response.body
43
+ end
44
+ end
45
+ TEST
46
+
47
+ MINITEST_MODEL_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(MINITEST_MODEL_TEST)
48
+ require File.expand_path(File.dirname(__FILE__) + '!PATH!/test_config.rb')
49
+
50
+ describe "!NAME! Model" do
51
+ it 'can construct a new instance' do
52
+ @!DNAME! = !NAME!.new
53
+ refute_nil @!DNAME!
54
+ end
55
+ end
56
+ TEST
57
+
58
+ def setup_test
59
+ require_dependencies 'rack-test', :require => 'rack/test', :group => 'test'
60
+ require_dependencies 'minitest', :version => "~>2.6.0", :require => 'minitest/autorun', :group => 'test'
61
+ insert_test_suite_setup MINITEST_SETUP
62
+ create_file destination_root("test/test.rake"), MINITEST_RAKE
63
+ end
64
+
65
+ # Generates a controller test given the controllers name
66
+ def generate_controller_test(name)
67
+ minitest_contents = MINITEST_CONTROLLER_TEST.gsub(/!NAME!/, name.to_s.camelize)
68
+ controller_test_path = File.join('test',options[:app],'controllers',"#{name.to_s.underscore}_controller_test.rb")
69
+ create_file destination_root(controller_test_path), minitest_contents, :skip => true
70
+ end
71
+
72
+ def generate_model_test(name)
73
+ minitest_contents = MINITEST_MODEL_TEST.gsub(/!NAME!/, name.to_s.camelize).gsub(/!DNAME!/, name.to_s.underscore)
74
+ path = options[:app] == '.' ? '/..' : '/../..'
75
+ minitest_contents.gsub!(/!PATH!/,path)
76
+ model_test_path = File.join('test',options[:app],'models',"#{name.to_s.underscore}_test.rb")
77
+ create_file destination_root(model_test_path), minitest_contents, :skip => true
78
+ end
@@ -26,6 +26,9 @@ module Padrino
26
26
  # Show help if no argv given
27
27
  require_arguments!
28
28
 
29
+ # Execute controller generation
30
+ #
31
+ # @api private
29
32
  def create_controller
30
33
  self.destination_root = options[:root]
31
34
  if in_app_root?
@@ -26,6 +26,9 @@ module Padrino
26
26
  # Show help if no argv given
27
27
  require_arguments!
28
28
 
29
+ # Execute mailer generation
30
+ #
31
+ # @api private
29
32
  def create_mailer
30
33
  self.destination_root = options[:root]
31
34
  if in_app_root?
@@ -27,6 +27,9 @@ module Padrino
27
27
  # Show help if no argv given
28
28
  require_arguments!
29
29
 
30
+ # Execute the model generation
31
+ #
32
+ # @api private
30
33
  def create_model
31
34
  self.destination_root = options[:root]
32
35
  if in_app_root?
@@ -28,6 +28,8 @@ module Padrino
28
28
  require_arguments!
29
29
 
30
30
  # Create the Padrino Plugin
31
+ #
32
+ # @api private
31
33
  def setup_plugin
32
34
  if options[:list] # list method ran here
33
35
  plugins = {}
@@ -32,7 +32,7 @@ module Padrino
32
32
 
33
33
  # Definitions for the available customizable components
34
34
  component_option :orm, "database engine", :aliases => '-d', :choices => [:activerecord, :datamapper, :mongomapper, :mongoid, :sequel, :couchrest, :ohm, :mongomatic, :ripple], :default => :none
35
- component_option :test, "testing framework", :aliases => '-t', :choices => [:rspec, :shoulda, :cucumber, :bacon, :testspec, :riot], :default => :none
35
+ component_option :test, "testing framework", :aliases => '-t', :choices => [:rspec, :shoulda, :cucumber, :bacon, :testspec, :riot, :minitest], :default => :none
36
36
  component_option :mock, "mocking library", :aliases => '-m', :choices => [:mocha, :rr], :default => :none
37
37
  component_option :script, "javascript library", :aliases => '-s', :choices => [:jquery, :prototype, :rightjs, :mootools, :extcore, :dojo], :default => :none
38
38
  component_option :renderer, "template engine", :aliases => '-e', :choices => [:haml, :erb, :liquid, :slim], :default => :haml
@@ -42,6 +42,8 @@ module Padrino
42
42
  require_arguments!
43
43
 
44
44
  # Copies over the Padrino base application App
45
+ #
46
+ # @api private
45
47
  def setup_project
46
48
  valid_constant?(options[:app] || name)
47
49
  @app_name = (options[:app] || name).gsub(/\W/, "_").underscore.camelize
@@ -61,6 +63,8 @@ module Padrino
61
63
  end
62
64
 
63
65
  # For each component, retrieve a valid choice and then execute the associated generator
66
+ #
67
+ # @api private
64
68
  def setup_components
65
69
  return if options[:template]
66
70
  @_components = options.dup.slice(*self.class.component_types)
@@ -72,6 +76,8 @@ module Padrino
72
76
  end
73
77
 
74
78
  # Bundle all required components using bundler and Gemfile
79
+ #
80
+ # @api private
75
81
  def bundle_dependencies
76
82
  if options[:bundle]
77
83
  run_bundler
@@ -79,13 +85,15 @@ module Padrino
79
85
  end
80
86
 
81
87
  # Finish message
88
+ #
89
+ # @api private
82
90
  def finish_message
83
91
  say
84
92
  say "="*65, :green
85
93
  say "#{name} is ready for development!", :green
86
94
  say "="*65, :green
87
- say "$ cd #{options[:root]}/#{name}", :bold
88
- say "$ bundle install", :bold unless options[:bundle]
95
+ say "$ cd #{options[:root]}/#{name}"
96
+ say "$ bundle install" unless options[:bundle]
89
97
  say "="*65, :green
90
98
  say
91
99
  end