#!/usr/bin/env ruby # vim:set filetype=ruby: require "pathname" require "optparse" options = { :interactive => false } parser = OptionParser.new do |opts| opts.banner = "Usage: akephalos [--interactive, --use-htmlunit-snapshot] | [--server] " opts.on("-s", "--server", "Run in server mode (default)") opts.on("-i", "--interactive", "Run in interactive mode") { options[:interactive] = true } opts.on("--use-htmlunit-snapshot", "Use the snapshot of htmlunit") { options[:use_htmlunit_snapshot] = true } opts.on_tail("-h", "--help", "Show this message") { puts opts; exit } end parser.parse! root = Pathname(__FILE__).expand_path.dirname.parent lib = root + 'lib' src = root + 'src' case when options[:use_htmlunit_snapshot] require "fileutils" FileUtils.mkdir_p("vendor/htmlunit") Dir["vendor/htmlunit/*.jar"].each { |jar| File.unlink(jar) } Dir.chdir("vendor") do $stdout.print "Downloading latest snapshot... " $stdout.flush %x[curl -O http://build.canoo.com/htmlunit/artifacts/htmlunit-2.8-SNAPSHOT-with-dependencies.zip &> /dev/null] puts "done" $stdout.print "Extracting dependencies... " $stdout.flush %x[unzip -j -d htmlunit htmlunit-2.8-SNAPSHOT-with-dependencies.zip htmlunit-2.8-SNAPSHOT/lib htmlunit-2.8-SNAPSHOT/lib/* &> /dev/null] puts "done" File.unlink "htmlunit-2.8-SNAPSHOT-with-dependencies.zip" end $stdout.puts "="*40 $stdout.puts "The latest HtmlUnit snapshot has been extracted to vendor/htmlunit!" when options[:interactive] $:.unshift('vendor', lib, src) require 'rubygems' require 'akephalos' require 'akephalos/console' Akephalos::Console.start else unless socket_file = ARGV[0] puts parser.help exit end if RUBY_PLATFORM == "java" $:.unshift("vendor", lib, src) require 'akephalos/server' Akephalos::Server.start!(socket_file) else require 'rubygems' require 'jruby-jars' jruby = JRubyJars.core_jar_path jruby_stdlib = JRubyJars.stdlib_jar_path java_args = [ "-Xmx128M", "-cp", [JRubyJars.core_jar_path, JRubyJars.stdlib_jar_path].join(":"), "org.jruby.Main" ] ruby_args = [ "-I", "vendor:#{lib}:#{src}", "-r", "akephalos/server", "-e", "Akephalos::Server.start!(#{socket_file.inspect})" ] # Bundler sets ENV["RUBYOPT"] to automatically load bundler/setup.rb, but # since the akephalos server doesn't have any gem dependencies and is # always executed with the same context, we clear RUBYOPT before running # exec. ENV["RUBYOPT"] = "" exec("java", *(java_args + ruby_args)) end end