|
| 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 | +typedef ClasspathResult = { |
| 10 | + var target:String; |
| 11 | + var cwd:String; |
| 12 | + var out:String; |
| 13 | + var classpath:Array<String>; |
| 14 | +} |
| 15 | + |
| 16 | +class HaxeClasspath { |
| 17 | + static var targets = [ |
| 18 | + "js", |
| 19 | + "hl", |
| 20 | + "cpp", |
| 21 | + "cppia", |
| 22 | + "cs", |
| 23 | + "java", |
| 24 | + "jvm", |
| 25 | + "lua", |
| 26 | + "swf", |
| 27 | + "neko", |
| 28 | + "php", |
| 29 | + "python", |
| 30 | + "interp" |
| 31 | + ]; |
| 32 | + |
| 33 | + static var stdRoot = FileSystem.fullPath(Path.join([Sys.getCwd(), "current", "std"])); |
| 34 | + |
| 35 | + static function getClasspath(?hxml:String = "build.hxml"):ClasspathResult { |
| 36 | + final cwd = Utils.getCallSite(); |
| 37 | + hxml = Path.isAbsolute(hxml) ? hxml : Path.join([cwd, hxml]); |
| 38 | + if (!FileSystem.exists(hxml)) throw 'Cannot find hxml file $hxml'; |
| 39 | + |
| 40 | + Sys.putEnv("HAXE_STD_PATH", stdRoot); |
| 41 | + final proc = new Process("haxe", [ |
| 42 | + "--cwd", cwd, |
| 43 | + hxml, |
| 44 | + "-cp", FileSystem.absolutePath("res/classpath/src"), |
| 45 | + "--macro", "ClassPathMacro.run()", |
| 46 | + "--no-output" |
| 47 | + ]); |
| 48 | + |
| 49 | + try { |
| 50 | + final code = proc.exitCode(); |
| 51 | + final out = proc.stdout.readAll().toString(); |
| 52 | + proc.close(); |
| 53 | + |
| 54 | + var output:Null<String> = null; |
| 55 | + var target:Null<String> = null; |
| 56 | + var classpath:Array<String> = []; |
| 57 | + |
| 58 | + final targetPrefix = "[TARGET]: "; |
| 59 | + final cpPrefix = "[CLASSPATH]: "; |
| 60 | + final outPrefix = "[OUT]: "; |
| 61 | + |
| 62 | + for (l in out.split("\n")) { |
| 63 | + if (l.startsWith(targetPrefix)) { |
| 64 | + target = l.substr(targetPrefix.length); |
| 65 | + } else if (l.startsWith(cpPrefix)) { |
| 66 | + var cp = l.substr(cpPrefix.length); |
| 67 | + classpath.push(cp); |
| 68 | + } else if (l.startsWith(outPrefix)) { |
| 69 | + output = l.substr(outPrefix.length); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return { |
| 74 | + target: target, |
| 75 | + cwd: cwd, |
| 76 | + out: output, |
| 77 | + classpath: classpath |
| 78 | + }; |
| 79 | + } catch (e) { |
| 80 | + Utils.displayError(Std.string(e)); |
| 81 | + proc.close(); |
| 82 | + throw e; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + static function extractTargetStd(cwd:String, cp:String):Array<String> { |
| 87 | + var path = FileSystem.fullPath(Path.isAbsolute(cp) ? cp : Path.join([cwd, cp])); |
| 88 | + if (!path.startsWith(stdRoot)) return [cp, null]; |
| 89 | + |
| 90 | + cp = path; // Use resolved path for std |
| 91 | + var path = cp.substr(stdRoot.length); |
| 92 | + path = StringTools.replace(path, '\\', '/'); |
| 93 | + while (path.charCodeAt(0) == '/'.code) path = path.substr(1); |
| 94 | + return [cp, path.split('/').shift()]; |
| 95 | + } |
| 96 | + |
| 97 | + public static function list(?hxml:String = "build.hxml"):Void { |
| 98 | + try { |
| 99 | + var data = getClasspath(hxml); |
| 100 | + for (cp in data.classpath) { |
| 101 | + Sys.println(cp); |
| 102 | + } |
| 103 | + } catch (e) { |
| 104 | + Utils.displayError(Std.string(e)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static function getDapConfig(?hxml:String = "build.hxml"):Void { |
| 109 | + try { |
| 110 | + var data = getClasspath(hxml); |
| 111 | + Sys.println([ |
| 112 | + '{', |
| 113 | + ' name="HashLink",', |
| 114 | + ' request="launch",', |
| 115 | + ' type="hl",', |
| 116 | + ' cwd="${data.cwd}",', |
| 117 | + ' classPaths={${data.classpath.map(cp -> "\'" + cp + "\'").join(", ")}},', |
| 118 | + ' program="${data.out}"', |
| 119 | + '}' |
| 120 | + ].join('\n')); |
| 121 | + } catch (e) { |
| 122 | + Utils.displayError(Std.string(e)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public static function listModules(?hxml:String = "build.hxml"):Void { |
| 127 | + try { |
| 128 | + var data = getClasspath(hxml); |
| 129 | + final old = Sys.getCwd(); |
| 130 | + Sys.setCwd(data.cwd); |
| 131 | + |
| 132 | + function findModules(path:String) { |
| 133 | + if (Path.extension(path) == "hx") return Sys.println(path); |
| 134 | + if (!FileSystem.isDirectory(path)) return; |
| 135 | + |
| 136 | + path = Path.addTrailingSlash(path); |
| 137 | + for (f in FileSystem.readDirectory(path)) findModules(path + f); |
| 138 | + } |
| 139 | + |
| 140 | + var ignoredTargets = []; |
| 141 | + if (data.target != null) { |
| 142 | + if (data.target != "java" && data.target != "jvm") ignoredTargets = ignoredTargets.concat(["java", "jvm"]); |
| 143 | + if (data.target != "cpp" && data.target != "cppia") ignoredTargets.push("cpp"); |
| 144 | + if (data.target != "js") ignoredTargets.push("js"); |
| 145 | + if (data.target != "hl") ignoredTargets.push("hl"); |
| 146 | + if (data.target != "cs") ignoredTargets.push("cs"); |
| 147 | + if (data.target != "lua") ignoredTargets.push("lua"); |
| 148 | + if (data.target != "neko") ignoredTargets.push("neko"); |
| 149 | + if (data.target != "php") ignoredTargets.push("php"); |
| 150 | + if (data.target != "python") ignoredTargets.push("python"); |
| 151 | + if (data.target != "swf") ignoredTargets.push("flash"); |
| 152 | + } |
| 153 | + |
| 154 | + for (cp in data.classpath) { |
| 155 | + switch extractTargetStd(data.cwd, cp) { |
| 156 | + // Non-std |
| 157 | + case [cp, null]: findModules(cp); |
| 158 | + |
| 159 | + // Top level std |
| 160 | + case [cp, ""]: |
| 161 | + cp = Path.addTrailingSlash(cp); |
| 162 | + var sub = FileSystem.readDirectory(cp); |
| 163 | + for (f in sub) { |
| 164 | + if (ignoredTargets.contains(f)) continue; |
| 165 | + findModules(cp + f); |
| 166 | + } |
| 167 | + |
| 168 | + case [_, t] if (ignoredTargets.contains(t)): |
| 169 | + case [cp, _]: findModules(cp); |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + Sys.setCwd(old); |
| 174 | + } catch (e) { |
| 175 | + Utils.displayError(Std.string(e)); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments