Skip to content
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
Closed

Optionally support auto-switching #40

postmodern opened this issue Dec 12, 2012 · 35 comments
Assignees
Milestone

Comments

@postmodern
Copy link
Owner

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
Copy link
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
Copy link
Owner Author

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
Copy link
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
Copy link
Contributor

👍

postmodern added a commit that referenced this issue Dec 13, 2012
* This has the added benefit of detecting a new .ruby-version after a
  `git pull` or `echo "1.9" > .ruby-version`.
@postmodern
Copy link
Owner Author

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.

@ku1ik
Copy link
Contributor

ku1ik commented Dec 13, 2012

Can't wait for it!

@postmodern
Copy link
Owner Author

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
* 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
Copy link
Contributor

mpapis commented Dec 14, 2012

just added rvm/rvm#1388 - works great

@wilmoore
Copy link
Contributor

Just commented on one of the tests.

@postmodern
Copy link
Owner Author

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
Copy link
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
Copy link
Owner Author

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

@postmodern
Copy link
Owner Author

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

@postmodern
Copy link
Owner Author

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

@wilmoore
Copy link
Contributor

"${dir%/*}" 👍

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

@mpapis
Copy link
Contributor

mpapis commented Dec 17, 2012

assertNull in shell ;)

@postmodern
Copy link
Owner Author

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

@mpapis
Copy link
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
Copy link
Owner Author

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
Copy link
Contributor

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

@postmodern
Copy link
Owner Author

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
Copy link
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
Copy link
Contributor

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

@mpapis
Copy link
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
Copy link
Owner Author

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
Copy link
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
* This allows existing PROMPT_COMMANDs to check $?, before chruby_auto
  is invoked.
@ku1ik
Copy link
Contributor

ku1ik 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
Copy link
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?

@mpapis
Copy link
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
Copy link
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.

@postmodern
Copy link
Owner Author

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
Copy link
Contributor

Appears to work as requested.

@postmodern
Copy link
Owner Author

Without further ado, merging this into 0.3.0 branch.

@postmodern
Copy link
Owner Author

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

@postmodern
Copy link
Owner Author

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
Projects
None yet
Development

No branches or pull requests

6 participants