Created
December 24, 2012 03:17
-
-
Save ywjno/4367303 to your computer and use it in GitHub Desktop.
set system envs by ruby shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'rubygems' | |
configs = { | |
'envs' => | |
{'RUBY_HOME' => 'ruby_home_path', 'JRUBY_HOME' => 'jruby_home_path', 'JAVA_HOME' => 'java_home_path'}, | |
'add_path_env' => | |
{'RUBY_HOME' => 'bin', 'JRUBY_HOME' => 'bin', 'JAVA_HOME' => 'bin'} | |
} | |
host_os = RbConfig::CONFIG['host_os'] | |
if host_os =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/ | |
reg_path = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment" | |
desc "Set envs and add path" | |
task :all => ['windows:add_path', 'windows:set_evn'] | |
desc "Set envs" | |
task :set_evn do | |
configs['envs'].each do |key, value| | |
system("reg add \"#{reg_path}\" /v \"#{key}\" /d \"#{value}\" /f > nul") | |
end | |
puts "Set envs over." | |
end | |
desc "Add path" | |
task :add_path do | |
path = ENV["path"] | |
configs['add_path_env'].keys.reverse.each do |key| | |
path = "%#{key}%\\#{configs['add_path_env'][key]};" + path | |
end | |
system("reg add \"#{reg_path}\" /v \"test_path\" /d \"#{path}\" /f > nul") | |
puts "Add path over." | |
end | |
end | |
if host_os =~ /linux|darwin|mac os/ | |
shell_file = ENV["SHELL"].split("/")[-1] == "zsh" ? "~/.zshrc" : "~/.bashrc" | |
desc "Set envs and add path" | |
task :all => ['linux:set_evn', 'linux:add_path'] | |
desc "Set envs" | |
task :set_evn do | |
File.open(shell_file, 'a') do |file| | |
configs['envs'].each do |key, value| | |
file.puts "#{key}=#{value}" | |
end | |
end | |
puts "Set envs over." | |
end | |
desc "Add path" | |
task :add_path do | |
path = "$PATH" | |
File.open(shell_file, 'a') do |file| | |
configs['add_path_env'].keys.reverse.each do |key| | |
path = "$#{key}/#{configs['add_path_env'][key]}:" + path | |
end | |
file.puts path | |
end | |
puts "Add path over." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment