diff --git a/res/classpath/src/ClassPathMacro.hx b/res/classpath/src/ClassPathMacro.hx index 445893a..1107dc0 100644 --- a/res/classpath/src/ClassPathMacro.hx +++ b/res/classpath/src/ClassPathMacro.hx @@ -1,3 +1,4 @@ +import haxe.macro.Compiler; import haxe.macro.Context; import haxe.io.Path; import sys.FileSystem; @@ -8,6 +9,7 @@ class ClassPathMacro { var targets = ["js", "hl", "cpp", "cppia", "cs", "java", "jvm", "lua", "swf", "neko", "php", "python", "interp"]; var target = first(defines.keys(), targets); Sys.println('[TARGET]: $target'); + Sys.println('[OUT]: ${Compiler.getOutput()}'); var ownPath = FileSystem.fullPath(Path.join([Context.resolvePath("ClassPathMacro.hx"), '..'])); diff --git a/src/HaxeClasspath.hx b/src/HaxeClasspath.hx new file mode 100644 index 0000000..002f416 --- /dev/null +++ b/src/HaxeClasspath.hx @@ -0,0 +1,178 @@ +import haxe.io.Path; +import sys.FileSystem; +import sys.io.Process; + +import tools.Utils; + +using StringTools; + +typedef ClasspathResult = { + var target:String; + var cwd:String; + var out:String; + var classpath:Array; +} + +class HaxeClasspath { + static var targets = [ + "js", + "hl", + "cpp", + "cppia", + "cs", + "java", + "jvm", + "lua", + "swf", + "neko", + "php", + "python", + "interp" + ]; + + static var stdRoot = FileSystem.fullPath(Path.join([Sys.getCwd(), "current", "std"])); + + static function getClasspath(?hxml:String = "build.hxml"):ClasspathResult { + final cwd = Utils.getCallSite(); + hxml = Path.isAbsolute(hxml) ? hxml : Path.join([cwd, hxml]); + if (!FileSystem.exists(hxml)) throw 'Cannot find hxml file $hxml'; + + Sys.putEnv("HAXE_STD_PATH", stdRoot); + final proc = new Process("haxe", [ + "--cwd", cwd, + hxml, + "-cp", FileSystem.absolutePath("res/classpath/src"), + "--macro", "ClassPathMacro.run()", + "--no-output" + ]); + + try { + final code = proc.exitCode(); + final out = proc.stdout.readAll().toString(); + proc.close(); + + var output:Null = null; + var target:Null = null; + var classpath:Array = []; + + final targetPrefix = "[TARGET]: "; + final cpPrefix = "[CLASSPATH]: "; + final outPrefix = "[OUT]: "; + + for (l in out.split("\n")) { + if (l.startsWith(targetPrefix)) { + target = l.substr(targetPrefix.length); + } else if (l.startsWith(cpPrefix)) { + var cp = l.substr(cpPrefix.length); + classpath.push(cp); + } else if (l.startsWith(outPrefix)) { + output = l.substr(outPrefix.length); + } + } + + return { + target: target, + cwd: cwd, + out: output, + classpath: classpath + }; + } catch (e) { + Utils.displayError(Std.string(e)); + proc.close(); + throw e; + } + } + + static function extractTargetStd(cwd:String, cp:String):Array { + var path = FileSystem.fullPath(Path.isAbsolute(cp) ? cp : Path.join([cwd, cp])); + if (!path.startsWith(stdRoot)) return [cp, null]; + + cp = path; // Use resolved path for std + var path = cp.substr(stdRoot.length); + path = StringTools.replace(path, '\\', '/'); + while (path.charCodeAt(0) == '/'.code) path = path.substr(1); + return [cp, path.split('/').shift()]; + } + + public static function list(?hxml:String = "build.hxml"):Void { + try { + var data = getClasspath(hxml); + for (cp in data.classpath) { + Sys.println(cp); + } + } catch (e) { + Utils.displayError(Std.string(e)); + } + } + + public static function getDapConfig(?hxml:String = "build.hxml"):Void { + try { + var data = getClasspath(hxml); + Sys.println([ + '{', + ' name="HashLink",', + ' request="launch",', + ' type="hl",', + ' cwd="${data.cwd}",', + ' classPaths={${data.classpath.map(cp -> "\'" + cp + "\'").join(", ")}},', + ' program="${data.out}"', + '}' + ].join('\n')); + } catch (e) { + Utils.displayError(Std.string(e)); + } + } + + public static function listModules(?hxml:String = "build.hxml"):Void { + try { + var data = getClasspath(hxml); + final old = Sys.getCwd(); + Sys.setCwd(data.cwd); + + function findModules(path:String) { + if (Path.extension(path) == "hx") return Sys.println(path); + if (!FileSystem.isDirectory(path)) return; + + path = Path.addTrailingSlash(path); + for (f in FileSystem.readDirectory(path)) findModules(path + f); + } + + var ignoredTargets = []; + if (data.target != null) { + if (data.target != "java" && data.target != "jvm") ignoredTargets = ignoredTargets.concat(["java", "jvm"]); + if (data.target != "cpp" && data.target != "cppia") ignoredTargets.push("cpp"); + if (data.target != "js") ignoredTargets.push("js"); + if (data.target != "hl") ignoredTargets.push("hl"); + if (data.target != "cs") ignoredTargets.push("cs"); + if (data.target != "lua") ignoredTargets.push("lua"); + if (data.target != "neko") ignoredTargets.push("neko"); + if (data.target != "php") ignoredTargets.push("php"); + if (data.target != "python") ignoredTargets.push("python"); + if (data.target != "swf") ignoredTargets.push("flash"); + } + + for (cp in data.classpath) { + switch extractTargetStd(data.cwd, cp) { + // Non-std + case [cp, null]: findModules(cp); + + // Top level std + case [cp, ""]: + cp = Path.addTrailingSlash(cp); + var sub = FileSystem.readDirectory(cp); + for (f in sub) { + if (ignoredTargets.contains(f)) continue; + findModules(cp + f); + } + + case [_, t] if (ignoredTargets.contains(t)): + case [cp, _]: findModules(cp); + }; + } + + Sys.setCwd(old); + } catch (e) { + Utils.displayError(Std.string(e)); + } + } +} diff --git a/src/HaxeManager.hx b/src/HaxeManager.hx index 224680e..d3700c7 100644 --- a/src/HaxeManager.hx +++ b/src/HaxeManager.hx @@ -36,8 +36,13 @@ class HaxeManager { Sys.println(Utils.getCurrentFull().or("")); case ["list", []]: for (v in Utils.getVersions()) Sys.println(v); - case ["list-modules", []]: HaxeModules.listModules(); - case ["list-modules", [hxml]]: HaxeModules.listModules(hxml); + case ["list-classpath", []]: HaxeClasspath.list(); + case ["list-classpath", [hxml]]: HaxeClasspath.list(hxml); + case ["list-modules", []]: HaxeClasspath.listModules(); + case ["list-modules", [hxml]]: HaxeClasspath.listModules(hxml); + + case ["dap-config", []]: HaxeClasspath.getDapConfig(); + case ["dap-config", [hxml]]: HaxeClasspath.getDapConfig(hxml); case ["--help", []]: displayUsage(); case ["--help", ["download"]]: HaxeDownload.displayUsage(); diff --git a/src/HaxeModules.hx b/src/HaxeModules.hx deleted file mode 100644 index f9d0397..0000000 --- a/src/HaxeModules.hx +++ /dev/null @@ -1,121 +0,0 @@ -import haxe.io.Path; -import sys.FileSystem; -import sys.io.Process; - -import tools.Utils; - -using StringTools; - -class HaxeModules { - static var targets = [ - "js", - "hl", - "cpp", - "cppia", - "cs", - "java", - "jvm", - "lua", - "swf", - "neko", - "php", - "python", - "interp" - ]; - - public static function listModules(?hxml:String = "build.hxml"):Void { - final cwd = Utils.getCallSite(); - hxml = Path.isAbsolute(hxml) ? hxml : Path.join([cwd, hxml]); - if (!FileSystem.exists(hxml)) throw 'Cannot find hxml file $hxml'; - - final stdRoot = FileSystem.fullPath(Path.join([Sys.getCwd(), "current", "std"])); - Sys.putEnv("HAXE_STD_PATH", stdRoot); - final proc = new Process("haxe", [ - "--cwd", cwd, - hxml, - "-cp", FileSystem.absolutePath("res/classpath/src"), - "--macro", "ClassPathMacro.run()", - "--no-output" - ]); - - try { - final code = proc.exitCode(); - final out = proc.stdout.readAll().toString(); - proc.close(); - - var target:Null = null; - var classpath:Array = []; - final targetPrefix = "[TARGET]: "; - final cpPrefix = "[CLASSPATH]: "; - - for (l in out.split("\n")) { - if (l.startsWith(targetPrefix)) { - target = l.substr(targetPrefix.length); - } else if (l.startsWith(cpPrefix)) { - var cp = l.substr(cpPrefix.length); - classpath.push(cp); - } - } - - final old = Sys.getCwd(); - Sys.setCwd(cwd); - - function findModules(path:String) { - if (Path.extension(path) == "hx") return Sys.println(path); - if (!FileSystem.isDirectory(path)) return; - - path = Path.addTrailingSlash(path); - for (f in FileSystem.readDirectory(path)) findModules(path + f); - } - - function extractTargetStd(cp:String):Array { - var path = FileSystem.fullPath(Path.isAbsolute(cp) ? cp : Path.join([cwd, cp])); - if (!path.startsWith(stdRoot)) return [cp, null]; - - cp = path; // Use resolved path for std - var path = cp.substr(stdRoot.length); - path = StringTools.replace(path, '\\', '/'); - while (path.charCodeAt(0) == '/'.code) path = path.substr(1); - return [cp, path.split('/').shift()]; - } - - var ignoredTargets = []; - if (target != null) { - if (target != "java" && target != "jvm") ignoredTargets = ignoredTargets.concat(["java", "jvm"]); - if (target != "cpp" && target != "cppia") ignoredTargets.push("cpp"); - if (target != "js") ignoredTargets.push("js"); - if (target != "hl") ignoredTargets.push("hl"); - if (target != "cs") ignoredTargets.push("cs"); - if (target != "lua") ignoredTargets.push("lua"); - if (target != "neko") ignoredTargets.push("neko"); - if (target != "php") ignoredTargets.push("php"); - if (target != "python") ignoredTargets.push("python"); - if (target != "swf") ignoredTargets.push("flash"); - } - - for (cp in classpath) { - switch extractTargetStd(cp) { - // Non-std - case [cp, null]: findModules(cp); - - // Top level std - case [cp, ""]: - cp = Path.addTrailingSlash(cp); - var sub = FileSystem.readDirectory(cp); - for (f in sub) { - if (ignoredTargets.contains(f)) continue; - findModules(cp + f); - } - - case [_, t] if (ignoredTargets.contains(t)): - case [cp, _]: findModules(cp); - }; - } - - Sys.setCwd(old); - } catch (e) { - Utils.displayError(Std.string(e)); - proc.close(); - } - } -}