What is *jour and why they are killer apps for RailsCamp08

Posted by Dr Nic on June 18, 2008

what is *jour?
Uploaded with plasq’s Skitch!

RailsConf 2008 was a few weeks ago, and I didn’t go. I made babies instead. But if you ignore the whoohaa of MagLev and DHH’s Surplus talk, there was one piece of genuine, new awesomeness that came out of RailsConf. This awesomeness, plus Twitter created at least 6 new projects in the last month. These apps are going to make RailsCamp awesome.

That is if RailsConf never happened this piece of delicious coding happiness never would have occurred. It was built at RailsConf, by RailsConf attendees. It was built to solve a problem that only happens at conferences. Crappy internet.

The answer is gitj… no wait, I have more introductory story. [skip it]

Instead of going to RailsConf this year I “get” to go to RailsCamp, which is like a conference for Rails developers, but without the conference part. What’s left without the conference? Hacking, Guitar Hero3, rejectconf-esque talks, and alcohol. Its’ the Lord’s way of saying “you’re in the right profession.”

RailsCamp has two minor drawbacks, since it is held out in the country. One, there is no internet connection. Two, there is no internet connection. Now I know that is only one draw back but I thought it was such a big one it was worth mentioning twice [oh I miss Red Dwarf].

So, 6 months ago we solved one internet-related problem: we turned to Git and git-svn clone to take all our svn repositories with us camping. 6 months on, git has taken over the Ruby OSS world, thanks mostly to GitHub which made remote hosting of git repos easy peasy. But, Github won’t be with us at RailsCamp. So we’re back to where we were 6 mths ago - being gumbies at hosting remote git repos.

So the question to be solved: how do you host a git repository, or indeed, lots of git repositories from your development machine; and conversely, how do you find someone else’s git repositories on their machine and clone it?

And whilst we’re at it… how do you easily share the RubyGems you have installed on your machine with other people, and how do you easily find gems on other people’s machines that you don’t currently have, if you don’t have access to rubyforge.org or github.com to get them?

And furthermore… if you’re running a fun little web app off your dev machine, how can you easily advertise it and other people find it and use it? “Hey, what’s your machine’s name?” “drnic-mac” “Thanks. ..pause… Um, what port’s the app on?” “4050”…

Same problem. Local devs, running local services, but how to share with everyone in the room?

Bonjour

The core solution is a technology created by the boffins at Apple that is now completely open-source (afaik): Bonjour. From wikipedia:

Bonjour, formerly Rendezvous, is Apple Inc.’s trade name for its implementation of Zeroconf, a service discovery protocol. Bonjour locates devices such as printers, as well as other computers, and the services that those devices offer on a local network using multicast Domain Name System service records.

In the Ruby world there are two RubyGems to integrate Bonjour into your app: dnssd and net-mdns. The former is only for Mac OS X, and the latter is a Ruby-only implementation that can be used anywhere. Bingo.

gitjour

So, Chad Fowler, Evan Phoenix, Rich Kilmer and a cast of others started a humble project to solve the first problem: sharing git repositories across a local network, called gitjour.

This project became a happy hack place for many many people.

one or two additions to gitjour
Uploaded with plasq’s Skitch!

NOTE: I’ve included all the installation instructions that should work. If they don’t then try building the gems from src.

On OS X:

    sudo gem install dnssd
    sudo gem install gitjour

    or from source

    sudo gem install dnssd
    git clone git://github.com/chad/gitjour.git
    cd gitjour
    rake install_gem

On Linux/Windows:

    sudo gem install net-mdns
    sudo gem install_gem nogoth-gitjour --source=http://gems.github.com

    or from source

    sudo gem install net-mdns
    git clone git://github.com/nogoth/gitjour.git
    cd gitjour
    rake install

The gitjour project uses dnssd gem, with mac os x dependencies, and GitHub user ‘nogoth’ has ported the project to use the net-mdns gem for Linux/Windows.

Now, open up two terminal windows so you can emulate both sides of “serving” and “finding” a git repository.

Window 1:

    cd a/git/project
    gitjour serve

Window 2:

    gitjour list
    gitjour clone project

In Window 1, you can also serve an entire folder of repositories. Lots of them all at one time. Go into your project’s parent folder and run gitjour serve and you’ll see it Registered dr_nic_magic_models on port 9418. Starting service. a lot. In Window 2, you just gitjour clone xxx the project you want.

See, genius. No mucking around with Apache or setting up local “remote” repositories in your public folder so people can pull from it. Just run gitjour serve from your parent folder and you are instantly hosting ALL of your git repositories. Top Tip: move any private work into another folder first wink

So, that’s the git-sharing problem fixed.

pastejour

After RailsConf, twitter was alive with the coolness of gitjour, and very quickly John Barnette created pastejour to publish and find single pastie’s of code, given that without the internet you don’t have pastie websites.

To install on OS X:

    sudo gem install dnssd
    sudo gem install jbarnette-pastejour --source=http://gems.github.com

    or install from src:

    sudo gem install dnssd
    git clone git://github.com/jbarnette/pastejour.git
    cd pastejour
    rake install

On Linux/Windows:

    sudo gem install net-mdns
    sudo gem install nogoth-pastejour --source=http://gems.github.com

    or install from src:

    sudo gem install net-mdns
    git clone git://github.com/nogoth/pastejour.git
    cd pastejour
    rake install

Pastejour publishes a single block of text until one person retrieves it. Essentially, you yell at the person that there is a paste for them and they get it.

Window 3:

    pastejour <<< "Hello Window 2"

Window 4:

    $ pastejour -f
    Searching for servers (3 seconds)
    Found pastejour at 'drnic'
    $ pastejour drnic
    (drnic from drnic-mac.local.:42424)
    Hello Window 2

gemjour

After John’s announcement on Twitter, I asked “do we have a gemjour yet for sharing gems?”

Later that day, Evan Phoenix replies with “@drnic Enjoy! http://github.com/evanphx/gemjour/tree/master

three-jours
Uploaded with plasq’s Skitch!

This gem serves up all your gems to anyone who wants to install them from you. Don’t have merb but want to try it? Grab it from someone else.

To install on OS X:

    sudo gem install dnssd
    sudo gem install evanphx-gemjour --source=http://gems.github.com

    or install from src:

    sudo gem install dnssd
    git clone git://github.com/evanphx/gemjour.git
    cd gemjour
    rake install

On Linux/Windows:

    sudo gem install net-mdns
    sudo gem install nogoth-gemjour --source=http://gems.github.com

    or install from src:

    sudo gem install net-mdns
    git clone git://github.com/nogoth/gemjour.git
    cd gemjour
    rake install

Window 5:

    gemjour serve

Window 6:

    gemjour list
    gemjour list someuser
    gemjour diff someuser  -- shows diff btw you and them
    sudo gemjour install someuser newgem -- install newgem from someuser
    sudo gemjour install_diff someuser -- installs all gems from someuser you don't have

Go around the room and steal everyone gems in a few minutes.

appjour

Room full of people hacking up cool things? Publish and find them easily with appjour, written by Lachie Cox

To install on OS X:

    sudo gem install dnssd
    sudo gem install lachie-appjour --source=http://gems.github.com

    or install from src:

    sudo gem install dnssd
    git clone git://github.com/lachie/appjour.git
    cd appjour
    rake install

To use it, after you’ve started a web app that you want to publish, open another window to run appjour:

Window 7:

    cd some/web/app
    script/serve -p 4000  # or whatever for your framework
    appjour leet_app 4000

Window 8:

    $ appjour
    Searching for servers (3 seconds)
  found web app called 'leet_app'

  $ appjour leet_app
    ... app loaded into browser

Mongrel publishing bonjour events

Courtenay hacked in a few lines to mongrel_rails so that each mongrel publishes itself to Bonjour.

The magic of this is that mongrels, or the applications behind them, could use Bonjour to self-discover each other within a mongrel cluster, and then talk to each other.

One man’s solution is another man’s problem

As new people join the circus, new gems, git repositories, web apps and pasties are added and the new problem is: how to keep in touch with the newness?

First solution: publish them to Growl with dejour by Aaron Patterson, tagged “Discover awesome services near you.”

On Mac OS X:

    sudo gem install dnssd
    sudo gem install aaronp-dejour --source=http://gems.github.com

    or install from src:

    sudo gem install dnssd
    git clone git://github.com/aaronp/dejour.git
    cd dejour
    rake install

There is no fork of dejour for Linux/Windows yet.

Window 9:

    $ dejour

All your current services should appear as Growl notifications (from Windows 1, 3, 5 and 7 above).

But Growl notifications fly-by pretty quickly. How to keep track?

Finally, *jour to the rescue (starjour)

I really felt that this problem was best solved with a GUI. Mostly cause I was keen to try out RubyCocoa. I don’t think I did productive work for a week, whilst I read books, tutorials and anything I could find on RubyCocoa, Cocoa, Xcode + Interface Builder, Objective-C and after trying and failing a few times starjour was created. By Lachie Cox (who also wrote appjour).

Starjour - panels branch
Uploaded with plasq’s Skitch!

F@#$@#.

Really, I just couldn’t get my head around Cocoa Bindings at the time, but my experience of using RubyCocoa was very positive, once I’d learnt the underlying framework and the thinking-patterns of a Cocoa developer. Best book to read to learn Cocoa/RubyCocoa? The book is by Aaron Hillegass - Cocoa Programming for Mac OS X (3rd ed). After struggling with other people’s books and tutorials this one was wonderful. The “CollectionView” message in the image above was me telling Lachie that I’d figured out and used CollectionViews to replace his TableView. Now i just need to bind the bonjour message type (gitjour/gemjour etc) to an image. And more.

But, nonetheless, starjour (read: *jour as all these apps were collectively named on Twitter) did one job: it sat there showing which services were currently available.

Unlike all the others, this one used the native Cocoa framework for Bonjour, fwiw, and is doomed as an OS X-only app.

On Mac OS X, we don’t really have a place to download and run it, so grab it from src:

    git clone git://github.com/lachie/starjour.git
    cd starjour
    rake run

This requires Xcode and RubyCocoa (available on OS X Leopard or via rubycocoa.com on sourceforge)

Wow! What just happened last month?

Chronologically: gitjour, pastejour, gemjour, dejour, starjour and appjour. And I think there are others. Its incredible.

Firstly, it is going to be an awesome RailsCamp - these tools are killer apps for an internet-less hackfest like RailsCamp, or massive internet-starved conferences like RailsConf.

Secondly, it has been very exciting to watch the creation of these projects over the last few weeks, with the *jour meme bubbling through twitter and irc. Another happy month for our open source communities.

Using Ruby within TextMate snippets and commands

Posted by Dr Nic on June 11, 2008

I didn’t know you could run Ruby within TextMate snippets. As a consequence, a lot of the TextMate bundles I work on either have simplistic Snippets or the advanced code is run via Commands with code pushed into explicit Ruby files in the Support folder.

But sometimes I just want a clever snippet. For example, I want the ‘cla’ snippet to use the current filename to create the default class name instead of the current ‘ClassName’ default. I want default foreign key names to be meaningful.

I’ve now figured this out (thanks to Ciaran Walsh), and …

Um, lost already? Ok, let me show you via screencast on Snippets and Commands with Ruby (QuickTime (11Mb)):


TextMate Snippets running Ruby from Dr Nic on Vimeo.

Want to learn more about living with TextMate and Ruby?

The TextMate website has a series of videos, including one by the Ruby.tmbundle’s own James Edward Gray II (JEG2).

In addition, there is the latest TextMate for Rails 2 Peepcode written by myself and spoken by Geoffrey Grosenbach. Its cheap at $9, good value at $15.50, and perhaps overpriced in the $20-$30 range. Lucky its only $9.

The snippets used throughout the video

The current Ruby.tmbundle snippet (activated via ‘cla’):

class ${1:ClassName}
	$0
end

An attempt to use regular expressions to convert the filename to a classname:

class ${1:${TM_FILENAME/[[:alpha:]]+|(_)/(?1::\u$0)/g}}
	$0
end

The final snippet, with embedded Ruby to do the heavy lifting (note: added ’singluarize’ to the snippet):

class ${1:`#!/usr/bin/env ruby
    require 'rubygems'
    require "active_support"
    puts ENV['TM_FILENAME'].gsub(/\.rb$/, '').camelize.singularize
    `}
	$0
end

Add this to your own Ruby.tmbundle, or clone mine (which is a clone of the original subversion repo).

Composite Primary Keys goes 1.0.0 for Rails 2.1

Posted by Dr Nic on June 06, 2008

Two years ago Dave Thomas did a keynote at the first RailsConf in 2006 and outlined a few things missing in Rails. One was the seeming unnecessary un-DRYness of duplicating associations and validations in Active Record models since the same information is in the DB schema. Another was support for Composite Primary Keys on Active Record models.

A few weeks later I created my first RubyGems as an attempt to solve these problems: Dr Nic’s Magic Models and Composite Primary Keys. The former was funny and an entertaining way to use const_missing? and Class.new. The latter was not funny. It started at the heights of “mildly entertaining,” dropped down from there and then over the subsequent months it never again rose above the humour-scale heights of “please shoot me in the foot.”

There were entire months that I hated ever having created the Composite Primary Keys project :)

Why? It sits precariously atop of ActiveRecord::Base, and a half dozen other ActiveRecord classes. It overrides methods, recreates entire blocks of SQL, and must work on all the different database adapters. That is, it is very sensitive to many changes in edge Rails. And refactoring ActiveRecord is a favourite pastime of the Rails Core team.

Unlike the Magic Models project which is perfectly useful for party tricks but probably partially pointless elsewhere, the CPK project just seemed to be so damned useful to some people.

Many people kept using it, several people contributed patches for adapter support and new features, and finally the Prophet arrived. The man that would lead the faithful forward to the Promised Land.

His name is Darrin “Champion” Holst.

Today, CPK was given the golden release number 1.0.0 and officially supports Rails 2.1 (but no longer supports 2.0.2 afaik).

CPK users might remember that we went 0.9.90 back in January, and its now June. This led Darrin to comment in his release email to the mailing list:

I’d like to name this release the “we’re running out of 0.9.x numbers, so it has to go to 1.0 sooner or later” release.

Maintaining CPK is like being on the team that paints the Sydney Harbour Bridge - it takes then 12 mths to paint it from one end to the other, which is just in time to restart painting the bloody thing again.

So in every way that I (tried to) abandon the project, I am proud of Darrin for looking after it and all its users.