Monday, October 03, 2005

TextMate and Rails


I've been twidling with TextMate's commands in an attempt to automate some of the common Rails commands.

Unfortunately, as far as I can tell TextMate doesn't have a way to invoke a pop-up for input (like Eclipse), so I've had to settle for sending in single arguments via selected text. So after creating a new app, I just create a setup.txt file in the project root where I can enter things like the port to start WEBrick and the names of the controllers, scaffolds, and models I want to generate.

Another issue: I've had to start WEBrick as a daemon with the -d switch; otherwise TextMate locks up. And another downside is I have to shut it down via command line after checking for it's process id. There's got to be a better way to start and stop the server from inside TextMate, but I haven't worked it out.

Here's the bundle of commands. You can unzip it to your /Users/[username]/Library/Application Support/TextMate/Bundles/ folder.

If anyone has any better versions of these commands, please let me know.

[UPDATE: Thanks to suggestions from the author of TextMate I now have some better ways to enter input and control WEBrick using HTML and JavaScript. I'll post the changes as soon as I've got them working the way I want. Check the comments for Allan's recommendations or see his blog.]

2 Comments:

Anonymous Anonymous said...

Accepting input can be done using AppleScript dialogs or CocoaDialog, see e.g. Text -> Encrypt Selection for the former (the latter is used to request input by the Run Ruby command/script).

To start and stop a process from TextMate, you can set the output to HTML and use JavaScript to control the process. See e.g. the Clock example in the experimental bundle. The actual API for this is the same as Apple's Dashboard API, you may find this post of interest.

12:50 AM  
Blogger victorcosby said...

Thanks for the leads, Allan.

Based on the Clock example, I now have a controller for starting and stopping WEBrick!

The only difficulty I had was (1) injecting $TM_PROJECT_DIRECTORY into the JavaScript and getting the stdout to display back from ruby.

To solve #1 I split up the concatenations of the here docs and sandwiched an echo between them:

cat <<'EOF'
... html header here...
EOF
echo "var projectPath = '$TM_PROJECT_DIRECTORY';"
cat <<'EOF'
var myCommand = null;
... rest of javascript and html...

And I still haven't found a way to solve #2. If I don't start the server as a daemon, I can end the command, but I can't get the response to return. If I start WEBrick as a daemon, the response is returned for display, but then I can no longer end the command. More tweaking may be required.

The deeper I dig into TextMate, the more I love working with it. Thanks for this great tool, Allan!

1:02 AM  

Post a Comment

<< Home