bat脚本案例:实现flutter工程模版的创建

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

目标

flutter创建plugin工程可以使用flutter create -t plugin xxx进行创建,但是默认的工程模版可能不满足我们的需求,为了提高工程效率及工程编码的规范,可以使用脚本创建一个自定义的工程模版,自定义模版主要实现了以下需求:

  1. 创建默认的工程目录及必要的dart文件
  2. 自动引入公共组件依赖库

本文演示了创建plugin工程模板,其他app、package、module类型同理,这里不另外介绍了。

相关技术点

  1. 命令行实现明文输入
  2. 文件(夹)的创建
  3. 文件的读取和内容的修改
  4. 文件删除和重命名

具体实现

@echo off & cd /d %~dp0
mode con cols=120
rem Swtich default charset from GB2312 to UTF-8 for later file reading/writing operations.
chcp 65001>nul

type assets\logo
echo This script will guide you through creating a flutter plugin project with specified 
echo dependencies and files.
echo +--------------------------------------------------------------------------------+

:input
rem Get input plugin name.
set /p plugin_name= Enter plugin name:
if "%plugin_name%"=="" goto input
call :print "Start creating new flutter plugin project..."

rem Create plugin project with specified name
echo flutter create -t plugin --platforms=ios,android --org your.org.name -i objc -a java --no-pub %plugin_name%>bin\temp.bat
call bin\temp.bat
del bin\temp.bat

call :print "Apply default configuration..."
rem Create code folders.
call :createFolders %plugin_name%

rem Create repository.dart
call :createNewFile %plugin_name%\lib\data\repository.dart
call :appendFileContent assets\repository.dart %plugin_name%\lib\data\repository.dart

rem Add dependencies.
call :addDependency %plugin_name%

pause
goto :eof

:error
echo !------------------------------------------------------------------!
set msg=%1
echo [Error]: %msg:"=%
echo !------------------------------------------------------------------!
goto :eof

:print
echo ====================================================================
set msg=%1
echo %msg:"=%
echo ====================================================================
goto :eof

:judgeFlutter
set JUDGE_INSTALL_RET=
for /f "tokens=1" %%i in ('flutter --version^|findstr /c:"flutter"') do set JUDGE_INSTALL_RET=%%i
if "Flutter"=="%JUDGE_INSTALL_RET%" (set %0=0) else (set %1=-1)
goto :eof

:createFolders
for %%i in (data page utils view localization) do md %1\lib\%%i
for %%i in (api bean cache enums) do (
	md %1\lib\data\%%i
	call :createNewFile %1\lib\data\%%i\%%i.dart
	echo class %%i {} >> %1\lib\data\%%i\%%i.dart
	echo %1\lib\data\%%i\%%i.dart
)
goto :eof

:createNewFile
echo ///>%1
echo /// Description for this class.>>%1
echo /// >>%1
echo /// * author %USERNAME% %DATE% %TIME%>>%1
echo /// >>%1
echo /// * version V1.0>>%1
echo /// >>%1
goto :eof

:appendFileContent
for /f "delims=" %%i in (%1) do if "%%i"=="" (echo.>>%2) else (echo %%i>>%2)
goto :eof

:addDependency
set src=%1\pubspec.yaml
set pubspec=%1\pubspec.yaml.temp
set hasSet=1
setlocal enabledelayedexpansion	
::for /f "delims=" %%i in (%src%) do (
for /f "eol== delims=" %%i in ('findstr /i /n .* %src%') do (
set "var=%%i"
set temp=!var:*:=!
set var=!temp: =!
if defined temp (
	rem Findout blank lines.
	if /i not "!var!"=="" (
		if "!var!"=="sdk:flutter" (
			if !hasSet!==1 (
				set hasSet=0
				echo !temp!>> %pubspec%
				echo     your_lib:>> %pubspec%
				echo       git:>> %pubspec%
				echo         url: git@sys-gitlab.your.repo:your/path/YourLib.git>> %pubspec%
				echo         path: your_lib>> %pubspec%
			) else (
				echo !temp!>> %pubspec%
			)
			
		) else (
			echo !temp!>> %pubspec%
		)
		
	) else (
		echo.>> %pubspec%
	)
) else (
	echo.>> %pubspec%
)

)
endlocal
del %src%
pushd %1
ren pubspec.yaml.temp pubspec.yaml
popd
goto :eof

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