Skip to content

Instantly share code, notes, and snippets.

@ibrasho
Forked from glv/bundler-exec.fish
Last active January 6, 2021 19:22
Show Gist options
  • Select an option

  • Save ibrasho/148ad91839879b88395b10c69cd6dd07 to your computer and use it in GitHub Desktop.

Select an option

Save ibrasho/148ad91839879b88395b10c69cd6dd07 to your computer and use it in GitHub Desktop.

Revisions

  1. ibrasho revised this gist Nov 5, 2020. 1 changed file with 7 additions and 12 deletions.
    19 changes: 7 additions & 12 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -36,15 +36,9 @@
    #
    # Configuration:
    #
    # 1. Drop this file in `~/.config/fish/bundler-exec.fish`
    # 1. Drop this file in `~/.config/fish/conf.d/bundler-exec.fish`
    #
    # 2. In `.config/fish/config.fish`, include these lines:
    #
    # if test -f ~/.config/fish/bundler-exec.fish
    # source ~/.config/fish/bundler-exec.fish
    # end
    #
    # 3. Add `~/.config/fish/functions/fish_user_key_bindings.fish` with
    # 2. Add `~/.config/fish/functions/fish_user_key_bindings.fish` with
    # these lines:
    #
    # function fish_user_key_bindings
    @@ -56,7 +50,7 @@
    # (or just add the inner if/end block to that function if you're
    # already defining it).
    #
    # 4. This *effectively* puts your bundle's "bin" directory at the
    # 3. This *effectively* puts your bundle's "bin" directory at the
    # start of `$PATH`, so you may as well actually put it there so
    # that completions and suggestions will also work. You can find
    # that path by going to a project directory that uses Bundler and
    @@ -68,6 +62,7 @@
    # executed directly.)
    #
    # Author: Glenn Vanderburg <[email protected]>
    # Updated by: Ibrahim AshShohail <[email protected]>

    begin
    # Support Functions
    @@ -82,7 +77,7 @@ begin
    if test -f "$dir/Gemfile" -o -f "$dir/gems.rb"
    return
    end
    set -l dir (dirname $dir)
    set dir (dirname $dir)
    end
    false
    end
    @@ -93,12 +88,12 @@ begin
    and _within-bundled-project

    # This version is slow but simple and safe.
    # set -l bundler_path (bundle exec gem environment path)
    set -l bundle_path (bundle exec gem environment path)

    # This version is faster but complex and probably brittle. If
    # you're seeing flaky behavior around all of this, switch to the
    # slow version above and see if that fixes it.
    set -l bundle_path (bundle config path | sed -e '/^Set /!d;s/^.*: "\(.*\)".*$/\1/')
    # set -l bundle_path (bundle config path | sed -e '/^Set /!d;s/^.*: "\(.*\)".*$/\1/')

    set -gx RUN_WITH_BUNDLER (command ls "$bundle_path/bin")
    else
  2. @glv glv revised this gist Sep 22, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -122,3 +122,5 @@ begin
    commandline -f execute
    end
    end

    _discover_bundle
  3. @glv glv revised this gist Sep 21, 2017. 1 changed file with 23 additions and 12 deletions.
    35 changes: 23 additions & 12 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -1,30 +1,36 @@
    # This sets up fish so that, if you type a command that should be
    # run using bundler, it first automatically prepends "bundle exec"
    # run using Bundler, it first automatically prepends "bundle exec"
    # to the command line buffer before executing it. Works for all
    # commands found in the "bin" directory of the current bundle.
    #
    # To override this behavior and run such a command without bundler,
    # To override this behavior and run such a command without Bundler,
    # prefix with "command" (e.g., `command rake -T`)
    #
    # Pros (vs binstubs or aliases):
    # * automatically adjusts to bundle changes
    # * explicit (the command visible on your terminal and in your history is
    # "bundle exec whatever", and it's a nice reminder that bundler magic is
    # "bundle exec whatever", and it's a nice reminder that Bundler magic is
    # happening with those commands)
    #
    # Cons:
    # * Might be surprising at first ("I didn't type that!")
    # * Slows down `cd` and other directory change operations a bit (to recompute
    # the `RUN_WITH_BUNDLER` environment variable)
    # * Only works if the first token on the command line needs to be run with
    # bundler. (For example, if you try `env RAILS_ENV=test rake db:create`,
    # that won't work because `env` doesn't need to run under bundler. But in
    # Bundler. (For example, if you try `env RAILS_ENV=test rake db:create`,
    # that won't work because `env` doesn't need to run under Bundler. But in
    # that very common case, I just use Rake's ability to take environment
    # overrides in *its* argument list instead: `rake RAILS_ENV=test db:create`.)
    # * Effectively puts a relative directory in your path (in fact, it works best
    # if you actually do that). This *can* open an attack vector for someone to
    # gain access to your machine, but this is generally not an issue if you
    # aren't root and are not sharing directories with other users. Plus, the
    # altered command line will at least help you notice if something unexpected
    # happened (such as a bundled executable shadowing the `ls` command).
    #
    # Deficiencies:
    # * If you work simultaneously in projects that use bundler and others that
    # use a similar tool, you might want to have things set up to work
    # * If you work simultaneously in projects that use Bundler and other projects
    # that use a similar tool, you might want to have things set up to work
    # appropriately with both tools. Currently this is not implemented so as
    # to compose well with similar packages.
    #
    @@ -50,11 +56,16 @@
    # (or just add the inner if/end block to that function if you're
    # already defining it).
    #
    # 4. This *effectively* puts `.bundle/bin` at the start of `$PATH`,
    # so you may as well actually put it there so that completions
    # and suggestions will also work. (But you can't *just* do this
    # step, because the scripts in `.bundle/bin` are not binstubs,
    # so they won't load the bundle if executed directly.)
    # 4. This *effectively* puts your bundle's "bin" directory at the
    # start of `$PATH`, so you may as well actually put it there so
    # that completions and suggestions will also work. You can find
    # that path by going to a project directory that uses Bundler and
    # running `bundle config path`, and then append "/bin" to the
    # value that is displayed. I have Bundler configured to always
    # put the project bundle in `.bundle`, but YMMV. (Note: don't do
    # this step without steps 1-3, because the scripts in the bundle's
    # bin directory are not binstubs, so they won't load the bundle if
    # executed directly.)
    #
    # Author: Glenn Vanderburg <[email protected]>

  4. @glv glv revised this gist Sep 21, 2017. 1 changed file with 20 additions and 14 deletions.
    34 changes: 20 additions & 14 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    # commands found in the "bin" directory of the current bundle.
    #
    # To override this behavior and run such a command without bundler,
    # prefix with "command" (e.g., "command rake -T")
    # prefix with "command" (e.g., `command rake -T`)
    #
    # Pros (vs binstubs or aliases):
    # * automatically adjusts to bundle changes
    @@ -14,8 +14,8 @@
    #
    # Cons:
    # * Might be surprising at first ("I didn't type that!")
    # * Slows down cd and other directory change operations a bit (to recompute
    # the RUN_WITH_BUNDLER variable)
    # * Slows down `cd` and other directory change operations a bit (to recompute
    # the `RUN_WITH_BUNDLER` environment variable)
    # * Only works if the first token on the command line needs to be run with
    # bundler. (For example, if you try `env RAILS_ENV=test rake db:create`,
    # that won't work because `env` doesn't need to run under bundler. But in
    @@ -30,26 +30,32 @@
    #
    # Configuration:
    #
    # 1. Drop this file in ~/.config/fish/bundler-exec.fish
    # 1. Drop this file in `~/.config/fish/bundler-exec.fish`
    #
    # 2. In .config/fish/config.fish, include these lines:
    # 2. In `.config/fish/config.fish`, include these lines:
    #
    # if test -f ~/.config/fish/bundler-exec.fish
    # source ~/.config/fish/bundler-exec.fish
    # end
    # if test -f ~/.config/fish/bundler-exec.fish
    # source ~/.config/fish/bundler-exec.fish
    # end
    #
    # 3. Add ~/.config/fish/functions/fish_user_key_bindings.fish with
    # 3. Add `~/.config/fish/functions/fish_user_key_bindings.fish` with
    # these lines:
    #
    # function fish_user_key_bindings
    # if functions -q _maybe-add-bundle-exec-and-execute
    # bind \r _maybe-add-bundle-exec-and-execute
    # end
    # end
    # function fish_user_key_bindings
    # if functions -q _maybe-add-bundle-exec-and-execute
    # bind \r _maybe-add-bundle-exec-and-execute
    # end
    # end
    #
    # (or just add the inner if/end block to that function if you're
    # already defining it).
    #
    # 4. This *effectively* puts `.bundle/bin` at the start of `$PATH`,
    # so you may as well actually put it there so that completions
    # and suggestions will also work. (But you can't *just* do this
    # step, because the scripts in `.bundle/bin` are not binstubs,
    # so they won't load the bundle if executed directly.)
    #
    # Author: Glenn Vanderburg <[email protected]>

    begin
  5. @glv glv revised this gist Sep 20, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@
    # bundler. (For example, if you try `env RAILS_ENV=test rake db:create`,
    # that won't work because `env` doesn't need to run under bundler. But in
    # that very common case, I just use Rake's ability to take environment
    # overrides in *its* argument list instead: `rake RAILS_ENV=test db:create`.
    # overrides in *its* argument list instead: `rake RAILS_ENV=test db:create`.)
    #
    # Deficiencies:
    # * If you work simultaneously in projects that use bundler and others that
  6. @glv glv revised this gist Sep 20, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,11 @@
    # * Might be surprising at first ("I didn't type that!")
    # * Slows down cd and other directory change operations a bit (to recompute
    # the RUN_WITH_BUNDLER variable)
    # * Only works if the first token on the command line needs to be run with
    # bundler. (For example, if you try `env RAILS_ENV=test rake db:create`,
    # that won't work because `env` doesn't need to run under bundler. But in
    # that very common case, I just use Rake's ability to take environment
    # overrides in *its* argument list instead: `rake RAILS_ENV=test db:create`.
    #
    # Deficiencies:
    # * If you work simultaneously in projects that use bundler and others that
    @@ -95,6 +100,7 @@ begin
    set -l toks (commandline --tokenize)
    if contains $toks[1] $RUN_WITH_BUNDLER
    commandline "bundle exec $toks"
    commandline -f repaint
    end
    commandline -f execute
    end
  7. @glv glv revised this gist Sep 19, 2017. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,12 @@
    # * Slows down cd and other directory change operations a bit (to recompute
    # the RUN_WITH_BUNDLER variable)
    #
    # Deficiencies:
    # * If you work simultaneously in projects that use bundler and others that
    # use a similar tool, you might want to have things set up to work
    # appropriately with both tools. Currently this is not implemented so as
    # to compose well with similar packages.
    #
    # Configuration:
    #
    # 1. Drop this file in ~/.config/fish/bundler-exec.fish
  8. @glv glv revised this gist Sep 19, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,9 @@
    #
    # Pros (vs binstubs or aliases):
    # * automatically adjusts to bundle changes
    # * explicit (the command in your history is "bundle exec whatever", and it's
    # a nice reminder that bundler magic is happening with those commands)
    # * explicit (the command visible on your terminal and in your history is
    # "bundle exec whatever", and it's a nice reminder that bundler magic is
    # happening with those commands)
    #
    # Cons:
    # * Might be surprising at first ("I didn't type that!")
  9. @glv glv revised this gist Sep 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -63,7 +63,7 @@ begin
    if _bundler-installed
    and _within-bundled-project

    # This version is slow but simple and safe
    # This version is slow but simple and safe.
    # set -l bundler_path (bundle exec gem environment path)

    # This version is faster but complex and probably brittle. If
  10. @glv glv revised this gist Sep 19, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,8 @@
    #
    # (or just add the inner if/end block to that function if you're
    # already defining it).
    #
    # Author: Glenn Vanderburg <[email protected]>

    begin
    # Support Functions
  11. @glv glv revised this gist Sep 19, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -67,9 +67,9 @@ begin
    # This version is faster but complex and probably brittle. If
    # you're seeing flaky behavior around all of this, switch to the
    # slow version above and see if that fixes it.
    set -l bundler_path (bundle config path | sed -e '/^Set /!d;s/^.*: "\(.*\)".*$/\1/')
    set -l bundle_path (bundle config path | sed -e '/^Set /!d;s/^.*: "\(.*\)".*$/\1/')

    set -gx RUN_WITH_BUNDLER (command ls "$bundler_path/bin")
    set -gx RUN_WITH_BUNDLER (command ls "$bundle_path/bin")
    else
    set -ge RUN_WITH_BUNDLER
    end
  12. @glv glv revised this gist Sep 19, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -69,8 +69,7 @@ begin
    # slow version above and see if that fixes it.
    set -l bundler_path (bundle config path | sed -e '/^Set /!d;s/^.*: "\(.*\)".*$/\1/')

    set -l bundler_bin "$bundler_path/bin"
    set -gx RUN_WITH_BUNDLER (command ls $bundler_bin)
    set -gx RUN_WITH_BUNDLER (command ls "$bundler_path/bin")
    else
    set -ge RUN_WITH_BUNDLER
    end
  13. @glv glv revised this gist Sep 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    # prefix with "command" (e.g., "command rake -T")
    #
    # Pros (vs binstubs or aliases):
    # * never needs updating (i.e., foolproof)
    # * automatically adjusts to bundle changes
    # * explicit (the command in your history is "bundle exec whatever", and it's
    # a nice reminder that bundler magic is happening with those commands)
    #
  14. @glv glv created this gist Sep 19, 2017.
    93 changes: 93 additions & 0 deletions bundler-exec.fish
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    # This sets up fish so that, if you type a command that should be
    # run using bundler, it first automatically prepends "bundle exec"
    # to the command line buffer before executing it. Works for all
    # commands found in the "bin" directory of the current bundle.
    #
    # To override this behavior and run such a command without bundler,
    # prefix with "command" (e.g., "command rake -T")
    #
    # Pros (vs binstubs or aliases):
    # * never needs updating (i.e., foolproof)
    # * explicit (the command in your history is "bundle exec whatever", and it's
    # a nice reminder that bundler magic is happening with those commands)
    #
    # Cons:
    # * Might be surprising at first ("I didn't type that!")
    # * Slows down cd and other directory change operations a bit (to recompute
    # the RUN_WITH_BUNDLER variable)
    #
    # Configuration:
    #
    # 1. Drop this file in ~/.config/fish/bundler-exec.fish
    #
    # 2. In .config/fish/config.fish, include these lines:
    #
    # if test -f ~/.config/fish/bundler-exec.fish
    # source ~/.config/fish/bundler-exec.fish
    # end
    #
    # 3. Add ~/.config/fish/functions/fish_user_key_bindings.fish with
    # these lines:
    #
    # function fish_user_key_bindings
    # if functions -q _maybe-add-bundle-exec-and-execute
    # bind \r _maybe-add-bundle-exec-and-execute
    # end
    # end
    #
    # (or just add the inner if/end block to that function if you're
    # already defining it).

    begin
    # Support Functions

    function _bundler-installed
    which bundle > /dev/null 2>&1
    end

    function _within-bundled-project
    set -l dir (pwd)
    while test (dirname $dir) != "/"
    if test -f "$dir/Gemfile" -o -f "$dir/gems.rb"
    return
    end
    set -l dir (dirname $dir)
    end
    false
    end

    # Keep RUN_WITH_BUNDLER updated on directory changes
    function _discover_bundle --on-variable PWD
    if _bundler-installed
    and _within-bundled-project

    # This version is slow but simple and safe
    # set -l bundler_path (bundle exec gem environment path)

    # This version is faster but complex and probably brittle. If
    # you're seeing flaky behavior around all of this, switch to the
    # slow version above and see if that fixes it.
    set -l bundler_path (bundle config path | sed -e '/^Set /!d;s/^.*: "\(.*\)".*$/\1/')

    set -l bundler_bin "$bundler_path/bin"
    set -gx RUN_WITH_BUNDLER (command ls $bundler_bin)
    else
    set -ge RUN_WITH_BUNDLER
    end
    end

    # Before executing the current commandline buffer, prepend
    # "bundle exec" if the first token is one of the commands in
    # RUN_WITH_BUNDLER.
    #
    # (This depends on _discover_bundle running correctly on each
    # directory change, and on \r being re-bound to this function in
    # fish_user_key_bindings.)
    function _maybe-add-bundle-exec-and-execute
    set -l toks (commandline --tokenize)
    if contains $toks[1] $RUN_WITH_BUNDLER
    commandline "bundle exec $toks"
    end
    commandline -f execute
    end
    end