lib/bivouac/commands/generate.rb in bivouac-0.0.2 vs lib/bivouac/commands/generate.rb in bivouac-0.0.3

- old
+ new

@@ -1,52 +1,112 @@ #!/usr/bin/env ruby # bivouac - 0.0.1 # (c)2007 Grégoire Lejeune require 'rubygems' +require 'optparse' +require 'ostruct' require 'bivouac/template' require 'active_support' require File.dirname($0) + '/../config/environment.rb' module Bivouac class Generate include Bivouac::Template def initialize( argv ) - @script_arguments = argv - # if @script_arguments.include?( '-h' ) or @script_arguments.include?( '--help' ) - # help( ) - # exit - # end + @generator_type = argv.shift - @app = Bivouac::Environment.new( ) - @generation_app_name = @app.environment.appname + @options = OpenStruct.new( :noview => 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, migrate, view" + + opts.separator "" + 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("-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("-r", "--route ROUTE,ROUTE,...", "Add ROUTES to the controller") { |routes| + if @generator_type != 'controller' + puts @opts + exit + end + @options.routes = routes.split(',') + } + + opts.separator "" + opts.separator "Common options:" + opts.on_tail("-h", "--help", "Show this message") do + puts opts + exit + end + end + + if ['-h', '--help'].include?( @generator_type ) + puts @opts + exit + end - @file_type = s( ) - @generation_name = s( ) - @generation_class_name = @generation_name.classify - @generation_file_name = @generation_class_name.underscore + @opts.parse! argv + if argv.length < 1 + puts @opts + exit + end + + @script_arguments = argv.dup - @destination_file = File.dirname( $0 ) + "/../app/" + @file_type.pluralize + "/" + @generation_file_name.underscore + ".rb" + @app = Bivouac::Environment.new( ) + @generation_app_name = @app.environment.appname + @generation_type = @app.environment.orgtype end def run - send( @file_type.to_sym ) + @script_arguments.each do |@generation_name| + @generation_class_name = @generation_name.classify + @generation_file_name = @generation_class_name.underscore + send( @generator_type.to_sym ) + end end private def controller + @destination_file = File.dirname( $0 ) + "/../app/" + @generator_type.pluralize + "/" + @generation_file_name.underscore + ".rb" @generation_view_name = @generation_class_name.underscore + + 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("', '") + "'" + else + "" + end + createFile( @destination_file ) do |io| io.puts template( "generate/controller", binding ) end - view( ) + view( ) if @options.noview == false end def migrate - @generation_table_name = (@generation_app_name + "_" + @generation_name).tableize + @generation_table_name = (@generation_class_name.downcase + "_" + @generation_name).tableize + @destination_file = File.dirname( $0 ) + "/../db/migrate/" + @generation_file_name.underscore + ".rb" createFile( @destination_file ) do |io| io.puts template( "generate/migrate", binding ) end @@ -55,44 +115,31 @@ io.puts template( "generate/create", binding ) end end def model + @destination_file = File.dirname( $0 ) + "/../app/" + @generator_type.pluralize + "/" + @generation_file_name.underscore + ".rb" createFile( @destination_file ) do |io| io.puts template( "generate/model", binding ) end migrate( ) end - def view( ) - @destination_file = File.dirname( $0 ) + "/../app/views/" + @generation_file_name.underscore + ".rb" + def view + if @generation_type.downcase == "erb" + @destination_file = File.dirname( $0 ) + "/../app/views/" + @generation_file_name.underscore + ".html" + else + @destination_file = File.dirname( $0 ) + "/../app/views/" + @generation_file_name.underscore + ".rb" + end + + @generation_view_name = @generation_class_name.underscore createFile( @destination_file ) do |io| - io.puts template( "generate/view", binding ) + io.puts template( "generate/view_#{@generation_type.downcase}", binding ) end end - def s - begin - return @script_arguments.shift - rescue - help( ) - exit - end - end - - def help - puts <<-'END_PRODUCT'.gsub(/^\s+/, "") - Usage: script/generate generator [args] - - Bivouac Info: - -h, --help Show this help message and quit. - - Generators - Builtin: controller, model, migrate - END_PRODUCT - end - end end -Bivouac::Generate.new( ARGV ).run( ) \ No newline at end of file + +Bivouac::Generate.new( ARGV ).run( )