Skip to content
Browse files

create a preflight settings class so we can reuse defaults and such, …

…elsewhere
  • Loading branch information...
1 parent 60cd766 commit 2c3ee0a38fb54d51021cc6cf3d75dfc6871fda20 Steve Conover & Shawn Morel committed Nov 9, 2011
Showing with 65 additions and 26 deletions.
  1. +12 −15 bin/preflight
  2. +42 −0 lib/preflight/settings.rb
  3. +1 −1 preflight.gemspec
  4. +3 −3 spec/bundler_spec.rb
  5. +5 −5 web/jetty-init.erb
  6. +2 −2 web/jetty.xml.erb
View
27 bin/preflight
@@ -4,6 +4,9 @@ require "yaml"
require "erb"
require "fileutils"
require "etc"
+
+require "preflight/settings"
+
include FileUtils
def halt(message)
@@ -22,17 +25,11 @@ halt("usage: preflight <project_path>") unless ARGV.length==1 && File.directory?
@path_to_project = ARGV.shift
@created = []
-config_file = File.join(@path_to_project, "config/preflight.yml")
-halt("#{config_file} not found") unless File.exists?(config_file)
-@preflight_config = YAML.load(ERB.new(File.read(config_file)).result)
+@settings = Preflight::Settings.load_from_project(@path_to_project)
-@app_root = @preflight_config['app_root'] || File.expand_path(@path_to_project)
-@app_user = @preflight_config['app_user'] || Etc.getpwuid(File.stat(@app_root).uid).name
-@java_options = @preflight_config['java_options'] || "-Xmx2048m -client"
-@https_port = @preflight_config['https_port'] || "4443"
-@http_port = @preflight_config['http_port'] || "9080"
+puts @settings.inspect
-unless @preflight_config['jruby']
+unless @settings.jruby?
halt(%{Your config/preflight.yml must at least contain a pointer to the desired jruby-complete jar.
Ex:
@@ -46,10 +43,10 @@ script_start_time = Time.now
@jruby_jar_file = "vendor/jruby.jar"
@preflight_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
-@gem_home = "file:" + File.expand_path(File.join(@app_root, @jruby_jar_file)) + "!/META-INF/jruby.home/lib/ruby/gems/1.8"
+@gem_home = "file:" + File.expand_path(File.join(@settings.app_root, @jruby_jar_file)) + "!/META-INF/jruby.home/lib/ruby/gems/1.8"
@gem_path = @gem_home + ":vendor/bundler_gem"
-@java_dash_jar = "GEM_HOME=\"#{@gem_home}\" GEM_PATH=\"#{@gem_path}\" java #{@java_options} -jar"
+@java_dash_jar = "GEM_HOME=\"#{@gem_home}\" GEM_PATH=\"#{@gem_path}\" java #{@settings.java_options} -jar"
def jruby!(cmd)
x! "cd #{@path_to_project} && #{@java_dash_jar} #{@jruby_jar_file} #{cmd}"
@@ -108,7 +105,7 @@ end
def install_jetty(vendor_dir)
jetty_dir = File.join(vendor_dir, "jetty")
Dir["#{vendor_dir}/jett*"].to_a.each{|f|rm_rf(f)}
- download @preflight_config['jetty'], File.join(vendor_dir, "jetty.zip")
+ download @settings.jetty, File.join(vendor_dir, "jetty.zip")
unzip File.join(vendor_dir, "jetty.zip"), vendor_dir, '*/start.jar */lib/jetty*.jar */lib/servl*.jar'
rm File.join(vendor_dir, "jetty.zip")
mv Dir["#{vendor_dir}/jetty*"].to_a.first, jetty_dir
@@ -118,7 +115,7 @@ def install_jetty(vendor_dir)
end
def install_jruby_rack_jar(vendor_dir)
- download @preflight_config['jruby-rack'], File.join(vendor_dir, "jruby-rack.jar")
+ download @settings.jruby_rack, File.join(vendor_dir, "jruby-rack.jar")
end
def write_jetty_xml(jetty_dir)
@@ -151,11 +148,11 @@ vendor_dir = File.join(@path_to_project, "vendor")
mkdir_p File.join(@path_to_project, "bin")
mkdir_p File.join(@path_to_project, "vendor")
-download @preflight_config['jruby'], File.join(@path_to_project, @jruby_jar_file)
+download @settings.jruby, File.join(@path_to_project, @jruby_jar_file)
create_ruby_script ruby_script_path
create_rake_script rake_script_path
bundle_install if File.exists?(File.join(@path_to_project, "Gemfile.lock"))
-if @preflight_config['jetty']
+if @settings.jetty?
jetty_dir = install_jetty(vendor_dir)
install_jruby_rack_jar vendor_dir #raise if jar not found
write_jetty_xml jetty_dir
View
42 lib/preflight/settings.rb
@@ -0,0 +1,42 @@
+require "ostruct"
+require "yaml"
+require "erb"
+
+module Preflight
+ class Settings < OpenStruct
+ def self.load_from_project(project_dir)
+ config_file = File.join(project_dir, "config/preflight.yml")
+ raise("#{config_file} not found") unless File.exists?(config_file)
+ yaml = YAML.load(ERB.new(File.read(config_file)).result)
+ Settings.new(project_dir, yaml)
+ end
+
+ def initialize(project_dir, user_defined_options)
+ contents = {}
+ contents["app_root"] = user_defined_options["app_root"] || File.expand_path(project_dir)
+ contents["app_user"] = user_defined_options["app_user"] || Etc.getpwuid(File.stat(contents["app_root"]).uid).name
+ contents["java_options"] = user_defined_options["java_options"] || "-Xmx2048m"
+ contents["https_port"] = user_defined_options["https_port"] || "4443"
+ contents["http_port"] = user_defined_options["http_port"] || "4080"
+ contents["jruby_rack"] = user_defined_options["jruby-rack"] if user_defined_options.key?("jruby-rack")
+ contents["jetty"] = user_defined_options["jetty"] if user_defined_options.key?("jetty")
+ contents["jruby"] = user_defined_options["jruby"] if user_defined_options.key?("jruby")
+
+ @keys = contents.keys.sort
+
+ super(contents)
+ end
+
+ def jruby?
+ respond_to?(:jruby)
+ end
+
+ def jetty?
+ respond_to?(:jetty)
+ end
+
+ def inspect
+ self.class.name + ":\n" + @keys.map{|k|" #{k.ljust(20)} = #{send(k.to_sym)}"}.join("\n") + "\n"
+ end
+ end
+end
View
2 preflight.gemspec
@@ -14,6 +14,6 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.test_files = `git ls-files -- spec/*`.split("\n")
- s.require_paths = ["bin"] #Need at least one require path...
+ s.require_paths = ["lib"]
# s.executables = %w(bundle)
end
View
6 spec/bundler_spec.rb
@@ -32,7 +32,7 @@
end
it "can be used from a script fed to jruby." do
- rake_result = x(%{echo 'require \\"rubygems\\"; require \\"bundler\\"; puts Bundler::VERSION' | spec/sample_projects/has_gems_via_bundler/bin/ruby})
+ rake_result = x(%{spec/sample_projects/has_gems_via_bundler/bin/ruby -e 'require \\"rubygems\\"; require \\"bundler\\"; puts Bundler::VERSION'})
rake_result[:stderr].should == ""
rake_result[:stdout].should include("1.0.18")
rake_result[:exitstatus].should == 0
@@ -49,15 +49,15 @@
it "installed gems are available via normal require" do
rake_result = x("cd spec/sample_projects/has_gems_via_bundler && " +
- %{echo 'require \\"rubygems\\"; require \\"bundler/setup\\"; require \\"spruz/bijection\\"; puts Spruz::Bijection.name' | bin/ruby})
+ %{bin/ruby -e 'require \\"rubygems\\"; require \\"bundler/setup\\"; require \\"spruz/bijection\\"; puts Spruz::Bijection.name'})
rake_result[:stderr].should == ""
rake_result[:stdout].should == "Spruz::Bijection\n"
rake_result[:exitstatus].should == 0
end
it "installed gems are available via Bundler.require" do
rake_result = x("cd spec/sample_projects/has_gems_via_bundler && " +
- %{echo 'require \\"rubygems\\"; require \\"bundler\\"; Bundler.require; puts Spruz::Bijection.name' | bin/ruby})
+ %{bin/ruby -e 'require \\"rubygems\\"; require \\"bundler\\"; Bundler.require; puts Spruz::Bijection.name'})
rake_result[:stderr].should == ""
rake_result[:stdout].should == "Spruz::Bijection\n"
rake_result[:exitstatus].should == 0
View
10 web/jetty-init.erb
@@ -5,15 +5,15 @@
# processname: finance-jetty
THIS_DIR=$(dirname $0)
-WEB_ROOT=<%=@app_root%>
+WEB_ROOT=<%=@settings.app_root%>
-JAVA_OPTIONS="<%=@java_options%>"
-JETTY_HOME=<%=@app_root%>/vendor/jetty
-JETTY_RUN=<%=@app_root%>/vendor/jetty/run
+JAVA_OPTIONS="<%=@settings.java_options%>"
+JETTY_HOME=<%=@settings.app_root%>/vendor/jetty
+JETTY_RUN=<%=@settings.app_root%>/vendor/jetty/run
JETTY_PORT=4443
JETTY_LOGS=$WEB_ROOT/log
# Avoid unnecessary su call if we're already the app user
-test `whoami` != <%=@app_user%> && JETTY_USER=<%=@app_user%>
+test `whoami` != <%=@settings.app_user%> && JETTY_USER=<%=@settings.app_user%>
# Startup script for jetty under *nix systems (it works under NT/cygwin too).
View
4 web/jetty.xml.erb
@@ -29,7 +29,7 @@
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">
- <Set name="port"><%=@https_port%></Set>
+ <Set name="port"><%=@settings.https_port%></Set>
<Set name="maxIdleTime">30000</Set>
<Set name="password"><SystemProperty name="jetty.ssl.password" default="foobar" /></Set>
<Set name="keystore"><SystemProperty name="jetty.ssl.keystore" default="etc/fake.p12" /></Set>
@@ -41,7 +41,7 @@
<Call name="addConnector">
<Arg>
<New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
- <Set name="port"><%=@http_port%></Set>
+ <Set name="port"><%=@settings.http_port%></Set>
<Set name="maxIdleTime">30000</Set>
</New>
</Arg>

0 comments on commit 2c3ee0a

Please sign in to comment.
Something went wrong with that request. Please try again.