Skip to content

Commit

Permalink
Merge pull request #11 from whoward/template-files
Browse files Browse the repository at this point in the history
Use ERB for generated configuration files
  • Loading branch information
Neurogami committed May 29, 2012
2 parents ae91fa3 + 1e77321 commit d40d56d
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 157 deletions.
51 changes: 2 additions & 49 deletions lib/rawr/app_bundler.rb
@@ -1,5 +1,6 @@
require 'fileutils'
require 'rawr/bundler'
require 'rawr/bundler_template'

# See http://developer.apple.com/documentation/Java/Reference/Java_InfoplistRef/Articles/JavaDictionaryInfo.plistKeys.html for details

Expand Down Expand Up @@ -77,55 +78,7 @@ def generate_info_plist
mac_icon_filename = @mac_icon_path.sub(File.dirname(@mac_icon_path) + '/', '')

File.open "Info.plist", 'w' do |file|
file << <<-INFO_ENDL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>CFBundleName</key>
<string>#{@project_name}</string>
<key>CFBundleVersion</key>
<string>100.0</string>
<key>CFBundleAllowMixedLocalizations</key>
<string>true</string>
<key>CFBundleExecutable</key>
<string>JavaApplicationStub</string>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleIconFile</key>
<string>#{mac_icon_filename}</string>
<key>Java</key>
<dict>
<key>MainClass</key>
<string>#{@main_java_class}</string>
<key>JVMVersion</key>
<string>#{@target_jvm_version}*</string>
<key>ClassPath</key>
<array>
<string>$JAVAROOT/#{@project_name}.jar</string>
</array>
<key>WorkingDirectory</key>
<string>#{@working_directory}</string>
<key>Properties</key>
<dict>
<key>apple.laf.useScreenMenuBar</key>
<string>true</string>
#{"<key>java.library.path</key>\n<string>$JAVAROOT/" + @java_library_path + "</string>" unless @java_library_path.nil? || @java_library_path.strip.empty?}
</dict>
<key>VMOptions</key>
<array>
#{@jvm_arguments.split(' ').map {|arg| "<string>" + arg + "</string>\n"}}
</array>
</dict>
</dict>
</plist>
INFO_ENDL
file << BundlerTemplate.find('app_bundler', 'Info.plist').result(binding)
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions lib/rawr/bundler_template.rb
@@ -0,0 +1,25 @@
require 'erb'

module Rawr
class BundlerTemplate < ERB

def self.find(bundle_type, template_name)
filename = File.join(File.dirname(__FILE__), 'templates', bundle_type, "#{template_name}.erb")

new(filename)
end

def initialize(filename)
# As of JRuby 1.6.7 $SAFE is not supported in ERB, but this shouldn't
# matter anyways since any templates loaded should be trusted.
#
# '>' causes ERB to eat newlines on lines which contain only ERB <% %>
# tags (for pretty output). This can probably be improved a bit.
super(File.read(filename), nil, '>')
end

#TODO: consider providing access to configuration here and adding a
# render() function, rather than rendering in the Bundle class

end
end
82 changes: 2 additions & 80 deletions lib/rawr/creator.rb
@@ -1,4 +1,5 @@
require 'rawr/configuration'
require 'rawr/bundler_template'

module Rawr
class Creator
Expand Down Expand Up @@ -26,86 +27,7 @@ def self.create_manifest_file options

def self.create_java_main_file java_file, java_package, java_class
File.open(java_file, "w+") do |java_main_file|
java_main_file << <<-ENDL
package #{java_package};
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.javasupport.JavaEmbedUtils;
public class #{java_class} {
public static void debuggery(String message){
System.err.println("DEBUGGERY:" + message); // JGBDEBUG
}
public static void main(String[] args) throws Exception {
RubyInstanceConfig config = new RubyInstanceConfig();
config.setArgv(args);
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList(0), config);
String mainRubyFile = "main";
String runConfigFile = "run_configuration";
ArrayList<String> config_data = new ArrayList<String>();
try{
java.io.InputStream ins = Main.class.getClassLoader().getResourceAsStream(runConfigFile);
if (ins == null ) {
System.err.println("Did not find configuration file '" + runConfigFile + "', using defaults.");
} else {
config_data = getConfigFileContents(ins);
}
}
catch(IOException ioe) {
System.err.println("Error loading run configuration file '" + runConfigFile + "', using defaults: " + ioe);
}
catch(java.lang.NullPointerException npe) {
System.err.println("Error loading run configuration file '" + runConfigFile + "', using defaults: " + npe );
}
for(String line : config_data) {
String[] parts = line.split(":");
if("main_ruby_file".equals(parts[0].replaceAll(" ", ""))) {
mainRubyFile = parts[1].replaceAll(" ", "");
}
if("source_dirs".equals(parts[0].replaceAll(" ", ""))) {
String[] source_dirs = parts[1].split(";");
for(String s : parts[1].split(";") ){
String d = s.replaceAll(" ", "");
runtime.evalScriptlet( "$: << '"+d+"/'" );
}
}
}
runtime.evalScriptlet("require '" + mainRubyFile + "'");
}
public static URL getResource(String path) {
return Main.class.getClassLoader().getResource(path);
}
private static ArrayList<String> getConfigFileContents(InputStream input) throws IOException, java.lang.NullPointerException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
ArrayList<String> contents = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
contents.add(line);
}
reader.close();
return(contents);
}
}
ENDL
java_main_file << BundlerTemplate.find('java_runner', 'runner.java').result(binding)
end
end

Expand Down
30 changes: 2 additions & 28 deletions lib/rawr/exe_bundler.rb
Expand Up @@ -2,6 +2,7 @@

require 'rawr/bundler'
require 'rawr/platform'
require 'rawr/bundler_template'

module Rawr
class ExeBundler < Bundler
Expand Down Expand Up @@ -45,34 +46,7 @@ def deploy(options)


File.open(@launch4j_config_file, 'w') do |file|
file << <<-CONFIG_ENDL
<launch4jConfig>
<dontWrapJar>true</dontWrapJar>
<headerType>#{@executable_type}</headerType>
<jar>#{@project_name}.jar</jar>
<outfile>#{@project_name}.exe</outfile>
<errTitle></errTitle>
<jarArgs></jarArgs>
<chdir></chdir>
<customProcName>true</customProcName>
<stayAlive>false</stayAlive>
<icon>#{@icon_path}</icon>
<jre>
<path></path>
<minVersion>#{@minimum_windows_jvm_version}.0</minVersion>
<maxVersion></maxVersion>
<initialHeapSize>0</initialHeapSize>
<maxHeapSize>0</maxHeapSize>
<args>#{ @jvm_arguments unless @jvm_arguments.nil? || @jvm_arguments.strip.empty? } #{ "-Djava.library.path=" + @java_library_path unless @java_library_path.nil? || @java_library_path.strip.empty?}</args>
</jre>
<messages>
<startupErr>#{@startup_error_message}</startupErr>
<bundledJreErr>#{@bundled_jre_error_message}</bundledJreErr>
<jreVersionErr>#{@jre_version_error_message}</jreVersionErr>
<launcherErr>#{@launcher_error_message}</launcherErr>
</messages>
</launch4jConfig>
CONFIG_ENDL
file << BundlerTemplate.find('exe_bundler', 'configuration.xml').result(binding)
end

file_dir_name = File.dirname(__FILE__)
Expand Down
52 changes: 52 additions & 0 deletions lib/rawr/templates/app_bundler/Info.plist.erb
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>CFBundleName</key>
<string><%= @project_name %></string>
<key>CFBundleVersion</key>
<string>100.0</string>
<key>CFBundleAllowMixedLocalizations</key>
<string>true</string>
<key>CFBundleExecutable</key>
<string>JavaApplicationStub</string>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleIconFile</key>
<string><%= mac_icon_filename %></string>
<key>Java</key>
<dict>
<key>MainClass</key>
<string><%= @main_java_class %></string>
<key>JVMVersion</key>
<string><%= @target_jvm_version %>*</string>
<key>ClassPath</key>
<array>
<string>$JAVAROOT/<%= @project_name %>.jar</string>
</array>
<key>WorkingDirectory</key>
<string><%= @working_directory %></string>
<key>Properties</key>
<dict>
<key>apple.laf.useScreenMenuBar</key>
<string>true</string>
<% unless @java_library_path.nil? || @java_library_path.strip.empty? %>
<key>java.library.path</key>
<string>$JAVAROOT/<%= @java_library_path %></string>
<% end %>
</dict>
<key>VMOptions</key>
<array>
<% @jvm_arguments.split(' ').each do |arg| %>
<string><%= arg %></string>
<% end %>
</array>
</dict>
</dict>
</plist>
26 changes: 26 additions & 0 deletions lib/rawr/templates/exe_bundler/configuration.xml.erb
@@ -0,0 +1,26 @@
<launch4jConfig>
<dontWrapJar>true</dontWrapJar>
<headerType><%= @executable_type %></headerType>
<jar><%= @project_name %>.jar</jar>
<outfile><%= @project_name %>.exe</outfile>
<errTitle></errTitle>
<jarArgs></jarArgs>
<chdir></chdir>
<customProcName>true</customProcName>
<stayAlive>false</stayAlive>
<icon><%= @icon_path %></icon>
<jre>
<path></path>
<minVersion><%= @minimum_windows_jvm_version %>.0</minVersion>
<maxVersion></maxVersion>
<initialHeapSize>0</initialHeapSize>
<maxHeapSize>0</maxHeapSize>
<args><%= @jvm_arguments unless @jvm_arguments.nil? || @jvm_arguments.strip.empty? %> <%= "-Djava.library.path=" + @java_library_path unless @java_library_path.nil? || @java_library_path.strip.empty? %></args>
</jre>
<messages>
<startupErr><%= @startup_error_message %></startupErr>
<bundledJreErr><%= @bundled_jre_error_message %></bundledJreErr>
<jreVersionErr><%= @jre_version_error_message %></jreVersionErr>
<launcherErr><%= @launcher_error_message %></launcherErr>
</messages>
</launch4jConfig>
78 changes: 78 additions & 0 deletions lib/rawr/templates/java_runner/runner.java.erb
@@ -0,0 +1,78 @@
package <%= java_package %>;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;


import java.util.ArrayList;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.javasupport.JavaEmbedUtils;

public class <%= java_class %> {
public static void debuggery(String message) {
System.err.println("DEBUGGERY:" + message); // JGBDEBUG
}

public static void main(String[] args) throws Exception {
RubyInstanceConfig config = new RubyInstanceConfig();
config.setArgv(args);
Ruby runtime = JavaEmbedUtils.initialize(new ArrayList(0), config);
String mainRubyFile = "main";
String runConfigFile = "run_configuration";

ArrayList<String> config_data = new ArrayList<String>();
try{
java.io.InputStream ins = Main.class.getClassLoader().getResourceAsStream(runConfigFile);
if (ins == null ) {
System.err.println("Did not find configuration file '" + runConfigFile + "', using defaults.");
} else {
config_data = getConfigFileContents(ins);
}
}
catch(IOException ioe) {
System.err.println("Error loading run configuration file '" + runConfigFile + "', using defaults: " + ioe);
}
catch(java.lang.NullPointerException npe) {
System.err.println("Error loading run configuration file '" + runConfigFile + "', using defaults: " + npe );
}

for(String line : config_data) {

String[] parts = line.split(":");
if("main_ruby_file".equals(parts[0].replaceAll(" ", ""))) {
mainRubyFile = parts[1].replaceAll(" ", "");
}

if("source_dirs".equals(parts[0].replaceAll(" ", ""))) {
String[] source_dirs = parts[1].split(";");

for(String s : parts[1].split(";") ){
String d = s.replaceAll(" ", "");
runtime.evalScriptlet( "$: << '"+d+"/'" );
}
}
}

runtime.evalScriptlet("require '" + mainRubyFile + "'");
}

public static URL getResource(String path) {
return Main.class.getClassLoader().getResource(path);
}

private static ArrayList<String> getConfigFileContents(InputStream input) throws IOException, java.lang.NullPointerException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
ArrayList<String> contents = new ArrayList<String>();

while ((line = reader.readLine()) != null) {
contents.add(line);
}
reader.close();
return(contents);
}
}

0 comments on commit d40d56d

Please sign in to comment.