A powershell module for managing your ruby environments on Windows.
I’ve been working with Ruby in a Windows environment for a little over a year now. I’m sad to say that community support for Windows developers is lackluster. We are second-class citizens.
The most frustrating example of this is the lack of decent ruby version switchers. rvm doesn’t install on Windows at all. Ditto for rbenv. uru is a valiant attempt, but it is cumbersome to install and it’s API is less than intuitive.
The Need
This wouldn’t much of an issue if ruby installations were backwards compatible, but that is not the case. Even minor version releases of Ruby can incur breaking changes ruining your execution environment.
For development purposes, it’s a good idea to install the new ruby version, switch your ruby environment, then run all your tests on all your projects to verify compatibility. If you need to roll back, just switch your ruby environment and everything is good.
The Strategy
As I started digging into how tools like rvm and rbenv work, I became surprised a the difficuly of reimplementing them on Windows. Aside from the installation features (e.g. rvm install ruby-version
), ruby version management is basically just editing the PATH
variable. In other words, the great barrier, the monumental technical challenge that prevents anyone from developing an easy-to-install , easy-to-use ruby version switcher is: string manipulation.
The Requirements
A ruby version switcher needs to know the location of installed rubies. It needs to be able to alter the PATH
for the current session such that the desired ruby is the one being used.
Note
It is not the norm for Windows developers to think about altering their terminal session. It is the norm that alterations to the PATH are permanent. Reorienting our thinking around editing our Session as against our Environment has many benefits which I won’t go into here–except to say that it makes the issue of version switching much simpler.
My Solution
I wrote a powershell module called psundle-ruby (compatible with psundle) to discover and manage ruby versions. If you have not looked at psundle
, I encourage you to do so as it makes installation of this module as simple as:
Install-PsundleModule 'crmckenzie' 'psundle-ruby'
If you have already installed rubies in your Windows environment, you can execute Register-RubiesInPath
to make Psundle aware of them. Otherwise you can invoke Register-Ruby
for each ruby location not found in your PATH
.
Invoke Use-Ruby
to switch to the desired version. The argument to Use-Ruby
is an expression. The command switches to the first ruby it finds that matches the expression.
For example, if I have the following rubies installed on my machine:
* ruby-1.9.3
* ruby-2
* ruby-2.1
* ruby-2.2
I can invoke Use-Ruby "1"
to switch to ruby-1.9.3. Invoking Use-Ruby "2"
will switch me to ruby-2.
Invoke Set-DefaultRuby
to permanently alter your PATH
variable to automatically select the chosen ruby.
Committment to Maintain
I commit to maintaining this powershell module through the end of 2016. If you find issues, please report them or (even better) submit a pull-request. I will reevaluate my committment at the end of 2016 based on the level of interest and usage of this module.
Vim & Vundle
I finally bit the bullet and learned to use Vim competently. One of the things I was really impressed by in the Vim space is a plugin called Vundle. Vundle is a package manager for Vim plugins. At first, swimming in a sea of package managers, I was loathe to learn another one–but Vundle is extremely simple. It uses github as a package repository. “Installing” a package is basically as simple as running a git clone
to the right location. Updating the package is a git pull
. Security is managed by github. Genius.
Powershell
As a developer on Windows I find Powershell to be an extremely useful tool, especially when running in an excellent terminal emulator such as ConEmu. One of the problems that Powershell has is that there is no good way to install modules. The closest thing is PsGet.
What’s wrong with PsGet?
Nothing. PsGet is great. However, not every powershell module can be made public, and not every powershell module developer goes through the process of registering their modules at PsGet.
Introducing Psundle
I thought to myself, “Hell, if Vundle can install modules directly from github, I should be able to implement something similar in Powershell” and Psundle was born.
Psundle is a package manager for Powershell modules written in Powershell. It’s only dependency is that git
is available in the PATH
.
Disclaimer
Psundle is an alpha-quality product. It works, but API details may change. It will improve if you use it and submit your issues and/or Pull Requests through github.
Installation
You can install Psundle by running the following script in powershell:
iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/crmckenzie/psundle/master/tools/install.ps1'))
In your powershell profile, make sure you Import-Module Psundle
. Your powershell profile is located at
"$home/Documents/WindowsPowershell/Profile.ps1"
I advise that you don’t just run some dude’s script you found on the internet. Review the script first (it’s easy to understand). Please, please, please report any installation errors with the self-installer.
Installing Powershell Modules With Psundle
Install-PsundleModule "owner" "repo"
For example, if you want to install the module I wrote for managing ruby versions on Windows, you would run:
Install-PsundleModule "crmckenzie" "psundle-ruby" http://github.com/crmckenzie/psundle-ruby
What does this accomplish?
As long as you have imported the Psundle module in your profile, Psundle will automatically load any modules it manages into your powershell session.
Other Features
Show-PsundleEnvironment
Executing Show-PsundleEnvironment
gives output like this:
Module | Path | Updates | HasUpdates |
---|---|---|---|
Psundle | C:\Users\Username\Documents\WindowsPowerShell\Modu… {dbed58a | Updating readme to resolve installation … | True |
ChefDk | C:\Users\Username\Documents\WindowsPowerShell\Modu… | False | |
Ruby | C:\Users\Username\Documents\WindowsPowerShell\Modu… | False | |
VSCX | C:\Users\Username\Documents\WindowsPowerShell\Modu… | False |
Update-PsundleModule
I can update a module by running:
Update-PsundleModule "owner" "repo"
If I’m feeling brave, I can also Update-PsundleModules
to update everything in one step.
Requirements For Installed Modules
Because Psundle ultimately just uses git clone
to install powershell modules, Powershell modules in github need to be in the same structure that would be installed on disk.
Primarily, this means that the psm1
and psd1
files for the module should be in the repo root.
Committment to Develop
I’m making a blind committment to maintain this module through the end of 2016. “Maintenance” means I will answer issues and respond to pull requests for at least that length of time.
Whether I continue maintaining the module depends on whether or not people use it.