Bash completion for script/generate
November 18th, 2007 by michael
The Rails generator script/generate knows pretty well what things it can generate. In fact, it knows much better than I do. So, I think it could really give me some help when I’m typing along on the command line.
If you save the snippet below as /etc/bash_completion.d/generate you can enjoy this help, too.
_generate()
{
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if [ ! -d "$PWD/script" ]; then
return 0
fi
if [ $COMP_CWORD == 1 ] && [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-h -v\\
--help --version'\\
-- $cur ))
return 0
fi
if [ $COMP_CWORD == 2 ] && [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-p -f -s -q -t -c\\
--pretend --force --skip --quiet --backtrace --svn'\\
-- $cur ))
return 0
fi
COMPREPLY=( $(script/generate --help | \\
awk -F ': ' '/^ (Plugins|Rubygems|Builtin|User):/ { gsub(/, */, "\n", $2); print $2 }' | \\
command grep "^$cur" \\
))
}
complete -F _generate $default generate

Hmm, when I dbl-click after “script/generate” all my generator names are merged together into long names:
app_layoutncheck_migration_versionndatabase_yml_mysqlndeploynhome_route
controllernintegration_testnmailernmigrationnmodelnobservernpluginnresourcenscaffoldnsession_migration
hobonhobo_front_controllernhobo_migrationnhobo_modelnhobo_model_controllernhobo_model_resourcenhobo_rapidnhobo_user_controllernhobo_user_modelnhobofield_model
magic_modelnhobonhobo_front_controllernhobo_migrationnhobo_modelnhobo_model_controllernhobo_model_resourcenhobo_rapidnhobo_user_controllernhobo_user_modelnhobofield_modelninstall_rubigen_scriptsnjavascript_test
The relevant section of the –help output is:
Installed Generators
Plugins: hobo, hobo_front_controller, hobo_migration, hobo_model, hobo_model_controller, hobo_model_resource, hobo_rapid, hobo_user_controller, hobo_user_model, hobofield_model
Rubygems: magic_model, hobo, hobo_front_controller, hobo_migration, hobo_model, hobo_model_controller, hobo_model_resource, hobo_rapid, hobo_user_controller, hobo_user_model, hobofield_model, install_rubigen_scripts, javascript_test
Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration
User: app_layout, check_migration_version, database_yml_mysql, deploy, home_route
I can’t follow the bash/awk code so am not sure how to fix this
Thoughts?
BTW, I added ‘User’ into (Plugins|Rubygems|Builtin|User) on line 29.
Hi Nic, I’ve added the ‘User’ output.
Regarding the run-together output, I’m not quite clear how you double click on the command line.
Here’s the relevant AWK (cleaned up!) code again
awk -F ': ' '/^ (Plugins|Rubygems|Builtin|User):/ { gsub(/, */, "\\n", $2); print $2 }'The
-Foption set the input field separator to ‘:‘. The regex matches against prefixes that mark lines containing generators. Thegsubreplaces ‘,‘ with ‘\n‘ in the second field (as separated by ‘:‘).You’re using a Mac, right? I have a very vague idea that it might expect some other character than ‘
\nas the line separator.I fixed the “n” -> “\n” which fixed that problem, but now I’m getting mysterious blank space (not actual spaces since I can’t delete them) instead of a list of words to choose from. Wacky.
I think I might just rewrite the script in ruby so I know what its doing