Not only must the spice flow... but the flow must also spice!

September 26, 2009

Viewing source for local gems in Windows

As a Rails programmer, every once and a while I want to look at the source files for Ruby gems installed on my system. The way I have done this in the past was to go to github and search for the gem, and then navigate through the source tree 1 click at a time. But this is slow and handicaps you from the supreme navigational powers and otherwise aesthetic delights of your favorite text editor! And while I always sort of knew that all the gem sources were sitting around on my file system somewhere I just hadn't put in any thought to quickly and easily accessing them. So today I decided that this had to change!

In my post about setting up your Rails server and console with Console2 in Windows I also described how to set up a macros file which could be loaded in to the command prompt when Console2 starts up with this command line argument: -r "/k doskey /macrofile=C:\macros". Today I'm going to extend the utility of the macros file a bit and leverage E Text Editor's command line tool for opening the source directory for locally installed gems in Windows - as a project! If you're not using E then I'm sure your editor, whatever it may be, can do something like this too, though, so follow along.

To get started, open up your macros file (for me this is located at C:\macros) and add the following three lines:
gls=dir/w %GEMS%\$*
gll=dir %GEMS%\$*
ge=e %GEMS%\$1

This defines 3 new (though somewhat familiar) aliases, all of which I have namespaced with a "g" for gem - but you can name these anything you want, of course.

gls = horizontal listing of all the gems in your local gems folder.
gll = vertical listing of all the gems in your local gems folder.
ge = open the passed in folder (i.e. gem name and version number) as a project in E Text Editor

Why not just use gem list, you ask? Well, using the gls and gll aliases is helpful because all gems live in a folder designated by both their name and version number - which is how rubygems keeps multiple gem versions separate. And since copy + paste is faster than typing this stuff out getting the actual directory listing through gls or gll is of great convenience. You'll see exactly what I'm referring to in a minute, but first let's finish the macros setup...

Ok, so the only thing we really need to do now is create that %GEMS% environment variable (or alternatively, just use the full path to your Ruby gems in your macros file instead, but this is easy enough and adds some flexibility). Again, refer to this article if you're not sure how to add an environment variable in your version of Windows.



Note: The path to your Ruby gems folder is relative to where you installed Ruby at, so make sure that the path shown in the above image is correct for your system before entering it in. But for me it was C:\Ruby\lib\ruby\gems\1.8\gems.

Great, now let's try out our new aliases! Again, if you already followed along with my post about Console2 then you should be able to just fire up Console for your project and go (but make sure to close and reopen Console if it was already open during the previous steps - to reload the environment). Otherwise, if you're using the vanilla Windows command prompt you're going to need to 1) create and 2) load in your macros file to your current session with doskey /macrofile=C:\macros (assuming C:\macros is the path to your macros file).

Anyway, first enter gls to get the gems directory listing:



And then all you've got to do to view source on one of the listed gems is type ge <gem_directory_name> - which I propose that you fill in the <gem_directory_name> by copying and pasting it out of the directory listing. For example, to view source on the map_by_method gem, the full command would be ge map_by_method-0.8.3.

Remember: in Console2 the default for copying and pasting is to hold down Shift and drag the mouse (copy) and then middle mouse button click (paste).





Too easy! And by the way, if you want to get E to go back to your rails project folder, I find that the quickest way to do this is to go back to your command prompt and enter e . which opens e to the current path/folder. Or you can pass any file or folder path to the e command line tool to open it in E (much like the mate command for TextMate on the Mac).

So that's it! Easiest way I have found to do this in Windows yet (but please let me know if there's a better alternative!). Thanks again Console2 and E!

No comments:

Post a Comment