-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "bundle search" subcommand #360
Conversation
I like this! Is it possible to fallback to |
Should be possible but I think grep might find things in directories ag would ignore - like the .git directory of any git dependencies. |
# Arguments: | ||
# 1. What to search for | ||
# 2. Which gem names to search (defaults to all gems) | ||
ag "$1" $(bundle show --paths $2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unquoted $2
only works by coincidence because gems names rarely (never?) contain spaces. I would do the following anyway:
pattern="$1"; shift
ag "$pattern" $(bundle show --paths "$@")
It still relies on the implicit word-splitting of $(bundle show ...)
, but there's not a great (read: POSIX) way around that. It'll break if bundle show
reports directories with spaces.
Updated to better handle parameters. When
|
Nice job bundler printing errors on stdout... |
Gosh dangit, I got a little too inspired by this after listening the bikeshed.fm podcast. I now know more Bash than I ever wanted to know 😆. Oh well, maybe someone can enjoy the fruits of my labor. Caveats:
❤️ #!/usr/bin/env sh
set -e
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
echo "
Usage: $(basename "$0") [options] pattern
Search your bundle for the provided pattern
* Requires bundler 1.8+ for execution as a bundler subcommand.
bundle-search tries to find a proper backend for you. It attempts to use:
1. ag
2. grep
Examples:
- bundle search Kernel.warn
- bundle search current_user clearance
Arguments:
1. What to search for
2. Whichever options you want passed to you search backend.
Credits:
- @derekprior for the idea, inspiration and initial implementation
- @ianks for the Bash tom-foolery
"
exit
fi
search_path() {
if SEARCH_PATH=$(bundle show --paths "$@"); then
echo $SEARCH_PATH
else
echo $(/usr/bin/env ruby -rubygems -e 'puts Gem.user_dir')
fi
}
search_cmd() {
if hash ag 2>/dev/null; then
echo "ag -G .*\.rb"
else
echo "grep -R --color --include=\*.rb"
fi
}
eval "$(search_cmd) "$@" "$PATTERN" $(search_path)" |
# Requires bundler 1.8+ for execution as a bundler subcommand. | ||
# Examples: | ||
# bundle search Kernal.warn | ||
# bundle search current_user clearance |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I wanted to search for a query that has multiple words, do I just quote it?
bundle search "Change your password" clearance
Might be worth adding to the example.
This seems cool and helpful. 👍 Is there any Vim integration we can add, too, like filling and opening the quickfix list? http://vim.wikia.com/wiki/Automatically_open_the_quickfix_window_on_:make |
Good idea @croaky. I have some other things I am working on so I cant explore the VIM integration yet, but I would love to have something like this in VIM. |
Bundler 1.8+ added support for git-style subcommands. Any scripts starting with `bundler-` in your path are executable as bundler subcommands. This adds `search` as a subcommand that uses `ag` to search for a string among all gems in your bundle (default) or optionally a specific gem. I've found this useful for finding the source of puzzling deprecations, finding what gem provides a method, and other various things.
Thanks @derekprior! |
Bundler 1.8+ added support for git-style subcommands. Any scripts
starting with
bundler-
in your path are executable as bundlersubcommands.
This adds
search
as a subcommand that usesag
to search for a stringamong all gems in your bundle (default) or optionally a specific gem.
I've found this useful for finding the source of puzzling deprecations,
finding what gem provides a method, and other various things.
This script isn't nearly perfect. It fails confusingly when
bundle show --paths
fails (if, for instance, you are not in bundler project).
shellcheck
complainsabout quoting but providing the quotes it wants breaks the command.
But, it works. Pushing this up now to see if others might find it useful and to
recruit help in making it a bit more robust.