|
| 1 | +import haxe.io.Path; |
| 2 | +import sys.FileSystem; |
| 3 | +import sys.io.Process; |
| 4 | + |
| 5 | +import tools.Utils; |
| 6 | + |
| 7 | +using StringTools; |
| 8 | + |
| 9 | +class HaxeModules { |
| 10 | + static var targets = [ |
| 11 | + "js", |
| 12 | + "hl", |
| 13 | + "cpp", |
| 14 | + "cppia", |
| 15 | + "cs", |
| 16 | + "java", |
| 17 | + "jvm", |
| 18 | + "lua", |
| 19 | + "swf", |
| 20 | + "neko", |
| 21 | + "php", |
| 22 | + "python", |
| 23 | + "interp" |
| 24 | + ]; |
| 25 | + |
| 26 | + public static function listModules(?hxml:String = "build.hxml"):Void { |
| 27 | + final cwd = Utils.getCallSite(); |
| 28 | + hxml = Path.isAbsolute(hxml) ? hxml : Path.join([cwd, hxml]); |
| 29 | + if (!FileSystem.exists(hxml)) throw 'Cannot find hxml file $hxml'; |
| 30 | + |
| 31 | + final stdRoot = FileSystem.fullPath(Path.join([Sys.getCwd(), "current", "std"])); |
| 32 | + Sys.putEnv("HAXE_STD_PATH", stdRoot); |
| 33 | + final proc = new Process("haxe", [ |
| 34 | + "--cwd", cwd, |
| 35 | + hxml, |
| 36 | + FileSystem.absolutePath("extra/fzf/classpath.hxml") |
| 37 | + ]); |
| 38 | + |
| 39 | + try { |
| 40 | + final code = proc.exitCode(); |
| 41 | + final out = proc.stdout.readAll().toString(); |
| 42 | + proc.close(); |
| 43 | + |
| 44 | + var target:Null<String> = null; |
| 45 | + var classpath:Array<String> = []; |
| 46 | + final targetPrefix = "[TARGET]: "; |
| 47 | + final cpPrefix = "[CLASSPATH]: "; |
| 48 | + |
| 49 | + for (l in out.split("\n")) { |
| 50 | + if (l.startsWith(targetPrefix)) { |
| 51 | + target = l.substr(targetPrefix.length); |
| 52 | + } else if (l.startsWith(cpPrefix)) { |
| 53 | + var cp = l.substr(cpPrefix.length); |
| 54 | + classpath.push(cp); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + final old = Sys.getCwd(); |
| 59 | + Sys.setCwd(cwd); |
| 60 | + |
| 61 | + function findModules(path:String) { |
| 62 | + if (Path.extension(path) == "hx") return Sys.println(path); |
| 63 | + if (!FileSystem.isDirectory(path)) return; |
| 64 | + |
| 65 | + path = Path.addTrailingSlash(path); |
| 66 | + for (f in FileSystem.readDirectory(path)) findModules(path + f); |
| 67 | + } |
| 68 | + |
| 69 | + function extractTargetStd(cp:String):Array<String> { |
| 70 | + var path = FileSystem.fullPath(Path.isAbsolute(cp) ? cp : Path.join([cwd, cp])); |
| 71 | + if (!path.startsWith(stdRoot)) return [cp, null]; |
| 72 | + |
| 73 | + cp = path; // Use resolved path for std |
| 74 | + var path = cp.substr(stdRoot.length); |
| 75 | + path = StringTools.replace(path, '\\', '/'); |
| 76 | + while (path.charCodeAt(0) == '/'.code) path = path.substr(1); |
| 77 | + return [cp, path.split('/').shift()]; |
| 78 | + } |
| 79 | + |
| 80 | + var ignoredTargets = []; |
| 81 | + if (target != null) { |
| 82 | + if (target != "java" && target != "jvm") ignoredTargets = ignoredTargets.concat(["java", "jvm"]); |
| 83 | + if (target != "cpp" && target != "cppia") ignoredTargets.push("cpp"); |
| 84 | + if (target != "js") ignoredTargets.push("js"); |
| 85 | + if (target != "hl") ignoredTargets.push("hl"); |
| 86 | + if (target != "cs") ignoredTargets.push("cs"); |
| 87 | + if (target != "lua") ignoredTargets.push("lua"); |
| 88 | + if (target != "neko") ignoredTargets.push("neko"); |
| 89 | + if (target != "php") ignoredTargets.push("php"); |
| 90 | + if (target != "python") ignoredTargets.push("python"); |
| 91 | + if (target != "swf") ignoredTargets.push("flash"); |
| 92 | + } |
| 93 | + |
| 94 | + for (cp in classpath) { |
| 95 | + switch extractTargetStd(cp) { |
| 96 | + // Non-std |
| 97 | + case [cp, null]: findModules(cp); |
| 98 | + |
| 99 | + // Top level std |
| 100 | + case [cp, ""]: |
| 101 | + cp = Path.addTrailingSlash(cp); |
| 102 | + var sub = FileSystem.readDirectory(cp); |
| 103 | + for (f in sub) { |
| 104 | + if (ignoredTargets.contains(f)) continue; |
| 105 | + findModules(cp + f); |
| 106 | + } |
| 107 | + |
| 108 | + case [_, t] if (ignoredTargets.contains(t)): |
| 109 | + case [cp, _]: findModules(cp); |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + Sys.setCwd(old); |
| 114 | + } catch (e) { |
| 115 | + Utils.displayError(Std.string(e)); |
| 116 | + proc.close(); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments