Sunday Nov 23, 2008

Open simply whatever non-NetBeans Ruby (on Rails) project in NetBeans

Subtitle: Poor man's, but effective and simple solution

This is a tip (or trick) for how to quickly open any project (Rails or non-Rails one) without any NetBeans metadata in NetBeans. E.g. from RubyForge, Kenai, GitHub, ... or just your local one. In short, all you need is to run nb_ruby_project command (which is Ruby few-liner (excluding NetBeans metadata)) and press C-S-1 in NetBeans.

nb_ruby_project is downloadable/viewable on pastie or here.

You need just change the two lines in the script to point to your netbeans executable and optionally to your userdir if you are using non-default one (e.g. --userdir option during start-up).

NB_BIN = '/path/to/netbeans/bin/netbeans'
# NB_USER_DIR = '/path/to/optional/userdir'

You might also start-up NetBeans before using the script (but not necessary)

$ nb_ruby_project 
Usage: nb_ruby_project <ruby_project_dir> [--rails]

For example see the following two-step (checking out and opening in NetBeans) terminal session for how to open RubyGems project from RubyForge:

$ svn co svn:// rubygems
$ nb_ruby_project rubygems

Project: rubygems (non-Rails)

Will create following files (NetBeans metadata) in:

Press "y<Enter>" to agree: y
Mentioned NetBeans metadata created

Opening 'Rakefile' in NetBeans...

Now the project's Rakefile should be opened in the running NetBeans session (or new NetBeans instance is started). Just press there C-S-1 (Menu | Navigate | Select in Projects) and press <Enter> to confirm the dialog as shown on the image below.

Open RubyGems in NetBeans

After the project is opened you might want to set different Ruby platform, add more sources and/or roots, rename the project, etc.

This is just workaround for the issue 126410 which should enable to do this from NetBeans directly. But I'm using the script very often for RubyForge-alike sites in the meantime, so sharing.... It might be enhanced with bunch of small features, like asking for source roots, being Ruby-independent, etc. But it's just a temporary workaround... hopefully not for too long.

Note that you can open all the gems you have installed on your machine, see your $GEM_HOME/gems directory. You might also use the script from CLI to quickly open projects already containing NetBeans metadata (metadata creation will be just detected and skipped) during browsing your harddisk.

Tuesday Nov 11, 2008

Remote Debugging: explore Ruby code easily

In the NetBeans 7.0 (dev) you may attach debugger to any process which have been started from the command line, with all the goodies of UI debugger frontend. This means that if you just found some strange behaviour or bug in some CLI tool or you are just curious how e.g. gem list, rake -T, etc. commands work, just run then in debug mode from a terminal and attach to them from the frontend.
There is no need to setup a NetBeans project, even not to open the file or doing whatever in NetBeans, just invoking Menu | Debug | Attach action there.

Let's say we are curious how does gem list --local works.

  • you need to have have ruby-debug-ide in version 0.4.1 (just released) or later installed
  • Run rdebug-ide -p 7000 --stop -- `which gem` list --local. On Windows just replace `which gem` with the c:\\full\\path\\to\\the\\gem command
    (the --stop switch ensures that the debugger stops on the first line. This might not be always what you want if you've set the breakpoints in advance in the NetBeans frontend (e.g. after the first run with the --stop switch). Then just omit the switch and debugger will run until the first breakpoint is hit)
  • In NetBeans invoke Menu | Debug | Attach choose Ruby Debugger in the Debugger combobox if there is more choices (you have also e.g. Java support installed)

That's all. Debugger will stop on the first line of the debuggee. After few steps you are inside RubyGems guts peeking around for how does it actually work.

RubyGems debugging

This is the first step letting you to attach NetBeans debugger to the application run from CLI and/or on another machine. Next step is to provide functionality similar to ruby-debug that you will be able to put a call like:

  if something_peculiar_has_happened
    require 'ruby-debug-ide'
    debugger(7000) # 7000 being a port

into your application and attach to it from the frontend.

Altough this is kind of "remote" debugging and you may attach to the process which was run on another machine, there are not yet sources association implemented in NetBeans. So you would have to have sources located on the same paths on the local machine as the one on the debuggee's machine. But for the case similar to the above which are probably the most frequent one this might be very usefull.

Worth to add that the support were added quite recently. I've also made the code base little bit more "aggressive" which should help to stabilize the debugger a bit more among the layers (from base backend up to the frontend) and prevent kind of random exception. So if you found any problems the feedback is more then welcomed. Either let me know here or through the mailing list or Issuezilla. Thanks


Martin Krauskopf


« November 2008 »