padrino-gen 0.11.3 → 0.11.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/README.rdoc +1 -1
  3. data/lib/padrino-gen.rb +14 -22
  4. data/lib/padrino-gen/command.rb +4 -5
  5. data/lib/padrino-gen/generators/actions.rb +63 -48
  6. data/lib/padrino-gen/generators/app.rb +15 -17
  7. data/lib/padrino-gen/generators/app/app.rb.tt +2 -2
  8. data/lib/padrino-gen/generators/cli.rb +9 -9
  9. data/lib/padrino-gen/generators/component.rb +62 -0
  10. data/lib/padrino-gen/generators/components/actions.rb +48 -57
  11. data/lib/padrino-gen/generators/components/mocks/mocha.rb +2 -1
  12. data/lib/padrino-gen/generators/components/mocks/rr.rb +1 -1
  13. data/lib/padrino-gen/generators/components/orms/minirecord.rb +2 -2
  14. data/lib/padrino-gen/generators/components/orms/mongoid.rb +7 -7
  15. data/lib/padrino-gen/generators/components/orms/ohm.rb +3 -3
  16. data/lib/padrino-gen/generators/components/stylesheets/compass.rb +1 -1
  17. data/lib/padrino-gen/generators/components/stylesheets/compass/partials/_base.scss +5 -3
  18. data/lib/padrino-gen/generators/components/stylesheets/less.rb +1 -1
  19. data/lib/padrino-gen/generators/components/stylesheets/sass.rb +1 -1
  20. data/lib/padrino-gen/generators/components/stylesheets/scss.rb +1 -1
  21. data/lib/padrino-gen/generators/controller.rb +6 -10
  22. data/lib/padrino-gen/generators/mailer.rb +7 -11
  23. data/lib/padrino-gen/generators/migration.rb +4 -9
  24. data/lib/padrino-gen/generators/model.rb +6 -12
  25. data/lib/padrino-gen/generators/plugin.rb +9 -12
  26. data/lib/padrino-gen/generators/project.rb +18 -28
  27. data/lib/padrino-gen/generators/runner.rb +25 -25
  28. data/lib/padrino-gen/padrino-tasks/activerecord.rb +11 -12
  29. data/lib/padrino-gen/padrino-tasks/datamapper.rb +3 -3
  30. data/lib/padrino-gen/padrino-tasks/mongoid.rb +9 -9
  31. data/lib/padrino-gen/padrino-tasks/mongomapper.rb +4 -5
  32. data/lib/padrino-gen/padrino-tasks/sequel.rb +1 -2
  33. data/test/helper.rb +1 -1
  34. data/test/test_component_generator.rb +85 -0
  35. data/test/test_generator.rb +1 -1
  36. data/test/test_plugin_generator.rb +1 -1
  37. data/test/test_project_generator.rb +11 -4
  38. metadata +12 -15
@@ -2,19 +2,16 @@ module Padrino
2
2
  module Generators
3
3
 
4
4
  ##
5
- # Responsible for applications within a Padrino project. Creates and mounts the application and gives the user related information.
5
+ # Responsible for applications within a Padrino project. Creates and mounts
6
+ # the application and gives the user related information.
6
7
  #
7
8
  class App < Thor::Group
8
9
 
9
- # Add this generator to our padrino-gen
10
10
  Padrino::Generators.add_generator(:app, self)
11
11
 
12
- # Define the source template root
13
12
  def self.source_root; File.expand_path(File.dirname(__FILE__)); end
14
- # Defines the banner for this CLI generator
15
- def self.banner; "padrino-gen project [name]"; end
13
+ def self.banner; "padrino-gen app [name]"; end
16
14
 
17
- # Include related modules
18
15
  include Thor::Actions
19
16
  include Padrino::Generators::Actions
20
17
 
@@ -25,35 +22,36 @@ module Padrino
25
22
  class_option :tiny, :desc => 'Generate tiny app skeleton', :aliases => '-i', :default => false, :type => :boolean
26
23
  class_option :namespace, :desc => 'The name space of your padrino project', :aliases => '-n', :default => '', :type => :string
27
24
 
28
- # Show help if no argv given
25
+ # Show help if no ARGV given
29
26
  require_arguments!
30
27
 
31
- # Copies over the Padrino base admin application
28
+ ##
29
+ # Copies over the Padrino base admin application.
32
30
  #
33
- # @api private
34
31
  def create_app
35
32
  self.destination_root = options[:root]
36
33
  @app_folder = name.gsub(/\W/, '_').underscore
37
- @app_name = name.gsub(/\W/, '_').underscore.camelize
34
+ @app_name = @app_folder.camelize
38
35
  if in_app_root?
39
36
  @project_name = options[:namespace].underscore.camelize
40
37
  @project_name = fetch_project_name(@app_folder) if @project_name.empty?
38
+ lowercase_app_folder = @app_folder.downcase
41
39
  self.behavior = :revoke if options[:destroy]
42
- app_skeleton(@app_folder.downcase, options[:tiny])
43
- empty_directory destination_root("public/#{@app_folder.downcase}")
44
- append_file destination_root('config/apps.rb'), "\nPadrino.mount('#{@project_name}::#{@app_name}', :app_file => Padrino.root('#{@app_folder.downcase}/app.rb')).to('/#{@app_folder.downcase}')"
40
+ app_skeleton(lowercase_app_folder, options[:tiny])
41
+ empty_directory destination_root("public/#{lowercase_app_folder}")
42
+ append_file destination_root('config/apps.rb'), "\nPadrino.mount('#{@project_name}::#{@app_name}', :app_file => Padrino.root('#{lowercase_app_folder}/app.rb')).to('/#{lowercase_app_folder}')"
45
43
 
46
44
  return if self.behavior == :revoke
47
45
  say
48
46
  say '=' * 65, :green
49
47
  say "Your #{@app_name} application has been installed."
50
- say '='*65, :green
48
+ say '=' * 65, :green
51
49
  say "This application has been mounted to /#{@app_name.downcase}"
52
50
  say "You can configure a different path by editing 'config/apps.rb'"
53
51
  else
54
52
  say 'You are not at the root of a Padrino application! (config/boot.rb not found)'
55
53
  end
56
54
  end
57
- end # App
58
- end # Generators
59
- end # Padrino
55
+ end
56
+ end
57
+ end
@@ -7,7 +7,7 @@ module <%= @project_name %>
7
7
  enable :sessions
8
8
 
9
9
  ##
10
- # Caching support
10
+ # Caching support.
11
11
  #
12
12
  # register Padrino::Cache
13
13
  # enable :caching
@@ -22,7 +22,7 @@ module <%= @project_name %>
22
22
  #
23
23
 
24
24
  ##
25
- # Application configuration options
25
+ # Application configuration options.
26
26
  #
27
27
  # set :raise_errors, true # Raise exceptions (will stop application) (default for test)
28
28
  # set :dump_errors, true # Exception backtraces are written to STDERR (default for production/development)
@@ -3,21 +3,20 @@ require 'thor/group'
3
3
  module Padrino
4
4
  module Generators
5
5
  ##
6
- # This class bootstrap +config/boot+ and perform +Padrino::Generators.load_components!+ for handle
7
- # 3rd party generators
6
+ # This class bootstrap +config/boot+ and perform
7
+ # +Padrino::Generators.load_components!+ for handle 3rd party generators.
8
8
  #
9
9
  class Cli < Thor::Group
10
10
 
11
- # Include related modules
12
11
  include Thor::Actions
13
12
 
14
13
  class_option :root, :desc => "The root destination", :aliases => '-r', :default => ".", :type => :string
15
14
  class_option :help, :type => :boolean, :desc => "Show help usage"
16
15
 
17
- # We need to TRY to load boot because some of our app dependencies maybe have
16
+ ##
17
+ # We need to try to load boot because some of our app dependencies maybe have
18
18
  # custom generators, so is necessary know who are.
19
19
  #
20
- # @api private
21
20
  def load_boot
22
21
  begin
23
22
  ENV['PADRINO_LOG_LEVEL'] ||= 'test'
@@ -38,8 +37,9 @@ module Padrino
38
37
  end
39
38
  end
40
39
 
40
+ ##
41
41
  # Loads the components available for all generators.
42
- # @private
42
+ #
43
43
  def setup
44
44
  Padrino::Generators.load_components!
45
45
 
@@ -53,6 +53,6 @@ module Padrino
53
53
  puts "Please specify generator to use (#{Padrino::Generators.mappings.keys.join(", ")})"
54
54
  end
55
55
  end
56
- end # Cli
57
- end # Generators
58
- end # Padrino
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,62 @@
1
+ module Padrino
2
+ module Generators
3
+
4
+ ##
5
+ # Responsible for add components within a Padrino project.
6
+ #
7
+ class Component < Thor::Group
8
+
9
+ Padrino::Generators.add_generator(:component, self)
10
+
11
+ def self.source_root; File.expand_path(File.dirname(__FILE__)); end
12
+ def self.banner; "padrino-gen component [options]"; end
13
+
14
+ include Thor::Actions
15
+ include Padrino::Generators::Actions
16
+ include Padrino::Generators::Components::Actions
17
+
18
+ desc "Description:\n\n\tpadrino-gen component add components into a Padrino project"
19
+
20
+ class_option :root, :desc => 'The root destination', :aliases => '-r', :default => '.', :type => :string
21
+ class_option :adapter, :desc => 'SQL adapter for ORM (sqlite, mysql, mysql2, mysql-gem, postgres)', :aliases => '-a', :default => 'sqlite', :type => :string
22
+
23
+ defines_component_options :default => false
24
+
25
+ ##
26
+ # For each component, retrieve a valid choice and then execute the associated generator.
27
+ #
28
+ def setup_components
29
+ self.destination_root = options[:root]
30
+ if in_app_root?
31
+ @_components = options.dup.slice(*self.class.component_types)
32
+ if @_components.values.delete_if(&:blank?).empty?
33
+ self.class.start(["-h"])
34
+ say
35
+ say "Current Selected Components:"
36
+ list = []
37
+ self.class.component_types.each do |comp|
38
+ list << [comp, fetch_component_choice(comp)]
39
+ end
40
+ print_table(list, :indent => 2)
41
+ exit
42
+ end
43
+
44
+ self.class.component_types.each do |comp|
45
+ next if @_components[comp].blank?
46
+
47
+ choice = @_components[comp] = resolve_valid_choice(comp)
48
+ existing = fetch_component_choice(comp)
49
+ if existing != 'none' && existing != choice
50
+ next unless yes?("Switch #{comp} to '#{choice}' from '#{existing}' ?[yes/no]:")
51
+ end
52
+ @project_name = fetch_component_choice(:namespace)
53
+ execute_component_setup(comp, choice)
54
+ store_component_choice(comp, choice)
55
+ end
56
+ else
57
+ say 'You are not at the root of a Padrino application! (config/boot.rb not found)'
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -2,25 +2,25 @@ module Padrino
2
2
  module Generators
3
3
  module Components
4
4
  module Actions
5
- # For orm database components
6
- # Generates the model migration file created when generating a new model
5
+ ##
6
+ # Generates the model migration file created when generating a new model.
7
7
  #
8
8
  # @param [String] filename
9
- # File name of model migration
9
+ # File name of model migration.
10
10
  # @param [String] name
11
- # Name of model
11
+ # Name of model.
12
12
  # @param [Array<String>] columns
13
- # Array of column names and property type
13
+ # Array of column names and property type.
14
14
  # @param [Hash] options
15
- # Additional migration options, e.g { :base => "....text...", :up => "..text...",
16
- # :down => "..text...", column_format => "t.column :#{field}, :#{kind}" }
15
+ # Additional migration options, e.g
16
+ # { :base => "....text...", :up => "..text...",
17
+ # :down => "..text...", column_format => "t.column :#{field}, :#{kind}" }
17
18
  # @example
18
19
  # output_model_migration("AddPerson", "person", ["name:string", "age:integer"],
19
20
  # :base => AR_MIGRATION,
20
21
  # :column_format => Proc.new { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
21
22
  # :up => AR_MODEL_UP_MG, :down => AR_MODEL_DOWN_MG)
22
23
  #
23
- # @api private
24
24
  def output_model_migration(filename, name, columns, options={})
25
25
  if behavior == :revoke
26
26
  remove_migration(filename)
@@ -28,7 +28,7 @@ module Padrino
28
28
  return if migration_exist?(filename)
29
29
  model_name = name.to_s.pluralize
30
30
  field_tuples = columns.map { |value| value.split(":") }
31
- field_tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } # fix datetime
31
+ field_tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] }
32
32
  column_declarations = field_tuples.map(&options[:column_format]).join("\n ")
33
33
  contents = options[:base].dup.gsub(/\s{4}!UP!\n/m, options[:up]).gsub(/!DOWN!\n/m, options[:down])
34
34
  contents = contents.gsub(/!NAME!/, model_name.underscore.camelize).gsub(/!TABLE!/, model_name.underscore)
@@ -40,19 +40,20 @@ module Padrino
40
40
  end
41
41
  end
42
42
 
43
- # For orm database components
44
- # Generates a standalone migration file based on the given options and columns
43
+ ##
44
+ # Generates a standalone migration file based on the given options and columns.
45
45
  #
46
46
  # @param [String] filename
47
- # File name of model migration
47
+ # File name of model migration.
48
48
  # @param [String] name
49
- # Name of model
49
+ # Name of model.
50
50
  # @param [Array<String>] columns
51
- # Array of column names and property type
51
+ # Array of column names and property type.
52
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}" }
53
+ # Additional migration options, e.g
54
+ # { :base "...text...", :change_format => "...text...",
55
+ # :add => proc { |field, kind| "add_column :#{table_name}, :#{field}, :#{kind}" },
56
+ # :remove => proc { |field, kind| "remove_column :#{table_name}, :#{field}" }
56
57
  # @example
57
58
  # output_migration_file(migration_name, name, columns,
58
59
  # :base => AR_MIGRATION, :change_format => AR_CHANGE_MG,
@@ -60,7 +61,6 @@ module Padrino
60
61
  # :remove => Proc.new { |field, kind| "t.remove :#{field}" }
61
62
  # )
62
63
  #
63
- # @api private
64
64
  def output_migration_file(filename, name, columns, options={})
65
65
  if behavior == :revoke
66
66
  remove_migration(name)
@@ -70,7 +70,7 @@ module Padrino
70
70
  migration_scan = filename.underscore.camelize.scan(/(Add|Remove)(?:.*?)(?:To|From)(.*?)$/).flatten
71
71
  direction, table_name = migration_scan[0].downcase, migration_scan[1].downcase.pluralize if migration_scan.any?
72
72
  tuples = direction ? columns.map { |value| value.split(":") } : []
73
- tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } # fix datetime
73
+ tuples.map! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] }
74
74
  add_columns = tuples.map(&options[:add]).join("\n ")
75
75
  remove_columns = tuples.map(&options[:remove]).join("\n ")
76
76
  forward_text = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, add_columns) if tuples.any?
@@ -85,21 +85,18 @@ module Padrino
85
85
  end
86
86
  end
87
87
 
88
- # For migration files
89
- # returns the number of the latest(most current) migration file
88
+ ##
89
+ # Returns the number of the latest(most current) migration file.
90
90
  #
91
- # @api private
92
91
  def return_last_migration_number
93
92
  Dir[destination_root('db/migrate/*.rb')].map { |f|
94
93
  File.basename(f).match(/^(\d+)/)[0].to_i
95
94
  }.max.to_i || 0
96
95
  end
97
96
 
98
- # For migration files
99
- # returns the number of the migration that is being created
100
- # returna timestamp instead if :migration_format: in .components is "timestamp"
97
+ ##
98
+ # Returns timestamp instead if :migration_format: in .components is "timestamp"
101
99
  #
102
- # @api private
103
100
  def current_migration_number
104
101
  if fetch_component_choice(:migration_format).to_s == 'timestamp'
105
102
  Time.now.utc.strftime("%Y%m%d%H%M%S")
@@ -108,25 +105,22 @@ module Padrino
108
105
  end.to_s
109
106
  end
110
107
 
111
- # Return true if the migration already exist
108
+ ##
109
+ # Return true if the migration already exist.
112
110
  #
113
111
  # @param [String] filename
114
- # File name of the migration file
112
+ # File name of the migration file.
115
113
  #
116
- # @param [Boolean] Boolean if file exists
117
- #
118
- # @api private
119
114
  def migration_exist?(filename)
120
115
  Dir[destination_root("db/migrate/*_#{filename.underscore}.rb")].size > 0
121
116
  end
122
117
 
123
- # For the removal of migration files
124
- # removes the migration file based on the migration name
118
+ ##
119
+ # Removes the migration file based on the migration name.
125
120
  #
126
121
  # @param [String] name
127
- # File name of the migration
122
+ # File name of the migration.
128
123
  #
129
- # @api private
130
124
  def remove_migration(name)
131
125
  migration_path = Dir[destination_root('db/migrate/*.rb')].find do |f|
132
126
  File.basename(f) =~ /#{name.to_s.underscore}/
@@ -137,37 +131,35 @@ module Padrino
137
131
  end
138
132
  end
139
133
 
140
- # For testing components
141
- # Injects the test class text into the test_config file for setting up the test gen
134
+ ##
135
+ # Injects the test class text into the test_config file for setting up the test gen.
142
136
  #
143
137
  # @param [String] suite_text
144
- # Class name for test suite
138
+ # Class name for test suite.
145
139
  # @param [Hash] options
146
- # Additional options to pass into injection
140
+ # Additional options to pass into injection.
147
141
  #
148
142
  # @example
149
143
  # insert_test_suite_setup('...CLASS_NAME...')
150
144
  # => inject_into_file("test/test_config.rb", TEST.gsub(/CLASS_NAME/, @app_name), :after => "set :environment, :test")
151
145
  #
152
- # @api private
153
146
  def insert_test_suite_setup(suite_text, options={})
154
147
  options.reverse_merge!(:path => "test/test_config.rb")
155
148
  create_file(options[:path], suite_text.gsub(/CLASS_NAME/, "#{@project_name}::#{@app_name}"))
156
149
  end
157
150
 
158
- # For mocking components
159
- # Injects the mock library include into the test class in test_config for setting up mock gen
151
+ ##
152
+ # Injects the mock library include into the test class in test_config
153
+ # for setting up mock gen
160
154
  #
161
155
  # @param [String] library_name
162
- # name of mocking library
156
+ # Name of mocking library.
163
157
  # @param [Hash] options
164
- # Additional options to pass into injection
165
158
  #
166
159
  # @example
167
- # insert_mock_library_include('Mocha::API')
160
+ # insert_mocking_include('Mocha::API'):
168
161
  # => inject_into_file("test/test_config.rb", " include Mocha::API\n", :after => /class.*?\n/)
169
162
  #
170
- # @api private
171
163
  def insert_mocking_include(library_name, options={})
172
164
  options.reverse_merge!(:indent => 2, :after => /class.*?\n/, :path => "test/test_config.rb")
173
165
  return unless File.exist?(destination_root(options[:path]))
@@ -175,26 +167,25 @@ module Padrino
175
167
  inject_into_file(options[:path], include_text, :after => options[:after])
176
168
  end
177
169
 
178
- # Returns space characters of given count
170
+ ##
171
+ # Returns space characters of given count.
179
172
  #
180
173
  # @example
181
174
  # indent_spaces(2)
182
175
  #
183
- # @api private
184
176
  def indent_spaces(count)
185
177
  ' ' * count
186
178
  end
187
179
 
188
- # For Controller action generation
189
- # Takes in fields for routes in the form of get:index post:test delete:yada and such
180
+ ##
181
+ # Takes in fields for routes in the form of get:index post:test delete:yada.
190
182
  #
191
183
  # @param [Array<String>] fields
192
- # Array of controller actions and route name
184
+ # Array of controller actions and route name.
193
185
  #
194
186
  # @example
195
- # controller_actions "get:index", "post:test"o
187
+ # controller_actions("get:index", "post:test")
196
188
  #
197
- # @api private
198
189
  def controller_actions(fields)
199
190
  field_tuples = fields.map { |value| value.split(":") }
200
191
  action_declarations = field_tuples.map do |request, name|
@@ -202,7 +193,7 @@ module Padrino
202
193
  end
203
194
  action_declarations.join("\n").gsub(/^/, " " * 2).gsub(/^\s*$/, "")
204
195
  end
205
- end # Actions
206
- end # Components
207
- end # Generators
208
- end # Padrino
196
+ end
197
+ end
198
+ end
199
+ end
@@ -1,9 +1,10 @@
1
1
  def setup_mock
2
- require_dependencies 'mocha', :group => 'test'
2
+ require_dependencies 'mocha', :group => 'test', :require => false
3
3
  case options[:test].to_s
4
4
  when 'rspec'
5
5
  inject_into_file 'spec/spec_helper.rb', " conf.mock_with :mocha\n", :after => "RSpec.configure do |conf|\n"
6
6
  else
7
+ inject_into_file 'test/test_config.rb', "require 'mocha/api'", :after => "require File.expand_path(File.dirname(__FILE__) + \"/../config/boot\")\n"
7
8
  insert_mocking_include "Mocha::API"
8
9
  end
9
10
  end
@@ -7,7 +7,7 @@ def setup_mock
7
7
  inject_into_file "test/test_config.rb","require 'riot/rr'\n", :after => "\"/../config/boot\")\n"
8
8
  when 'minitest'
9
9
  insert_mocking_include "RR::Adapters::MiniTest", :path => "test/test_config.rb"
10
- else # default include
10
+ else
11
11
  insert_mocking_include "RR::Adapters::TestUnit", :path => "test/test_config.rb"
12
12
  end
13
13
  end