Skip to content

Commit

Permalink
WARBLER-7: Add config.webinf_files option with more support for custo…
Browse files Browse the repository at this point in the history
…m web.xml files
  • Loading branch information
nicksieger committed Mar 31, 2010
1 parent cd9616f commit 7089755
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
17 changes: 17 additions & 0 deletions lib/warbler/config.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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{^/}
Expand Down
22 changes: 9 additions & 13 deletions lib/warbler/war.rb
Expand Up @@ -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)
Expand Down
23 changes: 20 additions & 3 deletions spec/warbler/war_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 << "<web-app></web-app>"
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 << "<web-app><%= webxml.rails.env %></web-app>"
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
6 changes: 6 additions & 0 deletions warble.rb
Expand Up @@ -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.
Expand Down

0 comments on commit 7089755

Please sign in to comment.