May 06, 2005

Problems with RDoc::usage() from RubyScript2Exe

RDoc::usage() is great for printing out Ruby program usage text because the text is automatically extacted from the comments (similar to JavaDoc, but parsing is done at run-time and the text available while the program runs). Another great Ruby utility is RubyScript2Exe, which packs up ruby application into a Windows EXE executable.

The problem is that usage() generates an error when running from the executable. Here is the fix for $libdir/rdoc/usage.rb:

  def RDoc.usage_no_exit(*args)
#
# Rubyscript2exe has to run bootstrap code before main program file.
# This gives to usage() the wrong file name extracted from
# the bottom of the caller stack.
# Rubyscript2exe writes main program file into $0 
# so we should get the main program file from $0.
#
#   main_program_file, = caller[-1].split(/:\d/, 2)
    main_program_file = $0
    comment = File.open(main_program_file) do |file|
      find_comment(file)
    end
    ...
end

This also circumvents another usage() bug: usage_no_exit() extacts the top-most caller from the stack, then splits the username from the line number at colon symbol (:). Except when the filename contains a colon, as on C: drive on Window machines.

# this chokes on "C:\dir\file.rb:25" 
# because the filename contains a colon
    main_program_file, = caller[-1].split(/:/, 2)
# should be
    main_program_file, = caller[-1].split(/:\d/, 2)
Posted by laza at May 6, 2005 07:50 PM | TrackBack
Comments
Post a comment









Remember personal info?