Sunday, September 28, 2014

More Late Night Ruby Fun

Late Friday/Early Saturday morning, after the wife and kids found solace in sleep, I sat awake in a fevered pitch to patch my laptops and ruby install. I had to install the bash exploit patch, because security!

I also had to take a good stab at compiling the ruby TK library that comes with it.
No luck there though.

I had an adventure when I updated Ruby to Ruby 2.1.3. Basically, I deleted my whole ruby installation and recompiled from source. This left me with no ruby gems. Fortunately, because of my previous late night ruby thinking session, I had a list of gems. It's a slightly dated list, I'd installed the rubyXL gem a couple of night ago, but it is a good start.
I've automated gem installation before, using a simple script to install the gems from a list. I decided to create a couple of stand alone methods and a short script to 1. Create a list of gems currently installed on the system. 2. Install the list of gems (while determining if a gem has already been installed). 3. Determine if any gems in the list I have are missing without installing any new gems.

Objective 1 was easily accomplished. I took my one-liner from the other night and broke it down into a method using a "do... end" block.

I chose to do the method this way because I believe it makes the method more readable. Plus, the rest of the methods in the file are multiline by necessity and it echoes their format.


Objective 2 provided more of a challenge. The original script I wrote installed all the gems listed whether they were already installed or not. Sometimes one gem depends on another for installation, so it gets installed before a gem further up the list. You can still "install" the gem, even if it's already been installed as a dependency, but this adds to the time the script takes to execute. I decided that if I had already installed a gem, whether as a dependency or as part of a previous installation, then I didn't want to reinstall it.


That was a good enough place to stop for the night.
Some cautionary notes:
I'm doing this on Ubuntu 14.04, and you'll see a sudo in there. A unix purist might say that it would be better to su to the superuser and execute without the sudo. I got no problem with that. Understand what you are doing when you use sudo. I execute this script because I wrote it and I know what it says. Don' t do nothin' with sudo you don't understand. That's how things get borked fast.

Also, I don't check for the existence of the gemlist.txt file. I haven't needed that functionality so I haven't created it.

Last night when I got home from work, I watched a little tv while doing laundry,  and then remembered that I'd left my little script here hanging. I still needed a way to check and see if a gem was in the gemlist.txt file.


The hardest part about writing this script was figuring out how to get two arrays created and then subtracting them. For some reason split wasn't behaving as expected. I chalk it up to general tiredness.

At the bottom of this file, I stitched my methods together using an if else ladder.

Everything works!
I know that choice #2 isn't really doing what it says it does. If a gem is already installed on the system, but not in the gemlist file, then it's not on both lists, and it's not installed, nor is it placed in the gemlist file.

Also, I'm sure that there's a better way to do the menu. Perhaps using a hash or something.
One thing this file won't do is install gems that aren't in your install path. Currently I have one gem installed that's built from the gemspec in the gem itself.
One way around this would be to store the gemlist in a csv file or perhaps a spreadsheet, and carry the installation path in there.
Right now though, the file is useful.
I've saved it as a gist on github.

Maybe you can find it useful too!

No comments: