Permalink
Browse files

making omfg better

  • Loading branch information...
1 parent 5803e39 commit a367f2793a8495fa88887d053c50cc7882efd7a1 @tenderlove committed Aug 18, 2008
Showing with 62 additions and 3 deletions.
  1. +40 −1 bin/omfg
  2. +7 −2 lib/zomg/idl/nodes/node.rb
  3. +15 −0 test/test_node.rb
View
41 bin/omfg
@@ -1,9 +1,48 @@
#!/usr/bin/env ruby
+# == Synopsis
+#
+# omfg: generate ruby code given an OMG IDL file
+#
+# == Usage
+#
+# omfg [OPTION] ... FILE
+#
+# -h, --help:
+# show help
+#
+# -o [file]
+# output to file [file]
+#
+# --prefix [module]
+# module prefix the output with [module]
+require 'getoptlong'
+require 'rdoc/usage'
require 'rubygems'
require 'zomg'
+opts = GetoptLong.new(
+ ['--prefix', GetoptLong::OPTIONAL_ARGUMENT],
+ ['-o', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--help', '-h', GetoptLong::OPTIONAL_ARGUMENT]
+ )
+
+prefix = nil
+outfile = ARGV[1]
+opts.each do |opt, arg|
+ case opt
+ when '--prefix'
+ prefix = arg
+ when '--help'
+ RDoc::usage
+ when '-o'
+ outfile = arg
+ end
+end
+
tree = ARGV[0] ?
ZOMG::IDL.parse(File.read(ARGV[0])) : ZOMG::IDL.parse($stdin.read)
-puts tree.to_ruby
+rubycode = tree.to_ruby(prefix)
+outfile ? File.open(outfile, 'wb') { |f| f.write(rubycode) } : puts(rubycode)
+
View
9 lib/zomg/idl/nodes/node.rb
@@ -27,9 +27,14 @@ def to_ruby_sexp
RubySexp.new.accept(self)
end
- def to_ruby
+ def to_ruby(prefix = nil)
r2r = Ruby2Ruby.new
- r2r.process(to_ruby_sexp)
+ ruby_code = r2r.process(to_ruby_sexp)
+ if prefix
+ "module #{prefix}\n#{ruby_code}\nend"
+ else
+ ruby_code
+ end
end
end
%w{ Boolean Char Double Float Long Octet Short UnsignedLong
View
15 test/test_node.rb
@@ -0,0 +1,15 @@
+require 'helper'
+
+class NodeTest < ZOMG::Test
+ def setup
+ @tree = ZOMG::IDL::Parser.parse_file(array('array1.idl'))
+ end
+
+ def test_to_ruby
+ assert_nothing_raised {
+ @tree.to_ruby
+ }
+ ruby_with_prefix = @tree.to_ruby('A::B')
+ assert_match(/module A::B/, ruby_with_prefix)
+ end
+end

0 comments on commit a367f27

Please sign in to comment.