Skip to content

Commit

Permalink
✨ adding system::windowsAddToPath
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Dec 3, 2024
1 parent 23c9b70 commit a009a71
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 39 deletions.
17 changes: 15 additions & 2 deletions tests.d/1004-lib-system/00.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,28 @@ function test_system::windowsSetEnvVar() {
function powershell() { echo "powershell: $*"; }

echo "→ system::windowsSetEnvVar VAR VALUE"
system::windowsSetEnvVar VAR VALUE
OSTYPE=msys system::windowsSetEnvVar VAR VALUE
echo
echo "→ system::windowsSetEnvVar VAR ''"
system::windowsSetEnvVar VAR ''
OSTYPE=msys system::windowsSetEnvVar VAR ''
echo

unset -f powershell
test::endTest "Testing system::windowsSetEnvVar" 0
}

function test_system::windowsAddToPath() {
# shellcheck disable=SC2317
function powershell() { echo "powershell: $*"; }

echo "→ system::windowsAddToPath /coucou"
OSTYPE=msys system::windowsAddToPath /coucou
echo

unset -f powershell
test::endTest "Testing system::windowsAddToPath" 0
}

function main() {
testSystem::os
testSystem::env
Expand All @@ -159,6 +171,7 @@ function main() {
testSystem::commandExists
test_system::addToPath
test_system::windowsSetEnvVar
test_system::windowsAddToPath
}

main
32 changes: 29 additions & 3 deletions tests.d/1004-lib-system/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,37 @@ Exit code: `0`

```plaintext
→ system::windowsSetEnvVar VAR VALUE
powershell: -NoProfile -NonInteractive -Command $ErrorActionPreference = 'Stop'; [Environment]::SetEnvironmentVariable('VAR', 'VALUE', [System.EnvironmentVariableTarget]::User); exit $LASTEXITCODE;
powershell: -NoProfile -NonInteractive -Command $ErrorActionPreference = 'Stop'; $key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true); $key.SetValue('VAR', 'VALUE', 'ExpandString');; exit $LASTEXITCODE;
→ system::windowsSetEnvVar VAR ''
powershell: -NoProfile -NonInteractive -Command $ErrorActionPreference = 'Stop'; [Environment]::SetEnvironmentVariable('VAR', '', [System.EnvironmentVariableTarget]::User); exit $LASTEXITCODE;
powershell: -NoProfile -NonInteractive -Command $ErrorActionPreference = 'Stop'; [Environment]::SetEnvironmentVariable('VAR', [NullString]::Value, [System.EnvironmentVariableTarget]::User); exit $LASTEXITCODE;
powershell: -NoProfile -NonInteractive -Command $ErrorActionPreference = 'Stop'; $key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true); $key.DeleteValue('VAR');; exit $LASTEXITCODE;
```

### Testing system::windowsAddToPath

Exit code: `0`

**Standard** output:

```plaintext
→ system::windowsAddToPath /coucou
powershell: -NoProfile -NonInteractive -Command $ErrorActionPreference = 'Stop';
$pathToAdd = '\coucou';
$key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true);
$oldPath = $key.GetValue('Path', '', 'DoNotExpandEnvironmentNames').TrimEnd([IO.Path]::PathSeparator);
if ($currentPath -notlike "*$pathToAdd*") {
$newPath = '{0}{1}{2}' -f $oldPath, [IO.Path]::PathSeparator, $pathToAdd;
$key.SetValue('Path', $newPath, 'ExpandString');
};
$key.Dispose();
; exit $LASTEXITCODE;
```

**Error** output:

```log
INFO The directory ⌜\coucou⌝ has been added to the windows user PATH.
```

3 changes: 2 additions & 1 deletion tests.d/1103-self-release/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ DEBUG Found function: ⌜system::isRoot⌝
DEBUG Found function: ⌜system::addToPath⌝
DEBUG Found function: ⌜system::windowsSetEnvVar⌝
DEBUG Found function: ⌜system::windowsAddToPath⌝
DEBUG Found function: ⌜test::commentTest⌝
DEBUG Found function: ⌜test::endTest⌝
INFO Found 121 functions with documentation.
INFO Found 122 functions with documentation.
▶ called io::writeToFile $GLOBAL_VALET_HOME/extras/lib-valet.md
INFO The documentation has been generated in ⌜$GLOBAL_VALET_HOME/extras/lib-valet.md⌝.
▶ called io::writeToFile $GLOBAL_VALET_HOME/extras/lib-valet
Expand Down
83 changes: 61 additions & 22 deletions tests.d/1107-self-document/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -2441,20 +2441,20 @@ local osName="${RETURNED_VALUE}"

## system::windowsAddToPath

Add the given path to the PATH environment variable on Windows.
Add the given path to the PATH environment variable on Windows (current user only).

Will also export the PATH variable in the current bash.
## test::commentTest

Call this function to add a paragraph in the report file.

- $1: **comment** _as string_:
the text to add in the report file
- $1: **path** _as string_:
the path to add to the PATH environment variable.
The path can be in unix format, it will be converted to windows format.

```bash
test::commentTest "This is a comment."
system::windowsAddToPath "/path/to/bin"
```

> This function is only available on Windows, it uses `powershell` to directly modify the registry.

## system::windowsSetEnvVar

Expand All @@ -2469,7 +2469,19 @@ Set an environment variable for the current user on Windows.
system::windowsSetEnvVar "MY_VAR" "my_value"
```

> This function is only available on Windows, it uses `powershell`.
> This function is only available on Windows, it uses `powershell` to directly modify the registry.

## test::commentTest

Call this function to add a paragraph in the report file.

- $1: **comment** _as string_:
the text to add in the report file

```bash
test::commentTest "This is a comment."
```


## test::endTest
Expand Down Expand Up @@ -5044,20 +5056,20 @@ function system::os() { :; }

# ## system::windowsAddToPath
#
# Add the given path to the PATH environment variable on Windows.
# Add the given path to the PATH environment variable on Windows (current user only).
#
# Will also export the PATH variable in the current bash.
# ## test::commentTest
#
# Call this function to add a paragraph in the report file.
#
# - $1: **comment** _as string_:
# the text to add in the report file
# - $1: **path** _as string_:
# the path to add to the PATH environment variable.
# The path can be in unix format, it will be converted to windows format.
#
# ```bash
# test::commentTest "This is a comment."
# system::windowsAddToPath "/path/to/bin"
# ```
#
# > This function is only available on Windows, it uses `powershell` to directly modify the registry.
#
function system::windowsAddToPath() { :; }

# ## system::windowsSetEnvVar
Expand All @@ -5073,10 +5085,23 @@ function system::windowsAddToPath() { :; }
# system::windowsSetEnvVar "MY_VAR" "my_value"
# ```
#
# > This function is only available on Windows, it uses `powershell`.
# > This function is only available on Windows, it uses `powershell` to directly modify the registry.
#
function system::windowsSetEnvVar() { :; }

# ## test::commentTest
#
# Call this function to add a paragraph in the report file.
#
# - $1: **comment** _as string_:
# the text to add in the report file
#
# ```bash
# test::commentTest "This is a comment."
# ```
#
function test::commentTest() { :; }

# ## test::endTest
#
# Call this function after each test to write the test results to the report file.
Expand Down Expand Up @@ -6756,16 +6781,16 @@ function test::endTest() { :; }

"system::windowsAddToPath": {
"prefix": "system::windowsAddToPath",
"description": "Add the given path to the PATH environment variable on Windows...",
"description": "Add the given path to the PATH environment variable on Windows (current user only)...",
"scope": "",
"body": [ "system::windowsAddToPath \"${1:**comment**}\"$0" ]
"body": [ "system::windowsAddToPath \"${1:**path**}\"$0" ]
},

"system::windowsAddToPath#withdoc": {
"prefix": "system::windowsAddToPath#withdoc",
"description": "Add the given path to the PATH environment variable on Windows...",
"description": "Add the given path to the PATH environment variable on Windows (current user only)...",
"scope": "",
"body": [ "# ## system::windowsAddToPath\n# \n# Add the given path to the PATH environment variable on Windows.\n# \n# Will also export the PATH variable in the current bash.\n# ## test::commentTest\n# \n# Call this function to add a paragraph in the report file.\n# \n# - \\$1: **comment** _as string_:\n# the text to add in the report file\n# \n# ```bash\n# test::commentTest \"This is a comment.\"\n# ```\n# \nsystem::windowsAddToPath \"${1:**comment**}\"$0" ]
"body": [ "# ## system::windowsAddToPath\n# \n# Add the given path to the PATH environment variable on Windows (current user only).\n# \n# Will also export the PATH variable in the current bash.\n# \n# - \\$1: **path** _as string_:\n# the path to add to the PATH environment variable.\n# The path can be in unix format, it will be converted to windows format.\n# \n# ```bash\n# system::windowsAddToPath \"/path/to/bin\"\n# ```\n# \n# > This function is only available on Windows, it uses `powershell` to directly modify the registry.\n# \nsystem::windowsAddToPath \"${1:**path**}\"$0" ]
},

"system::windowsSetEnvVar": {
Expand All @@ -6779,7 +6804,21 @@ function test::endTest() { :; }
"prefix": "system::windowsSetEnvVar#withdoc",
"description": "Set an environment variable for the current user on Windows...",
"scope": "",
"body": [ "# ## system::windowsSetEnvVar\n# \n# Set an environment variable for the current user on Windows.\n# \n# - \\$1: **variable name** _as string_:\n# the name of the environment variable to set.\n# - \\$2: **variable value** _as string_:\n# the value of the environment variable to set.\n# \n# ```bash\n# system::windowsSetEnvVar \"MY_VAR\" \"my_value\"\n# ```\n# \n# > This function is only available on Windows, it uses `powershell`.\n# \nsystem::windowsSetEnvVar \"${1:**variable name**}\" \"${2:**variable value**}\"$0" ]
"body": [ "# ## system::windowsSetEnvVar\n# \n# Set an environment variable for the current user on Windows.\n# \n# - \\$1: **variable name** _as string_:\n# the name of the environment variable to set.\n# - \\$2: **variable value** _as string_:\n# the value of the environment variable to set.\n# \n# ```bash\n# system::windowsSetEnvVar \"MY_VAR\" \"my_value\"\n# ```\n# \n# > This function is only available on Windows, it uses `powershell` to directly modify the registry.\n# \nsystem::windowsSetEnvVar \"${1:**variable name**}\" \"${2:**variable value**}\"$0" ]
},

"test::commentTest": {
"prefix": "test::commentTest",
"description": "Call this function to add a paragraph in the report file...",
"scope": "",
"body": [ "test::commentTest \"${1:**comment**}\"$0" ]
},

"test::commentTest#withdoc": {
"prefix": "test::commentTest#withdoc",
"description": "Call this function to add a paragraph in the report file...",
"scope": "",
"body": [ "# ## test::commentTest\n# \n# Call this function to add a paragraph in the report file.\n# \n# - \\$1: **comment** _as string_:\n# the text to add in the report file\n# \n# ```bash\n# test::commentTest \"This is a comment.\"\n# ```\n# \ntest::commentTest \"${1:**comment**}\"$0" ]
},

"test::endTest": {
Expand Down Expand Up @@ -7038,7 +7077,7 @@ function test::endTest() { :; }
```log
INFO Generating documentation for the core functions only.
INFO Found 121 functions with documentation.
INFO Found 122 functions with documentation.
INFO The documentation has been generated in ⌜/tmp/valet.d/d1-1/lib-valet.md⌝.
INFO The prototype script has been generated in ⌜/tmp/valet.d/d1-1/lib-valet⌝.
INFO The vscode snippets have been generated in ⌜/tmp/valet.d/d1-1/valet.code-snippets⌝.
Expand Down
73 changes: 63 additions & 10 deletions valet.d/lib-system
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,78 @@ function system::addToPath() {
# system::windowsSetEnvVar "MY_VAR" "my_value"
# ```
#
# > This function is only available on Windows, it uses `powershell`.
# > This function is only available on Windows, it uses `powershell` to directly modify the registry.
function system::windowsSetEnvVar() {
local varName="${1:-}"
local varValue="${2:-}"

system::os
if [[ "${RETURNED_VALUE}" != "windows" ]]; then
core::fail "This function is only available on Windows."
fi

if [[ -z ${varName} ]]; then
core::fail "The variable name is missing."
fi

if ! io::runPs1Command "[Environment]::SetEnvironmentVariable('${varName//\'/\'\'}', '${varValue//\'/\'\'}', [System.EnvironmentVariableTarget]::User)"; then
core::fail "Failed to set the environment variable ⌜${varName}⌝."
fi
if [[ -n ${varValue} ]]; then
if ! io::runPs1Command "\$key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', \$true); \$key.SetValue('${varName//\'/\'\'}', '${varValue//\'/\'\'}', 'ExpandString');"; then
core::fail "Failed to set the environment variable ⌜${varName}⌝."
fi
else

# due to a bug in powershell, we seem to have several ways to unset a variable
# depending on the powershell version
# we try the second one here
if [[ -z ${varValue} ]]; then
if ! io::runPs1Command "[Environment]::SetEnvironmentVariable('${varName//\'/\'\'}', [NullString]::Value, [System.EnvironmentVariableTarget]::User)"; then
log::debug "Failed to unset the environment variable ⌜${varName}⌝ using null string."
if ! io::runPs1Command "\$key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', \$true); \$key.DeleteValue('${varName//\'/\'\'}');"; then
core::fail "Failed to unset the environment variable ⌜${varName}⌝ using null string."
fi
fi
}

# ## system::windowsAddToPath
#
# Add the given path to the PATH environment variable on Windows (current user only).
#
# Will also export the PATH variable in the current bash.
#
# - $1: **path** _as string_:
# the path to add to the PATH environment variable.
# The path can be in unix format, it will be converted to windows format.
#
# ```bash
# system::windowsAddToPath "/path/to/bin"
# ```
#
# > This function is only available on Windows, it uses `powershell` to directly modify the registry.
function system::windowsAddToPath() {
local pathToAdd="${1:-}"

system::os
if [[ "${RETURNED_VALUE}" != "windows" ]]; then
core::fail "This function is only available on Windows."
fi

log::debug "Adding directory ⌜${pathToAdd}⌝ to the PATH."

if [[ -z ${pathToAdd} ]]; then
core::fail "The path to add to the PATH is missing."
fi

io::convertToWindowsPath "${pathToAdd}"
pathToAdd="${RETURNED_VALUE}"

local script="
\$pathToAdd = '${pathToAdd//\'/\'\'}';
\$key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', \$true);
\$oldPath = \$key.GetValue('Path', '', 'DoNotExpandEnvironmentNames').TrimEnd([IO.Path]::PathSeparator);
if (\$currentPath -notlike \"*\$pathToAdd*\") {
\$newPath = '{0}{1}{2}' -f \$oldPath, [IO.Path]::PathSeparator, \$pathToAdd;
\$key.SetValue('Path', \$newPath, 'ExpandString');
};
\$key.Dispose();
"

if ! io::runPs1Command "${script}"; then
core::fail "Failed to add the directory ⌜${pathToAdd}⌝ to the PATH."
fi

log::info "The directory ⌜${pathToAdd}⌝ has been added to the windows user PATH."
}
2 changes: 1 addition & 1 deletion valet.d/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.27.193
0.27.202

0 comments on commit a009a71

Please sign in to comment.