话不多说,先看效果
源码贴上
@echo off
:: 因为没有参数,所以这里会跳到 Dog
call :Dog%1 %2 %3
exit
:: =========================主程序========================= ::
:Dog
set BS=
:InputLoop
cls
set /p proj=输入工程名:
if "%proj%"=="" goto InputLoop
:: 输入判断
if exist "%proj%" (
echo 工程已存在,3s后退出...
timeout /t 3 /nobreak >nul
goto END
)
:: 创建工程
echo.
echo 创建Python工程^(%proj%^)
:: 创建工程目录
set prefix=创建工程目录
set file=%proj%
set /p=%BS%[ ] %prefix%<nul
mkdir "%file%" >nul 2>nul
if exist "%proj%" (
echo %BS%[ √ ] %prefix%
) else (
echo %BS%[ × ] %prefix%
goto CreateProjectFailed
)
:: 生成python虚拟环境(venv)
set prefix=生成python虚拟环境
set file=%proj%\venv\Scripts\pip.exe
set /p=%BS%[ ] %prefix%<nul
start /b %~n0%~x0 DotLoop %file%
python.exe -m venv "%proj%\venv" >nul 2>nul
if exist "%file%" (
echo %BS%[ √ ] %prefix%
) else (
echo %BS%[ × ] %prefix%
goto CreateProjectFailed
)
:: 添加main^.py到工程
set prefix=添加main^.py到工程
set file=%proj%\main.py
set /p=%BS%[ ] %prefix%<nul
call :GenerateMainFile
if exist "%file%" (
echo %BS%[ √ ] %prefix%
) else (
echo %BS%[ × ] %prefix%
goto CreateProjectFailed
)
:: 添加vscode配置
set prefix=添加vscode配置
set file=%proj%\.vscode\settings.json
set /p=%BS%[ ] %prefix%<nul
call :ConfigureVscodeSettings
if exist "%file%" (
echo %BS%[ √ ] %prefix%
) else (
echo %BS%[ × ] %prefix%
goto CreateProjectFailed
)
:: 创建工程完成
echo 创建工程完成
:: 打开工程
echo 使用code打开工程
cmd /c code "%proj%"
timeout /t 3 /nobreak >nul
:: 结束
goto :eof
:: =================================Main End================================== ::
:: =========================Define Your Function Here========================= ::
:: ======================Configure Vscode Settings Start====================== ::
:ConfigureVscodeSettings
mkdir "%proj%\.vscode" >nul 2>nul
(
echo {
echo "python.defaultInterpreterPath": "${workspaceFolder}\\venv\\Scripts\\python.exe",
echo "python.terminal.activateEnvInCurrentTerminal": true,
echo "python.formatting.provider": "black",
echo "python.formatting.blackArgs": [
echo "--skip-string-normalization",
echo "--line-length",
echo "120"
echo ],
echo }
)>"%proj%\.vscode\settings.json"
goto :eof
:: ======================Configure Vscode Settings End======================== ::
:: ======================Generate main.py Start=============================== ::
:GenerateMainFile
(
echo def print_hello^(^):
echo print^('hello'^)
echo.
echo.
echo if __name__ == '__main__':
echo print_hello^(^)
echo.
)>"%proj%\main.py"
goto :eof
:: ======================Generate main.py End================================= ::
:: Function End
:: File has exist
:ProjectAlreadyExist
rmdir /s/q "%proj%"
for %%i in (3 2 1) do (
set /p=%BS%工程已经存在, %%i秒后退出<nul
timeout /t 1 /nobreak >nul 2>nul
)
set /p=%BS% <nul
goto END
:: ======================start 函数区====================== ::
:: ======================================================== ::
:: 循环打印(. .. ...),直到给定的文件出现
:: @param %1 文件
:: 调用格式 call %0 "param"
:DogDotLoop
if not exist "%1" (set /p=.<nul) else ( exit )
timeout /t 1 /nobreak >nul
if not exist "%1" (set /p=.<nul) else ( exit )
timeout /t 1 /nobreak >nul
if not exist "%1" (set /p=.<nul) else ( exit )
timeout /t 1 /nobreak >nul
if not exist "%1" (set /p= <nul) else ( exit )
goto DogDotLoop
exit
:: ======================================================== ::
:: 循环打印(............),直到给定的文件出现
:: @param %1 文件
:: 调用格式 call %0 "param"
:DogDotNoLoop
if not exist "%1" (set /p=.<nul) else ( goto :eof )
timeout /t 1 /nobreak >nul
goto DogDotNoLoop
exit
:END