#!/usr/bin/env ruby # vim:set filetype=ruby: require "pathname" require "optparse" require 'rubygems' require "akephalos/htmlunit_downloader" options = { :interactive => false, :default_jvm_max_memory => '128'} parser = OptionParser.new do |opts| opts.banner = "Usage: akephalos [--interactive, --use-htmlunit-snapshot, --memory [number]] | [--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("-m", "--memory [number]", "Max memory for the Java Virtual Machine, defaults to #{options[:default_jvm_max_memory]} or env variable $akephalos_jvm_max_memory") do |memory| options[:akephalos_jvm_max_memory] = memory.to_s end if options[:akephalos_jvm_max_memory].nil? if ENV['akephalos_jvm_max_memory'].nil? options[:akephalos_jvm_max_memory] = options[:default_jvm_max_memory] else options[:akephalos_jvm_max_memory] = ENV['akephalos_jvm_max_memory'] end end puts "Using #{options[:akephalos_jvm_max_memory]} MB for the JVM" ENV['htmlunit_version'] ||= "2.9" HtmlUnit.download_htmlunit(ENV["htmlunit_version"]) 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 + 'vendor' case when options[:use_htmlunit_snapshot] require "fileutils" FileUtils.mkdir_p("vendor/html-unit") Dir["vendor/html-unit/*.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.10-SNAPSHOT-with-dependencies.zip] puts "done" $stdout.print "Extracting dependencies... " $stdout.flush %x[unzip -j -d html-unit htmlunit-2.10-SNAPSHOT-with-dependencies.zip htmlunit-2.10-SNAPSHOT/lib htmlunit-2.10-SNAPSHOT/lib/* &> /dev/null] puts "done" File.unlink "htmlunit-2.10-SNAPSHOT-with-dependencies.zip" end $stdout.puts "="*40 $stdout.puts "The latest HtmlUnit snapshot has been extracted to vendor/html-unit!" when options[:interactive] $LOAD_PATH.unshift('vendor', lib, src) require 'akephalos' require 'akephalos/console' Akephalos::Console.start else unless port = ARGV[0] puts parser.help exit end if RUBY_PLATFORM == "java" $LOAD_PATH.unshift("vendor", lib, src) require 'akephalos/server' Akephalos::Server.start!(port) else require 'jruby-jars' jruby = JRubyJars.core_jar_path jruby_stdlib = JRubyJars.stdlib_jar_path java_args = [ "-Xmx#{options[:akephalos_jvm_max_memory]}M", "-cp", [JRubyJars.core_jar_path, JRubyJars.stdlib_jar_path].join(File::PATH_SEPARATOR), "org.jruby.Main", "-X+O" ] ruby_args = [ "-Ku", "-I", ["#{Dir.pwd}/.akephalos/#{ENV['htmlunit_version']}", lib, src].join(File::PATH_SEPARATOR), "-r", "akephalos/server", "-e", "Akephalos::Server.start!(#{port.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. if ENV['JRUBY_1_8'] ENV["RUBYOPT"] = '' else ENV["RUBYOPT"] = "--1.9" end exec("java", *(java_args + ruby_args)) end end