目标
flutter创建plugin工程可以使用flutter create -t plugin xxx
进行创建,但是默认的工程模版可能不满足我们的需求,为了提高工程效率及工程编码的规范,可以使用脚本创建一个自定义的工程模版,自定义模版主要实现了以下需求:
- 创建默认的工程目录及必要的dart文件
- 自动引入公共组件依赖库
本文演示了创建plugin工程模板,其他app、package、module类型同理,这里不另外介绍了。
相关技术点
- 命令行实现明文输入
- 文件(夹)的创建
- 文件的读取和内容的修改
- 文件删除和重命名
具体实现
@echo off & cd /d %~dp0
mode con cols=120
chcp 65001>nul
echo +---------------------------------------------------------------------------------+
call :showLogo
echo This script will guide you through creating a flutter plugin project with specified
echo dependencies and file directories
echo.
echo +---------------------------------------------------------------------------------+
rem Mark original path to access assets files for later use.
set scriptPath=%cd%
:inputPlugin
set /p pluginName= Input plugin name:
if "%pluginName%"=="" goto input
if "%pluginName%"=="test" (
call :error "'test' is not allowed as a project name, enter another name."
goto inputPlugin
)
:inputProject
set /p v= Input project path(such as D:\FlutterWorkspace\Porjects), or press Entry to use default:
if not "%v%"=="" call :changeDir %v%
set projectPath=%cd%\%pluginName%
echo [INFO] Project path is: %projectPath%
call :print "Start creating new flutter plugin project..."
echo flutter create -t plugin --platforms=ios,android --org hik.flutter.ebg -i objc -a java --no-pub %pluginName%>temp.bat
call temp.bat
if errorlevel 1 (
echo [INFO] Your flutter version is too low please update.
echo flutter create -t plugin --org hik.flutter.ebg -i objc -a java --no-pub %pluginName%>temp.bat
call temp.bat
)
del temp.bat
call :print "Apply default configuration..."
call :createFolders %projectPath%
rem Add dependency
call :addFlutterDependency %projectPath%
call :addEBGRepository %projectPath%\android
call :addEBGRepository %projectPath%\example\android
pause
goto :eof
:error
echo !---------------------------------------------------------------------------------!
echo.
set msg=%1
echo [Error]: %msg:"=%
echo.
echo !---------------------------------------------------------------------------------!
goto :eof
:print
echo ===================================================================================
echo.
set msg=%1
echo %msg:"=%
echo.
echo ===================================================================================
goto :eof
:judgeInstall
set JUDGE_INSTALL_RET=
for /f "tokens=1" %%i in ('%1 --version^|findstr /c:"%1"') do set JUDGE_INSTALL_RET=%%i
if /i "%1"=="%JUDGE_INSTALL_RET%" (set %2=0) else (set %2=-1)
goto :eof
:changeDir
if not exist %1 md %1
if not exist %1 (
call :error "Invalid directory, example: D:\test\test"
goto inputProject
)
cd /d %1
goto :eof
:createFolders
for %%i in (data page utils view localization) do (
md %1\lib\%%i
if "%%i"=="page" (
call :createNewFile %1\lib\page\router.dart
echo class router {} >> %1\lib\page\router.dart
echo %1\lib\page\router.dart
) else (
if "%%i"=="data" (
rem Create repository.dart
call :createNewFile %1\lib\data\repository.dart
call :appendFileContent %scriptPath%\assets\repository.dart %1\lib\data\repository.dart
) else (
call :createNewFile %1\lib\%%i\%%i.dart
echo class %%i {} >> %1\lib\%%i\%%i.dart
echo %1\lib\%%i\%%i.dart
)
)
)
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
goto :eof
:appendFileContent
for /f "delims=" %%i in (%1) do if "%%i"=="" (echo.>>%2) else (echo %%i>>%2)
goto :eof
:addFlutterDependency
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 package_name:>> %pubspec%
echo git:>> %pubspec%
echo url: git@sys-gitlab.yourhost.com:Your/Path/Name.git>> %pubspec%
echo path: package_name>> %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
:addEBGRepository
set src=%1\build.gradle
set temp=%src%.temp
setlocal enabledelayedexpansion
for /f "eol== delims=" %%i in ('findstr /i /n .* %src%') do (
set "var=%%i"
set line=!var:*:=!
set shrink=!line: =!
if defined line (
if /i not "!shrink!"=="" (
if "!shrink!"=="repositories{" (
echo !line!>> %temp%
echo maven {>> %temp%
echo url 'http://your.host.com/artifactory/maven-down/'>> %temp%
echo credentials {>> %temp%
echo username "username">> %temp%
echo password "password">> %temp%
echo }>> %temp%
echo }>> %temp%
) else (
echo !line!>> %temp%
)
) else (
echo.>> %temp%
)
) else (
echo.>> %temp%
)
)
endlocal
del %src%
pushd %1
ren build.gradle.temp build.gradle
popd
goto :eof
:showLogo
if not exist .outlogo (
echo CiAgX18gICAgICAgX19fX18gICAgIF9fX19fXyAgICAgIF9fX19fICAgIAogL1xfXCAgICAgKSBfX18gKCAgIC9fL1xfX19cICAgICkgX19fICggICAKKCAoICggICAgLyAvXF8vXCBcICApICkgX19fLyAgIC8gL1xfL1wgXCAgCiBcIFxfXCAgLyAvXy8gKF9cIFwvXy8gLyAgX19fIC8gL18vIChfXCBcIAogLyAvIC9fX1wgXCApXy8gLyAvXCBcIFxfL1xfX1xcIFwgKV8vIC8gLyAKKCAoX19fX18oXCBcL19cLyAvICApXykgIFwvIF8vIFwgXC9fXC8gLyAgCiBcL19fX19fLyApX19fX18oICAgXF9cX19fXy8gICAgKV9fX19fKCAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAK>.logo
certutil -decode .logo .outlogo>nul
del .logo
)
type .outlogo
goto :eof