Skip to content

Commit eb39add

Browse files
gmittertaciidgh
authored andcommitted
Implement ProcessEnv.swift on Windows
1 parent dadf766 commit eb39add

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

Sources/Basic/ProcessEnv.swift

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,34 @@ public enum ProcessEnv {
2121

2222
/// Set the given key and value in the process's environment.
2323
public static func setVar(_ key: String, value: String) throws {
24-
// FIXME: Need to handle Windows.
24+
#if os(Windows)
25+
guard 0 != key.withCString(encodedAs: UTF16.self, { keyStr in
26+
value.withCString(encodedAs: UTF16.self) { valStr in
27+
SetEnvironmentVariableW(keyStr, valStr)
28+
}
29+
}) else {
30+
throw SystemError.setenv(Int32(GetLastError()), key)
31+
}
32+
#else
2533
guard SPMLibc.setenv(key, value, 1) == 0 else {
2634
throw SystemError.setenv(errno, key)
2735
}
36+
#endif
2837
}
2938

3039
/// Unset the give key in the process's environment.
3140
public static func unsetVar(_ key: String) throws {
32-
// FIXME: Need to handle Windows.
41+
#if os(Windows)
42+
guard 0 != key.withCString(encodedAs: UTF16.self, { keyStr in
43+
SetEnvironmentVariableW(keyStr, nil)
44+
}) else {
45+
throw SystemError.unsetenv(Int32(GetLastError()), key)
46+
}
47+
#else
3348
guard SPMLibc.unsetenv(key) == 0 else {
3449
throw SystemError.unsetenv(errno, key)
3550
}
51+
#endif
3652
}
3753

3854
/// The current working directory of the process.
@@ -42,10 +58,17 @@ public enum ProcessEnv {
4258

4359
/// Change the current working directory of the process.
4460
public static func chdir(_ path: AbsolutePath) throws {
45-
// FIXME: Need to handle Windows.
4661
let path = path.pathString
62+
#if os(Windows)
63+
guard 0 != path.withCString(encodedAs: UTF16.self, {
64+
SetCurrentDirectoryW($0)
65+
}) else {
66+
throw SystemError.chdir(Int32(GetLastError()), path)
67+
}
68+
#else
4769
guard SPMLibc.chdir(path) == 0 else {
4870
throw SystemError.chdir(errno, path)
4971
}
72+
#endif
5073
}
5174
}

0 commit comments

Comments
 (0)