chruby icon indicating copy to clipboard operation
chruby copied to clipboard

.ruby-version support for chruby-exec

Open ichilton opened this issue 11 years ago • 30 comments

Hi,

Have you thought about adding support for reading the ruby version from .ruby-version to chruby-exec?

Thanks,

Ian

ichilton avatar Oct 11 '13 11:10 ichilton

What would the use case be? cd /project; command should be enough to trigger auto-loading?

postmodern avatar Oct 11 '13 23:10 postmodern

The use case is whatever the use case for chruby-exec is, but without the need to specify the version manually.

i.e some people like @regularfry don't use the functions in chruby.sh, they use chruby-exec to load a shell with the ruby environment defined, as detailed: [http://www.blackkettle.org/blog/2013/08/13/how-i-ruby-part-1-development/](in his blog post).

This would mean rather than:

chruby-exec 1.9.3-p429 -- bash

you could do:

chruby-exec -- bash

...and it would automatically detect the version from .ruby-version the same way auto.sh does.

My other use case would be for scripts/cron jobs etc running gem binaries - I want to be able to do: chruby-exec -- commandhere and have it automatically pick the right ruby version from .ruby-version, rather than having to specify it, which means when I change ruby versions on a project, i've got to remember all the places i've hardcoded the version - like init scripts, cron jobs etc.

Thanks,

Ian

ichilton avatar Oct 12 '13 07:10 ichilton

Why can't you do cd /project && $SHELL or bash -l -c 'cd /project && ./bin/foo'?

postmodern avatar Oct 12 '13 08:10 postmodern

To put this into perspective, this is all that chruby-exec does under the hood: https://github.com/postmodern/chruby/blob/master/bin/chruby-exec#L36-L40 Perhaps we could add a chruby-auto-exec command, but I think it's possibly only using the auto.sh hooks that run before each command.

postmodern avatar Oct 12 '13 08:10 postmodern

I could, but it would just be good to be able to do: "cd /project && chruby-exec -- cmd" without having to have chruby +auto loaded in the profile and worrying about whether it had loaded in that instance.

A chruby-auto-exec would be cool.

It could obviously use/share the .ruby-version parsing code from auto.sh so should be quite simple?

ichilton avatar Oct 12 '13 09:10 ichilton

+1 it'd be great to have something like rvm in <path> do <command>

tamird avatar Oct 19 '13 21:10 tamird

That would be great!

ichilton avatar Nov 08 '13 14:11 ichilton

@tamird how would that be different from cd path && command?

postmodern avatar Nov 08 '13 23:11 postmodern

@postmodern it would be the same, but would not require a "special" shell where chruby/auto.sh has been sourced.

tamird avatar Nov 08 '13 23:11 tamird

Yeah, it would be much neater for doing scripts, startup config, cron jobs etc - you just tell chruby you want to execute this command it would sort loading the right ruby version.

Even better, would be the ability to read a .env file and also setup some config environments, but I suspect that might be further than you want to go.

ichilton avatar Nov 11 '13 09:11 ichilton

For starting cron jobs, you really should specify which SHELL you want to run the command under, and have everything preconfigured. Otherwise, you become depending on an rvm-like command for the environment.

postmodern avatar Nov 11 '13 10:11 postmodern

What do you mean by have everything preconfigured? - have an example?

ichilton avatar Nov 11 '13 10:11 ichilton

If you want your cron job to run in an ideal shell environment, run $SHELL -l -c "..." with any additional configuration at the top of the cron file or in the shell's dot-files.

postmodern avatar Nov 11 '13 10:11 postmodern

True, chruby-exec hides the $SHELL -l -c '...' part, but it still requires you set SHELL and have your shell configured as desired.

postmodern avatar Nov 11 '13 11:11 postmodern

Yep, agree with this for env's. but it would still be much neater for chruby-exec to pick the ruby version up from .ruby-version

ichilton avatar Nov 11 '13 11:11 ichilton

What about users who do not want auto-switching ever enabled?

postmodern avatar Nov 11 '13 12:11 postmodern

They wouldn't enable it? - you were talking above of having a separate command - chruby-auto-exec ?

ichilton avatar Nov 11 '13 14:11 ichilton

To avoid adding yet another command, I was thinking of adding a --auto option. There needs to be a way for users to explicitly specify they want to use .ruby-version.

postmodern avatar Nov 11 '13 19:11 postmodern

That sounds great. Thanks @postmodern !

tamird avatar Nov 11 '13 19:11 tamird

I'm still on the fence about this feature, as it seems we're shoe horning additional functionality into chruby-exec. Perhaps we should add a chruby-shell, to run commands with chruby pre-loaded.

postmodern avatar Nov 11 '13 20:11 postmodern

Er, turn chruby-exec into something like chruby-shell.

chruby-exec --auto -- "cd /srv/my_app && ruby command here"

postmodern avatar Nov 11 '13 20:11 postmodern

@postmodern seems like a nice, general solution to the "I need this behavior for this command" problem.

tamird avatar Nov 11 '13 20:11 tamird

@tamird better description would be when you need to use chruby, but can't modify the shell's configuration to load chruby.sh and auto.sh.

postmodern avatar Nov 11 '13 20:11 postmodern

sure :)

tamird avatar Nov 11 '13 20:11 tamird

Does there need to be .ruby-version support at the chruby level at all? Seems like something a wrapper should be handling.

regularfry avatar Nov 11 '13 20:11 regularfry

@regularfry what do you mean by wrapper?

postmodern avatar Nov 11 '13 20:11 postmodern

@regularfry I guess that's why it's implemented separately in auto.sh - if you don't want that functionality, you don't have to load it and it's not slowing plain chruby (or chruby-exec as you use) down by checking it...

ichilton avatar Nov 11 '13 22:11 ichilton

@postmodern --auto sounds good!

ichilton avatar Nov 11 '13 22:11 ichilton

has there been any progress here?

tamird avatar Dec 01 '14 17:12 tamird

I know it's been about 7 years, but this feature request would still be useful. 8)

mullermp avatar Aug 31 '21 20:08 mullermp