Program.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Xml.Linq;
  8. using System.Diagnostics;
  9. using Mvp.Xml.XInclude;
  10. using System.Xml;
  11. namespace BuildOurOwnBabylonJS
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. if (args.Length < 3)
  18. {
  19. DisplayUsage();
  20. Environment.Exit(1);
  21. return;
  22. }
  23. // Parsing arguments
  24. var ourOwnBabylonJSXmlFilePath = "";
  25. var scriptsFolderPath = "";
  26. var shadersFolderPath = "";
  27. var outputFolderPath = "";
  28. var JSKompactorFolderPath = "";
  29. foreach (var arg in args)
  30. {
  31. var order = arg.Substring(0, 3);
  32. switch (order)
  33. {
  34. case "/i:":
  35. ourOwnBabylonJSXmlFilePath = arg.Substring(3);
  36. break;
  37. case "/w:":
  38. scriptsFolderPath = arg.Substring(3);
  39. break;
  40. case "/s:":
  41. shadersFolderPath = arg.Substring(3);
  42. break;
  43. case "/o:":
  44. outputFolderPath = arg.Substring(3);
  45. break;
  46. case "/k:":
  47. JSKompactorFolderPath = arg.Substring(3);
  48. break;
  49. default:
  50. DisplayUsage();
  51. Environment.Exit(1);
  52. return;
  53. }
  54. }
  55. if (String.IsNullOrEmpty(ourOwnBabylonJSXmlFilePath)
  56. || String.IsNullOrEmpty(scriptsFolderPath)
  57. || String.IsNullOrEmpty(shadersFolderPath)
  58. || String.IsNullOrEmpty(outputFolderPath))
  59. {
  60. DisplayUsage();
  61. Environment.Exit(1);
  62. return;
  63. }
  64. try
  65. {
  66. ParseListOfFiles(ourOwnBabylonJSXmlFilePath);
  67. if (String.IsNullOrEmpty(JSKompactorFolderPath))
  68. JSKompactorFolderPath = "executables";
  69. var batchFilePath = WriteBatchFile(scriptsFolderPath, ComupteDependencies(),
  70. shadersFolderPath, outputFolderPath, JSKompactorFolderPath);
  71. CallBatchFile(batchFilePath);
  72. }
  73. catch (Exception ex)
  74. {
  75. Error(ex);
  76. Environment.Exit(1);
  77. }
  78. Environment.Exit(0);
  79. }
  80. private static void ParseListOfFiles(string path)
  81. {
  82. var reader = new XIncludingReader(XmlReader.Create(path));
  83. var document = XDocument.Load(reader);
  84. var files = document.Root;
  85. var scriptElements = files.Elements(Script.TAGNAME);
  86. foreach (var scriptElement in scriptElements)
  87. {
  88. Script.Load(scriptElement, scriptElements);
  89. }
  90. }
  91. private static IEnumerable<string> ComupteDependencies()
  92. {
  93. var scripts = Script.Scripts;
  94. var result = new List<string>(scripts.Count);
  95. foreach (var script in scripts)
  96. {
  97. var v = script.Value;
  98. if (v == null)
  99. continue;
  100. v.GetDependenciesList(ref result);
  101. }
  102. return result;
  103. }
  104. private static string WriteBatchFile(string scriptsFolderPath, IEnumerable<string> scripts,
  105. string shadersFolderPath, string outputFolderPath, string jskompactorPath)
  106. {
  107. if (scripts == null || scripts.Count() == 0)
  108. throw new Exception("A list of files was not provided.");
  109. var count = scripts.Count();
  110. var output = "";
  111. for (var i = count - 1; i >= 0; --i)
  112. {
  113. output += "," + scripts.ElementAt(i);
  114. }
  115. output = output.Substring(1);
  116. var batchFilePath = "jskompactor.bat";
  117. using (var batchFile = new StreamWriter(batchFilePath, false))
  118. {
  119. batchFile.Write("\"" + jskompactorPath + "\\JSKompactor.exe\" /i:\"" + output
  120. + "\" /o:\"" + outputFolderPath + "\\ourOwnBabylon.js\" /w:\"" + scriptsFolderPath + "\" /s:\"" + shadersFolderPath + "\"");
  121. }
  122. return batchFilePath;
  123. }
  124. private static void CallBatchFile(string batchFilePath)
  125. {
  126. if (String.IsNullOrEmpty(batchFilePath) || !File.Exists(batchFilePath))
  127. throw new Exception("No batch file");
  128. Process.Start(batchFilePath);
  129. }
  130. private static void DisplayUsage()
  131. {
  132. Console.WriteLine("BuildOurOwnBabylonJS usage: BuildOurOwnBabylonJS.exe /w:\"Working folder\" /i:\"Path of the xml file containing list of files to merge\" /s:\"Shaders folder\" /o:\"Output folder\" [/k:\"Path to JSKompactor.exe\"]");
  133. }
  134. private static void Error(Exception ex)
  135. {
  136. Console.ForegroundColor = ConsoleColor.Red;
  137. Console.WriteLine();
  138. Console.WriteLine(ex.Message);
  139. Console.ResetColor();
  140. }
  141. }
  142. }