Creating executable rails 3 generators

For inspiration I have been looking into rails 3.0pre

I found this in the file bin/rails

require ‘rails/generators’
require ‘generators/rails/app/app_generator’


Aha! Require the app generator and then start it :slight_smile:

then in framework.task

desc “Applies the template supplied by LOCATION=/path/to/template”
task :template do

require ‘rails/generators’
require ‘generators/rails/app/app_generator’
generator = [ Rails.root ],
{}, :destination_root => Rails.root
generator.apply template, :verbose => false

namespace :update do
def invoke_from_app_generator(method)
require ‘rails/generators’
require ‘generators/rails/app/app_generator’

  generator = ["rails"],

{ :with_dispatchers => true },
:destination_root => Rails.root

desc "Update config/boot.rb from your current rails install"
task :configs do
  invoke_from_app_generator :create_boot_file
  invoke_from_app_generator :create_config_files

desc "Update Prototype javascripts from your current rails

task :javascripts do
invoke_from_app_generator :create_prototype_files

desc "Add new scripts to the application script/ directory"
task :scripts do
  invoke_from_app_generator :create_script_files

Here the utility method invoke_from_app_generator requires the app
generator and invokes a specific method.
Specific tasks then use this utility method to execute specific parts
of the app generator as needed…

and in railties3.0pre.gemspec

s.default_executable = %q{rails}
s.executables = [“rails”]

Defines the executable rails found in the bin folder

So I assume, to make a gem executable, you simply need to

  1. define the executables in the gemspec
  2. put an executable in the bin folder, fx bin/my_executable
  3. have that executable require a generator and then call start on the
  4. install the gem
  5. run the executable passing in any defined arguments as per the

Commens, ideas, suggestions?



It almost works… except I run into the following issue

module Nifty
module Generators
class ScaffoldGenerator < Base

def initialize(*args, &block)

    args_for_c_m.each do |arg|
      if arg == '!'
        options[:invert] = true    <<<<<< line 53

if @model_attributes.empty?
options[:skip_model] = true <<<<<<<<


$ nifty_scaffold blip
thor-0.12.3/lib/thor/core_ext/hash_with_indifferent_access.rb:26:in [] =': can't modify frozen hash (RuntimeError) from /opt/local/lib/ruby/gems/1.9.1/gems/railties-3.0.pre/lib/rails/ vendor/thor-0.12.3/lib/thor/core_ext/hash_with_indifferent_access.rb: 26:in[]=’
from /opt/local/lib/ruby/gems/1.9.1/gems/very_nifty_generators-0.1.2/
lib/generators/nifty/scaffold/scaffold_generator.rb:53:in `initialize’

Is there a nice way to fix this? Do I have to create a non-frozen
custom options collection or someone has a better idea?


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs