[Build]PowerShell based Building Scripts
Keywords: Build, Windows CMD, PowerShell based Building Scripts, batch, BAT
Power Shell
How to setup building environment of MSCV (VC++)
.ps1:
if (!(Test-Path vswhere.exe)) {
Invoke-WebRequest -Uri "https://github.com/microsoft/vswhere/releases/download/2.8.4/vswhere.exe" -OutFile vswhere.exe
}
# setup vsdevcmd.bat
# https://github.com/microsoft/vswhere/wiki/Find-VC
$path = .\vswhere.exe -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath
if ($path) {
$batpath = join-path $path 'Common7\Tools\vsdevcmd.bat'
if (test-path $batpath) {
cmd /s /c """$batpath"" $args && set" | Where-Object { $_ -match '(\w+)=(.*)' } | ForEach-Object {
$null = new-item -force -path "Env:\$($Matches[1])" -value $Matches[2]
}
}
# dbghelp.dll, administrator permission needed
$debuggerpath = join-path $path 'Common7\IDE\Extensions\TestPlatform\Extensions\Cpp\x64\dbghelp.dll'
if (!(Test-Path 'C:\Program Files (x86)\Windows Kits\10\Debuggers\x64')) {
New-Item 'C:\Program Files (x86)\Windows Kits\10\Debuggers\x64' -ItemType Directory -Force
Copy-Item $debuggerpath 'C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\dbghelp.dll'
}
}
.bat:
@echo off
set PROTOBUF_ARCH=x64
set VSDEVCMD=C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat
if exist "%VSDEVCMD%" (
@REM Tell VsDevCmd.bat to set the current directory, in case [USERPROFILE]\source exists. See:
@REM C:\Program Files (x86)\Microsoft Visual Studio\%COMPILER%\Community\Common7\Tools\vsdevcmd\core\vsdevcmd_end.bat
set VSCMD_START_DIR=%CD%
call "%VSDEVCMD%" -arch=%PROTOBUF_ARCH%
) else (
echo ERROR: Cannot find Visual Studio %COMPILER%
exit /b 2
)
How to replace string in text file
example.ps1:
(Get-Content ../source/build.cs) | ForEach-Object { $_ -replace '//UserList.Add\("NeilWang"\);', 'UserList.Add("NeilWang");' } | Set-Content ../source/build.cs
Result:
//UserList.Add("NeilWang");
would be replaced as UserList.Add("NeilWang");
.
Reference:
https://stackoverflow.com/a/60065/1645289
If want to run powershell script from Windows batch. do follows.
example.bat:
powershell -Command "(Get-Content ../source/build.cs) | ForEach-Object { $_ -replace '//UserList.Add\(\"NeilWang\"\);', 'UserList.Add(\"NeilWang\");' } | Set-Content ../source/build.cs"
How to call powershell script file(.ps1) from CMD
Example:
powershell -ExecutionPolicy Bypass -File example.ps1
Reference:
PowerShell says “execution of scripts is disabled on this system.”
https://stackoverflow.com/a/9167524/1645289
Batch (CMD)
How to delete file if exists
IF EXIST [Filename] (
del [Filename]
) ELSE (
...
)
or:
if exist c:\folder\file del c:\folder\file
Origin: Delete files in batch with without error message
https://stackoverflow.com/a/16552500/1645289
How to delete all fiels
::removes all files from the directory,
del /q destination\*
::recursively removes all nested directories
for /d %%x in (destination\*) do @rd /s /q "%%x"
How to delete all files and folders in a folder by cmd call
https://stackoverflow.com/a/1502935/1645289
How to delete directory (folder)
if exist "c:\yourFolder\" rd /q /s "c:\yourFolder"
Delete a directory and its files using command line but don’t throw error if it doesn’t exist
https://stackoverflow.com/a/14504615/1645289
How to clean all directories of current folder
for /d %%D in (../Plugins/*) do (
if exist %~dp0..\Plugins\%%D\Binaries rd /q /s %~dp0..\Plugins\%%D\Binaries
if exist %~dp0..\Plugins\%%D\Intermediate rd /q /s %~dp0..\Plugins\%%D\Intermediate
)
How to pause an exe after startup
start "" "CMD " /c myapp.exe ^& pause
Origin:
https://stackoverflow.com/a/53454665/1645289
How to get current directory path
%~dp0
, example:
%~dp0\MyApp\main.exe
Reference
Examples
Building script of WebRTC:
https://github.com/crow-misia/libwebrtc-bin/blob/main/build.windows.ps1
Books
Book List
The PowerShell Scripting and Toolmaking Book
https://leanpub.com/powershell-scripting-toolmaking
“This defines entrepreneur and entrepreneurship - the entrepreneur always searches for change, responds to it, and exploits it as an opportunity.” ― Peter F. Drucker, Innovation and Entrepreneurship: Practice and Principles