Guest User

Untitled

a guest
Jan 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. /**
  2. * @fileoverview Script to package up an existing RingoJS application as a WAR.
  3. */
  4.  
  5. //require('core/string');
  6. var {join, Path, makeDirectory, move, copy, exists, symbolicLink, base, removeTree} = require('fs');
  7. var engine = require('ringo/engine');
  8. var shell = require('ringo/shell');
  9. var Parser = require('ringo/args').Parser;
  10.  
  11. export('createApplication', 'main', 'description');
  12.  
  13. var description = "Package an existing RingoJS application as a WAR";
  14.  
  15. function copyStream(file, out) {
  16. var input = new java.io.FileInputStream(file);
  17. try {
  18. var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
  19. while (true) {
  20. var readCount = input.read(buffer);
  21. if (readCount < 0) {
  22. break;
  23. }
  24. out.write(buffer, 0, readCount);
  25. }
  26. } finally {
  27. input.close();
  28. }
  29. }
  30.  
  31. function zip(directory, zipfile) {
  32. directory = java.io.File(directory);
  33. zipfile = java.io.File(zipfile);
  34. var base = directory.toURI();
  35. var queue = new java.util.LinkedList();
  36. queue.push(directory);
  37. var out = new java.io.FileOutputStream(zipfile);
  38. var res = out;
  39. try {
  40. var zout = new java.util.zip.ZipOutputStream(out);
  41. res = zout;
  42. while (!queue.isEmpty()) {
  43. directory = queue.pop();
  44. var files = directory.listFiles();
  45. for (var i in files) {
  46. var kid = files[i];
  47. var name = base.relativize(kid.toURI()).getPath();
  48. if (kid.isDirectory()) {
  49. queue.push(kid);
  50. name = name.charAt(name.length - 1) == '/' ? name : name + "/";
  51. zout.putNextEntry(new java.util.zip.ZipEntry(name));
  52. } else {
  53. zout.putNextEntry(new java.util.zip.ZipEntry(name));
  54. copyStream(kid, zout);
  55. zout.closeEntry();
  56. }
  57. }
  58. }
  59. } finally {
  60. res.close();
  61. }
  62. }
  63.  
  64. /**
  65. * Create a new RingoJS web application at the given path.
  66. * @param path the path where to create the application
  67. */
  68. function createApplication(path, options) {
  69. if (!path) {
  70. throw new Error("No destination path given");
  71. }
  72.  
  73. var home = engine.properties["ringo.home"];
  74. var war = new Path(path);
  75. var isDir = path.lastIndexOf('.') <= path.lastIndexOf('/');
  76. var dest = isDir ? path : path.substr(0, path.lastIndexOf('.'));
  77.  
  78. if(exists(dest)) removeTree(dest);
  79.  
  80. copyTree(home, "apps/" + (options.appengine ? "appengine" : "webapp"), dest);
  81.  
  82. copyTree(".", "", join(dest, "WEB-INF", "app"));
  83. copyTree(home, "modules", join(dest, "WEB-INF", "modules"));
  84. // uncomment this line if /lib is removed from webapp
  85. var libDir = join(dest, "WEB-INF", "lib");
  86. if(!exists(libDir)) makeDirectory(libDir);
  87. fixWebappDirs(dest);
  88. copyJars(home, dest);
  89.  
  90. copyTree("./jars", "", join(dest, "WEB-INF", "lib"));
  91. copyTree("./config", "", join(dest, "WEB-INF", "classes"));
  92. //copyPackages(home, dest);
  93. //TODO clean
  94. //removeTree(join(dest, "WEB-INF", "app", ".git"));
  95.  
  96. if(options.appengine) {
  97. if(exists("./appengine-web.xml")) {
  98. var webxml = join(dest, "WEB-INF", "appengine-web.xml");
  99. if(exists(webxml)) removeTree(webxml);
  100. copy("./appengine-web.xml", webxml);
  101. }
  102. removeTree(join(dest, "WEB-INF", "app", "jars"));
  103. removeTree(join(dest, "WEB-INF", "app", "appengine-web.xml"));
  104. }
  105.  
  106. if(!isDir) {
  107. zip(dest, war);
  108. removeTree(dest);
  109. }
  110. }
  111.  
  112. function copyPackages(home, dest) {
  113. var libdest = join(dest, "WEB-INF", "lib");
  114. var modulesdest = join(dest, "WEB-INF", "modules");
  115. var packages = java.io.File(new Path(join(home, "packages"))).listFiles();
  116. for (var i in packages) {
  117. var p = packages[i];
  118. if(p.isDirectory()) {
  119. var n = p.getName();
  120. try {
  121. copyTree(home, join("packages", n, "jars"), libdest);
  122. } catch(e) {
  123. }
  124. copyTree(home, join("packages", n, "lib"), modulesdest);
  125. }
  126. }
  127. }
  128.  
  129. function copyTree(home, from, to) {
  130. var source = new Path(home, from);
  131. if (!source.exists() || !source.isDirectory()) {
  132. // throw new Error("Can't find directory " + source);
  133. return;
  134. }
  135. source.copyTree(to);
  136. }
  137.  
  138. function fixWebappDirs(dest) {
  139. var webinf = join(dest, "WEB-INF");
  140. makeDirectory(join(webinf, "classes"));
  141. makeDirectory(join(webinf, "packages"));
  142. var staticDir = join(webinf, "app", "static");
  143. if (exists(staticDir)) {
  144. move(staticDir, join(dest, "static"));
  145. }
  146. }
  147.  
  148. function copyJars(home, dest) {
  149. var jars = [
  150. "ringo.jar",
  151. "js.jar",
  152. //"slf4j/slf4j-api-1.5.10.jar" //interferes with appengine
  153. ];
  154. var libsrc = join(home, "lib");
  155. var libdest = join(dest, "WEB-INF", "lib");
  156. for each (var jar in jars) {
  157. copy(join(libsrc, jar), join(libdest, base(jar)));
  158. }
  159. }
  160.  
  161.  
  162. /**
  163. * Create a new RingoJS web application from the command line.
  164. * @param args
  165. */
  166. function main(args) {
  167. var script = args.shift();
  168. var parser = new Parser();
  169. parser.addOption("a", "appengine", null, "Add AppEngine specific files");
  170. parser.addOption("h", "help", null, "Print help message and exit");
  171. var opts = parser.parse(args);
  172. if (opts.help) {
  173. print("Packages an existing RingoJS application as a web application archive (WAR)");
  174. print("Usage:");
  175. print(" ringo " + script + " [path of webapp dir or WAR file]");
  176. print("Options:");
  177. print(parser.help());
  178. return;
  179. }
  180.  
  181. var path = args[0]
  182. || shell.readln("Please enter the path for your WAR or webapp dir: ");
  183.  
  184. if (!path) {
  185. print("No path, exiting.");
  186. } else if (opts.package) {
  187. createPackage(path, opts);
  188. } else {
  189. createApplication(path, opts);
  190. }
  191. }
  192.  
  193. if (require.main == module.id) {
  194. try {
  195. main(system.args);
  196. } catch (error) {
  197. print(error);
  198. print("Use -h or --help to get a list of available options.");
  199. }
  200. }
Add Comment
Please, Sign In to add comment