bat脚本案例:实现flutter一键下载和安装

  • 时间:
  • 浏览:
  • 来源:互联网

目标

flutter安装的过程主要分为两步:

  1. 下载flutter SDK的zip包并解压
  2. 配置相关环境变量

本案例演示了如何利用bat脚本从SVN仓库下载flutter SDK并配置相关环境变量,并给出了具体实现代码。

相关技术点

  1. 命令行实现明文/密文输入
  2. svn命令下载文件
  3. 利用7zip解压zip文件
  4. 判断及设置系统环境变量
  5. 判断本机是否安装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

本文链接http://www.dzjqx.cn/news/show-617167.html