1

Background: I wrote a short script in MRI 1.9.2p320 for updating all my projects – http://git.io/eez8Kg . Tool which i utilize for the updating/pulling itself (git-up) is also able to automatically bundle project after it discover changed Gemfile. The problem is, that when i change directory in the Ruby script, it does not respect rvmrc/gemset of projects (each project has it's own gemset).

Question: How can i achieve to make cd in Ruby script behave same as in my terminal

I've tried:

  • replace Dir.chdir with cd command in the `s
  • replace Dir.chdir with cd command in the `s prepended with zsh -ci

Code:

Find.find('../..') do |f|
  if f =~ /\.git$/
    repository = f.gsub /\.git$/, ''
    Logger.new(STDOUT).info('Checking '){repository}
    Dir.chdir(repository){
      puts `git up`
    }
  end
end
Mailo Světel
  • 24,002
  • 5
  • 30
  • 40
  • What's the result of puts `pwd` Is it what you expect? I believe jruby 1.7.0 and 1.7.1 do not work as expected, not sure about MRI. – kstevens715 Jan 07 '13 at 11:11
  • Pwd should be in backticks – kstevens715 Jan 07 '13 at 11:11
  • It's mri 1.9.2p320. It's changing the directories, but it does not change gemset – Mailo Světel Jan 07 '13 at 12:29
  • Yes i could, but it would be writing gemset changing again. I want to get working what is already done – Mailo Světel Jan 07 '13 at 13:04
  • Uhm,it could be dynamically done.. rvm is wrapping some stuff around the cd function and I'm pretty sure mri isn't able to access this since it's most likely bash specific. I mean you only need a handful of codeline to read the rvmrc and use the gemset out of it. It's basicly what your .rvmrc does. – gulty Jan 07 '13 at 13:08
  • I appreciate you all are trying to help. But i don't want workaround, i want to get working what is working somewhere else – Mailo Světel Jan 07 '13 at 13:15
  • Okay, let me explain it to you again: RVM is using a Wrapperfunction like this: cd () { builtin cd "$@"; local result=$?; __rvm_project_rvmrc; __rvm_after_cd; return $result } <-- Now my question: How did you even load RVM within your rubyscript? You know you have to initiate RVM when starting your shell by adding the PATH. And this: [ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function <---- If you use your script rvm won't load properly and you wont have those functions unless you somehow initiate them.. – gulty Jan 07 '13 at 13:57
  • gulty: I don't and don't want to load rvm in the ruby script since there is already configured shell to do that. On IRC some guy proposed Open3 would solve my issue and it seems it does. I'll try it at evening more deeply – Mailo Světel Jan 07 '13 at 16:12

1 Answers1

0

Open3.html#popen3 FTW

zshin, zshout, zsherr = Open3.popen3('zsh') # Open new shell
zshin.puts 'cd ~/Projects/superimportant_project' # Change directory
zshin.puts 'rvm-prompt' # Verify rvmrc is respected
zshin.puts 'exit' # Exit the shell
while (line = zshout.gets)
  puts line
end

Exiting the shell is not mandatory, but for example rvm-prompt stayed hanging, without letting nothing else happen. If you try same thing with ls, you not gonna need the exit line.

Mailo Světel
  • 24,002
  • 5
  • 30
  • 40