Skip to content

Commit

Permalink
发布 1.0.0 正式版
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerWordTree committed Dec 30, 2023
1 parent e421768 commit ab7ac26
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 更新历史

## 1.0.0 / 2023-12-30

发布 1.0.0 正式版

## 0.3.1 / 2023-12-29

- 切换回 REG.EXE 方式设置环境变量.
Expand Down
20 changes: 10 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

批量添加 Windows 环境变量的工具.

根据配置文件, 对 Windows 环境变量进行设置.
根据配置文件, 对 Windows 环境变量进行设置, 当前工作目录为配置文件所在目录.

## 配置文件

Expand Down Expand Up @@ -42,17 +42,17 @@

段名`REPLACE`, 段内定义多个键值. 键为环境变量名称, 值为环境变量内容. 值会替换环境变量, 可以使用`(Removed)`占位符表示删除环境变量.

- 追加(APPEND)

段名`APPEND`, 段内定义多个键值. 键为环境变量名称, 值为环境变量内容. 值会追加到环境变量后.

- 插入(INSERT)

段名`INSERT`, 段内定义多个键值. 键为环境变量名称, 值为环境变量内容. 值会插入到环境变量前.

- 追加(APPEND)

段名`APPEND`, 段内定义多个键值. 键为环境变量名称, 值为环境变量内容. 值会追加到环境变量后.

- 变量和转义

配置文件中`%`包裹的变量, 在执行时会被展开, 当前工作目录为配置文件所在目录. 需要保留`%`时, 用`%%`方式进行转义.
配置文件中`%`包裹的变量, 在执行时会被展开. 需要保留`%`时, 用`%%`方式进行转义.

### 执行顺序

Expand All @@ -72,8 +72,8 @@ SCOPE=USER
[REPLACE]
JAVA_HOME=%CD%\JDK11

[APPEND]
PATH=;%%JAVA_HOME%%\Bin
[INSERT]
PATH=%%POWERSHELL_HOME%%;
```

```ini
Expand All @@ -82,8 +82,8 @@ SCOPE=MACHINE
[REPLACE]
POWERSHELL_HOME=%CD%\POWERSHELL7

[INSERT]
PATH=%%POWERSHELL_HOME%%;
[APPEND]
PATH=;%%JAVA_HOME%%\Bin
```

## 命令行:
Expand Down
2 changes: 1 addition & 1 deletion Script/Map.List.CMD
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
SETLOCAL
SET "@PREFIX=%~1." & SET "@FMT=%~2"
Powershell.EXE -Command "& { Get-Item Env:${Env:@PREFIX}@LIST -ErrorAction Ignore | ForEach-Object Value | ForEach-Object {$PSItem.Split(';',[StringSplitOptions]::RemoveEmptyEntries)} | ForEach-Object {$e=(Get-Item Env:${Env:@PREFIX}$PSItem -ErrorAction Ignore);IF ($e -eq $null) {New-Object System.Collections.DictionaryEntry($PSItem,'')} else {$e.name=$PSItem};$e} | ForEach-Object {${Env:@FMT} -f $PSItem.Name,$PSItem.Value}; Exit -Not $? }"
ENDLOCAL EXIT /B %ERRORLEVEL%
ENDLOCAL & EXIT /B %ERRORLEVEL%
18 changes: 9 additions & 9 deletions WinEnv.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
::环境变量管理
::@author FB
::@version 0.3.1
::@version 1.0.0

::Script:Argument.Parser.CMD::
::Script:Config.FileRead.CMD::
Expand Down Expand Up @@ -59,8 +59,8 @@ IF /I "%_ARG.OPTION.H%" == "TRUE" (
ECHO.
ECHO - /o ^| -o
ECHO 指定要执行的操作.
ECHO 必选参数, `1`设置环境变量, `2`恢复环境变量, `3`退出.
ECHO 默认为等待用户选择.
ECHO 必选参数: `1`设置环境变量, `2`恢复环境变量, `3`退出.
ECHO 默认操作为等待用户选择.
ECHO.
ECHO - /h ^| -h
ECHO 显示帮助
Expand Down Expand Up @@ -104,6 +104,7 @@ FOR %%A IN ("REPLACE","INSERT","APPEND") DO (
::::展开变量
CALL SET "_KEY=%%~I"
CALL Environment.Get.CMD "%%_CONFIG.SCOPE%%" "%%_KEY%%" || SET "@=(Removed)"
::::转义变量
CALL String.Replace.CMD "%%@%%" "%%%%" "%%%%%%%%"
CALL Map.Put.CMD "_CONFIG_OLD.REPLACE" "%%_KEY%%" "%%@%%"
)
Expand All @@ -127,9 +128,8 @@ FOR /F "tokens=1,* usebackq delims==" %%A IN (
CALL :SETENV "%%~A" "%%~B%%%%~A%%"
CALL SET "_KEY=%%~A" & CALL SET "_VALUE=%%~B"
CALL Environment.Get.CMD "%%_CONFIG.SCOPE%%" "%%_KEY%%"
CALL SET "_VALUE=%%_VALUE%%%%@%%"
CALL ECHO %%_KEY%%=%%_VALUE%%
CALL Environment.Set.CMD "%%_CONFIG.SCOPE%%" "%%_KEY%%" "%%_VALUE%%"
CALL ECHO %%_KEY%%=%%_VALUE%%%%@%%
CALL Environment.Set.CMD "%%_CONFIG.SCOPE%%" "%%_KEY%%" "%%_VALUE%%%%@%%"
)
FOR /F "tokens=1,* usebackq delims==" %%A IN (
`CALL Map.List.CMD "_CONFIG.APPEND" "{0}={1}"`
Expand All @@ -138,9 +138,8 @@ FOR /F "tokens=1,* usebackq delims==" %%A IN (
CALL :SETENV "%%~A" "%%%%~A%%%%~B"
CALL SET "_KEY=%%~A" & CALL SET "_VALUE=%%~B"
CALL Environment.Get.CMD "%%_CONFIG.SCOPE%%" "%%_KEY%%"
CALL SET "_VALUE=%%@%%%%_VALUE%%"
CALL ECHO %%_KEY%%=%%_VALUE%%
CALL Environment.Set.CMD "%%_CONFIG.SCOPE%%" "%%_KEY%%" "%%_VALUE%%"
CALL ECHO %%_KEY%%=%%@%%%%_VALUE%%
CALL Environment.Set.CMD "%%_CONFIG.SCOPE%%" "%%_KEY%%" "%%@%%%%_VALUE%%"
)
GOTO :EXIT

Expand Down Expand Up @@ -178,6 +177,7 @@ IF "%_OPTION%" == "" (
)
ENDLOCAL & EXIT /B %_EXIT_CODE%

::设置变量
:SETENV
SET "_KEY=%~1"
IF "%_KEY%" == "" GOTO :EOF
Expand Down
6 changes: 3 additions & 3 deletions WinEnv.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ SCOPE=USER
JAVA_HOME=%CD%\JDK11
POWERSHELL_HOME=%CD%\POWERSHELL7

[APPEND]
PATH=;%%JAVA_HOME%%\Bin

[INSERT]
PATH=%%POWERSHELL_HOME%%;

[APPEND]
PATH=;%%JAVA_HOME%%\Bin

0 comments on commit ab7ac26

Please sign in to comment.