Skip to content

Commit 27d8d3b

Browse files
committed
Add Windows platform support
1 parent d1b7f2c commit 27d8d3b

File tree

6 files changed

+68
-23
lines changed

6 files changed

+68
-23
lines changed

hmm.cmd

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@haxelib --global run hmm %*

run.n

8.54 KB
Binary file not shown.

src/hmm/Hmm.hx

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class Hmm {
3939

4040
Shell.init({
4141
hmmDirectory: Sys.getCwd(),
42-
workingDirectory: args.pop()
42+
workingDirectory: args.pop(),
43+
isWin: Sys.systemName() == "Windows"
4344
});
4445

4546
var commandType = args.shift();

src/hmm/commands/SetupCommand.hx

+27-5
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,48 @@ package hmm.commands;
33
import sys.FileSystem;
44
import sys.io.File;
55
import haxe.io.Path;
6+
67
import hmm.utils.Shell;
8+
import hmm.errors.SystemError;
79

810
class SetupCommand implements ICommand {
9-
public static var HMM_FILE_NAME = "hmm";
10-
public static var HMM_LINK_PATH = "/usr/local/bin/hmm";
1111

1212
public var type(default, null) = "setup";
1313

1414
public function new() {
1515
}
1616

1717
public function run(args : Array<String>) {
18-
Shell.createHmmLink(getHmmScriptFilePath(), HMM_LINK_PATH);
18+
Shell.createHmmLink(getHmmScriptFilePath(), getLinkPath());
1919
}
2020

2121
function getHmmScriptFilePath() {
22-
return Path.join([Shell.hmmDirectory, HMM_FILE_NAME]);
22+
return Path.join([Shell.hmmDirectory, getHmmFileName()]);
2323
}
2424

2525
public function getUsage() {
26-
return 'creates a symbolic link to $HMM_FILE_NAME at $HMM_LINK_PATH';
26+
return if (Shell.isWin) {
27+
'creates a ${getHmmFileName()} script at ${getLinkPath()}';
28+
} else {
29+
'creates a symbolic link to ${getHmmFileName()} at ${getLinkPath()}';
30+
}
31+
}
32+
33+
public static function getLinkPath() {
34+
return if (Shell.isWin) {
35+
Path.join([getHaxePath(), "hmm.cmd"]);
36+
} else {
37+
"/usr/local/bin/hmm";
38+
}
39+
}
40+
41+
static function getHaxePath() {
42+
var haxePath = Sys.getEnv("HAXEPATH");
43+
if (haxePath == null) throw new SystemError("HAXEPATH environment variable is not defined", 1);
44+
return haxePath;
45+
}
46+
47+
public static function getHmmFileName() {
48+
return Shell.isWin ? "hmm.cmd" : "hmm";
2749
}
2850
}

src/hmm/utils/AnsiColors.hx

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ abstract AnsiColor(String) to String {
1515
}
1616

1717
class AnsiColors {
18+
public static var disabled : Bool;
19+
1820
public static function red(input : String) {
1921
return color(input, Red);
2022
}
@@ -52,6 +54,6 @@ class AnsiColors {
5254
}
5355

5456
public static function color(input : String, ansiColor : AnsiColor) : String {
55-
return '${ansiColor}$input${AnsiColor.None}';
57+
return disabled ? input : '${ansiColor}$input${AnsiColor.None}';
5658
}
5759
}

src/hmm/utils/Shell.hx

+35-16
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@ typedef ShellOptions = {
2121
throwError: Bool
2222
};
2323

24+
typedef ShellInitOptions = {
25+
hmmDirectory : String,
26+
workingDirectory : String,
27+
isWin : Bool
28+
}
29+
2430
class Shell {
2531
public static var hmmDirectory(default, default) : String;
2632
public static var workingDirectory(default, default) : String;
33+
public static var isWin(default, default) : Bool;
2734

28-
public static function init(options : { hmmDirectory : String, workingDirectory : String}) : Void {
35+
public static function init(options : ShellInitOptions) : Void {
2936
hmmDirectory = options.hmmDirectory;
3037
workingDirectory = options.workingDirectory;
38+
isWin = options.isWin;
39+
AnsiColors.disabled = isWin;
3140
setCwd(workingDirectory, { log: false });
3241
}
3342

@@ -105,21 +114,22 @@ class Shell {
105114
var outputString = commandResult.stdout;
106115
var outputLines = outputString.split("\n");
107116
var result = { statusCode: commandResult.statusCode, isInstalled: false, path: null, libraryName: null, version: null };
108-
for (outputLine in outputLines) {
109-
if (~/is not installed/i.match(outputLine)) {
117+
var notInstalledRegex = ~/is not installed/i;
118+
var versionRegex = ~/^\s*-D\s*(.*)=(.*)\s*$/;
119+
var pathRegex = ~/^(\/|[A-Z]:)/;
120+
for (line in outputLines) {
121+
var outputLine = line.trim();
122+
if (notInstalledRegex.match(outputLine)) {
110123
result.isInstalled = false;
111124
return result;
112125
}
113126
result.isInstalled = true;
114-
if (outputLine.trim().startsWith("/") && result.path == null) {
127+
if (result.path == null && pathRegex.match(outputLine)) {
115128
// capture the path to the library if it has not yet been captured
116129
result.path = outputLine;
117-
} else if (outputLine.trim().startsWith("-D") && result.libraryName == null && result.version == null) {
118-
var regex = ~/^\s*-D\s*(.*)=(.*)\s*$/;
119-
if (regex.match(outputLine)) {
120-
result.libraryName = regex.matched(1);
121-
result.version = regex.matched(2);
122-
}
130+
} else if (result.libraryName == null && result.version == null && versionRegex.match(outputLine)) {
131+
result.libraryName = versionRegex.matched(1);
132+
result.version = versionRegex.matched(2);
123133
}
124134
if (result.isInstalled && result.path != null && result.libraryName != null && result.version != null) {
125135
return result;
@@ -256,10 +266,15 @@ class Shell {
256266
}
257267

258268
public static function createHmmLink(realPath : String, linkPath : String) : Void {
259-
// TODO: windows support?
260-
runCommand("chmod", ["+x", realPath], { log: true, throwError: true });
261-
runCommand("sudo", ["rm", linkPath], { log: true, throwError: false });
262-
runCommand("sudo", ["ln", "-s", realPath, linkPath], { log: true, throwError: true });
269+
if (isWin) {
270+
var safeRealPath = realPath.replace("/", "\\");
271+
var safeLinkPath = linkPath.replace("/", "\\");
272+
runCommand("copy", ["/Y", safeRealPath, safeLinkPath], { log: true, throwError: true });
273+
} else {
274+
runCommand("chmod", ["+x", realPath], { log: true, throwError: true });
275+
runCommand("sudo", ["rm", linkPath], { log: true, throwError: false });
276+
runCommand("sudo", ["ln", "-s", realPath, linkPath], { log: true, throwError: true });
277+
}
263278
}
264279

265280
public static function updateHmm() {
@@ -268,8 +283,12 @@ class Shell {
268283
}
269284

270285
public static function removeHmm() {
271-
// TODO: windows support?
272-
runCommand("sudo", ["rm", hmm.commands.SetupCommand.HMM_LINK_PATH], { log: true, throwError: true });
286+
var linkPath = hmm.commands.SetupCommand.getLinkPath();
287+
if (isWin) {
288+
runCommand("del", [linkPath], { log: true, throwError: true });
289+
} else {
290+
runCommand("sudo", ["rm", linkPath], { log: true, throwError: true });
291+
}
273292
haxelib(["--global", "remove", "hmm"], { log: true, throwError: true });
274293
}
275294

0 commit comments

Comments
 (0)