July 12th, 2008
You watch any screen-cast of any of the Rubystars out there, they’re using Textmate on a Mac. Textmate is great, really it is! It has two serious flaws however. They are that you need a Mac to run it and … that you need a Mac to run it.
Now, I have nothing against Macs. They look nice, they work well and have lots of cool little (and not so little) apps to help a developer with productivity. It would be more accurate to say that I have nothing against Mac OS X. What I hate about the Mac hardware is the compromises that have been made in the pursuit of – what I have to admit is – attractive design. I have a Dell laptop, it’s a lot uglier but it does plug into my docking station both in the office and at home with no fiddling with wires for monitors, keyboards, mice etc. Keeran and Mark would both insist that it’s worth it just to have something pretty on your desk. I’m not convinced…
Anyway this article is turning into a rant about something entirely different to what I intended.
I’m going to be assuming that anyone interested in this article is OS agnostic and is already passionate about BDD. Oops, just lost half the small numbers of readers that got beyond my Mac-attack…
This article is about using the Gnome default text editor, gedit for developing in Ruby. On the face of it and even after some half-hearted digging, gedit is not a patch on Textmate. However, I would argue all the fundamentals are there and with the wide variety of plugins available. It is a good alternative.
Textmate has benefited from a dedicated team of people willing to put time into developing bundles to enhance the functionality. This is certainly true of functionality relating to software development and Ruby/Rails development in particular. There is very little one can’t do with a few deft strokes and taps of the keyboard when it comes to Textmate.
The thing is, Gedit is just as receptive to extension with a mixture of plugins and external commands. This article is about my first tentative step to creating a decent set of tools to help Ruby developers who want a free alternative to Textmate.
The first thing you need to do is enable the External Tools plugin for Gedit. By the way, if you’re using Ubuntu, like I am, there are a bunch of useful plugins that aren’t bundled by default. They’re not important for the purposes of this article but there are a few gems in there. To install them type sudo apt-get install gedit-plugins or just follow this link apt://gedit-plugins
To enable the External Tools plugin open Gedit and follow these steps
Now you’re ready to add your rspec running tool. To do this open the External Tools dialog by navigating to Tools -> External Tools. First make the dialog bigger, the Command(s) text box is way too small. Click on the New button to create a new tool. Call it “Run Rspec” and give it a meaningful description. The shortcut key I elected to use is Shift+Ctrl+F10. Now copy the following into the Command(s) text area.
#!/usr/bin/ruby # Copyright (c) 2004 - 2008 Beanlogic Limited # # This file is part of gedit_on_rails. # # gedit_on_rails is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # gedit_on_rails is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with gedit_on_rails. If not, see <http://www.gnu.org/licenses/>. # # This is based on the Rspec Textmate bundle SPEC=ENV['GEDIT_CURRENT_DOCUMENT_PATH'] DIR=ENV['GEDIT_CURRENT_DOCUMENT_DIR'] PROJECT_DIR="#{DIR}/../.." REPORT="#{PROJECT_DIR}/doc/rspec_report.html" rspec_rails_plugin = File.join(PROJECT_DIR,'vendor','plugins','rspec','lib') if File.directory?(rspec_rails_plugin) $LOAD_PATH.unshift(rspec_rails_plugin) end require 'spec' require 'spec/runner/formatter/html_formatter' module Spec module Runner module Formatter # Formats backtraces so they're clickable in Firefox class HtmlFormatter def backtrace_line(line) line.gsub(/([^:]*\.rb):(\d*)/) do "<a href=\"gedit://#{File.expand_path($1)}?#{$2}\">#{$1}:#{$2}</a> " end end end end end end if File.exists? REPORT File.delete REPORT end argv = [SPEC] argv << "--c" argv << "--format" argv << "html:#{REPORT}" ::Spec::Runner::CommandLine.run( ::Spec::Runner::OptionParser.parse(argv, STDERR, STDOUT)) if File.exists? REPORT `firefox #{REPORT}` end
Close the dialog and open one of your specs from a Rails project. Now you can quickly run the spec with a quick keystroke and get the results displayed nicely in a web browser! Now for the caveats. This script makes a number of assumptions.
The eagle-eyed amongst you will have noticed that we’re overriding part of the HtmlFormatter that ships with Rspec. This draws a lot of inspiration from the Textmate bundle for Rspec. the replacement method not only adds a link to any file displayed in a stacktrace but also specifies a protocol of gedit://. This protocol doesn’t exist unfortunately. If it did, this article would be half the length.
Anyway, we’ll create it and make Firefox understand it! Save this script as “gediturl” somewhere useful and ensure that it executable. A good place might be /user/local/bin and that’s what I’ll be assuming.
#!/usr/bin/ruby # Copyright (c) 2004 - 2008 Beanlogic Limited # # This file is part of gedit_on_rails. # # gedit_on_rails is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # gedit_on_rails is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with gedit_on_rails. If not, see <http://www.gnu.org/licenses/>. # # This file handles the openning of rspec examples GEDIT = `which gedit`.chomp url = ARGV.first url = url[8..url.length] file, line = url.split('?') `#{GEDIT} #{file} +#{line}`
Open a new tab or window in firefox and type about:config in the address bar. You might get a warning about messing about with the config. I wasn’t worried about that but if you are back up your Firefox profile before you continue.
You should be presented with a long list of configuration keys and values. Right click somewhere in this list and select New -> String. Give it a name of network.protocol-handler.app.gedit and a value of /usr/local/bin/gediturl
Now when you run your spec in Gedit again and maybe one of the specs fail, the stacktrace for each fail will be presented as useful links that open the files in the trace in Gedit. Not only that it will move the cursor to correct line in that file. I was pretty pleased with myself when I did that because it makes Gedit a really useful tool for behavior driven development.
I found Firefox fairly unpredictable when I added the protocol handler. It didn’t work at first but then worked after no apparent changes were made. If anyone experiences difficulties here and finds out why, please let me know and I’ll add it here.
Gnome Editor (gedit) could become a very powerful Ruby and Ruby on Rails IDE given a little bit of work. I’m planning to start a project perhaps in the form of a Rubygem to empower gedit in this way. I think that then it could really challenge Textmate as a non-IDE alternative for Ruby development. We’re going to need a hell of a lot more than being able to run Rspec files with a keystroke so anybody who wants to help please get in touch. Specific areas of interest is plugin development for both Gedit and Firefox and whether they too could be bundled into a gem.
If I have made any mistakes or stupid assumptions or if I’ve just reinvented someone else’s wheel, please let me know and I’ll correct them.
Sorry, comments are closed for this article.
July 16th, 2008 at 01:48 PM Nice article. I don't find many non-Mac based Rails articles. I am not sure why really. They will always cater for Mac first then have a little footnote saying, "prefix 'ruby ' to the commands in Windows", but never any Linux-based articles. A text editor for windows which is Textmate-like (it even uses Textmate bundles…) is E-TextEditor (http://www.e-texteditor.com/). I used this when I was at another computer as I am one of the few Mac users I know. One thing I did miss moving to Textmate is Visual Studio's Intellitype. The popup box which would analyse your classes and the language classes. Other programs such as XCode or Netbeans would do only the language classes. Therefore, you cannot make new classes and use them quickly. It really can speed up development. Especially in languages like C#, for using methods like FormsAuthentication.HashPasswordForStoringInConfigFile(). Takes say 4 taps instead of 55 taps. Maybe in Textmate2, whenever that is out…
July 16th, 2008 at 03:05 PM
Hi Damon,
My first comment! Woop! :-)
Try using Intellij’s ruby plugin for IDEA. It’s pretty good if it’s autocompletion/suggestion you’re after. And free too (well, for 30 days).
It’s still a bit hit and miss to be relied on if you deviate any from the standard Rails app structure (e.g. by adding a presenters folder) at the moment but I’ve no doubt it will get better.
I should have sorted the paragraphs in comments out now too.
E
September 29th, 2008 at 04:38 PM
I created this cheat sheet for rails development in gedit: http://www.scribd.com/doc/6299312/Gedit-Cheat-Sheet-for-Rails-Development
September 30th, 2008 at 02:14 PM
I’m actually going to be working on adding some snippets for rails, ruby, rspec and some other taken from the text mate bundle. Once I get them all done I’ld be glad to hook you up with a copy. I know it’s not much but I think it would be usefull thanks for the great article by the way. I’m also a fan of Gedit
October 1st, 2008 at 06:56 AM
@LT
That’s great, a good learning resource. I think I found most of those by accident just because I’m used to pressing tab for what IDEA calls Live Templates.
@Mitchell
That would be great. When I get a spare moment I’ll create an open source project somewhere so all this cool Gedit Rails stuff has a home.