且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 os x 服务器上通过 ssh 更改 rvm gemset

更新时间:2023-09-30 10:36:10

好吧,毕竟.

def exec_via_bash(line)
    puts %Q(#{line})
    exec = 'bash -c "#{line}"'
    puts `#{exec}`
end

def RubySetup
    # reload bash profile
    homedir = ENV['HOME']
    exec_via_bash %Q(source #{homedir}/.bash_profile);

    exec_via_bash %Q(source #{homedir}/.bashrc);

    # reload ruby
    exec_via_bash %Q(source #{homedir}/.rvm/scripts/rvm);

    ruby_setup = %Q([[ -s "#{homedir}/.rvm/scripts/rvm" ]] && source "#{homedir}/.rvm/scripts/rvm")

    puts ruby_setup
    ruby_setup
end

if ARGV.empty?
    puts "there is not enough arguments passed. maybe you forget ruby file to exec?"
    exit(1)
end

ruby_script_path = ARGV.shift;

exec_file_absolute_path = File.expand_path(ruby_script_path)

unless File.exists? exec_file_absolute_path
    puts "file #{exec_file_absolute_path} doesn't exists!"
    exit(1)
end

exec_file_directory = File.dirname(exec_file_absolute_path)
exec_bundle = %Q'bundle exec ruby #{exec_file_absolute_path}' + ' ' + ARGV.join(' ')



# change directory
Dir.chdir(exec_file_directory);
# print %x(ls);

# configure gemset name
version = %x(cat .ruby-version).strip;
gemset = %x(cat .ruby-gemset).strip;
change_rvm_gemset = %Q(rvm use #{version}\@#{gemset});

ruby_setup = RubySetup()

exec_bash_login_line = [ruby_setup, change_rvm_gemset, exec_bundle].join ' && ';
puts 'exec bash login line: ' + exec_bash_login_line
forced = %Q(bash --login -c '#{exec_bash_login_line}');
puts forced, "\n";
puts %x(#{forced});

好吧,这个脚本不是一种美,但效果很好.

ok, this script is not a kind of beauty, but it works well.

使用示例?

ruby script.rb ~/bla/bla/bla/run_your_program.rb --first_argument --second_argument a,b,c --etc

正如我之前所说:

我已经通过 ssh 在服务器上.

I've already on the server via ssh.

所以,我需要通过 launchd 运行脚本.

So, I need to run scripts via launchd.

我应该这样做

# part of launchd worker
<string>bash</string>
<string>-c</string>
<string>ruby ~/PathToCharmScript.rb -r a</string>

附:

请帮助我为其他人改进此脚本!

Please, help me with improvements of this script for others!

这是一个要点:wow_this_works