RunTravis.hx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using StringTools;
  2. import yaml.*;
  3. import sys.*;
  4. import sys.io.*;
  5. private typedef TravisConfig = {
  6. before_install: Array<String>,
  7. script: Array<String>
  8. }
  9. /**
  10. Will be run by TravisCI.
  11. See ".travis.yml" at project root for TravisCI settings.
  12. */
  13. class RunTravis {
  14. /**
  15. Run a command using `Sys.command()`.
  16. If the command exits with non-zero code, exit the whole script with the same code.
  17. If `useRetry` is `true`, the command will be re-run if it exits with non-zero code (3 trials).
  18. It is useful for running network-dependent commands.
  19. */
  20. static function runCommand(cmd:String, args:Array<String>, useRetry:Bool = false):Void {
  21. var trials = useRetry ? 3 : 1;
  22. var exitCode:Int = 1;
  23. while (trials-->0) {
  24. Sys.println('Command: $cmd $args');
  25. exitCode = Sys.command(cmd, args);
  26. Sys.println('Command exited with $exitCode: $cmd $args');
  27. if (exitCode == 0) {
  28. return;
  29. } else if (trials > 0) {
  30. Sys.println('Command will be re-run...');
  31. }
  32. }
  33. Sys.exit(exitCode);
  34. }
  35. static function getHaxelibPath(libName:String) {
  36. var proc = new sys.io.Process("haxelib", ["path", libName]);
  37. var result;
  38. var code = proc.exitCode();
  39. while(true) {
  40. result = proc.stdout.readLine();
  41. if (!result.startsWith("-L")) {
  42. break;
  43. }
  44. }
  45. proc.close();
  46. if (code != 0) {
  47. Sys.println(result);
  48. Sys.exit(code);
  49. }
  50. trace('Haxelib path for $libName: $result');
  51. return result;
  52. }
  53. static function changeDirectory(path:String) {
  54. Sys.println('Changing directory to $path');
  55. Sys.setCwd(path);
  56. }
  57. static function setupFlashPlayerDebugger():Void {
  58. Sys.putEnv("DISPLAY", ":99.0");
  59. runCommand("sh", ["-e", "/etc/init.d/xvfb", "start"]);
  60. Sys.putEnv("AUDIODEV", "null");
  61. runCommand("sudo", ["apt-get", "install", "-qq", "libgd2-xpm", "ia32-libs", "ia32-libs-multiarch", "-y"], true);
  62. runCommand("wget", ["-nv", "http://fpdownload.macromedia.com/pub/flashplayer/updaters/11/flashplayer_11_sa_debug.i386.tar.gz"], true);
  63. runCommand("tar", ["-xf", "flashplayer_11_sa_debug.i386.tar.gz", "-C", Sys.getEnv("HOME")]);
  64. File.saveContent(Sys.getEnv("HOME") + "/mm.cfg", "ErrorReportingEnable=1\nTraceOutputFileEnable=1");
  65. runCommand(Sys.getEnv("HOME") + "/flashplayerdebugger", ["-v"]);
  66. }
  67. static function runFlash(swf:String):Void {
  68. Sys.command(Sys.getEnv("HOME") + "/flashplayerdebugger", [swf, "&"]);
  69. //wait a little until flashlog.txt is created
  70. var flashlogPath = Sys.getEnv("HOME") + "/.macromedia/Flash_Player/Logs/flashlog.txt";
  71. for (t in 0...5) {
  72. runCommand("sleep", ["2"]);
  73. if (FileSystem.exists(flashlogPath))
  74. break;
  75. }
  76. if (!FileSystem.exists(flashlogPath)) {
  77. Sys.println('$flashlogPath not found.');
  78. Sys.exit(1);
  79. }
  80. //read flashlog.txt continously
  81. var traceProcess = new Process("tail", ["-f", "-v", flashlogPath]);
  82. var line = "";
  83. while (true) {
  84. try {
  85. line = traceProcess.stdout.readLine();
  86. Sys.println(line);
  87. if (line.indexOf("SUCCESS: ") >= 0) {
  88. Sys.exit(line.indexOf("SUCCESS: true") >= 0 ? 0 : 1);
  89. }
  90. } catch (e:haxe.io.Eof) {}
  91. }
  92. Sys.exit(1);
  93. }
  94. static function parseCommand(cmd:String) {
  95. var args = [];
  96. var offset = 0;
  97. var cur = new StringBuf();
  98. var inString = false;
  99. while(true) {
  100. switch(cmd.fastCodeAt(offset++)) {
  101. case '"'.code:
  102. inString = !inString;
  103. case ' '.code if (!inString):
  104. if (cur.length > 0) {
  105. args.push(cur.toString());
  106. cur = new StringBuf();
  107. }
  108. case '\\'.code:
  109. cur.addChar(cmd.fastCodeAt(offset++));
  110. case "$".code:
  111. switch (cmd.fastCodeAt(offset)) {
  112. case '('.code:
  113. ++offset;
  114. var env = new StringBuf();
  115. while(true) {
  116. switch(cmd.fastCodeAt(offset++)) {
  117. case ')'.code:
  118. break;
  119. case c:
  120. env.addChar(c);
  121. }
  122. }
  123. cur.add(Sys.getEnv(env.toString()));
  124. case _:
  125. cur.addChar("$".code);
  126. }
  127. case c:
  128. cur.addChar(c);
  129. }
  130. if (offset == cmd.length) {
  131. break;
  132. }
  133. }
  134. if (cur.length > 0) {
  135. args.push(cur.toString());
  136. }
  137. return args;
  138. }
  139. static function parseTravisFile(path:String, ignoreBeforeInstall = false) {
  140. var yaml:TravisConfig = yaml.Yaml.read(path, Parser.options().useObjects());
  141. if (!ignoreBeforeInstall) {
  142. for (code in yaml.before_install) {
  143. var args = parseCommand(code);
  144. var cmd = args.shift();
  145. runCommand(cmd, args);
  146. }
  147. }
  148. for (code in yaml.script) {
  149. var args = parseCommand(code);
  150. var cmd = args.shift();
  151. runCommand(cmd, args);
  152. }
  153. }
  154. static function getPhpDependencies() {
  155. runCommand("sudo", ["apt-get", "install", "php5", "-y"], true);
  156. }
  157. static function getCppDependencies(unitDir:String) {
  158. //hxcpp dependencies
  159. runCommand("sudo", ["apt-get", "install", "gcc-multilib", "g++-multilib", "-y"], true);
  160. //install and build hxcpp
  161. runCommand("haxelib", ["git", "hxcpp", "https://github.com/HaxeFoundation/hxcpp.git"], true);
  162. Sys.setCwd(Sys.getEnv("HOME") + "/haxelib/hxcpp/git/project/");
  163. runCommand("neko", ["build.n"]);
  164. Sys.setCwd(unitDir);
  165. }
  166. static function getJavaDependencies() {
  167. runCommand("haxelib", ["git", "hxjava", "https://github.com/HaxeFoundation/hxjava.git"], true);
  168. }
  169. static function getCsDependencies() {
  170. runCommand("sudo", ["apt-get", "install", "mono-devel", "mono-mcs", "-y"], true);
  171. runCommand("haxelib", ["git", "hxcs", "https://github.com/HaxeFoundation/hxcs.git"], true);
  172. }
  173. static function main():Void {
  174. var cwd = Sys.getCwd();
  175. var unitDir = cwd + "unit/";
  176. var optDir = cwd + "optimization/";
  177. Sys.setCwd(unitDir);
  178. switch (Sys.getEnv("TARGET")) {
  179. case "macro", null:
  180. runCommand("haxe", ["compile-macro.hxml"]);
  181. //generate documentation
  182. runCommand("haxelib", ["git", "hxparse", "https://github.com/Simn/hxparse", "development", "src"], true);
  183. runCommand("haxelib", ["git", "hxtemplo", "https://github.com/Simn/hxtemplo", "master", "src"], true);
  184. runCommand("haxelib", ["git", "hxargs", "https://github.com/Simn/hxargs.git"], true);
  185. runCommand("haxelib", ["git", "markdown", "https://github.com/dpeek/haxe-markdown.git", "master", "src"], true);
  186. runCommand("haxelib", ["git", "hxcpp", "https://github.com/HaxeFoundation/hxcpp.git"], true);
  187. runCommand("haxelib", ["git", "hxjava", "https://github.com/HaxeFoundation/hxjava.git"], true);
  188. runCommand("haxelib", ["git", "hxcs", "https://github.com/HaxeFoundation/hxcs.git"], true);
  189. runCommand("haxelib", ["git", "dox", "https://github.com/dpeek/dox.git"], true);
  190. Sys.setCwd(Sys.getEnv("HOME") + "/haxelib/dox/git/");
  191. runCommand("haxe", ["run.hxml"]);
  192. runCommand("haxe", ["gen.hxml"]);
  193. runCommand("haxelib", ["run", "dox", "-o", "bin/api.zip", "-i", "bin/xml"]);
  194. case "neko":
  195. runCommand("haxe", ["compile-neko.hxml"]);
  196. runCommand("neko", ["unit.n"]);
  197. case "php":
  198. getPhpDependencies();
  199. runCommand("haxe", ["compile-php.hxml"]);
  200. runCommand("php", ["php/index.php"]);
  201. case "cpp":
  202. getCppDependencies(unitDir);
  203. runCommand("haxe", ["compile-cpp.hxml"]);
  204. runCommand("./cpp/Test-debug", []);
  205. runCommand("rm", ["-rf", "cpp"]);
  206. runCommand("haxe", ["compile-cpp.hxml", "-D", "HXCPP_M64"]);
  207. runCommand("./cpp/Test-debug", []);
  208. case "js":
  209. runCommand("haxe", ["compile-js.hxml"]);
  210. runCommand("node", ["-e", "var unit = require('./unit.js').unit; unit.Test.main(); process.exit(unit.Test.success ? 0 : 1);"]);
  211. if (Sys.getEnv("TRAVIS_SECURE_ENV_VARS") == "true") {
  212. //https://saucelabs.com/opensource/travis
  213. runCommand("npm", ["install", "wd"], true);
  214. runCommand("curl", ["https://gist.github.com/santiycr/5139565/raw/sauce_connect_setup.sh", "-L", "|", "bash"], true);
  215. runCommand("haxelib", ["git", "nodejs", "https://github.com/dionjwa/nodejs-std.git", "master", "src"], true);
  216. runCommand("haxe", ["compile-saucelabs-runner.hxml"]);
  217. runCommand("nekotools", ["server", "&"]);
  218. runCommand("node", ["RunSauceLabs.js"]);
  219. }
  220. Sys.println("Test optimization:");
  221. Sys.setCwd(optDir);
  222. runCommand("haxe", ["run.hxml"]);
  223. case "java":
  224. getJavaDependencies();
  225. runCommand("haxe", ["compile-java.hxml"]);
  226. runCommand("java", ["-jar", "java/Test-Debug.jar"]);
  227. case "cs":
  228. getCsDependencies();
  229. runCommand("haxe", ["compile-cs.hxml"]);
  230. runCommand("mono", ["cs/bin/Test-Debug.exe"]);
  231. runCommand("haxe", ["compile-cs-unsafe.hxml"]);
  232. runCommand("mono", ["cs_unsafe/bin/Test-Debug.exe"]);
  233. case "flash9":
  234. setupFlashPlayerDebugger();
  235. runCommand("haxe", ["compile-flash9.hxml", "-D", "fdb"]);
  236. runFlash("unit9.swf");
  237. case "flash8":
  238. setupFlashPlayerDebugger();
  239. runCommand("haxe", ["compile-flash8.hxml", "-D", "fdb"]);
  240. runFlash("unit8.swf");
  241. case "as3":
  242. setupFlashPlayerDebugger();
  243. //setup flex sdk
  244. runCommand("wget", ["http://mirror.cc.columbia.edu/pub/software/apache/flex/4.12.0/binaries/apache-flex-sdk-4.12.0-bin.tar.gz"], true);
  245. runCommand("tar", ["-xf", "apache-flex-sdk-4.12.0-bin.tar.gz", "-C", Sys.getEnv("HOME")]);
  246. var flexsdkPath = Sys.getEnv("HOME") + "/apache-flex-sdk-4.12.0-bin";
  247. Sys.putEnv("PATH", Sys.getEnv("PATH") + ":" + flexsdkPath + "/bin");
  248. var playerglobalswcFolder = flexsdkPath + "/player";
  249. FileSystem.createDirectory(playerglobalswcFolder + "/11.1");
  250. runCommand("wget", ["-nv", "http://download.macromedia.com/get/flashplayer/updaters/11/playerglobal11_1.swc", "-O", playerglobalswcFolder + "/11.1/playerglobal.swc"], true);
  251. File.saveContent(flexsdkPath + "/env.properties", 'env.PLAYERGLOBAL_HOME=$playerglobalswcFolder');
  252. runCommand("mxmlc", ["--version"]);
  253. runCommand("haxe", ["compile-as3.hxml", "-D", "fdb"]);
  254. runFlash("unit9_as3.swf");
  255. case "openfl-samples":
  256. getCppDependencies(unitDir);
  257. runCommand("haxelib", ["git", "hxlibc", "https://github.com/openfl/hxlibc"]);
  258. runCommand("haxelib", ["git", "actuate", "https://github.com/jgranick/actuate"]);
  259. runCommand("haxelib", ["git", "box2d", "https://github.com/jgranick/box2d"]);
  260. runCommand("haxelib", ["git", "swf", "https://github.com/openfl/swf"]);
  261. runCommand("haxelib", ["git", "layout", "https://github.com/jgranick/layout"]);
  262. runCommand("haxelib", ["git", "format", "https://github.com/HaxeFoundation/format"]);
  263. runCommand("haxelib", ["git", "svg", "https://github.com/openfl/svg"]);
  264. runCommand("haxelib", ["git", "lime", "https://github.com/openfl/lime"]);
  265. runCommand("haxelib", ["git", "lime-build", "https://github.com/openfl/lime-build"]);
  266. runCommand("haxelib", ["git", "lime-tools", "https://github.com/openfl/lime-tools"]);
  267. runCommand("haxelib", ["git", "openfl-native", "https://github.com/openfl/openfl-native"]);
  268. runCommand("haxelib", ["git", "openfl", "https://github.com/openfl/openfl"]);
  269. runCommand("haxelib", ["git", "openfl-samples", "https://github.com/Simn/openfl-samples"]);
  270. runCommand("haxelib", ["run", "openfl", "rebuild", "linux"]);
  271. runCommand("haxelib", ["run", "openfl", "rebuild", "tools"]);
  272. var path = getHaxelibPath("openfl-samples");
  273. var old = Sys.getEnv("pwd");
  274. Sys.putEnv("pwd", path);
  275. parseTravisFile(haxe.io.Path.join([path, ".travis.yml"]), true);
  276. if (old != null) {
  277. Sys.putEnv("pwd", old);
  278. }
  279. case "polygonal-ds":
  280. runCommand("haxelib", ["git", "polygonal-ds", "https://github.com/Simn/ds"]);
  281. runCommand("haxelib", ["git", "polygonal-core", "https://github.com/polygonal/core", "master", "src"]);
  282. runCommand("haxelib", ["git", "polygonal-printf", "https://github.com/polygonal/printf", "master", "src"]);
  283. changeDirectory(getHaxelibPath("polygonal-ds"));
  284. runCommand("haxe", ["-cp", "src", "-cp", "test", "-lib", "polygonal-core", "-lib", "polygonal-printf", "-main", "UnitTest", "-js", "unit.js"]);
  285. runCommand("node", ["unit.js"]);
  286. case "flambe":
  287. runCommand("git", ["clone", "https://github.com/aduros/flambe"]);
  288. runCommand("sh", ["flambe/bin/run-travis"]);
  289. case "hxtemplo":
  290. getJavaDependencies();
  291. getPhpDependencies();
  292. getCppDependencies(unitDir);
  293. runCommand("haxelib", ["git", "hxparse", "https://github.com/Simn/hxparse", "development", "src"]);
  294. runCommand("haxelib", ["git", "hxtemplo", "https://github.com/Simn/hxtemplo"]);
  295. changeDirectory(getHaxelibPath("hxtemplo"));
  296. runCommand("haxe", ["build.hxml"]);
  297. runCommand("node", ["bin/hxtemplo.js"]);
  298. runCommand("neko", ["bin/hxtemplo.n"]);
  299. runCommand("java", ["-jar", "bin/java/Test.jar"]);
  300. runCommand("php", ["bin/php/index.php"]);
  301. runCommand("./bin/cpp/Test", []);
  302. case "munit":
  303. runCommand("haxelib", ["git", "mconsole", "https://github.com/massiveinteractive/mconsole", "master", "src"]);
  304. runCommand("haxelib", ["git", "mcover", "https://github.com/massiveinteractive/MassiveCover", "master", "src"]);
  305. runCommand("haxelib", ["git", "mlib", "https://github.com/massiveinteractive/MassiveLib", "master", "src"]);
  306. runCommand("haxelib", ["git", "munit", "https://github.com/massiveinteractive/MassiveUnit", "master", "src"]);
  307. changeDirectory(haxe.io.Path.join([getHaxelibPath("munit"), "..", "tool"]));
  308. runCommand("haxe", ["build.hxml"]);
  309. runCommand("haxelib", ["run", "munit", "test", "-result-exit-code", "-neko"]);
  310. changeDirectory("../");
  311. runCommand("haxelib", ["run", "munit", "test", "-result-exit-code", "-neko"]);
  312. case target:
  313. throw "unknown target: " + target;
  314. }
  315. }
  316. }