From 70897559da1ee18aa382652db7066de9f33c1e96 Mon Sep 17 00:00:00 2001 From: Nick Sieger Date: Wed, 31 Mar 2010 16:57:54 -0500 Subject: [PATCH] WARBLER-7: Add config.webinf_files option with more support for custom web.xml files --- lib/warbler/config.rb | 17 +++++++++++++++++ lib/warbler/war.rb | 22 +++++++++------------- spec/warbler/war_spec.rb | 23 ++++++++++++++++++++--- warble.rb | 6 ++++++ 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/lib/warbler/config.rb b/lib/warbler/config.rb index 74aa070c..69339e12 100644 --- a/lib/warbler/config.rb +++ b/lib/warbler/config.rb @@ -73,6 +73,10 @@ class Config # by jar -cf.... attr_accessor :manifest_file + # Files for WEB-INF directory (next to web.xml). Contains web.xml by default. + # If there are .erb files they will be processed with webxml config. + attr_accessor :webinf_files + # Use Bundler to locate gems if Gemfile is found. Default is true. attr_accessor :bundler @@ -125,6 +129,7 @@ def initialize(warbler_home = WARBLER_HOME) @rails_root = File.expand_path(defined?(RAILS_ROOT) ? RAILS_ROOT : Dir.getwd) @war_name = File.basename(@rails_root) @bundler = true + @webinf_files = default_webinf_files auto_detect_frameworks yield self if block_given? update_gem_path @@ -153,6 +158,7 @@ def default_pathmaps p.java_libs = ["WEB-INF/lib/%f"] p.java_classes = ["WEB-INF/classes/%p"] p.application = ["WEB-INF/%p"] + p.webinf = ["WEB-INF/%{.erb$,}f"] p.gemspecs = ["#{@gem_path[1..-1]}/specifications/%f"] p.gems = ["#{@gem_path[1..-1]}/gems/%p"] p @@ -167,6 +173,17 @@ def default_webxml_config c end + def default_webinf_files + webxml = if File.exist?("config/web.xml") + "config/web.xml" + elsif File.exist?("config/web.xml.erb") + "config/web.xml.erb" + else + "#{WARBLER_HOME}/web.xml.erb" + end + FileList[webxml] + end + def update_gem_path if @gem_path != DEFAULT_GEM_PATH @gem_path = "/#{@gem_path}" unless @gem_path =~ %r{^/} diff --git a/lib/warbler/war.rb b/lib/warbler/war.rb index e0b8607e..2aa8fc93 100644 --- a/lib/warbler/war.rb +++ b/lib/warbler/war.rb @@ -31,20 +31,16 @@ def create(config_or_path) end def add_webxml(config) - webxml = nil - if File.exist?("config/web.xml") - webxml = "config/web.xml" - else - erb = if File.exist?("config/web.xml.erb") - "config/web.xml.erb" - else - "#{WARBLER_HOME}/web.xml.erb" - end - require 'erb' - erb = ERB.new(File.open(erb) {|f| f.read }) - webxml = StringIO.new(erb.result(erb_binding(config.webxml))) + config.webinf_files.each do |wf| + if wf =~ /\.erb$/ + require 'erb' + erb = ERB.new(File.open(wf) {|f| f.read }) + contents = StringIO.new(erb.result(erb_binding(config.webxml))) + @files[apply_pathmaps(config, wf, :webinf)] = contents + else + @files[apply_pathmaps(config, wf, :webinf)] = wf + end end - @files["WEB-INF/web.xml"] = webxml end def add_manifest(config) diff --git a/spec/warbler/war_spec.rb b/spec/warbler/war_spec.rb index a2db3019..9a22c11d 100644 --- a/spec/warbler/war_spec.rb +++ b/spec/warbler/war_spec.rb @@ -26,7 +26,7 @@ after(:each) do rm_rf "log" rm_rf ".bundle" - rm_f FileList["*.war", "config.ru", "*web.xml", "config/web.xml*", + rm_f FileList["*.war", "config.ru", "*web.xml*", "config/web.xml*", "config/warble.rb", "file.txt", 'manifest', 'Gemfile*'] Dir.chdir(@pwd) end @@ -118,14 +118,14 @@ def expand_webxml it "should use a config/web.xml if it exists" do mkdir_p "config" touch "config/web.xml" - @war.apply(@config) + @war.apply(Warbler::Config.new) @war.files["WEB-INF/web.xml"].should == "config/web.xml" end it "should use a config/web.xml.erb if it exists" do mkdir_p "config" File.open("config/web.xml.erb", "w") {|f| f << "Hi <%= webxml.public.root %>" } - @war.apply(@config) + @war.apply(Warbler::Config.new) @war.files["WEB-INF/web.xml"].should_not be_nil @war.files["WEB-INF/web.xml"].read.should == "Hi /" end @@ -405,4 +405,21 @@ class << t; public :instance_variable_get; end hash = eval("[" + IO.readlines(".bundle/environment.rb").grep(/rspec/).last + "]").first hash[:load_paths].each {|p| File.exist?(p).should be_true } end + + it "should allow adding additional WEB-INF files via config.webinf_files" do + File.open("myserver-web.xml", "w") do |f| + f << "" + end + @war.apply(Warbler::Config.new {|c| c.webinf_files = FileList['myserver-web.xml'] }) + file_list(%r{WEB-INF/myserver-web.xml}).should_not be_empty + end + + it "should allow expanding of additional WEB-INF files via config.webinf_files" do + File.open("myserver-web.xml.erb", "w") do |f| + f << "<%= webxml.rails.env %>" + end + @war.apply(Warbler::Config.new {|c| c.webinf_files = FileList['myserver-web.xml.erb'] }) + file_list(%r{WEB-INF/myserver-web.xml}).should_not be_empty + @war.files['WEB-INF/myserver-web.xml'].read.should =~ /web-app.*production/ + end end diff --git a/warble.rb b/warble.rb index 7ba08763..99b1676c 100644 --- a/warble.rb +++ b/warble.rb @@ -38,6 +38,12 @@ # functionality, uncomment here. # config.bundler = false + # Files for WEB-INF directory (next to web.xml). This contains + # web.xml by default. If there is an .erb-File it will be processed + # with webxml-config. You may want to exclude this file via + # config.excludes. + # config.webinf_files += FileList["jboss-web.xml"] + # Other gems to be included. You need to tell Warbler which gems # your application needs so that they can be packaged in the war # file.