/lib/rawr/creator.rb

https://github.com/tychobrailleur/rawr · Ruby · 131 lines · 110 code · 21 blank · 0 comment · 9 complexity · eeb4f23ff1684721098a016d7cf2c8fe MD5 · raw file

  1. require 'rawr/configuration'
  2. module Rawr
  3. class Creator
  4. def self.create_run_config_file(options)
  5. File.open(File.join(options.compile_dir, 'run_configuration'), "w+") do |run_config_file|
  6. run_config_file << "main_ruby_file: " + options.main_ruby_file + "\n"
  7. end
  8. end
  9. def self.create_manifest_file options
  10. metainf_dir_path = File.join options.compile_dir, 'META-INF'
  11. manifest_path = File.join metainf_dir_path, 'MANIFEST.MF'
  12. lib_dirs = options.classpath.map {|cp| cp.gsub('../', '')}
  13. lib_jars = options.jars.keys.map {|key| key.to_s + ".jar"}
  14. libraries = lib_dirs + lib_jars + ["."]
  15. File.open(manifest_path, "w+") do |manifest_file|
  16. manifest_file << "Manifest-Version: 1.0\n"
  17. manifest_file << "Class-Path: " + libraries.join("\n ") + "\n"
  18. manifest_file << "Main-Class: " + options.main_java_file + "\n"
  19. end
  20. end
  21. def self.create_java_main_file java_file, java_package, java_class
  22. File.open(java_file, "w+") do |java_main_file|
  23. java_main_file << <<-ENDL
  24. package #{java_package};
  25. import java.io.BufferedReader;
  26. import java.io.InputStreamReader;
  27. import java.io.InputStream;
  28. import java.io.IOException;
  29. import java.net.URL;
  30. import java.util.ArrayList;
  31. import org.jruby.Ruby;
  32. import org.jruby.RubyInstanceConfig;
  33. import org.jruby.javasupport.JavaEmbedUtils;
  34. public class #{java_class} {
  35. public static void main(String[] args) throws Exception {
  36. RubyInstanceConfig config = new RubyInstanceConfig();
  37. config.setArgv(args);
  38. Ruby runtime = JavaEmbedUtils.initialize(new ArrayList(0), config);
  39. String mainRubyFile = "main";
  40. ArrayList<String> config_data = new ArrayList<String>();
  41. try{
  42. java.io.InputStream ins = Main.class.getClassLoader().getResourceAsStream("run_configuration");
  43. if (ins == null ) {
  44. System.err.println("Did not find configuration file 'run_configuration', using defaults.");
  45. } else {
  46. config_data = getConfigFileContents(ins);
  47. }
  48. }
  49. catch(IOException ioe) {
  50. System.err.println("Error loading run configuration file 'run_configuration', using defaults: " + ioe);
  51. }
  52. catch(java.lang.NullPointerException npe) {
  53. System.err.println("Error loading run configuration file 'run_configuration', using defaults: " + npe );
  54. }
  55. for(String line : config_data) {
  56. String[] parts = line.split(":");
  57. if("main_ruby_file".equals(parts[0].replaceAll(" ", ""))) {
  58. mainRubyFile = parts[1].replaceAll(" ", "");
  59. }
  60. }
  61. runtime.evalScriptlet("require '" + mainRubyFile + "'");
  62. }
  63. public static URL getResource(String path) {
  64. return Main.class.getClassLoader().getResource(path);
  65. }
  66. private static ArrayList<String> getConfigFileContents(InputStream input) throws IOException, java.lang.NullPointerException {
  67. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  68. String line;
  69. ArrayList<String> contents = new ArrayList<String>();
  70. while ((line = reader.readLine()) != null) {
  71. contents.add(line);
  72. }
  73. reader.close();
  74. return(contents);
  75. }
  76. }
  77. ENDL
  78. end
  79. end
  80. def self.w msg
  81. warn(msg) if ENV['WORDY']
  82. end
  83. def self.create_default_config_file config_path, java_class, project_name=nil
  84. w "Creating default config file in '#{config_path}' with project_name #{project_name} ..."
  85. Rawr::Configuration.project_name = project_name if project_name
  86. w "Project name is #{Rawr::Configuration.project_name}"
  87. File.open(config_path, "w+") do |config_file|
  88. config_file << "configuration do |c|\n"
  89. Rawr::Configuration::OPTIONS.each do |option|
  90. warn "option #{option.name}: #{option.value || option.default}"
  91. doc_string = option.comment
  92. doc_string ||= "Undocumented option '#{option.name}'"
  93. config_file << "\t# #{doc_string}\n"
  94. config_file << "\t# default value: #{option.default.inspect}\n"
  95. config_file << "\t#\n"
  96. case option.name
  97. when :extra_user_jars
  98. config_file << "\t#c.extra_user_jars[:data] = { :directory => 'data/images/png',\n"
  99. config_file << "\t# :location_in_jar => 'images',\n"
  100. config_file << "\t# :exclude => /*.bak$/ }\n"
  101. else
  102. config_file << "\t#c.#{option.name} = #{option.default.inspect}\n"
  103. end
  104. config_file << "\n"
  105. end
  106. config_file << "end\n"
  107. end
  108. end
  109. end
  110. end