lib/bivouac/commands/generate.rb in bivouac-0.1.6 vs lib/bivouac/commands/generate.rb in bivouac-0.2.0
- old
+ new
@@ -1,143 +1,294 @@
#!/usr/bin/env ruby
-# bivouac - 0.0.1
-# (c)2007 Grégoire Lejeune
+# bivouac
+# (c)2007, 2008 Grégoire Lejeune
require 'rubygems'
require 'optparse'
require 'ostruct'
require 'bivouac/template'
+require 'bivouac/utils'
require 'active_support'
require 'active_record'
require 'find'
require File.dirname($0) + '/../config/environment.rb'
module Bivouac
class Generate
include Bivouac::Template
+ # Initialize the optionnal generators table
+ add_generator []
def initialize( argv )
# Generator type (controller, model, migration, view, ...)
@generator_type = argv.shift
- # Generator options
- @options = OpenStruct.new( :noview => false, :notest => false, :default_route => true, :routes => [] )
- @opts = OptionParser.new do |opts|
- opts.banner = "Usage: script/generate generator [options] [args]"
-
- opts.separator ""
- opts.separator "Generators"
- opts.separator " Builtin: controller, model, migration, view, test, scaffold"
-
- opts.separator ""
+ # Display help if asked
+ if ['-h', '--help'].include?( @generator_type )
+ help
+ exit
+ end
+
+ # Generator arguments.
+ # Example :
+ # script/generate controller Hello World
+ # @script_arguments = ['Hello', 'World']
+ @script_arguments = argv.dup
+
+ # Application environment
+ @app = Bivouac::Environment.new( )
+
+ # Database configuration
+ @database_configuration = @app.environment.db
+ if @database_configuration[:adapter] =~ /sqlite/
+ @database_configuration[:database] = File.expand_path( File.dirname( $0 ) + "/../" + @database_configuration[:database] )
+ end
+ end
+
+ def run
+ # If the générator is not installed, display help and exit
+ unless ( ["controller", "model", "migration", "view", "test"] + @@plugins_generators ).include?( @generator_type )
+ help
+ exit
+ end
+
+ # Call generator
+ send( @generator_type.to_sym )
+ return
+ end
+
+ private
+ def help
+ puts "Usage: script/generate generator [options] [args]"
+ puts ""
+ puts "Generators"
+ puts " Builtin: controller, model, migration, view, test"
+ puts " Plugins: " + @@plugins_generators.join(", ") if @@plugins_generators.size > 0
+ puts ""
+ puts "Commun options:"
+ puts " -h, --help Show general help message"
+ end
+
+ def controller
+ # controller generator options
+ controller_options = OpenStruct.new( :noview => false, :notest => false, :default_route => true, :routes => [] )
+
+ # Controller options
+ argument_options = OptionParser.new do |opts|
+ opts.banner = ""
opts.separator "controller generator options:"
- opts.on("-v", "--no-view", "Do not generate any view for the controller") { |@options.noview|
- if @generator_type != 'controller'
- puts @opts
- exit
- end
- @options.noview = true
+ opts.on("-v", "--no-view", "Do not generate any view for the controller") { |controller_options.noview|
+ controller_options.noview = true
}
- opts.on("-d", "--no-route", "Do not add the default route to the controller (/controller_name)") { |@options.default_route|
- if @generator_type != 'controller'
- puts @opts
- exit
- end
- @options.default_route = false
+ opts.on("-d", "--no-route", "Do not add the default route to the controller (/controller_name)") { |controller_options.default_route|
+ controller_options.default_route = false
}
opts.on("-r", "--route ROUTE,ROUTE,...", "Add ROUTES to the controller") { |routes|
- if @generator_type != 'controller'
- puts @opts
- exit
- end
- @options.routes = routes.split(',')
+ controller_options.routes = routes.split(',')
}
- opts.on("-t", "--no-test", "Do not generate any test for the controller") { |@options.notest|
- if @generator_type != 'controller'
- puts @opts
- exit
- end
- @options.notest = true
+ opts.on("-t", "--no-test", "Do not generate any test for the controller") { |controller_options.notest|
+ controller_options.notest = true
}
opts.separator ""
- opts.separator "Common options:"
opts.on_tail("-h", "--help", "Show this message") do
+ help
puts opts
exit
end
end
-
- if ['-h', '--help'].include?( @generator_type )
- puts @opts
- exit
- end
# parse options
- @opts.parse! argv
- if argv.length < 1
- puts @opts
- exit
+ argument_options.parse! @script_arguments
+ if @script_arguments.length < 1
+ help
+ puts argument_options
+ exit
end
- # Generator arguments.
- # Example :
- # script/generate controller Hello World
- # @script_arguments = ['Hello', 'World']
- @script_arguments = argv.dup
+ @script_arguments.each do |controller_name|
+ # Controller class name, passed to the template
+ @controller_class_name = controller_name.classify
+ # File name for the template
+ controller_file_name = @controller_class_name.underscore
+
+ # Destination file
+ destination_file = File.dirname( $0 ) + "/../app/controllers/" + controller_file_name + ".rb"
+
+ # Set view name, passed to the template
+ @view_name = @controller_class_name.underscore
+
+ # Set routes, passed to the template
+ default_route = @view_name
+ controller_options.routes << default_route if controller_options.default_route and controller_options.routes.include?( default_route ) == false
+ @controller_routes = if controller_options.routes.size > 0
+ " < R '/" + controller_options.routes.join("', '/") + "'"
+ else
+ ""
+ end
+
+ # Create controller
+ createFile( destination_file ) do |io|
+ io.puts template( "generate/controller", binding )
+ end
+
+ # Create view @view_name
+ view( @view_name ) if controller_options.noview == false
+
+ # Create test for @view_name with routes controller_options.routes
+ test( @view_name, controller_options.routes ) if controller_options.notest == false
+ end
+ end
+
+ def view( view_name = nil )
+ if @app.environment.orgtype.downcase == "erb"
+ raise "ERB applications are no longer supported. Sorry!"
+ end
- # Application environment
- @app = Bivouac::Environment.new( )
+ # View options
+ argument_options = OptionParser.new do |opts|
+ opts.banner = ""
+ opts.separator "view generator options:"
+ opts.on_tail("-h", "--help", "Show this message") do
+ help
+ puts opts
+ exit
+ end
+ end
- # Application name for the generator, passed to the template
- @generation_app_name = @app.environment.appname
+ # parse options if view name is not passed
+ if view_name.nil?
+ argument_options.parse! @script_arguments
+ if @script_arguments.length < 1
+ help
+ puts argument_options
+ exit
+ end
+
+ view_name = @script_arguments
+ else
+ view_name = [ view_name ]
+ end
- @generation_app_dir = @app.environment.appdir
+ view_name.each do |view|
+ # Set view name, passed to the template
+ @view_name = view.underscore
+
+ # Set destination file, passed to the template
+ @destination_file = File.dirname( $0 ) + "/../app/views/" + @view_name.underscore + ".rb"
- # Application organisation (goh, jof or erb)
- @generation_type = @app.environment.orgtype
-
- # Database configuration
- @database_configuration = @app.environment.db
- if @database_configuration[:adapter] =~ /sqlite/
- @database_configuration[:database] = File.expand_path( File.dirname( $0 ) + "/../" + @database_configuration[:database] )
+ # Create view file
+ createFile( @destination_file ) do |io|
+ io.puts template( "generate/view_goh", binding )
+ end
end
end
- def run
- @script_arguments.each do |@generation_name|
- # Class name passed to the template
- @generation_class_name = @generation_name.classify
- # File name for the template
- @generation_file_name = @generation_class_name.underscore
- # Call the generator
- send( @generator_type.to_sym )
+ def test( test_name = nil, routes = nil )
+ # Test options
+ argument_options = OptionParser.new do |opts|
+ opts.banner = ""
+ opts.separator "test generator options:"
+ opts.on_tail("-h", "--help", "Show this message") do
+ help
+ puts opts
+ exit
+ end
end
+
+ # parse options if view name is not passed
+ if test_name.nil?
+ argument_options.parse! @script_arguments
+ if @script_arguments.length < 1
+ help
+ puts argument_options
+ exit
+ end
+
+ test_name = @script_arguments.shift
+ routes = @script_arguments
+ end
+
+ # Create test file
+ destination_file = File.dirname( $0 ) + "/../test/test_" + test_name.underscore + ".rb"
+ createFile( destination_file ) do |io|
+ io.puts template( "generate/test_begin", binding )
+
+ routes.each do |@test_route|
+ io.puts template( "generate/test_views", binding )
+ end
+
+ io.puts template( "generate/test_end", binding )
+ end
end
- private
- def controller
- @destination_file = File.dirname( $0 ) + "/../app/controllers/" + @generation_file_name.underscore + ".rb"
- @generation_view_name = @generation_class_name.underscore
+ def model( model = nil )
+ # Model options
+ argument_options = OptionParser.new do |opts|
+ opts.banner = ""
+ opts.separator "model generator options:"
+ opts.on_tail("-h", "--help", "Show this message") do
+ help
+ puts opts
+ exit
+ end
+ end
- default_route = @generation_view_name
- @options.routes << default_route if @options.default_route and @options.routes.include?( default_route ) == false
- @generation_routes = if @options.routes.size > 0
- " < R '/" + @options.routes.join("', '/") + "'"
+ # parse options
+ if model.nil?
+ argument_options.parse! @script_arguments
+ if @script_arguments.length < 1
+ help
+ puts argument_options
+ exit
+ end
+ model_list = @script_arguments
else
- ""
+ model_list = [ model ]
end
+
+ model_list.each do |model_name|
+ # Model class name, passed to the template
+ @model_class_name = model_name.classify
- createFile( @destination_file ) do |io|
- io.puts template( "generate/controller", binding )
- end
+ destination_file = File.dirname( $0 ) + "/../app/models/" + @model_class_name.underscore + ".rb"
+ createFile( destination_file ) do |io|
+ io.puts template( "generate/model", binding )
+ end
- view( ) if @options.noview == false
- test( ) if @options.notest == false
+ migration( model_name )
+ end
end
- def migration( from_model = false )
+ def migration( model = nil )
+ # Test options
+ argument_options = OptionParser.new do |opts|
+ opts.banner = ""
+ opts.separator "migration generator options:"
+ opts.on_tail("-h", "--help", "Show this message") do
+ help
+ puts opts
+ exit
+ end
+ end
+
+ # parse options if view name is not passed
+ if model.nil?
+ argument_options.parse! @script_arguments
+ if @script_arguments.length < 1
+ help
+ puts argument_options
+ exit
+ end
+
+ model = @script_arguments
+ from_model = false
+ else
+ model = [ model ]
+ from_model = true
+ end
+
# Get old and new version
old_version = 0.0
begin
ActiveRecord::Base.establish_connection @database_configuration
conn = ActiveRecord::Base.connection
@@ -161,100 +312,31 @@
file_prefix = sprintf( "%03d", (files[0][0,3].to_i + 1) )
end
puts "\t* Migration from version #{old_version} to version #{@new_version}" if old_version > 0.0
- @generation_table_name = (@generation_app_name.downcase + "_" + @generation_name).tableize
+ model.each do |model_name|
+ # Model class name
+ @model_class_name = model_name.classify
+
+ # Model class name
+ @model_table_name = (@app.environment.appname.downcase + "_" + model_name).tableize
- @destination_file = File.dirname( $0 ) + "/../db/migrate/" + file_prefix + "_" + @generation_file_name.underscore + ".rb"
- createFile( @destination_file ) do |io|
- if from_model
- io.puts template( "generate/migrate", binding )
- else
- io.puts template( "generate/migration", binding )
+ destination_file = File.dirname( $0 ) + "/../db/migrate/" + file_prefix + "_" + model_name.underscore + ".rb"
+ createFile( destination_file ) do |io|
+ if from_model
+ io.puts template( "generate/migrate", binding )
+ else
+ io.puts template( "generate/migration", binding )
+ end
end
end
- @destination_file = File.dirname( $0 ) + "/../db/create.rb"
- createFile( @destination_file, false, false ) do |io|
+ destination_file = File.dirname( $0 ) + "/../db/create.rb"
+ createFile( destination_file, false, false ) do |io|
io.puts template( "generate/create", binding )
end
end
-
- def model
- @destination_file = File.dirname( $0 ) + "/../app/models/" + @generation_file_name.underscore + ".rb"
- createFile( @destination_file ) do |io|
- io.puts template( "generate/model", binding )
- end
-
- migration( true )
- end
-
- def view
- file_extension = ".rb"
- if @generation_type.downcase == "erb"
- raise "ERB applications are no longer supported. Sorry!"
- file_extension = ".html"
- end
- @destination_file = File.dirname( $0 ) + "/../app/views/" + @generation_file_name.underscore + file_extension
-
- @generation_view_name = @generation_class_name.underscore
- createFile( @destination_file ) do |io|
- io.puts template( "generate/view_#{@generation_type.downcase}", binding )
- end
- end
-
- def test
- @destination_file = File.dirname( $0 ) + "/../test/test_" + @generation_file_name.underscore + ".rb"
- createFile( @destination_file ) do |io|
- io.puts template( "generate/test_begin", binding )
- @options.routes.each do |@test_route|
- io.puts template( "generate/test_views", binding )
- end
- io.puts template( "generate/test_end", binding )
- end
- end
-
- def scaffold
- @generation_view_name = @generation_class_name.underscore
-
- # Generate model and migrate
- model( )
-
- # Generate scaffold controllers
- @destination_file = File.dirname( $0 ) + "/../app/controllers/list_" + @generation_file_name + ".rb"
- createFile( @destination_file ) do |io|
- io.puts template( "generate/scaffold_controller_list", binding )
- end
- @destination_file = File.dirname( $0 ) + "/../app/controllers/create_" + @generation_file_name + ".rb"
- createFile( @destination_file ) do |io|
- io.puts template( "generate/scaffold_controller_create", binding )
- end
- @destination_file = File.dirname( $0 ) + "/../app/controllers/view_" + @generation_file_name + ".rb"
- createFile( @destination_file ) do |io|
- io.puts template( "generate/scaffold_controller_view", binding )
- end
-
- # Generate scaffold views
- file_extension = ".rb"
- if @generation_type.downcase == "erb"
- raise "ERB applications are no longer supported. Sorry!"
- file_extension = ".html"
- end
- @destination_file = File.dirname( $0 ) + "/../app/views/list_" + @generation_file_name + file_extension
- createFile( @destination_file ) do |io|
- io.puts template( "generate/scaffold_view_list_#{@generation_type.downcase}", binding )
- end
- @destination_file = File.dirname( $0 ) + "/../app/views/create_" + @generation_file_name + file_extension
- createFile( @destination_file ) do |io|
- io.puts template( "generate/scaffold_view_create_#{@generation_type.downcase}", binding )
- end
- @destination_file = File.dirname( $0 ) + "/../app/views/view_" + @generation_file_name + file_extension
- createFile( @destination_file ) do |io|
- io.puts template( "generate/scaffold_view_view_#{@generation_type.downcase}", binding )
- end
- end
end
end
-
Bivouac::Generate.new( ARGV ).run( )