Subscribe to
Posts
Comments

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

4 Responses to “Bash completion for script/generate”

  1. on 09 Jun 2008 at 07:59Dr Nic

    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?

  2. on 09 Jun 2008 at 07:59Dr Nic

    BTW, I added ‘User’ into (Plugins|Rubygems|Builtin|User) on line 29.

  3. on 09 Jun 2008 at 09:51michael

    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 -F option set the input field separator to ‘: ‘. The regex matches against prefixes that mark lines containing generators. The gsub replaces ‘, ‘ 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 ‘\n as the line separator.

  4. on 15 Jun 2008 at 13:10Dr Nic

    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 :P

Leave a Reply

Fork me on GitHub