Skip to content

Commit

Permalink
Adding configuration and tasks for compiling ruby files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Stark committed Jun 12, 2010
1 parent a63c880 commit 31b3f30
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions Manifest.txt
Expand Up @@ -19,6 +19,7 @@ lib/warbler/version.rb
lib/warbler/war.rb
lib/warbler_war.jar
spec/sample/app/controllers/application.rb
spec/sample/app/helpers/application_helper.class
spec/sample/app/helpers/application_helper.rb
spec/sample/config/boot.rb
spec/sample/config/database.yml
Expand Down
4 changes: 4 additions & 0 deletions lib/warbler/config.rb
Expand Up @@ -81,6 +81,9 @@ class Config
# This also sets 'gem.path' inside web.xml.
attr_accessor :gem_path

# FileList of ruby files to compile to class files
attr_accessor :compiled_ruby_files

# Extra configuration for web.xml. Controls how the dynamically-generated web.xml
# file is generated.
#
Expand Down Expand Up @@ -128,6 +131,7 @@ def initialize(warbler_home = WARBLER_HOME)
@war_name = File.basename(@rails_root)
@bundler = true
@webinf_files = default_webinf_files
@compiled_ruby_files = FileList[]
auto_detect_frameworks
yield self if block_given?
update_gem_path
Expand Down
9 changes: 8 additions & 1 deletion lib/warbler/task.rb
Expand Up @@ -59,6 +59,7 @@ def define_tasks
define_main_task
namespace name do
define_clean_task
define_compile_task
define_files_task
define_jar_task
define_debug_task
Expand Down Expand Up @@ -99,8 +100,14 @@ def define_clean_task
task "clear" => "#{name}:clean"
end

def define_compile_task
task "compile_ruby_files" do
war.compile(config)
end
end

def define_files_task
task "files" do
task "files" => "compile_ruby_files" do
war.apply(config)
end
end
Expand Down
24 changes: 24 additions & 0 deletions lib/warbler/war.rb
Expand Up @@ -17,6 +17,30 @@ def initialize
@files = {}
end

def compile(config)
# Need to use the version of JRuby in the application to compile it
compiled_ruby_files = config.compiled_ruby_files.to_a

return if compiled_ruby_files.empty?

run_javac(config, compiled_ruby_files)
replace_compiled_ruby_files(config, compiled_ruby_files)
end

def run_javac(config, compiled_ruby_files)
%x{java -classpath #{config.java_libs.join(File::PATH_SEPARATOR)} org.jruby.Main -S jrubyc \"#{compiled_ruby_files.join('" "')}\"}
end

def replace_compiled_ruby_files(config, compiled_ruby_files)
# Exclude the rb files and recreate them. This
# prevents the original contents being used.
config.excludes = compiled_ruby_files

compiled_ruby_files.each do |ruby_source|
files[apply_pathmaps(config, ruby_source, :application)] = StringIO.new("require __FILE__.sub(/\.rb$/, '.class')")
end
end

# Apply the information in a Warbler::Config object in order to
# look for files to put into this war file.
def apply(config)
Expand Down
15 changes: 15 additions & 0 deletions spec/warbler/task_spec.rb
Expand Up @@ -82,6 +82,21 @@
Warbler::Task.new "warble", @config
end

it "should compile any ruby files specified" do
@config.compiled_ruby_files = FileList["app/helpers/application_helper.rb"]
silence { Rake::Task["warble"].invoke }

java_class_magic_number = [0xCA,0xFE,0xBA,0xBE].map { |magic_char| magic_char.chr }.join

Zip::ZipFile.open("#{@config.war_name}.war") do |zf|
java_class_header = zf.get_input_stream('WEB-INF/app/helpers/application_helper.class') {|io| io.read }[0..3]
ruby_class_definition = zf.get_input_stream('WEB-INF/app/helpers/application_helper.rb') {|io| io.read }

java_class_header.should == java_class_magic_number
ruby_class_definition.should == %{require __FILE__.sub(/.rb$/, '.class')}
end
end

it "should process symlinks by storing a file in the archive that has the same contents as the source" do
File.open("config/special.txt", "wb") {|f| f << "special"}
Dir.chdir("config") { ln_s "special.txt", "link.txt" }
Expand Down

0 comments on commit 31b3f30

Please sign in to comment.