目标
flutter安装的过程主要分为两步:
- 下载flutter SDK的zip包并解压
- 配置相关环境变量
本案例演示了如何利用bat脚本从SVN仓库下载flutter SDK并配置相关环境变量,并给出了具体实现代码。
相关技术点
- 命令行实现明文/密文输入
- svn命令下载文件
- 利用7zip解压zip文件
- 判断及设置系统环境变量
- 判断本机是否安装git
具体实现
@echo off
cd /d %~dp0
call :print "[Step1]: Download flutter SDK"
set zipFileName=flutter_windows_1.22.6-stable.zip
set svnDirUrl=https://your.svn.path
set zipPath=%cd%\%zipFileName%
set password=
set env=%cd%\flutter
rem Download sdk if not exist.
if not exist %zipPath% (
set /p username= Enter OAUsernName:
call :getPassword
setlocal EnableDelayedExpansion
echo Downloading...
call :downloadBySvn %zipPath% %username% !password!
endlocal
call :sleep 1
)
rem Sometimes download fails, skip to finish.
if not exist %zipPath% (
call :error "Download failed"
goto finish
)
call :print "[Step2]: Install flutter SDK"
if not exist %env% (
call :unzip %zipPath%
)
if not exist %env% (
call :error "Failed to unzip file!"
) else (
rem Setx need root authority to take effects, run as administrator.
call :print "[Step3]: Setup environments"
setx /m FLUTTER_HOME %env%
echo FLUTTER_HOME=%FLUTTER_HOME%
setx /m PATH "%PATH%;%env%\bin"
if not defined FLUTTER_STORAGE_BASE_URL setx /m FLUTTER_STORAGE_BASE_URL https://storage.flutter-io.cn
echo FLUTTER_STORAGE_BASE_URL=%FLUTTER_STORAGE_BASE_URL%
if not defined LUTTER_STORAGE_BASE_URL setx /m PUB_HOSTED_URL https://pub.flutter-io.cn
echo PUB_HOSTED_URL=%PUB_HOSTED_URL%
call :print "[Step4]: Enjoy flutter"
rem Because flutter relies on git, check git existence before execute flutter commands.
call :judgeGit return
if %return%==0 (
echo flutter doctor>temp.bat
call temp.bat
del temp.bat
) else (
call :error "Git not installed, please install git"
)
)
:finish
@pause
goto :eof
:error
echo !------------------------------------------------------------------!
set msg=%1
echo [Error]: %msg:"=%
echo !------------------------------------------------------------------!
goto :eof
:print
echo ====================================================================
set msg=%1
echo %msg:"=%
echo ====================================================================
goto :eof
:unzip
echo unzip:%1
rem For those has installed 7zip, simplely call 7z
.\tools\7zip\7z.exe x %1
goto :eof
:sleep
ping -n %1 127.0.0.1>nul
goto :eof
:downloadBySvn
rem For those has installed svn, simplely call svn
tools\svn\svn.exe export %svnDirUrl%/%zipFileName% %1 --username %2 --password %3
goto :eof
:getPassword
set "psCommand=powershell -Command "$pword = read-host 'Enter OAPassword' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
goto :eof
:judgeGit
set JUDGE_INSTALL_RET=
for /f "tokens=1" %%i in ('git --version^|findstr /c:"git"') do set JUDGE_INSTALL_RET=%%i
if "git"=="%JUDGE_INSTALL_RET%" (set %0=0) else (set %1=-1)
goto :eof

本文介绍了一种使用批处理脚本自动下载并安装Flutter SDK的方法,包括从SVN仓库获取Flutter SDK压缩包、解压并配置环境变量等步骤。
424

被折叠的 条评论
为什么被折叠?



