Skip to content

Commit

Permalink
Replace hxfzf extra with hx list-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kLabz committed Apr 23, 2024
1 parent c6d97c1 commit 4e58da0
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 97 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ On Unix, you can also run:
- `hx current --name` to get the name under which that version is installed
- `hx current --full` to get both name and version string (`[NAME] ([VERSION])`)

## List all Haxe modules for current hxml

Prints a list of all `.hx` files in all your classpath, to be passed to `fzf`
or other tools.

Usage: `hx list-modules [compile.hxml]` (will default to `build.hxml`)


## Included tools

`extra/` folder contains optional tools you can install individually with their
Expand All @@ -63,13 +71,6 @@ On Unix, you can also run:
Note that those have been written when Haxe Manager was unix only and probably
can't work at all on Windows.

### `hxfzf`

Prints a list of all `.hx` files in all your classpath, to be passed to `fzf`
or other tools.

Usage: `hxfzf [compile.hxml]` (will default to `build.hxml`)

### `++haxe`

Note if you're using Haxe >= 4.3.0: this is not useful anymore, since
Expand Down
3 changes: 0 additions & 3 deletions extra/fzf/classpath.hxml

This file was deleted.

44 changes: 0 additions & 44 deletions extra/fzf/hxfzf.sh

This file was deleted.

13 changes: 0 additions & 13 deletions extra/fzf/install.sh

This file was deleted.

27 changes: 0 additions & 27 deletions extra/fzf/src/ClassPathMacro.hx

This file was deleted.

1 change: 0 additions & 1 deletion extra/install-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
ROOT=$(dirname $(readlink -f $0))

sh "$ROOT/++haxe/install.sh"
sh "$ROOT/fzf/install.sh"
sh "$ROOT/local/install.sh"
sh "$ROOT/make-haxe/install.sh"
sh "$ROOT/rofi/install.sh"
Expand Down
5 changes: 3 additions & 2 deletions src/HaxeManager.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import tools.Utils;

import ansi.ANSI;
import tools.Utils;

using tools.NullTools;

Expand Down Expand Up @@ -32,6 +31,8 @@ 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 ["--help", []]: displayUsage();
case ["--help", ["download"]]: HaxeDownload.displayUsage();
Expand Down
119 changes: 119 additions & 0 deletions src/HaxeModules.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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,
FileSystem.absolutePath("extra/fzf/classpath.hxml")
]);

try {
final code = proc.exitCode();
final out = proc.stdout.readAll().toString();
proc.close();

var target:Null<String> = null;
var classpath:Array<String> = [];
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<String> {
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();
}
}
}

0 comments on commit 4e58da0

Please sign in to comment.