bat脚本案例:命令行输入参数自动生成推送证书
程序员文章站
2022-03-28 16:13:11
需求描述项目中APP推送证书是通过包名和友盟secret生成的,每次都要手动操作复制相关文件,修改相关参数,非常容易遗漏出错。此时脚本就可以排上用场了。知识点本案例中主要运用了以下几个技能:获取输入参数写入到文件调用exe文件并根据执行结果执行下一步操作读取配置文件模版并替换模版中的占位符生成真正的配置文件子程序的封装源码@echo offcd %~dp0call :input pname "input app package name:"call :input usecret...
需求描述
项目中APP推送证书是通过包名和友盟secret生成的,每次都要手动操作复制相关文件,修改相关参数,非常容易遗漏出错。此时脚本就可以排上用场了。
知识点
本案例中主要运用了以下几个技能:
- 获取输入参数写入到文件
- 调用exe文件并根据执行结果执行下一步操作
- 读取配置文件模版并替换模版中的占位符生成真正的配置文件
- 子程序的封装
源码
@echo off
cd %~dp0
call :input pname "input app package name:"
call :input usecret "input umeng master secret:"
set curDir=%~dp0
set outputDir=%curDir%\output\%pname%
set outputZip=%curDir%\output\%pname%.zip
set pkg_cert_generator=%curDir%\assets\pkg_cert_generator
set android_umeng_file=%pkg_cert_generator%\orgfile_UMENG_android.crt
set android_key_file=%pkg_cert_generator%\pushkey_UMENG_android.crt
set output_key_file=%outputDir%\pushkey_UMENG_android.crt
set output_meta_inf=%outputDir%\META-INF
set output_packageinfo=%output_meta_inf%\packageinfo.xml
set android_packageinfo=%curDir%\assets\packageinfo.xml
set android_util=%pkg_cert_generator%\PushcertEncrypt.exe
:: check file integrity
if not exist %pkg_cert_generator% (
echo %pkg_cert_generator% not found & pause>nul
goto :eof
)
if not exist %android_util% (
echo %pkg_cert_generator% not found & pause>nul
goto :eof
)
if exist %android_key_file% del %android_key_file%
::generate and copy android push key
echo %usecret%>%android_umeng_file%
echo =========
type %android_umeng_file%
echo =========
@pushd %pkg_cert_generator%
%android_util%|findstr "0x">null&&echo pushkey generate success || echo pushkey generate failed
@popd
if not exist %android_key_file% (
echo pushkey generate failed & @pause>nul
goto :eof
)
if not exist %outputDir% md %outputDir%
if exist %output_key_file% del %output_key_file%
copy %android_key_file% %output_key_file%
if errorlevel 1 (
echo copy key file failed & @pause>nul
goto :eof
)
::generate packageinfo.xml
if not exist %outputDir% md %outputDir%
if not exist %output_meta_inf% md %output_meta_inf%
if exist %output_packageinfo% del %output_packageinfo%
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%android_packageinfo%) do (
set str=%%a
set str=!str:PACKAGE_NAME=%pname%!
if not exist %output_packageinfo% (echo !str!>%output_packageinfo%) else (echo !str!>>%output_packageinfo%)
)
::generate zip file
::use winrar
::set zipPath=C:\Program Files (x86)\WinRAR\WinRAR.exe
::echo zipPath = %zipPath%
::%zipPath% a -r %outputZip% %outputDir%\*.*
::if not errorlevel 0 (
::set zipPath=C:\Program Files\WinRAR\WinRAR.exe
::echo zipPath = %zipPath%
::%zipPath% a -r %outputZip% %outputDir%
::)
@pause
goto :eof
)
::user 7zip
@pause
goto :eof
:input
set /p temp=%2:
if "%temp%"=="" goto :input
set %1=%temp%
echo confirm = %temp%
set temp=
goto :eof
:error
echo %1
@pause
本文地址:https://blog.csdn.net/csfchh/article/details/107583803