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

Optionally support auto-switching #40

Closed
postmodern opened this Issue Dec 12, 2012 · 35 comments

Comments

Projects
None yet
6 participants
@postmodern
Owner

postmodern commented Dec 12, 2012

Many users have requested the ability to automatically switch to a Ruby when entering a directory which contains a .ruby-version file. While in the directory and all sub-directories, the Ruby should remain set. Only when the user ascends the directory containing the .ruby-version file should chruby switch back to system Ruby.

This feature is optional and would be enabled by loading /usr/local/share/chruby/auto.sh.

@mpapis

This comment has been minimized.

Show comment
Hide comment
@mpapis

mpapis Dec 12, 2012

Contributor

also consider using http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html instead to avoid cd overwrite conflicts.

Contributor

mpapis commented Dec 12, 2012

also consider using http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html instead to avoid cd overwrite conflicts.

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 13, 2012

Owner

I assume you'd do something like PROMPT_COMMAND="chruby_auto; $PROMPT_COMMAND"? Other than the fact the command runs on every new prompt (meaning chruby_auto would have to return quickly if no action is needed), are there any other drawbacks?

Owner

postmodern commented Dec 13, 2012

I assume you'd do something like PROMPT_COMMAND="chruby_auto; $PROMPT_COMMAND"? Other than the fact the command runs on every new prompt (meaning chruby_auto would have to return quickly if no action is needed), are there any other drawbacks?

@mpapis

This comment has been minimized.

Show comment
Hide comment
@mpapis

mpapis Dec 13, 2012

Contributor

I think yes, to speed up you can do:

export last_chruby_pwd=""
chruby_auto_prompt(){
  [[ "$last_chruby_pwd" == "$PWD" ]] || {
    chruby_auto
    last_chruby_pwd="$PWD"
  }
}
Contributor

mpapis commented Dec 13, 2012

I think yes, to speed up you can do:

export last_chruby_pwd=""
chruby_auto_prompt(){
  [[ "$last_chruby_pwd" == "$PWD" ]] || {
    chruby_auto
    last_chruby_pwd="$PWD"
  }
}
@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Dec 13, 2012

Contributor

👍

Contributor

steveklabnik commented Dec 13, 2012

👍

postmodern added a commit that referenced this issue Dec 13, 2012

Use precmd_functions/PROMPT_COMMAND in auto.sh (issue #40).
* This has the added benefit of detecting a new .ruby-version after a
  `git pull` or `echo "1.9" > .ruby-version`.
@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 13, 2012

Owner

Now using precmd_functions and PROMPT_COMMAND. This has the added benefit of automatically detecting new .ruby-version files after git pull or user creation.

Owner

postmodern commented Dec 13, 2012

Now using precmd_functions and PROMPT_COMMAND. This has the added benefit of automatically detecting new .ruby-version files after git pull or user creation.

@sickill

This comment has been minimized.

Show comment
Hide comment
@sickill

sickill Dec 13, 2012

Contributor

Can't wait for it!

Contributor

sickill commented Dec 13, 2012

Can't wait for it!

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 14, 2012

Owner

Coming in 0.3.0. But first I need to release 0.2.5, with a brand new setup.sh and installation instructions that don't rely on @github's deprecated Downloads. Also still have to write extensive tests for auto-switching.

Owner

postmodern commented Dec 14, 2012

Coming in 0.3.0. But first I need to release 0.2.5, with a brand new setup.sh and installation instructions that don't rely on @github's deprecated Downloads. Also still have to write extensive tests for auto-switching.

postmodern added a commit that referenced this issue Dec 14, 2012

Added tests for chruby_auto (issue #40).
* Due to PROMPT_COMMAND and precmd not being called in non-interactive
  mode, I had to explicitly call chruby_auto after every cd invocation.
@mpapis

This comment has been minimized.

Show comment
Hide comment
@mpapis

mpapis Dec 14, 2012

Contributor

just added rvm/rvm#1388 - works great

Contributor

mpapis commented Dec 14, 2012

just added rvm/rvm#1388 - works great

@wilmoore

This comment has been minimized.

Show comment
Hide comment
@wilmoore

wilmoore Dec 15, 2012

Contributor

Just commented on one of the tests.

Contributor

wilmoore commented Dec 15, 2012

Just commented on one of the tests.

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 15, 2012

Owner

Copy/pasting my comment so no one missing it.

To avoid ascending each directory in search of a .ruby-version (I assume this would degrade performance) after every command, I decided to require the user cd into the project root.

Thoughts?

Owner

postmodern commented Dec 15, 2012

Copy/pasting my comment so no one missing it.

To avoid ascending each directory in search of a .ruby-version (I assume this would degrade performance) after every command, I decided to require the user cd into the project root.

Thoughts?

@mpapis

This comment has been minimized.

Show comment
Hide comment
@mpapis

mpapis Dec 15, 2012

Contributor

descending directories should not have any impact on performance, unix/linux does cache filesystem calls, so first call to a new directory might be slightly slower (like double disk seek time) but otherwise it's fast - tested it with RVM - you do not see the difference

Contributor

mpapis commented Dec 15, 2012

descending directories should not have any impact on performance, unix/linux does cache filesystem calls, so first call to a new directory might be slightly slower (like double disk seek time) but otherwise it's fast - tested it with RVM - you do not see the difference

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 15, 2012

Owner

Alright, I will add an ascending search for .ruby-version, if RUBY_VERSIONED_DIR isn't already set.

Owner

postmodern commented Dec 15, 2012

Alright, I will add an ascending search for .ruby-version, if RUBY_VERSIONED_DIR isn't already set.

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 17, 2012

Owner

OK, I think I covered all the edge-cases. Please review.

Owner

postmodern commented Dec 17, 2012

OK, I think I covered all the edge-cases. Please review.

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 17, 2012

Owner

Hmm, @github didn't auto-link to commit bf69302. Link to source and tests.

Owner

postmodern commented Dec 17, 2012

Hmm, @github didn't auto-link to commit bf69302. Link to source and tests.

@wilmoore

This comment has been minimized.

Show comment
Hide comment
@wilmoore

wilmoore Dec 17, 2012

Contributor

"${dir%/*}" 👍

That said; I couldn't figure out how this protects from infinitely looping once / is reached.

Contributor

wilmoore commented Dec 17, 2012

"${dir%/*}" 👍

That said; I couldn't figure out how this protects from infinitely looping once / is reached.

@mpapis

This comment has been minimized.

Show comment
Hide comment
@mpapis

mpapis Dec 17, 2012

Contributor

assertNull in shell ;)

Contributor

mpapis commented Dec 17, 2012

assertNull in shell ;)

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 17, 2012

Owner

Realized that the current implementation will not allow overriding the .ruby-version with an explicit call to chruby. Is this acceptable behaviour?

Owner

postmodern commented Dec 17, 2012

Realized that the current implementation will not allow overriding the .ruby-version with an explicit call to chruby. Is this acceptable behaviour?

@mpapis

This comment has been minimized.

Show comment
Hide comment
@mpapis

mpapis Dec 17, 2012

Contributor

I do not thinks so, it's quite important to allow switching to something else, especially to try other rubies with your project

Contributor

mpapis commented Dec 17, 2012

I do not thinks so, it's quite important to allow switching to something else, especially to try other rubies with your project

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 17, 2012

Owner

Next question, if a user explicitly selects a Ruby, should entering a project with a .ruby-version file override the already selected Ruby. Trying to avoid a chicken & egg problem between chruby and chruby_auto.

Owner

postmodern commented Dec 17, 2012

Next question, if a user explicitly selects a Ruby, should entering a project with a .ruby-version file override the already selected Ruby. Trying to avoid a chicken & egg problem between chruby and chruby_auto.

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Dec 17, 2012

Contributor

I would want entering the directory to set it to the version in .ruby-version regardless of what I've previously chosen.

Contributor

steveklabnik commented Dec 17, 2012

I would want entering the directory to set it to the version in .ruby-version regardless of what I've previously chosen.

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 17, 2012

Owner

Ah, my mistake. Due to the if [[ "$version_file" == "$RUBY_VERSION_FILE" ]]; then return check, we allow the user to override .ruby-version while within the project. I will sleep on this and then merge into the 0.3.0 branch, if there's no other comments.

Owner

postmodern commented Dec 17, 2012

Ah, my mistake. Due to the if [[ "$version_file" == "$RUBY_VERSION_FILE" ]]; then return check, we allow the user to override .ruby-version while within the project. I will sleep on this and then merge into the 0.3.0 branch, if there's no other comments.

@pushcx

This comment has been minimized.

Show comment
Hide comment
@pushcx

pushcx Dec 17, 2012

Contributor

I think .ruby-version is not the best name for this. It's a file specific only to chruby, and the contents are its name for your ruby (rather than some version specification that gets mapped to your ruby). In the future, it might have more info and thus be even less human-readable and generic.

I'd like to suggest .chruby or .chruby-version, similar to .rvm and .rbenv. I can put together a pull request if folks think this is a good idea and prefer one.

(And since this is the first time I've popped up on the project; thanks for making chruby, I switched a few days ago and have been really enjoying it.)

Contributor

pushcx commented Dec 17, 2012

I think .ruby-version is not the best name for this. It's a file specific only to chruby, and the contents are its name for your ruby (rather than some version specification that gets mapped to your ruby). In the future, it might have more info and thus be even less human-readable and generic.

I'd like to suggest .chruby or .chruby-version, similar to .rvm and .rbenv. I can put together a pull request if folks think this is a good idea and prefer one.

(And since this is the first time I've popped up on the project; thanks for making chruby, I switched a few days ago and have been really enjoying it.)

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Dec 17, 2012

Contributor

Isn't .ruby-version supported by both rvm and rbenv?

Contributor

steveklabnik commented Dec 17, 2012

Isn't .ruby-version supported by both rvm and rbenv?

@mpapis

This comment has been minimized.

Show comment
Hide comment
@mpapis

mpapis Dec 17, 2012

Contributor

.ruby-version is supported by rvm and rbfu, there is a ticket for rbenv rbenv/rbenv#199 / rbenv/rbenv#201

Contributor

mpapis commented Dec 17, 2012

.ruby-version is supported by rvm and rbfu, there is a ticket for rbenv rbenv/rbenv#199 / rbenv/rbenv#201

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 18, 2012

Owner

I do agree that the name .ruby-version implies a specific version (which is tedious) compared to fuzzy matching against a canonical Ruby name (name-version). Adding a chruby specific dot-file would increase dot-file proliferation and promote factionalism amongst Rubyists; adding tool specific dot-files to repos is the developer equivalent of flying your colors.

Owner

postmodern commented Dec 18, 2012

I do agree that the name .ruby-version implies a specific version (which is tedious) compared to fuzzy matching against a canonical Ruby name (name-version). Adding a chruby specific dot-file would increase dot-file proliferation and promote factionalism amongst Rubyists; adding tool specific dot-files to repos is the developer equivalent of flying your colors.

@pushcx

This comment has been minimized.

Show comment
Hide comment
@pushcx

pushcx Dec 18, 2012

Contributor

I was totally unaware that was a shared standard, that's wonderful. I retract my suggestion. :)

Contributor

pushcx commented Dec 18, 2012

I was totally unaware that was a shared standard, that's wonderful. I retract my suggestion. :)

postmodern added a commit that referenced this issue Dec 18, 2012

Safely append to PROMPT_COMMAND (issue #40).
* This allows existing PROMPT_COMMANDs to check $?, before chruby_auto
  is invoked.
@sickill

This comment has been minimized.

Show comment
Hide comment
@sickill

sickill Dec 18, 2012

Contributor

I'm with @mpapis on ability to override version specified in .ruby-version with call to chruby. That's not uncommon use-case.

Contributor

sickill commented Dec 18, 2012

I'm with @mpapis on ability to override version specified in .ruby-version with call to chruby. That's not uncommon use-case.

@wilmoore

This comment has been minimized.

Show comment
Hide comment
@wilmoore

wilmoore Dec 18, 2012

Contributor

What should ruby -v be after the following scenario?

  • cd into a directory with a .ruby-version file, thus $(ruby -v) -eq "rubinius-2.0.0-rc1".
  • direct call chruby ruby-1.9.3 w/ fuzzy match; $(ruby -v) -eq "ruby-1.9.3-p327"
  • cd tests -- what should ruby -v be now?
Contributor

wilmoore commented Dec 18, 2012

What should ruby -v be after the following scenario?

  • cd into a directory with a .ruby-version file, thus $(ruby -v) -eq "rubinius-2.0.0-rc1".
  • direct call chruby ruby-1.9.3 w/ fuzzy match; $(ruby -v) -eq "ruby-1.9.3-p327"
  • cd tests -- what should ruby -v be now?
@mpapis

This comment has been minimized.

Show comment
Hide comment
@mpapis

mpapis Dec 18, 2012

Contributor

@wilmoore I think it should be still what user picked in second step ruby-1.9.3-p327 - it should be reset only when user leaves context of current project / .ruby-version

Contributor

mpapis commented Dec 18, 2012

@wilmoore I think it should be still what user picked in second step ruby-1.9.3-p327 - it should be reset only when user leaves context of current project / .ruby-version

@wilmoore

This comment has been minimized.

Show comment
Hide comment
@wilmoore

wilmoore Dec 18, 2012

Contributor

it should be reset only when user leaves context of current project / .ruby-version

In the above scenario, when the user did cd tests, she moved from the root of the project to a descendant directory (still part of the project). That being the case, what takes precedent.

Contributor

wilmoore commented Dec 18, 2012

it should be reset only when user leaves context of current project / .ruby-version

In the above scenario, when the user did cd tests, she moved from the root of the project to a descendant directory (still part of the project). That being the case, what takes precedent.

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 18, 2012

Owner

Here's an example of the current behaviour:

[hal@tank ronin]$ pwd
/vault/1/code/ronin
[hal@tank ronin]$ cat ronin/.ruby-version 
1.9
[hal@tank ronin]$ cd ronin
[hal@tank ronin]$ chruby
   jruby-1.7.0
   rubinius-2.0.0-rc1
 * ruby-1.9.3-p327
[hal@tank ronin]$ cd lib
[hal@tank lib]$ chruby
   jruby-1.7.0
   rubinius-2.0.0-rc1
 * ruby-1.9.3-p327
[hal@tank lib]$ chruby rubinius
[hal@tank lib]$ chruby
   jruby-1.7.0
 * rubinius-2.0.0-rc1
   ruby-1.9.3-p327
[hal@tank lib]$ cd ronin/ui/cli/
[hal@tank cli]$ chruby
   jruby-1.7.0
 * rubinius-2.0.0-rc1
   ruby-1.9.3-p327
[hal@tank cli]$ cd ../../../../
[hal@tank ronin]$ pwd
/vault/1/code/ronin/ronin
[hal@tank ronin]$ chruby
   jruby-1.7.0
 * rubinius-2.0.0-rc1
   ruby-1.9.3-p327
[hal@tank ronin]$ cd ..
[hal@tank ronin]$ chruby
   jruby-1.7.0
   rubinius-2.0.0-rc1
   ruby-1.9.3-p327
Owner

postmodern commented Dec 18, 2012

Here's an example of the current behaviour:

[hal@tank ronin]$ pwd
/vault/1/code/ronin
[hal@tank ronin]$ cat ronin/.ruby-version 
1.9
[hal@tank ronin]$ cd ronin
[hal@tank ronin]$ chruby
   jruby-1.7.0
   rubinius-2.0.0-rc1
 * ruby-1.9.3-p327
[hal@tank ronin]$ cd lib
[hal@tank lib]$ chruby
   jruby-1.7.0
   rubinius-2.0.0-rc1
 * ruby-1.9.3-p327
[hal@tank lib]$ chruby rubinius
[hal@tank lib]$ chruby
   jruby-1.7.0
 * rubinius-2.0.0-rc1
   ruby-1.9.3-p327
[hal@tank lib]$ cd ronin/ui/cli/
[hal@tank cli]$ chruby
   jruby-1.7.0
 * rubinius-2.0.0-rc1
   ruby-1.9.3-p327
[hal@tank cli]$ cd ../../../../
[hal@tank ronin]$ pwd
/vault/1/code/ronin/ronin
[hal@tank ronin]$ chruby
   jruby-1.7.0
 * rubinius-2.0.0-rc1
   ruby-1.9.3-p327
[hal@tank ronin]$ cd ..
[hal@tank ronin]$ chruby
   jruby-1.7.0
   rubinius-2.0.0-rc1
   ruby-1.9.3-p327
@wilmoore

This comment has been minimized.

Show comment
Hide comment
@wilmoore

wilmoore Dec 19, 2012

Contributor

Appears to work as requested.

Contributor

wilmoore commented Dec 19, 2012

Appears to work as requested.

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 19, 2012

Owner

Without further ado, merging this into 0.3.0 branch.

Owner

postmodern commented Dec 19, 2012

Without further ado, merging this into 0.3.0 branch.

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 19, 2012

Owner

Merged. Any pre-release comments/commits should be directed towards the 0.3.0 branch.

Owner

postmodern commented Dec 19, 2012

Merged. Any pre-release comments/commits should be directed towards the 0.3.0 branch.

@postmodern postmodern closed this Dec 19, 2012

@postmodern

This comment has been minimized.

Show comment
Hide comment
@postmodern

postmodern Dec 21, 2012

Owner

This has been released in 0.3.0.

Owner

postmodern commented Dec 21, 2012

This has been released in 0.3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment