#!/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 ) # Generator type (controller, model, migrate, view, ...) @generator_type = argv.shift # Generator options @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, scaffold" 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 # parse options @opts.parse! argv if argv.length < 1 puts @opts exit end # Generator arguments. # Example : # script/generate controller Hello World # @script_arguments = ['Hello', 'World'] @script_arguments = argv.dup # Application environment @app = Bivouac::Environment.new( ) # Application name for the generator, passed to the template @generation_app_name = @app.environment.appname # Application organisation (goh, jof or erb) @generation_type = @app.environment.orgtype 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 ) end end private def controller @destination_file = File.dirname( $0 ) + "/../app/controllers/" + @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( ) if @options.noview == false end def migrate @generation_table_name = (@generation_app_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 @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 migrate( ) end 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_#{@generation_type.downcase}", 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 @destination_file = File.dirname( $0 ) + "/../app/views/list_" + @generation_file_name + ".rb" createFile( @destination_file ) do |io| io.puts template( "generate/scaffold_view_list", binding ) end @destination_file = File.dirname( $0 ) + "/../app/views/create_" + @generation_file_name + ".rb" createFile( @destination_file ) do |io| io.puts template( "generate/scaffold_view_create", binding ) end @destination_file = File.dirname( $0 ) + "/../app/views/view_" + @generation_file_name + ".rb" createFile( @destination_file ) do |io| io.puts template( "generate/scaffold_view_view", binding ) end end end end Bivouac::Generate.new( ARGV ).run( )