muto 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in muto.gemspec
4
+ gemspec
data/README.textile ADDED
@@ -0,0 +1,133 @@
1
+ h1. Overview
2
+
3
+ Muto is a lightweight little ruby/vbs/bat script that will allow you switch to another version of Ruby while also allowing you to alter Windows Environment
4
+ variables at the same time. You can do this with Muto without having to reload your command prompt/shell and all gem paths associated with the chosen
5
+ version of ruby are automatically updated as you switch versions.
6
+
7
+ h2. Why I put this together..
8
+
9
+ Firstly, I'm not trying to take anything from Pik, check it out first if you haven't already, there's a thousand things you can do with it and it might be
10
+ better suited to what your needs are: https://github.com/vertiginous/pik
11
+
12
+ I put Muto together because I needed to also frequently change a few environment variables every time I changed my version of ruby and I just got sick
13
+ of having to do it all the time and then having to reload my command prompt as well just to pick up the changes. Also I needed to be able to switch
14
+ ruby versions and persist the change to another version of Ruby across my entire Windows environment.
15
+
16
+ h2. Example Usage
17
+
18
+ <pre>
19
+ #From the command line
20
+
21
+ muto
22
+ =>Expected format: muto [ruby_version]
23
+ =>
24
+ =>Available Versions are:
25
+ => 186 ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
26
+ => 192 ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
27
+ =>
28
+ =>Currently using:
29
+ =>ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
30
+
31
+ ruby -v
32
+ =>ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
33
+
34
+ muto 192
35
+ =>System updated. Now using:
36
+ =>ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
37
+
38
+ ruby -v
39
+ =>ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
40
+ </pre>
41
+
42
+ h2. How Muto works..
43
+
44
+ Muto works by using an executable .bat file to call a ruby script which will alter your systems environment variables and send an environment changed
45
+ signal to your Windows environment. It'll then finish by calling a basic vb script to update the shell you're working in so that you won't need to
46
+ restart your command prompt.
47
+
48
+ The actual muto gem is not really a gem at all.. It's just basically a wrapper to help copy the muto scripts to your machine somewhere.
49
+
50
+
51
+ h2. Setup/Config
52
+
53
+ Use a normal Windows MRI version of Ruby to install Muto.
54
+ <pre>gem install muto</pre>
55
+
56
+ From the command line, change into a directory where you want to keep the scripts and run: extract_muto
57
+
58
+ <pre>cd c:\
59
+ extract_muto
60
+ #=>Extracting to C:\Muto
61
+ #=>Generating C:\Muto\muto.rb
62
+ #=>Generating C:\Muto\reset_command_line.vbs
63
+ #=>Generating C:\Muto\ruby_versions.yml
64
+ #=>Writing C:\Muto\muto.bat file..
65
+ #=>
66
+ #=>Add Ruby versions to your C:\Muto\ruby_versions.yml"
67
+ #=>Then add C:\Muto to your Windows PATH environment variable and you're good to go
68
+ </pre>
69
+
70
+
71
+ h3. ruby_versions.yml
72
+
73
+ As well as being able to switch to other versions of Ruby in your Windows environment, you can also define other User Environment Variables to change at
74
+ the same time. Muto switches versions of Ruby by just regex-ing out the ruby bin folder out of your Windows PATH variable and replacing it with the new
75
+ version you choose. This way none of the other paths in your environments PATH variable are altered.
76
+
77
+ The User Environment Variables are set completely differently! The values you give them are the full values used to define the variable so if there is an
78
+ existing variable already defined in your User Environment variables then it will be overwritten with the new value.
79
+
80
+ <pre>
81
+ # ruby_versions.yml example
82
+
83
+ ruby_versions:
84
+ ruby_186:
85
+ shortcut: 186
86
+ bin_folder: C:\ruby\bin
87
+ ruby_192:
88
+ shortcut: 192
89
+ bin_folder: C:\ruby192\bin
90
+
91
+ jruby_164_jre6:
92
+ shortcut: jr_j6
93
+ bin_folder: C:\jruby-1.6.4\bin
94
+ exe_name: jruby.exe
95
+ user_env_variables:
96
+ java_home: C:\Program Files\Java\jre6
97
+ jruby_164_jre7:
98
+ shortcut: jr_j7
99
+ bin_folder: C:\jruby-1.6.4\bin
100
+ exe_name: jruby.exe
101
+ user_env_variables:
102
+ java_home: C:\Program Files\Java\jre7
103
+
104
+ </pre>
105
+
106
+
107
+ h2. License
108
+
109
+ MIT License
110
+
111
+ Copyright (C) 2011 by Lucas Hills
112
+
113
+ Permission is hereby granted, free of charge, to any person obtaining a copy
114
+ of this software and associated documentation files (the "Software"), to deal
115
+ in the Software without restriction, including without limitation the rights
116
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
117
+ copies of the Software, and to permit persons to whom the Software is
118
+ furnished to do so, subject to the following conditions:
119
+
120
+ The above copyright notice and this permission notice shall be included in
121
+ all copies or substantial portions of the Software.
122
+
123
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
124
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
125
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
126
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
127
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
128
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
129
+ THE SOFTWARE.
130
+
131
+
132
+
133
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/extract_muto ADDED
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fileutils'
4
+ require 'rbconfig'
5
+
6
+ #
7
+ # The following script will just extract the contents of the deploy_scripts directory somewhere onto the users
8
+ # machine. The directory where the scripts are extracted to should be added to your Windows PATH environment variable
9
+ # to add the muto command to the command line
10
+ #
11
+ # Usage: extract_muto
12
+ #
13
+
14
+ class Muto
15
+
16
+ def self.deploy
17
+ begin
18
+ muto_root_dir = File.expand_path(File.join(Dir.pwd, 'Muto'))
19
+ puts "Extracting to #{muto_root_dir.to_s.gsub(/\//, '\\')}"
20
+ FileUtils.mkdir_p(muto_root_dir) unless File.directory?(muto_root_dir)
21
+
22
+ template_dir = File.expand_path(File.join(File.dirname(__FILE__), "/../lib/deploy_scripts"))
23
+ templates = File.join(template_dir, "/**/*")
24
+
25
+ Dir[templates].each do |source|
26
+
27
+ next if source.match(/muto.bat\z/) #ignore muto.bat here, going to write it next
28
+
29
+ destination = File.join(muto_root_dir, source.gsub(/^.*\/deploy_scripts/, ''))
30
+ if File.exist?(destination) || (File.directory? destination)
31
+ puts "Skipping #{destination.to_s.gsub(/\//, '\\')} because it already exists"
32
+ else
33
+ puts "Generating #{destination.to_s.gsub(/\//, '\\')}"
34
+ File.directory?(source) ? FileUtils.mkdir(destination) : FileUtils.cp(source, destination)
35
+ end
36
+ end
37
+
38
+ ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
39
+ ruby << Config::CONFIG['EXEEXT']
40
+
41
+ # The muto.bat script needs to have a hardcoded fullpath to a Ruby.exe so that it can function
42
+ # independently from the version of ruby installed on your system. The following few lines just
43
+ # amend the muto.bat script to use the fullpath of the version of ruby that you've used to extract
44
+ # the muto scripts
45
+
46
+ orig = File.open(File.expand_path(File.join(File.dirname(__FILE__), "/../lib/deploy_scripts", "muto.bat")))
47
+ dest = File.new(File.expand_path(File.join(muto_root_dir, "muto.bat")), "w")
48
+
49
+ puts "Writing #{dest.path.to_s.gsub(/\//, '\\')} file.."
50
+ begin
51
+ while (line = orig.readline)
52
+ dest.puts line.gsub(/C:\\ruby\\bin\\ruby.exe/, ruby)
53
+ end
54
+ rescue EOFError
55
+ orig.close
56
+ dest.close
57
+ end
58
+
59
+ puts "\nAdd Ruby versions to your #{muto_root_dir.to_s.gsub(/\//, '\\')}\\ruby_versions.yml"
60
+ puts "Then add #{muto_root_dir.to_s.gsub(/\//, '\\')} to your Windows PATH environment variable and you're good to go"
61
+
62
+ rescue
63
+ puts $!, $!.backtrace
64
+ puts "Installation failed"
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+
71
+ if RUBY_PLATFORM =~ /(:?mswin|mingw)/
72
+ Muto.deploy
73
+ else
74
+ puts "Muto will only work properly if extracted using a Windows MRI version of Ruby"
75
+ puts "mswin or mingw versions should work fine"
76
+ end
77
+
@@ -0,0 +1,22 @@
1
+ @ECHO OFF
2
+
3
+
4
+
5
+ C:\ruby\bin\ruby.exe %~dp0\muto.rb %* 2> NUL
6
+
7
+
8
+ :: The following 2 lines allow you to not have to reset your command line in order to pick up
9
+ :: the new Environment Variables
10
+
11
+ reset_command_line.vbs
12
+ call "%TEMP%\reset_command_line.bat"
13
+
14
+
15
+
16
+ :: The end of this script just outputs the now current version of Ruby being used by your system
17
+ :: Using 2> NUL just means that if the ruby/jruby command doesn't exist, the error message won't be output
18
+ :: to the command line.
19
+ :: TODO - need to add support for all Ruby executables here
20
+
21
+ @ruby -v 2> NUL
22
+ @jruby -v 2> NUL
@@ -0,0 +1,138 @@
1
+
2
+ begin; require 'rubygems'; rescue; end
3
+ require 'win32/registry'
4
+ require 'Win32API'
5
+ require 'yaml'
6
+
7
+ HWND_BROADCAST = 0xffff
8
+ WM_SETTINGCHANGE = 0x001A
9
+ SMTO_ABORTIFHUNG = 2
10
+
11
+ class Muto
12
+
13
+ def initialize
14
+ @commands = []
15
+ @ruby_versions = []
16
+ @all_bin_paths = []
17
+
18
+ @versions_yml = YAML.load(IO.read(File.join(File.dirname(__FILE__), 'ruby_versions.yml')))
19
+
20
+ if @versions_yml['ruby_versions'].nil?
21
+ puts "There aren't any version of Ruby defined in your ruby_versions.yml yet"
22
+ puts "Update #{File.expand_path(File.join(File.dirname(__FILE__), "ruby_versions.yml")).to_s.gsub(/\//, '\\')} first and try again"
23
+ puts "\nCurrently using:"
24
+ exit!
25
+ else
26
+
27
+ @versions_yml['ruby_versions'].each do |key, val|
28
+ @ruby_versions << @versions_yml['ruby_versions'][key]['shortcut'].to_s
29
+ @all_bin_paths << @versions_yml['ruby_versions'][key]['bin_folder']
30
+
31
+ begin
32
+ exe_name = (@versions_yml['ruby_versions'][key]['exe_name']) ? @versions_yml['ruby_versions'][key]['exe_name'] : 'ruby.exe'
33
+ ruby_exe = File.expand_path(File.join(@versions_yml['ruby_versions'][key]['bin_folder'], exe_name))
34
+ ruby_version = `"#{ruby_exe}" -v`
35
+
36
+ if File.exist?(ruby_exe)
37
+ add_version(:"#{@versions_yml['ruby_versions'][key]['shortcut'].to_s}", ruby_version)
38
+ else
39
+ puts "File does not exist: #{ruby_exe.to_s}"
40
+ end
41
+
42
+ rescue
43
+ puts "Error: File does not exist: #{ruby_exe.to_s}"
44
+ end
45
+
46
+ end
47
+
48
+
49
+ end
50
+
51
+ end
52
+
53
+ def add_version(name, description, hidden=false)
54
+ @commands << { :name => name, :desc => description, :hidden => hidden }
55
+ end
56
+
57
+ def run
58
+ if ARGV.empty?
59
+ help
60
+ else
61
+ begin
62
+ version = ARGV.shift.to_s
63
+ rescue
64
+ puts "Unknown Version"
65
+ help
66
+ end
67
+
68
+ if @ruby_versions.include?(version)
69
+ @versions_yml['ruby_versions'].each do |key, val|
70
+ update_user_path_variable(key.to_s) if @versions_yml['ruby_versions'][key]['shortcut'].to_s == version
71
+ end
72
+ else
73
+ puts "Unknown Version"
74
+ help
75
+ end
76
+ end
77
+ end
78
+
79
+ def help
80
+ puts "\nExpected format: muto [ruby_version]"
81
+ puts "\nAvailable Versions are:"
82
+ @commands.each do |command|
83
+ puts " #{command[:name]} \t - #{command[:desc]}" unless command[:hidden]
84
+ end
85
+
86
+ # The bat file muto.bat will always output the current version of Ruby being
87
+ # used by your system, so just adding this to make it read nice
88
+ puts "\nCurrently using:"
89
+ end
90
+
91
+
92
+ def broadcast_WM_SETTINGCHANGE_signal
93
+ # After setting a new environment variable, a 'WM_SETTINGCHANGE' signal needs to be broadcast to windows
94
+ # so that you won't need to log off and log back on again for the new settings to be propagated.
95
+ # The following uses the Win32API module to send the signal
96
+ #
97
+ # For more info read up here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms725497%28v=vs.85%29.aspx
98
+ #
99
+
100
+ call_timeout_function = Win32API.new('user32', 'SendMessageTimeout', 'LLLPLLP', 'L')
101
+ result = 0
102
+ call_timeout_function.call(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 'Environment', SMTO_ABORTIFHUNG, 5000, result)
103
+
104
+
105
+ # The muto.bat script will finish up by outputting the current version or Ruby being used on your system
106
+ # This next line is just sugar
107
+ puts "\nSystem updated. Now using:"
108
+ end
109
+
110
+ def update_user_path_variable(versions_yml_key)
111
+ reg_key, path_env_var = Win32::Registry::HKEY_CURRENT_USER.open('Environment').read('PATH')
112
+
113
+ #TODO - Fix regex to support all variants of path names
114
+ @all_bin_paths.each do |bin_path|
115
+ path_env_var.gsub!(bin_path, @versions_yml['ruby_versions'][versions_yml_key]['bin_folder'].to_s)
116
+ end
117
+
118
+ Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_WRITE) do |reg|
119
+ reg['PATH'] = path_env_var
120
+ end
121
+
122
+ if @versions_yml['ruby_versions'][versions_yml_key]['user_env_variables']
123
+ @versions_yml['ruby_versions'][versions_yml_key]['user_env_variables'].each do |key, val|
124
+ Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_WRITE) do |reg|
125
+ reg[key.upcase] = val
126
+ end
127
+ end
128
+ end
129
+
130
+ broadcast_WM_SETTINGCHANGE_signal
131
+
132
+ end
133
+ end
134
+
135
+ if __FILE__ == $0
136
+ s = Muto.new
137
+ s.run
138
+ end
@@ -0,0 +1,23 @@
1
+
2
+ Set oShell = WScript.CreateObject("WScript.Shell")
3
+ filename = oShell.ExpandEnvironmentStrings("%TEMP%\reset_command_line.bat")
4
+ Set objFileSystem = CreateObject("Scripting.fileSystemObject")
5
+ Set oFile = objFileSystem.CreateTextFile(filename, TRUE)
6
+
7
+ 'Grab System Environment variables'
8
+ set oEnv=oShell.Environment("System")
9
+ for each sitem in oEnv
10
+ oFile.WriteLine("SET " & sitem)
11
+ next
12
+ path = oEnv("PATH")
13
+
14
+ 'Grab User Environment variables'
15
+ set oEnv=oShell.Environment("User")
16
+ for each sitem in oEnv
17
+ oFile.WriteLine("SET " & sitem)
18
+ next
19
+
20
+ 'Output to Global PATH variable'
21
+ path = path & ";" & oEnv("PATH")
22
+ oFile.WriteLine("SET PATH=" & path)
23
+ oFile.Close
@@ -0,0 +1,19 @@
1
+ ruby_versions:
2
+ # ruby_186:
3
+ # shortcut: 186
4
+ # bin_folder: C:\ruby\bin
5
+ # ruby_192:
6
+ # shortcut: 192
7
+ # bin_folder: C:\ruby192\bin
8
+ # jruby_164_jre6:
9
+ # shortcut: jr_j6
10
+ # bin_folder: C:\jruby-1.6.4\bin
11
+ # exe_name: jruby.exe
12
+ # user_env_variables:
13
+ # java_home: C:\Program Files\Java\jre6
14
+ # jruby_164_jre7:
15
+ # shortcut: jr_j7
16
+ # bin_folder: C:\jruby-1.6.4\bin
17
+ # exe_name: jruby.exe
18
+ # user_env_variables:
19
+ # java_home: C:\Program Files\Java\jre7
@@ -0,0 +1,3 @@
1
+ module Muto
2
+ VERSION = "0.0.1"
3
+ end
data/lib/muto.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "muto/version"
2
+
3
+ module Muto
4
+ end
data/muto.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "muto/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "muto"
7
+ s.version = Muto::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Lucas Hills"]
10
+ s.email = ["lucas@lucashills.com"]
11
+ s.homepage = 'https://github.com/2potatocakes/muto'
12
+ s.summary = 'I got sick of manually having to change windows environment variables and versions of ruby in the control panel'
13
+ s.description = 'Set up different Windows Ruby environments and switch between them on the command line'
14
+
15
+ s.files = %w(Rakefile Gemfile README.textile muto.gemspec) + Dir.glob("{bin,lib}/**/*")
16
+ s.bindir = 'bin'
17
+ s.executables = %w(extract_muto)
18
+ s.post_install_message = %q{
19
+ ========================================================================
20
+ Muto installed!
21
+ ------------------------------------------------------------------------
22
+ This gem is basically just a wrapper to make deployment easier. Delete
23
+ the gem after you've deployed the scripts if you want. To extract the
24
+ scripts, cd into a directory where you'd like to keep them and from the
25
+ command line run:
26
+
27
+ extract_muto
28
+
29
+ ========================================================================
30
+ }
31
+
32
+ s.require_paths = ['lib']
33
+ s.extra_rdoc_files = ['README.textile']
34
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: muto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lucas Hills
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-24 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Set up different Windows Ruby environments and switch between them on
15
+ the command line
16
+ email:
17
+ - lucas@lucashills.com
18
+ executables:
19
+ - extract_muto
20
+ extensions: []
21
+ extra_rdoc_files:
22
+ - README.textile
23
+ files:
24
+ - Rakefile
25
+ - Gemfile
26
+ - README.textile
27
+ - muto.gemspec
28
+ - bin/extract_muto
29
+ - lib/deploy_scripts/muto.bat
30
+ - lib/deploy_scripts/muto.rb
31
+ - lib/deploy_scripts/reset_command_line.vbs
32
+ - lib/deploy_scripts/ruby_versions.yml
33
+ - lib/muto/version.rb
34
+ - lib/muto.rb
35
+ homepage: https://github.com/2potatocakes/muto
36
+ licenses: []
37
+ post_install_message: ! '
38
+
39
+ ========================================================================
40
+
41
+ Muto installed!
42
+
43
+ ------------------------------------------------------------------------
44
+
45
+ This gem is basically just a wrapper to make deployment easier. Delete
46
+
47
+ the gem after you''ve deployed the scripts if you want. To extract the
48
+
49
+ scripts, cd into a directory where you''d like to keep them and from the
50
+
51
+ command line run:
52
+
53
+
54
+ extract_muto
55
+
56
+
57
+ ========================================================================
58
+
59
+ '
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: I got sick of manually having to change windows environment variables and
81
+ versions of ruby in the control panel
82
+ test_files: []