ruby2jar 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ =begin
2
+ Command line interface for ruby2jar builder.
3
+
4
+ Copyright (C) 2008 Andrey "A.I." Sitnik <andrey@sitnik.ru>
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ =end
19
+
20
+ module Ruby2Jar
21
+ # Command line interface for builder. Check builder parameters and
22
+ # print error and warnings to console.
23
+ class Console < Listener
24
+ # Print message for standart builder error
25
+ def on_error(error)
26
+ if Error == error.class
27
+ STDERR.puts "ERROR: #{error.message}"
28
+ true
29
+ else
30
+ false
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,23 @@
1
+ =begin
2
+ Error on building.
3
+
4
+ Copyright (C) 2008 Andrey "A.I." Sitnik <andrey@sitnik.ru>
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ =end
19
+
20
+ module Ruby2Jar
21
+ # Error on JAR building
22
+ class Error < Exception; end
23
+ end
@@ -0,0 +1,85 @@
1
+ =begin
2
+ Rake task for building.
3
+
4
+ Copyright (C) 2008 Andrey "A.I." Sitnik <andrey@sitnik.ru>
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ =end
19
+
20
+ module Ruby2Jar
21
+ # Rake task to build JAR.
22
+ #
23
+ # Example:
24
+ # require "ruby2jar"
25
+ #
26
+ # PKG_NAME = "program"
27
+ # PKG_VERSION = "0.1"
28
+ #
29
+ # Ruby2Jar::Rake::JarTask.new do |jar|
30
+ # jar.files = FileList["lib/**/*", "bin/*"]
31
+ # jar.main = "bin/program"
32
+ # jar.name = PKG_NAME
33
+ # jar.version = PKG_VERSION
34
+ # jar.add_dependency "rspec"
35
+ # end
36
+ class JarTask
37
+ # Name of JAR package
38
+ attr_accessor :name
39
+
40
+ # Version of application to use in JAR name
41
+ attr_accessor :version
42
+
43
+ # Create an RDoc task named +task+ (default task name is +jar+)
44
+ def initialize(task = :jar)
45
+ @task = task
46
+ @builder = Builder.new
47
+ Console.new(@builder)
48
+ yield self if block_given?
49
+ if @builder.jar.nil? and not @name.nil?
50
+ if not @version.nil?
51
+ @builder.jar = "pkg/#{@name}-#{@version}.jar"
52
+ else
53
+ @builder.jar = "pkg/#{@name}.jar"
54
+ end
55
+ end
56
+ define
57
+ end
58
+
59
+ # Create the tasks
60
+ def define
61
+ task :package => [@task]
62
+ desc "Build the JAR file #{@builder.jar}"
63
+ task @task do
64
+ @builder.build
65
+ end
66
+
67
+ task :clobber => ["clobber_#{@task}"]
68
+ desc "Remove JAR file #{@builder.jar}"
69
+ task "clobber_#{@task}" do
70
+ File.delete @builder.jar if File.exists? @builder.jar
71
+ pkg = File.dirname(@builder.jar)
72
+ Dir.delete pkg if Dir.entries(pkg)
73
+ end
74
+ end
75
+
76
+ # Set parameters of JAR builder
77
+ def method_missing(method, *args)
78
+ if not @builder.method(method).nil?
79
+ @builder.method(method).call(*args)
80
+ else
81
+ super(method, *args)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,38 @@
1
+ =begin
2
+ Simple metaclass to create extensions for builder.
3
+
4
+ Copyright (C) 2008 Andrey "A.I." Sitnik <andrey@sitnik.ru>
5
+
6
+ This program is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ =end
19
+
20
+ module Ruby2Jar
21
+ # Simple metaclass to create extensions for builder, which provide user
22
+ # interface or special functions, such as creating Java Web Start files.
23
+ #
24
+ # It add all listener methods started by "before_" to builder.
25
+ class Listener
26
+ # Add listeners to +builder+
27
+ def initialize(builder = nil)
28
+ if not builder.nil?
29
+ @builder = builder
30
+ builder.methods.reject {|i| not i =~ /^(before_|on_error$)/}.each do |m|
31
+ if methods.include? m
32
+ builder.method(m).call << method(m)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,105 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Ruby2Jar::Builder do
4
+ before :each do
5
+ @builder = Ruby2Jar::Builder.new
6
+ @builder.path = "."
7
+ @builder.include_jruby = false
8
+ @builder.before_copy << lambda { @builder.stop }
9
+ end
10
+
11
+ it "should set result jar path automatically" do
12
+ tmpdir = TempDir.create
13
+ FileUtils.mkdir File.join(tmpdir, "my_project")
14
+ FileUtils.touch File.join(tmpdir, "my_application.rb")
15
+
16
+ @builder.path = File.join(tmpdir, "my_project")
17
+ @builder.build
18
+
19
+ @builder.jar.should == File.join(tmpdir, "my_project", "pkg", "my_project.jar")
20
+
21
+ @builder.path = File.join(tmpdir, "my_application.rb")
22
+ @builder.jar = nil
23
+ @builder.build
24
+
25
+ @builder.jar.should == File.join(tmpdir, "my_application.jar")
26
+
27
+ tmpdir.delete
28
+ end
29
+
30
+ it "should raise error if path didn't set" do
31
+ @builder.path = nil
32
+ @builder.method(:build).should raise_error(Ruby2Jar::Error, /didn't set path/)
33
+ end
34
+
35
+ it "should raise error if path isn't exist" do
36
+ nodir = TempDir.new
37
+
38
+ @builder.path = nodir
39
+ @builder.method(:build).should raise_error(Ruby2Jar::Error, /path is already exist/)
40
+ end
41
+
42
+ it "should raise error if result jar is already exist" do
43
+ tmpdir = TempDir.create
44
+ FileUtils.touch File.join(tmpdir, "result.jar")
45
+ @builder.jar = File.join(tmpdir, "result.jar")
46
+
47
+ @builder.method(:build).should raise_error(Ruby2Jar::Error, /jar/)
48
+
49
+ tmpdir.delete
50
+ end
51
+
52
+ it "should raise error if main script isn't exist" do
53
+ tmpdir = TempDir.create
54
+ @builder.path = tmpdir
55
+ @builder.main = "script.rb"
56
+
57
+ @builder.method(:build).should raise_error(Ruby2Jar::Error, /Main script isn't exist/)
58
+
59
+ FileUtils.touch File.join(tmpdir, "script.rb")
60
+ @builder.method(:build).should_not raise_error(Ruby2Jar::Error)
61
+
62
+ tmpdir.delete
63
+ end
64
+
65
+ it "should raise error if it can't find JRuby jar" do
66
+ jruby = ENV['JRUBY_HOME']
67
+ ENV['JRUBY_HOME'] = nil
68
+ @builder.include_jruby = true
69
+
70
+ @builder.method(:build).should raise_error(Ruby2Jar::Error, /Can't find JRuby/)
71
+
72
+ ENV['JRUBY_HOME'] = jruby
73
+ end
74
+
75
+ it "should raise error if JRuby jar isn't exist" do
76
+ @builder.include_jruby = true
77
+ tmpdir = TempDir.new
78
+ @builder.jruby = File.join(tmpdir, "jruby.jar")
79
+
80
+ @builder.method(:build).should raise_error(Ruby2Jar::Error, /JRuby isn't exist/)
81
+ end
82
+
83
+ it "should find JRuby jar by JRUBY_HOME enviroment" do
84
+ jruby = ENV['JRUBY_HOME']
85
+ ENV['JRUBY_HOME'] = File.join(File.dirname(__FILE__), "fixtures", "jruby")
86
+ @builder.include_jruby = true
87
+
88
+ @builder.build
89
+ @builder.jruby.should == File.join(ENV['JRUBY_HOME'], "lib", "jruby.jar")
90
+
91
+ ENV['JRUBY_HOME'] = jruby
92
+ end
93
+
94
+ it "should create temporal dir on start and delete it on finish " do
95
+ @build_dir = nil
96
+ @builder.before_copy << lambda {
97
+ @build_dir = @builder.build_dir
98
+ @build_dir.should_not be_nil
99
+ File.directory?(@build_dir).should be_true
100
+ }
101
+
102
+ @builder.build
103
+ File.directory?(@build_dir).should be_false
104
+ end
105
+ end
@@ -0,0 +1,100 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Ruby2Jar::Builder do
4
+ include DirHelper
5
+
6
+ it "should do all steps and extension functions in order" do
7
+ builder = TracerBuilder.new
8
+ builder.before_copy << lambda {
9
+ builder.steps << "touch"
10
+ }
11
+
12
+ builder.build
13
+ builder.steps.should == [
14
+ "before_start", "start",
15
+ "before_copy", "touch", "copy",
16
+ "before_create_init", "create_init",
17
+ "before_compile", "compile",
18
+ "before_package", "package",
19
+ "before_finish", "finish"]
20
+ end
21
+
22
+ it "should can stop finish it work" do
23
+ builder = TracerBuilder.new
24
+ builder.before_create_init << lambda {
25
+ builder.steps << "STOP"
26
+ builder.stop
27
+ }
28
+
29
+ builder.build
30
+ builder.steps.should == [
31
+ "before_start", "start",
32
+ "before_copy", "copy",
33
+ "before_create_init", "STOP",
34
+ "before_finish", "finish"]
35
+ end
36
+
37
+ it "should finish it work on exception" do
38
+ builder = TracerBuilder.new
39
+ builder.before_copy << lambda {
40
+ builder.steps << "SHOT"
41
+ raise "Shot"
42
+ }
43
+ builder.before_finish << lambda {
44
+ builder.steps << "HEADSHOT"
45
+ raise "Headshot"
46
+ }
47
+
48
+ begin
49
+ builder.build
50
+ rescue; end
51
+ builder.steps.should == [
52
+ "before_start", "start",
53
+ "before_copy", "SHOT", "on_error Shot",
54
+ "before_finish", "HEADSHOT", "finish"]
55
+ end
56
+
57
+ it "should raise error if it doesn't rescue on_error extensions" do
58
+ builder = TracerBuilder.new
59
+ builder.before_start << lambda { raise }
60
+
61
+ builder.on_error = [lambda { true }]
62
+ builder.method(:build).should_not raise_error
63
+
64
+ builder.on_error = [lambda { false }]
65
+ builder.method(:build).should raise_error
66
+ end
67
+
68
+ it "should remember added gems" do
69
+ builder = Ruby2Jar::Builder.new
70
+ builder.add_dependency "one"
71
+ builder.add_dependency "two", ">=0.1", "< 1.0"
72
+ builder.gems.should == [["one", []], ["two", [">=0.1", "< 1.0"]]]
73
+ end
74
+
75
+ it "should compile all Ruby and Java files" do
76
+ builder = Ruby2Jar::Builder.new
77
+ builder.path = File.join(File.dirname(__FILE__), "fixtures", "app")
78
+
79
+ builder.before_package << lambda {
80
+ builder.build_dir.should contain_files("ruby/app/main.class",
81
+ "ruby/app/greeters/english.class", "greeters/JavaGreeter.class")
82
+ builder.stop
83
+ }
84
+ builder.build
85
+ end
86
+
87
+ it "should create working JAR" do
88
+ builder = Ruby2Jar::Builder.new
89
+ builder.path = File.join(File.dirname(__FILE__), "fixtures", "app")
90
+ builder.main = "main.rb"
91
+ dir = TempDir.create
92
+ builder.include_jruby = true
93
+ builder.jar = File.join(dir, "result.jar")
94
+ builder.build
95
+
96
+ `java -jar #{builder.jar}`.should == "Hello World!\n"
97
+
98
+ dir.delete
99
+ end
100
+ end
@@ -0,0 +1,138 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Ruby2Jar::Builder do
4
+ include DirHelper
5
+
6
+ before :each do
7
+ @builder = Ruby2Jar::Builder.new
8
+ @builder.include_jruby = false
9
+ @app_path = TempDir.create
10
+ @builder.path = @app_path
11
+ @builder.before_create_init << lambda { @builder.stop }
12
+ @builder.gems_index = FakeGems.new
13
+ end
14
+
15
+ it "should copy all neccessary file from application dir" do
16
+ create_files @builder.path, ["1.rb", "1.no", "subdir/2.rb", "subdir/3.rb"]
17
+ Dir.chdir(@builder.path) do
18
+ @builder.files = FileList["**/*"].exclude("*.no")
19
+ end
20
+
21
+ @builder.before_create_init << lambda {
22
+ @builder.build_dir.should contain_files(
23
+ "ruby/app/1.rb", "ruby/app/subdir/2.rb", "ruby/app/subdir/3.rb")
24
+ }
25
+ @builder.build
26
+ end
27
+
28
+ it "should copy file if application is a one script" do
29
+ create_files @builder.path, "application.rb"
30
+ @builder.path = File.join(@builder.path, "application.rb")
31
+
32
+ @builder.before_create_init << lambda {
33
+ @builder.build_dir.should contain_files("ruby/app/application.rb")
34
+ }
35
+ @builder.build
36
+ end
37
+
38
+ it "should copy all Java file to archive root" do
39
+ create_files @builder.path, ["one.rb", "ext/subdir/pure.java"]
40
+
41
+ @builder.before_create_init << lambda {
42
+ @builder.build_dir.should contain_files("ruby/app/one.rb", "subdir/pure.java")
43
+ }
44
+ @builder.build
45
+ end
46
+
47
+ it "should delete java source file if compiled class file if already exist" do
48
+ create_files @builder.path, ["one.rb", "ext/pure.java", "ext/pure.class"]
49
+
50
+ @builder.before_create_init << lambda {
51
+ @builder.build_dir.should contain_files("ruby/app/one.rb", "pure.class")
52
+ }
53
+ @builder.build
54
+ end
55
+
56
+ it "should raise error if gem isn't found" do
57
+ @builder.gems_index.gems = {}
58
+ @builder.add_dependency "gem"
59
+ @builder.method(:build).should raise_error(Ruby2Jar::Error, /Can't find gem/)
60
+ end
61
+
62
+ it "should raise error if gem with different version is already added" do
63
+ @builder.gems_index.gems = {
64
+ "gem-1.0" => FakeSpecification.new("gem", "1.0", @builder.path),
65
+ "gem-0.9" => FakeSpecification.new("gem", "0.9", @builder.path)}
66
+ @builder.add_dependency "gem", "1.0"
67
+ @builder.add_dependency "gem", "0.9"
68
+
69
+ @builder.method(:build).should raise_error(Ruby2Jar::Error, /different version/)
70
+ end
71
+
72
+ it "should copy gem dependencies too" do
73
+
74
+ @builder.gems_index.gems = {
75
+ "one-" => FakeSpecification.new("one", "", @builder.path, ["two"]),
76
+ "two-" => FakeSpecification.new("two", "", @builder.path, ["one"])}
77
+ @builder.add_dependency "one"
78
+
79
+ @builder.before_create_init << lambda {
80
+ @builder.loaded_gems.should == ["one", "two"]
81
+ }
82
+ @builder.build
83
+ end
84
+
85
+ it "should load java config from gem" do
86
+ gem_path = TempDir.create
87
+ File.open(File.join(gem_path, "java.yaml"), "w") do |file|
88
+ file.write({"name" => "value"}.to_yaml)
89
+ end
90
+ @builder.gems_index.gems = {
91
+ "gem-" => FakeSpecification.new("gem", "", gem_path)}
92
+ @builder.add_dependency "gem"
93
+
94
+ @builder.before_create_init << lambda {
95
+ @builder.configs["gem"].should == {"name" => "value"}
96
+ }
97
+ @builder.build
98
+
99
+ gem_path.delete
100
+ end
101
+
102
+ it "should copy all neccessary file from gem dir" do
103
+ gem_path = TempDir.create
104
+ create_files gem_path, ["one.rb", "1.no", "subdir/two.rb", "ext/pure.java"]
105
+ File.open(File.join(gem_path, "java.yaml"), "w") do |file|
106
+ file.write({"jar" => {"exclude" => "*.no"}}.to_yaml)
107
+ end
108
+ @builder.gems_index.gems = {
109
+ "gem-" => FakeSpecification.new("gem", "", gem_path)}
110
+ @builder.add_dependency "gem"
111
+
112
+ @builder.before_create_init << lambda {
113
+ @builder.build_dir.should contain_files(
114
+ "ruby/gems/gem/one.rb", "ruby/gems/gem/subdir/two.rb", "pure.java")
115
+ }
116
+ @builder.build
117
+
118
+ gem_path.delete
119
+ end
120
+
121
+ it "should add gem and app paths to init_require_paths" do
122
+ @builder.gems_index.gems = {
123
+ "gem-" => FakeSpecification.new("gem", "", @builder.path)}
124
+ @builder.add_dependency "gem"
125
+ @builder.require_paths = ["one", "two"]
126
+
127
+ @builder.before_create_init << lambda {
128
+ @builder.init_require_paths.should include("ruby/gems/gem/lib")
129
+ @builder.init_require_paths.should include("ruby/app/one")
130
+ @builder.init_require_paths.should include("ruby/app/two")
131
+ }
132
+ @builder.build
133
+ end
134
+
135
+ after :each do
136
+ @app_path.delete
137
+ end
138
+ end