Skip to content

Instantly share code, notes, and snippets.

@johnnymillergh
Last active September 13, 2021 07:16
Show Gist options
  • Save johnnymillergh/a5e5f44431e64223d556e1d9898ced29 to your computer and use it in GitHub Desktop.
Save johnnymillergh/a5e5f44431e64223d556e1d9898ced29 to your computer and use it in GitHub Desktop.
Windows CMD Batch Script
@ECHO OFF
@REM ############################## auto-run ############################
@REM # Author: 钟俊, date: 2021-09-13 11:14:14.337 #
@REM # Copyright (c) 钟俊 #
@REM # Capability: Windows 10 Pro (20H2) #
@REM # Purpose: #
@REM # 1. Integrate easy and simple deployment, #
@REM # 2. Improve the efficiency of publishing frontend project (zip).#
@REM ####################################################################
SETLOCAL EnableDelayedExpansion
@REM UTF-8
chcp 65001
@REM #################### Configurable Environment Variables ###################
SET skipGitPull=false
SET microAppNumber=OPF_KK28H2DB
SET rarExe=C:\Program Files\WinRAR\WinRAR.exe
GOTO:MAIN
@REM ############################# Common Functions ############################
@REM Log trace.
@REM %~1 Info message
:logTrace
ECHO %date% %time% TRACE - %~1%
EXIT /B 0
@REM Log info.
@REM %~1 Info message
:logInfo
ECHO %date% %time% INFO - %~1%
EXIT /B 0
@REM Log warn.
@REM %~1 Warn message
:logWarn
ECHO %date% %time% WARN - %~1%
EXIT /B 0
@REM Log error.
@REM %~1 Error message
:logError
ECHO %date% %time% ERROR - %~1%
EXIT /B 0
@REM Set terminal title.
@REM %~1 Title string
:setTerminalTitle
TITLE %~1
EXIT /B 0
@REM ############################ Custom Functions #############################
@REM Pull the latest code of current branch from Git.
:gitPull
git pull
if %ERRORLEVEL% NEQ 0 (
CALL :logError "Failed to execute command `Git pull`"
EXIT /B %ERRORLEVEL%
)
EXIT /B 0
@REM Execute pre-build phase.
:executePreBuildPhase
FOR /F "tokens=*" %%i IN ('git rev-parse --abbrev-ref HEAD') DO SET currentBranch=%%i
CALL :logWarn "[PRE-BUILD] Current Git branch: %currentBranch%"
if %skipGitPull% == true (
CALL :logWarn "[PRE-BUILD] Skipped Git pull"
) else (
CALL :logInfo "[PRE-BUILD] Start to pull latest code from Git"
CALL :gitPull
)
CALL :logInfo "[PRE-BUILD] Cleaning 'build' directory"
CALL rmdir /Q /S build
CALL :logInfo "[PRE-BUILD] Recreating 'build' directory"
CALL mkdir "build/%microAppNumber%"
CALL :logInfo "[PRE-BUILD] Copying files into directory"
CALL robocopy %cd% %cd%\build\%microAppNumber% /e /xa:h /mir /xd %cd%\.idea /mir /xd %cd%\.git /mir /xd %cd%\build > nul
CALL :logInfo "[PRE-BUILD] Current directory:"
dir
EXIT /B 0
@REM Execute build phase.
:executeBuildPhase
CALL :logInfo "[BUILD] Start to build zip file"
CALL "%rarExe%" a -r -ep1 -afzip %cd%\build\%microAppNumber%.zip %cd%\build\%microAppNumber%
if %ERRORLEVEL% NEQ 0 (
CALL :logError "[BUILD] Failed to build zip file"
EXIT /B %ERRORLEVEL%
)
CALL :logInfo "[BUILD] Build zip file successfully"
EXIT /B 0
@REM Execute post build phase.
:executePostBuildPhase
CALL :logWarn "[POST_BUILD] Deleting temporary directory�"
CALL rmdir /Q /S build\%microAppNumber%
EXIT /B 0
@REM ############################# MAIN Procedures #############################
:MAIN
cls
@REM Pre-build phrase (Git pull)
CALL :executePreBuildPhase
if %ERRORLEVEL% NEQ 0 (
CALL :logError "[PRE-BUILD] Failed to execute pre-build phase"
EXIT /B %ERRORLEVEL%
) else (
CALL :logInfo "[PRE-BUILD] Succeeded to execute pre-build phase"
)
@REM Build phase
CALL :executeBuildPhase
if %ERRORLEVEL% NEQ 0 (
CALL :logError "[BUILD] Failed to execute build phase"
EXIT /B %ERRORLEVEL%
) else (
CALL :logInfo "[BUILD] Succeeded to execute build phase"
)
@REM Run post-build phase
CALL :executePostBuildPhase
if %ERRORLEVEL% NEQ 0 (
CALL :logError "[POST_BUILD] Failed to execute post-build phase"
EXIT /B %ERRORLEVEL%
) else (
CALL :logInfo "[POST_BUILD] Succeeded to execute post-build phase"
)
ENDLOCAL
@ECHO OFF
@REM ############################## auto-run ############################
@REM # Author: 钟俊, date: 4:31 AM, Dec. 7, 2020 #
@REM # Copyright (c) 钟俊 #
@REM # Capability: Windows 10 Pro (20H2) #
@REM # Purpose: #
@REM # 1. Integrate easy and simple deployment, #
@REM # 2. Reduce memory usage of IntelliJ IDEA to run services, #
@REM # 3. Improve the efficiency of local development. #
@REM ####################################################################
@rem Set local scope for the variables with windows NT shell
SETLOCAL EnableDelayedExpansion
@REM UTF-8
chcp 65001
@REM #################### Configurable Environment Variables ###################
SET skipGitPull=false
SET jvmSpringActiveProfile=test
SET jvmPandoraParameter=-Dpandora.location=C:\Users\Johnny\lib\taobao-hsf.sar-dev-SNAPSHOT.jar
SET jvmCommonParameter=-Dfile.encoding=UTF-8 -Xms256m -Xmx256m -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=2346
SET jvmEdasParameter=-Dvipserver.server.port=8080 -Daddress.server.domain=localhost
GOTO:MAIN
@REM ############################# Common Functions ############################
@REM Log info.
@REM %~1 Info message
:logInfo
ECHO %date% %time% [INFO] - %~1%
EXIT /B 0
@REM Log warn.
@REM %~1 Warn message
:logWarn
ECHO %date% %time% [WARN] - %~1%
EXIT /B 0
@REM Log error.
@REM %~1 Error message
:logError
ECHO %date% %time% [ERROR] - %~1%
EXIT /B 0
@REM Set terminal title.
@REM %~1 Title string
:setTerminalTitle
TITLE %~1
EXIT /B 0
@REM ############################ Custom Functions #############################
@REM Pull the latest code of current branch from Git.
:gitPull
git pull
if %ERRORLEVEL% NEQ 0 (
CALL :logError "Failed to execute command `Git pull`"
EXIT /B %ERRORLEVEL%
)
EXIT /B 0
@REM Execute pre-build phase.
:executePreBuildPhase
CALL :logWarn "[PRE-BUILD] Current active Spring profile: %jvmSpringActiveProfile%"
CALL :logInfo "[PRE-BUILD] Current directory:"
dir
CALL :logInfo "[PRE-BUILD] Java Version Information"
CALL java -version
CALL :logInfo "[PRE-BUILD] Gradle Version Information"
CALL gradle -v
FOR /F "tokens=*" %%i IN ('git rev-parse --abbrev-ref HEAD') DO SET currentBranch=%%i
CALL :logWarn "[PRE-BUILD] Current Git branch: %currentBranch%"
if %skipGitPull% == true (
CALL :logWarn "[PRE-BUILD] Skipped Git pull"
) else (
CALL :logInfo "[PRE-BUILD] Start to pull latest code from Git"
CALL :gitPull
)
EXIT /B 0
@REM Execute build phase.
:executeBuildPhase
CALL :logInfo "[BUILD] Start to clean and build by Gradle"
CALL gradle clean build
if %ERRORLEVEL% NEQ 0 (
CALL :logError "[BUILD] Failed to execute command `gradle clean build`"
EXIT /B %ERRORLEVEL%
) else (
CALL :logInfo "[BUILD] Gradle build success"
)
EXIT /B 0
@REM Execute run phase.
:executeRunPhase
FOR /F "tokens=*" %%i IN ('dir /B/A:- build\libs\*.jar') DO SET jarFileName=%%i
CALL :logWarn "[RUN] Generated JAR: %jarFileName%. Setting termial title as %jarFileName%"
CALL :setTerminalTitle %jarFileName%
SET runJarCommand=java %jvmCommonParameter% -Dspring.profiles.active=%jvmSpringActiveProfile% %jvmEdasParameter% %jvmPandoraParameter% -jar build\libs\%jarFileName%
CALL :logWarn "[RUN] Execute command: %runJarCommand%"
CALL java %jvmCommonParameter% -Dspring.profiles.active=%jvmSpringActiveProfile% %jvmEdasParameter% %jvmPandoraParameter% -jar build\libs\%jarFileName%
EXIT /B 0
@REM ############################# MAIN Procedures #############################
:MAIN
cls
@REM Pre-build phrase (Display version, Git pull)
CALL :executePreBuildPhase
if %ERRORLEVEL% NEQ 0 (
CALL :logError "[PRE-BUILD] Failed to execute pre-build phase"
EXIT /B %ERRORLEVEL%
)
@REM Build phase
CALL :executeBuildPhase
if %ERRORLEVEL% NEQ 0 (
CALL :logError "[BUILD] Failed to execute build phase"
EXIT /B %ERRORLEVEL%
)
@REM Run phase
CALL :executeRunPhase
ENDLOCAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment