Skip to content

Commit

Permalink
初始版本
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerWordTree committed Dec 21, 2023
0 parents commit a5e1b5a
Show file tree
Hide file tree
Showing 26 changed files with 704 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WinEnv.old
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 更新历史

## 0.1.0 / 2023-12-22

- 初始版本
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 FB

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
105 changes: 105 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# 运行环境设置

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

根据配置文件的内容, 对 Windows 环境变量进行设置.

支持头部插入, 支持结尾追加, 支持`%`变量.

## 配置文件

默认配置文件名与脚本同名但扩展名不同, 配置文件扩展名`.ini`, 备份文件扩展名`.old`.

非标准 INI 格式, 提供作用域和环境变量等配置.

### 格式

- 注释行(comment)

`#`开头的行.

- 无效行(invalid)

不含`=` 或者 值为空.

- 段(section)

`[]`包裹的行.

- 键(key)

行第一个`=`左边的字符串.

- 值(value)

行第一个`=`右边的字符串.

### 内容

- 作用域(SCOPE)

段外键值, 键`SCOPE`, 可选值`MACHINE`或者`USER`, 默认值`USER`. 明确定义作用域范围, `MACHINE`为全局, `USER`为用户.

- 替换(REPLACE)

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

- 追加(APPEND)

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

- 插入(INSERT)

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

- 变量和转义

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

### 示例

```ini
SCOPE=USER

[REPLACE]
JAVA_HOME=%CD%\JDK11

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

```ini
SCOPE=MACHINE

[REPLACE]
POWERSHELL_HOME=%CD%\POWERSHELL7

[INSERT]
PATH=%%POWERSHELL_HOME%%;
```

## 命令行:

命令行: WinEnv.cmd [配置文件[.ini]] [/o|-o <1|2|3>] [/h|-h]

- 配置文件

指定配置文件路径, 可以省略`.ini`. 备份文件会根据配置文件名生成, 扩展名为`.old`. 默认配置文件`WinEnv.ini`, 默认备份文件`WinEnv.old`.

- `/o` | `-o`

指定要执行的操作. 必选参数, `1`设置环境变量, `2`恢复环境变量, `3`退出. 默认为等待用户选择.

- `/h` | `-h`

显示帮助

### 示例

```bat
Env.cmd
Env.cmd XXX
Env.cmd XXX.ini
Env.cmd XXX /o 1
Env.cmd XXX.ini /o 2
```
14 changes: 14 additions & 0 deletions Script/Array.Clear.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
::@author FB
::@version 0.1.0

::Çå¿Õ
:: ²ÎÊý1: ʵÀýÃû³Æ

::Script:Array.Destroy.CMD::
::Script:Array.New.CMD::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CALL Array.Destroy.CMD "%~1"
CALL Array.New.CMD "%~1"
EXIT /B
14 changes: 14 additions & 0 deletions Script/Array.Destroy.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
::@author FB
::@version 0.1.0

::Ïú»Ù
:: ²ÎÊý1: ʵÀýÃû³Æ

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CALL SET "@=%%%~1.@ARRAY%%"
FOR /L %%A IN (0,1,%@%) DO (
SET "%~1.%%~A="
)
SET "%~1.@ARRAY="
EXIT /B
18 changes: 18 additions & 0 deletions Script/Array.New.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
::@author FB
::@version 0.1.0

::数据组织: A.1=* A.2=* A.3=*
::数据长度: %A.@ARRAY%
::数据创建: CALL Array.New.CMD A;
::数据添加: CALL Array_Push.CMD A "*";
::数据访问: CALL Array_Get.CMD A 1; CALL :ARRAY_POP A 2;
::数据遍历: CALL Array_List.CMD A "ECHO {0} {1}";
::数据销毁: CALL Array_Destroy.CMD A;

::创建
:: 参数1: 实例名称

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET /A "%~1.@ARRAY=0"
EXIT /B
12 changes: 12 additions & 0 deletions Script/Array.Push.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
::@author FB
::@version 0.1.0

::压入
:: 参数1: 实例名称
:: 参数2: 元素

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET "@=%~2"
CALL SET "%~1.%%%~1.@ARRAY%%=%%@%%"
SET /A "%~1.@ARRAY+=1"
EXIT /B
35 changes: 35 additions & 0 deletions Script/Config.ArgParser.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
::@author FB
::@version 0.1.0

::有序参数: 实例.PARAM, 保存为Array
::选项参数: 实例.OPTION, 保存为Map

::解析参数
:: 参数1: 实例名称
:: 参数N: 参数

::Script:Array.Clear.CMD::
::Script:Array.Push.CMD::
::Script:Map.Clear.CMD::
::Script:Map.Put.CMD::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET "@VAR=%~1"
CALL Array.Clear.CMD "%@VAR%.PARAM"
CALL Map.Clear.CMD "%@VAR%.OPTION"
:_ARG_PARSE_LOOP
SHIFT
IF "%1" == "" (SET "@=" & SET "@VAR=" & EXIT /B)
SET "@=%~1"
ECHO.%@% | FINDSTR "^[-/]" 1>NUL 2>&1 && (
ECHO.%~2 | FINDSTR /R /C:"^[-/]" /C:"^[ ]*$" 1>NUL 2>&1 && (
CALL Map.Put.CMD "%@VAR%.OPTION" "%%@:~1%%" "TRUE"
) || (
CALL Map.Put.CMD "%@VAR%.OPTION" "%%@:~1%%" "%~2"
SHIFT
)
) || (
CALL Array.Push.CMD "%@VAR%.PARAM" "%%@%%"
)
GOTO :_ARG_PARSE_LOOP
39 changes: 39 additions & 0 deletions Script/Config.FileRead.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
::@author FB
::@version 0.1.1

::数据结构为Object
::仅支持一级段
::由%包裹的变量不会被展开

::如果KEY是#开头,什么也不做
::如果VALUE不为空,保存到变量
::如果KEY被[]包裹,切换当前段

::读取配置文件
:: 参数1: 实例名称
:: 参数2: 配置文件

::Script:String.Trim.CMD::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET "@VAR=%~1" & SET "@SECTION=."
FOR /F "eol=# tokens=1,* usebackq delims==" %%A IN (
`TYPE "%~2" 2^>NUL`
) DO (
SET "@KEY=%%~A"
SET "@VALUE=%%~B"
CALL String.Trim.CMD "%%@KEY%%" & CALL SET "@KEY=%%@%%"
CALL String.Trim.CMD "%%@VALUE%%" & CALL SET "@VALUE=%%@%%"
CALL :_PARSE_LINE
)
SET "@VALUE=" & SET "@KEY=" & SET "@SECTION=" & SET "@VAR="
EXIT /B

:_PARSE_LINE
IF NOT "%@KEY:~,1%" == "#" IF NOT "%@VALUE%" == "" (
SET "%@VAR%%@SECTION%%@KEY%=%@VALUE%"
) ELSE IF "%@KEY:~,1%" == "[" IF "%@KEY:~-1%" == "]" (
SET "@SECTION=.%@KEY:~1,-1%."
)
GOTO :EOF
26 changes: 26 additions & 0 deletions Script/Config.FileWrite.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
::@author FB
::@version 0.1.0

::数据结构为Object
::仅支持一级段

::写入配置文件
:: 参数1: 实例名称
:: 参数2: 配置文件

::Script:Object.List.CMD::
::Script:Object.ListSub.CMD::
::Script:Object.ListAll.CMD::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

ECHO # This is an autogenerated file. Do not edit this file directly.>"%~2"
CALL Object.List.CMD "%~1" "{0}={1}">>"%~2"
FOR /F "usebackq delims=" %%A IN (
`CALL Object.ListSub.CMD "%~1" "{0}"`
) DO (
ECHO.>>"%~2"
CALL ECHO [%%~A]>>"%~2"
CALL Object.ListAll.CMD "%~1.%%~A" "{0}={1}">>"%~2"
)
EXIT /B
14 changes: 14 additions & 0 deletions Script/Map.Clear.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
::@author FB
::@version 0.1.0

::ÇĺżŐ
:: ˛ÎĘý1: ĘľŔýĂűłĆ

::Script:Map.Destroy.CMD::
::Script:Map.New.CMD::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CALL Map.Destroy.CMD "%~1"
CALL Map.New.CMD "%~1"
EXIT /B
14 changes: 14 additions & 0 deletions Script/Map.Destroy.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
::@author FB
::@version 0.1.0

::Ïú»Ù
:: ²ÎÊý1: ʵÀýÃû³Æ

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

CALL SET "@=%%%~1.@MAP%%"
FOR %%A IN (%@%) DO (
SET "%~1.%%~A="
)
SET "%~1.@MAP="
EXIT /B
18 changes: 18 additions & 0 deletions Script/Map.New.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
::@author FB
::@version 0.1.0

::数据组织: A.XX=* A.YY=* A.ZZ=*
::数据列表: %A.@MAP%
::数据创建: CALL Map_New.CMD A;
::数据添加: CALL Map_Push.CMD A XX "*";
::数据访问: CALL Map_Get.CMD A XX; CALL :ARRAY_REMOVE A YY;
::数据遍历: CALL Map_List.CMD A "ECHO {0} {1}";
::数据销毁: CALL Map_Destroy.CMD A;

::创建
:: 参数1: 实例名称

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET "%~1.@MAP=;"
EXIT /B
14 changes: 14 additions & 0 deletions Script/Map.Put.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
::@author FB
::@version 0.1.0

::加入
:: 参数1: 实例名称
:: 参数2: 键
:: 参数3: 值

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SET "@=%~3" & CALL SET "%~1.%~2=%%@%%"
CALL ECHO.%%%~1.@MAP%% | FIND /I ";%~2;" 1>NUL 2>&1 ^
|| CALL SET "%~1.@MAP=%%%~1.@MAP%%%~2;"
EXIT /B
12 changes: 12 additions & 0 deletions Script/Object.Destroy.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
::@author FB
::@version 0.1.0

::Ïú»Ù
:: ²ÎÊý1: ʵÀýÃû³Æ

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

FOR /F "tokens=1,* usebackq delims==" %%A IN (`SET "%~1." 2^>NUL`) DO (
SET "%%~A="
)
EXIT /B
16 changes: 16 additions & 0 deletions Script/Object.List.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
::@author FB
::@version 0.1.0

::遍历
:: 参数1: 实例名称
:: 参数2: 变量(不含""引号)/输出格式({0}键占位符, {1}值占位符)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@ECHO OFF
SETLOCAL
SET "@PREFIX=%~1."
IF DEFINED %2 (CALL SET "@CMD=%%%2%%") ELSE (SET "@CMD=%~2")
Powershell.EXE -Command "& { Get-Item Env:${Env:@PREFIX}* | ForEach-Object {$PSItem.Name = $PSItem.Name.Substring(${Env:@PREFIX}.Length); $PSItem} | Where-Object {$PSItem.Name -match '^[^.]+$'} | Sort-Object -Property Name | ForEach-Object {${Env:@CMD} -f $PSItem.Name,$PSItem.Value} }"
ENDLOCAL
EXIT /B
Loading

0 comments on commit a5e1b5a

Please sign in to comment.