Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:rawr/rawr
Browse files Browse the repository at this point in the history
  • Loading branch information
Neurogami committed Apr 25, 2010
2 parents 1a848bc + 5e6b3e0 commit a791147
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 124 deletions.
28 changes: 15 additions & 13 deletions README.md
Expand Up @@ -7,32 +7,34 @@ http://github.com/rawr/rawr


DESCRIPTION
--------------
-----------

Rawr is a packaging solution for JRuby applications. Rawr comes in two pieces, a
rawr command that creates a Java "main" file in your application and a
configuration file that will be used by Rawr to build the final jar and a rake
file that you can include into your project to do the building of the project.
Rawr is a packaging solution for JRuby applications. Rawr comes in two
pieces:

* a `rawr` command that creates a Java _main_ file in your application
and a configuration file that will be used by Rawr to build the final jar;
* a Rake task file that you can include into your project's Rakefile to
automate the creation of the packaged application.

SYNOPSIS
--------

rawr install
rake rawr:jar
java -jar package/jar/your_jar_file.jar
rawr install
rake rawr:jar
java -jar package/jar/your_jar_file.jar

REQUIREMENTS
------------

* jruby
* JRuby >= 1.4
* javac
* jar
* rake
* Rake

INSTALL
----------
-------

* sudo gem install rawr
sudo gem install rawr

LICENSE
-------
Expand Down
16 changes: 5 additions & 11 deletions lib/command.rb
Expand Up @@ -18,21 +18,15 @@ def self.fetch_jruby(args=ARGV)
JRubyRelease.get version, destination
end

def self.compile_ruby_dirs(src_dirs, dest_dir, jruby_jar, exclude, target_jvm, copy_only)
def self.compile_ruby_dirs(src_dirs, dest_dir, exclude, target_jvm)
require 'rawr_environment'
Rawr::ensure_jruby_environment
require 'jruby_batch_compiler'

puts "compile_ruby_dirs has src_dirs = #{src_dirs.inspect}"
if copy_only
Rawr::JRubyBatchCompiler.new.compile_dirs(src_dirs, dest_dir, {:jruby_jar => jruby_jar, :exclude => exclude, :copy_only => copy_only})
else
#TODO: Set target jvm here
rawr_dir = File.expand_path(File.dirname(__FILE__))
compiler_cmd = "require '#{rawr_dir}/jruby_batch_compiler'; " +
"Rawr::JRubyBatchCompiler.compile_argv"
sh 'java', '-jar', jruby_jar, '-e', compiler_cmd, *(src_dirs + [dest_dir])
end
options = Hash.new
options[:targer_jvm] = target_jvm
options[:exclude] = exclude
JRubyBatchCompiler.new.compile_dirs(src_dirs, dest_dir, options)
end
end
end
16 changes: 12 additions & 4 deletions lib/configuration.rb
Expand Up @@ -16,10 +16,10 @@ class << self; attr_accessor :current_config; end
Option.new(:main_ruby_file, String, 'main'),
Option.new(:main_java_file, String, 'org.rubyforge.rawr.Main'),

Option.new(:source_dirs, [FilePath], ['src', 'lib/ruby']),
Option.new(:source_exclude_filter, [Regexp], []),
Option.new(:jruby_jar, FilePath, 'lib/java/jruby-complete.jar'),
Option.new(:compile_ruby_files, Boolean, true),
Option.new(:source_dirs, [FilePath], ['src'], "A list of directories where source files reside"),
Option.new(:source_exclude_filter, [Regexp], [], "A list of regexps of files to exclude"),
Option.new(:compile_ruby_files, Boolean, true, "Whether Ruby source files should be compiled into .class files"),

Option.new(:java_lib_files, [FilePath], []),
Option.new(:java_lib_dirs, [FilePath], ['lib/java']),
Option.new(:files_to_copy, [FilePath], []), #FIXME: maybe needs file.sub(pwd, '')
Expand Down Expand Up @@ -187,6 +187,14 @@ def ruby_source_files
FileList[self.source_dirs].find_files_and_filter('*.rb', self.source_exclude_filter)
end

def ruby_source_files_to_compile
self.compile_ruby_files ? self.ruby_source_files : FileList.new
end

def ruby_source_files_to_copy
self.compile_ruby_files ? FileList.new : self.ruby_source_files
end

def non_source_file_list
FileList[self.source_dirs].find_files_and_filter('*', self.source_exclude_filter + [/\.(rb|java|class)$/])
end
Expand Down
85 changes: 12 additions & 73 deletions lib/jruby_batch_compiler.rb
Expand Up @@ -3,20 +3,9 @@

module Rawr
class JRubyBatchCompiler
def self.compile_argv
dest_dir = ARGV.pop
# TODO: add ability to carry options through
new.compile_dirs(ARGV, dest_dir)
end

def compile_dirs(src_dirs, dest_dir, options={})
puts " compile_dirs has src_dirs = #{src_dirs.inspect}"

#options[:jruby_jar] ||= 'lib/java/jruby-complete.jar'
options[:exclude] ||= []
options[:target_jvm] ||= '1.6'
copy_only = options[:copy_only] ||= false


#TODO: Allow for copy-only and some other options
ruby_globs = glob_ruby_files(src_dirs, options[:exclude])

Expand All @@ -27,30 +16,18 @@ def compile_dirs(src_dirs, dest_dir, options={})

next if files.empty? # Otherwise jrubyc breaks since we cannot compile nothing

if copy_only
copy_files(files, directory, dest_dir)
else
file_set = files.map {|file| "#{directory}/#{file}"}
raise "Empty file set in #{__FILE__}." if file_set.empty?
puts " Go compile #{file_set.inspect}"
begin
# JRuby >= 1.5
compiler = JRuby::Compiler
rescue NameError => e
# JRuby 1.4
# XXX: remove once JRuby 1.4 is no longer supported
compiler = JRubyCompiler
end
compiler.compile_files(file_set, directory, '', dest_dir)
file_set = files.map {|file| "#{directory}/#{file}"}
raise "Empty file set in #{__FILE__}." if file_set.empty?
puts " Go compile #{file_set.inspect}"
begin
# JRuby >= 1.5
compiler = JRuby::Compiler
rescue NameError => e
# JRuby 1.4
# XXX: remove once JRuby 1.4 is no longer supported
compiler = JRubyCompiler
end
end
end

def copy_files(files, src_dir, dest_dir)
files.each do |file|
#target_file = "#{dest_dir}/#{file}"
FileUtils.mkdir_p(File.dirname("#{dest_dir}/#{file}"))
File.copy("#{src_dir}/#{file}", "#{dest_dir}/#{file}")
compiler.compile_files(file_set, directory, '', dest_dir)
end
end

Expand Down Expand Up @@ -80,43 +57,5 @@ def reject_excluded_matches!(dir_globs, excludes)
excludes.any? {|exclude| file =~ exclude}
end
end

def self.compile_ruby_dirs(src_dirs, dest_dir, jruby_jar='lib/java/jruby-complete.jar', exclude=[], target_jvm='1.6', copy_only=false)
ruby_source_file_list = src_dirs.inject([]) do |list, directory|
list << Dir.glob("#{directory}/**/*.rb").
#reject{|file| File.directory?(file)}.
map!{|file| directory ? file.sub("#{directory}/", '') : file}.
#reject{|file| exclude.inject(false) {|rejected, filter| (file =~ filter) || rejected} }.
map!{|file| OpenStruct.new(:file => file, :directory => directory)}
end.flatten!

ruby_source_file_list.each do |data|
file = data.file
directory = data.directory

if copy_only
processed_file = file
target_file = "#{dest_dir}/#{file}"
else
relative_dir, name = File.split(file)
processed_file = Java::org::jruby::util::JavaNameMangler.mangle_filename_for_classpath(file, Dir.pwd, "", true) + '.class'
target_file = "#{dest_dir}/#{processed_file}"
end

if file_is_newer?("#{directory}/#{file}", target_file)
FileUtils.mkdir_p(File.dirname("#{dest_dir}/#{processed_file}"))

if copy_only
File.copy("#{directory}/#{processed_file}", "#{dest_dir}/#{processed_file}")
else
# There's no jrubyc.bat/com/etc for Windows. jruby -S works universally here
# TODO: Speed up compiling by not invoking java for each file...
sh "java -jar #{jruby_jar} -S jrubyc #{directory}/#{file}"
File.move("#{directory}/#{processed_file}", "#{dest_dir}/#{processed_file}")
end
end
end
end

end
end
55 changes: 32 additions & 23 deletions lib/rawr.rb
Expand Up @@ -13,6 +13,29 @@ def file_is_newer?(source, target)
!File.exists?(target) || (File.mtime(target) < File.mtime(source))
end

def generate_copy_tasks_for(files, source_or_not)
copied_file_list = FileList.new

files.each do |file_info|
orig_file_path = File.join(file_info.directory, file_info.filename)
dest_file_path = File.join(CONFIG.compiled_ruby_files_path, file_info.filename)
dest_dir = File.dirname(dest_file_path)

copied_file_list.add(dest_file_path)

directory dest_dir

file dest_file_path => [ orig_file_path, dest_dir ] do
puts "Copying #{source_or_not} file #{orig_file_path} to #{dest_file_path}"
copy orig_file_path, dest_file_path
end
end

return copied_file_list
end



specified_config_file = false
if Object.constants.include?('RAWR_CONFIG_FILE')
# RAWR_CONFIG_FILE can be set in the project's Rakefile
Expand Down Expand Up @@ -49,7 +72,6 @@ def file_is_newer?(source, target)

file jar_file_path => CONFIG.jar_output_dir
file jar_file_path => files_to_add do
puts "+ + Go build jar for #{jar_nick}"
jar_builders[jar_nick].build
end
}
Expand Down Expand Up @@ -93,7 +115,7 @@ def file_is_newer?(source, target)
}

COMPILED_RUBY_CLASSES = FileList.new
ruby_source_file_list = CONFIG.ruby_source_files
ruby_source_file_list = CONFIG.ruby_source_files_to_compile
ruby_source_file_list.each { |file_info|
orig_file_path = File.join(file_info.directory, file_info.filename)
dest_file_path = File.join(CONFIG.compiled_ruby_files_path, file_info.filename.pathmap('%X.class'))
Expand All @@ -107,33 +129,18 @@ def file_is_newer?(source, target)
require 'command'
Rawr::Command.compile_ruby_dirs(CONFIG.source_dirs,
CONFIG.compiled_ruby_files_path,
CONFIG.jruby_jar,
CONFIG.source_exclude_filter,
CONFIG.target_jvm_version,
!CONFIG.compile_ruby_files)
CONFIG.target_jvm_version)
end
}

COPIED_NON_SOURCE_FILES = FileList.new
non_source_file_list = CONFIG.non_source_file_list
non_source_file_list.each { |file_info|
orig_file_path = File.join(file_info.directory, file_info.filename)
dest_file_path = File.join(CONFIG.compiled_ruby_files_path, file_info.filename)
dest_dir = File.dirname(dest_file_path)

COPIED_NON_SOURCE_FILES.add(dest_file_path)

directory dest_dir

file dest_file_path => [ orig_file_path, dest_dir ] do
puts "Copying non-source file #{orig_file_path} to #{dest_file_path}"
copy orig_file_path, dest_file_path
end
}
COPIED_SOURCE_FILES = generate_copy_tasks_for(CONFIG.ruby_source_files_to_copy, "source")
COPIED_NON_SOURCE_FILES = generate_copy_tasks_for(CONFIG.non_source_file_list, "non-source")

desc 'Compiles all the Java source and Ruby source files in the source_dirs entry in the build_configuration.rb file.'
task :compile => COMPILED_JAVA_CLASSES
task :compile => COMPILED_RUBY_CLASSES
task :compile => COPIED_SOURCE_FILES
task :compile => COPIED_NON_SOURCE_FILES

desc "Compiles the Java source files specified in the source_dirs entry"
Expand Down Expand Up @@ -171,7 +178,10 @@ def file_is_newer?(source, target)

task :copy_other_file_in_source_dirs => COPIED_NON_SOURCE_FILES

file CONFIG.base_jar_complete_path => "rawr:compile"
file CONFIG.base_jar_complete_path => COMPILED_JAVA_CLASSES
file CONFIG.base_jar_complete_path => COMPILED_RUBY_CLASSES
file CONFIG.base_jar_complete_path => COPIED_SOURCE_FILES
file CONFIG.base_jar_complete_path => COPIED_NON_SOURCE_FILES
file CONFIG.base_jar_complete_path => CONFIG.meta_inf_dir
file CONFIG.base_jar_complete_path => CONFIG.jar_output_dir do
Rawr::Creator.create_manifest_file(CONFIG)
Expand All @@ -181,7 +191,6 @@ def file_is_newer?(source, target)
CONFIG.base_jar_complete_path,
{:directory => CONFIG.compile_dir,
:dir_mapping => root_as_base})
puts "Go build #{CONFIG.base_jar_complete_path}"
builder.build
end

Expand Down
10 changes: 10 additions & 0 deletions lib/rawr_environment.rb
@@ -1,4 +1,14 @@
module Rawr
# Returns a short human-readable description of the interpreter on
# which Rawr is running.
#
# @example Output on MRI 1.8.7
# "ruby 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]"
#
# @example Output on JRuby 1.4
# "jruby 1.4.0 (ruby 1.8.7 patchlevel 174) [amd64-java]"
#
# @return [String] a short description of the current Ruby interpreter
def ruby_environment
env_pieces = RUBY_DESCRIPTION.match(/^(.*?\)) .*?(\[.*?\])/)
return env_pieces[1..2].join(' ')
Expand Down

0 comments on commit a791147

Please sign in to comment.