Import a gemset without bundler

Here’s the scenario…

I’m trying to get a Rails 2.1.1 application up-and-running on my Mac (Lion). The application uses Ruby 1.8.7-p334. Bundler is not used so there’s no chance of doing a bundle install… drat!

I have a gemset export from another developer’s machine, which was created by doing this:

rvm gemset export my_app

That gives me a file which looks like this (truncated for brevity)…

# my_app.gems generated gem export file. Note…
actionmailer -v2.1.1
actionpack -v2.1.1
activerecord -v2.1.1
activeresource -v2.1.1
activesupport -v2.1.1

So, in theory it should be as easy as pie to import this gemset like this:

rvm gemset import my_app

Wrong!

The main problem arises when you install these gems, because the dependencies are not managed properly (this is what bundler was created for). So you end up with multiple versions of gems installed.

Pain.

The trick now is I need to be able to do a diff between my currently installed list of gems and the desired list given to me by the other developer. You could do this manually, but I’m far too lazy for that…

So, I created a little ruby script which will accept two files to be used for the diff operation, together with a third optional argument depending on whether I want to install or remove the gems.

Here’s the script…

#!/usr/bin/env ruby

unless ARGV.count >= 2
  puts "Please provide two files to perform diff..."
  exit
end

def load_file file
  arr = File.readlines(file).map(&:chomp).compact
  arr.shift if arr.first =~ /^#/
  arr
end

f1, f2 = load_file(ARGV[0]), load_file(ARGV[1])
command = ARGV[2] || 'install'

((f1 - f2) + (f2 - f1)).uniq.each do |gem|
  puts "gem #{command} #{gem}"
end

Save this into a file called file_diff.rb and make the file executable:

chmod +x file_diff.rb

Now, it’s easy to get the exact gems installed by following these simple steps…

./file_diff.rb file1 file2 uninstall > uninstall.sh
chmod x+ uninstall.sh
./uninstall.sh

And that’s it! You should now have the same gems in your gemset that your friend has in his gemset, as per his exported list.

Install Ruby 1.8.7 on OSX Lion using RVM

I’ve got a brand spanking new MacBook Pro which is running OS X Lion. I use RVM to manage my Ruby versions. So should you…probably.

Anyway, I needed to install Ruby 1.8.7 for a work application, and although doing this using rvm seems to work, it doesn’t. Although rvm will tell you it’s installed Ruby 1.8.7 cleanly, as soon as you try to install a gem it will blow up in your face with a segmentation fault…

/Users/m/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/timeout.rb:60: [BUG] Segmentation fault

Nice.

So after pulling my hair out for the best part of, oh… about 27 minutes, I came across the solution on stackoverflow. Thought I’d just post it here for my own blogging pleasure. Credit to Matt Sanders for the answer:

http://stackoverflow.com/a/7884957

As per Matt’s solution, after you’ve installed Xcode from the App Store, downloaded and installed the OSX gcc installer, run the following to get Ruby 1.8.7 installed cleanly using rvm (adjust the patch level to your own needs):

rvm remove 1.8.7-p334
CC=/usr/bin/gcc-4.2 rvm install 1.8.7-p334 --force

I’m happy now.

Enable tethering and Wi-Fi hotspot on your HTC Desire

I’ve just bought a new Samsung Galaxy 10.1 Tablet, which is a beautiful piece of kit. It’s Wi-Fi only – not 3G enabled, but I was assured that I could easily share my phone’s 3G connection with my tablet should I need data access on the move. However, while it turned out to be a rather simple solution, it’s not exactly made easy by Orange.

Before I show you what I did, here is my setup:

  • Samsung Galaxy 10.1 Tablet running Android 3.1. This is standard (NOT rooted).
  • HTC Desire running Android 2.2. This is standard (NOT rooted).
  • Orange is my network provider.

I have no idea if my solution will work given any other setup, but this is what worked for me.

On your HTC Desire…

…we will change the details of an Access Point Name, which can be found here:

Wireless & Networks > Mobile networks > Access Point Names

You should then see the following 3 Access Point Names:

  • Orange Internet
  • Consumer Broadband
  • Orange MMS

Select the Consumer Broadband option, and then make the following changes (case-sensitive!):

  1. Set APN to orangeinternet
  2. Set Username to Orange
  3. Set Password to Multimedia
  4. Set Authentication type to CHAP

Press the Menu button and then press Save.

Then just turn on your Portable Wi-Fi hotspot.

On your Galaxy Tablet…

…just connect to your phone, which will be recognized as just another Wi-Fi network. Provide the necessary credentials and you’re good to go.

These steps are really simple and hardly worth documenting, so I won’t. But this process is worth documenting because it’s hardly something that the average non-technical user would be able to find. Thanks Orange.

We now have a working Wi-Fi hotspot and an internet-enabled non-3G tablet.

Sweet.

Custom bash auto-completion using Ruby

Using what I showed you in the how to run Ruby code from within a bash script post, I will follow that up with an easy way to create a custom bash auto-completion function.

The Scenario…

I like to keep all my code and projects in my ~/Development directory. I typically create an alias which takes me to this path, and depending what projects I’m working on, I’ll add/remove aliases accordingly to get to the relevant projects quickly. But this is quite manual and cumbersome, and it requires constant maintenance. What I’d like is an auto-complete function that dynamically picks up any new directory I create in the ~/Development directory.

All the functions detailed below should either go into your .bashrc file, or a file that is sourced from your .bashrc file.

Get rid of that alias

The first thing to do is change my alias to a function. So, instead of this…

alias d="cd ~/Development"

…we want this:

function d { cd ~/Development/$1; }

Create the list of choices

Next, we need a function which returns a list of all the directories in the ~/Development directory, which will serve as our auto-completion choices. We can use Ruby here to make things nice and clean…

#!/usr/bin/env sh

/usr/bin/env ruby <<-EORUBY

class ProjectCompletion
  def initialize(command)
    @command = command
  end
  
  def matches
    projects.select do |task|
      task[0, typed.length] == typed
    end
  end
  
  def typed
    @command[/\s(.+?)$/, 1] || ''
  end
  
  def projects
    %x[ls ~/Development].split
  end
end

puts ProjectCompletion.new(ENV["COMP_LINE"]).matches

EORUBY

Bootstrap it!

Finally, all we need is to tell bash to use this script. We do that with this command (your own paths may vary of course):

complete -C ~/.dotfiles/completion_scripts/project_completion -o default d

Reload your shell and voila! You now have bash auto-completion for all your coding projects. This is very easily customised too, so you can create auto-complete scripts for all kinds of other things, locations, tasks etc.

You can now easily reach your coding projects using auto-complete, without the need for aliases. For example, if you had a directory structure like this…

~/Development
~/Development/project_1
~/Development/project_2

…you could easily see a list of projects doing this:

d proj<tab>

For more information see my dotfiles project.

Hope this helps.

Setting up Ubuntu 11.10 for Rails development… Just Right!

Further to my previous post on how to setup Ubuntu 11.10 for Rails development, I have now created a bash installer script to automate the whole laborious process for your pleasure… well it’s for my own pleasure really, but I like to share!

There are two installer scripts:

  1. Ubuntu user will setup your Ubuntu 11.10 environment will all the usual goodies you normally should install after a clean install / upgrade.
  2. Rails developer will setup your Ruby environment using RVM, along with all the supporting things that you will need to get up and running in no time.

The installers are updated regularly so check the git pages for full details, but some of the key things that are installed are…

The Ubuntu user installer will hook you up with these (among others)…

  • Medibuntu repositories
  • Ubuntu Restricted Extras
  • Configuration Editor (dconf-tools)
  • Compiz Config Settings Manager
  • Synaptic
  • Terminator
  • GNOME Tweak Tool
  • VLC
  • Jupiter
  • Caffeine
  • Web apps and Sushi file previewer
  • Simple LightDM Manager
  • Sysmonitor App Indicator
  • Dropbox
  • OpenJDK (Java 7) – optional

Some of the things the Rails developer installer will setup are…

  • SSH key – optional
  • Essential Ubuntu libraries
  • Ruby 1.8.7 from Ubuntu repostories with additional libraries
  • Git and Git Gui
  • MySQL 5.1 with admin GUI tools
  • MongoDB 2.0.1 – optional
  • ack
  • ImageMagick
  • RVM (Ruby Version Manager) with these rubies: 1.8.7 stable and 1.9.2 stable

Head over to the git repository for instructions on how to run the installers and also for more details about what it actually installs:

https://github.com/edjames/just_right

Simples.

Nice & clean config using dotfiles

Clean up your shell environment by using my dotfiles project.

It’s as easy as running a simple rake task… then you have a clean, customizable shell environment all set up and ready to go!

Please see the git repository for more details on what is does and how to install.

You can find it all here: https://github.com/edjames/dotfiles

As mentioned before, you will have to have Rake installed before you run the automated installer, but it will set things up nicely. Just right in fact.

Hope you like it.

Setting up Ubuntu 11.10 for Rails development

This method is depricated and has been replaced by an automated installer. See this post for more details:
Setting up Ubuntu 11.10 for Rails development… Just Right!

The setup for Ubuntu 11.10 almost identical to the 11.04 setup. A few libraries have changed but otherwise it’s exactly the same.

As before, I like to have a system Ruby installed. This has always been a precautionary measure but has saved me plenty of pain with past installs.

sudo apt-get install build-essential openssl ri ruby ruby1.8 ruby-dev rake libruby1.8 git-gui gitk libxslt1-dev libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison

Then install RVM (see the RVM website for the latest installation instructions).

$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

Then run the following command, and… READ THE NOTES!

rvm notes

Make sure you read the RVM post-installation notes. You will get an updated list of OS dependencies and instructions on how to modify your .bash file. If you don’t do this you will probably drive yourself crazy trying to get RVM to work. You will fail.

Finally, install whatever rubies you want:

rvm install 1.8.7
rvm install 1.9.2
rvm install 1.9.3

Finally, install MySQL (and the MySQL GUI tools)…

sudo apt-get install mysql-server libmysqlclient-dev libmysql-ruby mysql-admin

Happy coding!

Follow

Get every new post delivered to your Inbox.