Skip to content

Commit

Permalink
implement better error handling for build script
Browse files Browse the repository at this point in the history
  • Loading branch information
carfesh committed Mar 10, 2017
1 parent 474377d commit 4f92e58
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 113 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ fellow/Docs/WinFellow/WinFellow User Manual.pdf
*.obj
*.pdb
*.tlog
*.exe
*.exe
*.log
fellow/SRC/WIN32/MSVC/.vs/WinFellow/v15/
239 changes: 127 additions & 112 deletions fellow/SRC/WIN32/Scripts/CompileReleaseBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,153 +75,168 @@ else
}
$ErrorActionPreference="Stop"

$FELLOWPLATFORM="Win32"
$CMDLINETOOLS = [Environment]::GetEnvironmentVariable("VS120COMNTOOLS", "Machine")
Function Main()
{
$FELLOWPLATFORM="Win32"
$CMDLINETOOLS = [Environment]::GetEnvironmentVariable("VS120COMNTOOLS", "Machine")

ShowProgressIndicator 1 "Checking prerequisites..."
ShowProgressIndicator 1 "Checking prerequisites..."

$result = CheckForExeInSearchPath "git.exe"
$result = CheckForExeInSearchPath "lyx.exe"
$result = CheckForExeInSearchPath "7z.exe"
$result = CheckForExeInSearchPath "pdflatex.exe"
$result = CheckForExeInSearchPath "makensis.exe"
$result = CheckForExeInSearchPath "git.exe"
$result = CheckForExeInSearchPath "lyx.exe"
$result = CheckForExeInSearchPath "7z.exe"
$result = CheckForExeInSearchPath "pdflatex.exe"
$result = CheckForExeInSearchPath "makensis.exe"

Set-Alias SevenZip "7z.exe"
Set-Alias SevenZip "7z.exe"

$SourceCodeBaseDir = Resolve-Path (git rev-parse --show-cdup)
Write-Debug "Source Code Base Dir: $SourceCodeBaseDir"
$temp = [Environment]::GetEnvironmentVariable("TEMP", "User")
Write-Debug "Temporary Dir : $temp"
$TargetOutputDir = Resolve-Path ("$SourceCodeBaseDir\..")
Write-Debug "Target Output Dir : $TargetOutputDir"
$SourceCodeBaseDir = Resolve-Path (git rev-parse --show-cdup)
Write-Debug "Source Code Base Dir: $SourceCodeBaseDir"
$temp = [Environment]::GetEnvironmentVariable("TEMP", "User")
Write-Debug "Temporary Dir : $temp"
$TargetOutputDir = Resolve-Path ("$SourceCodeBaseDir\..")
Write-Debug "Target Output Dir : $TargetOutputDir"

$MSBuildLog = "$temp\WinFellow-MSBuild.log"
$MSBuildLog = "$temp\WinFellow-MSBuild.log"

Push-Location
Push-Location

cd $SourceCodeBaseDir
cd $SourceCodeBaseDir

if($FELLOWBUILDPROFILE -eq "Release")
{
ShowProgressIndicator 2 "Release build, checking Git working copy for local modifications..."

$result = (git status --porcelain)
if ($result -ne $0)
{
Write-Error "Local working copy contains modifications, aborting - a release build must always be produced from a clean and current working copy."
exit $result
}
$GitBranch = (git rev-parse --abbrev-ref HEAD)
$result = (git log origin/$GitBranch..HEAD)
if ($result -ne $0)
if($FELLOWBUILDPROFILE -eq "Release")
{
Write-Error "Local working copy (branch $GitBranch) contains commits there were not pushed yet - a release build must always be produced from a clean and current working copy."
exit $result
ShowProgressIndicator 2 "Release build, checking Git working copy for local modifications..."

$result = (git status --porcelain)
if ($result -ne $0)
{
Write-Error "Local working copy contains modifications, aborting - a release build must always be produced from a clean and current working copy."
exit $result
}
$GitBranch = (git rev-parse --abbrev-ref HEAD)
$result = (git log origin/$GitBranch..HEAD)
if ($result -ne $0)
{
Write-Error "Local working copy (branch $GitBranch) contains commits there were not pushed yet - a release build must always be produced from a clean and current working copy."
exit $result
}
}
}

ShowProgressIndicator 3 "Updating Git working copy to latest version..."
ShowProgressIndicator 3 "Updating Git working copy to latest version..."

$result = (git pull)
$result = (git pull)

ShowProgressIndicator 4 "Performing clean build of WinFellow..."
ShowProgressIndicator 4 "Performing clean build of WinFellow..."

Write-Debug "Using Visual Studio command-line tools from: $CMDLINETOOLS"
$result = ("$CMDLINETOOLS\VsDevCmd.bat")
Write-Debug "Using Visual Studio command-line tools from: $CMDLINETOOLS"
$result = ("$CMDLINETOOLS\VsDevCmd.bat")

if ((Get-Command "msbuild.exe" -ErrorAction SilentlyContinue) -eq $null)
{
Write-Error "Unable to find msbuild.exe in your PATH"
}
Write-Verbose "Executing MSBuild.exe..."
$result = (msbuild.exe $SourceCodeBaseDir\fellow\SRC\WIN32\MSVC\WinFellow.vcxproj /t:"Clean;Build" /p:Configuration=$FELLOWBUILDPROFILE /p:Platform=$FELLOWPLATFORM /fl /flp:logfile=$MSBuildLog)
if ((Get-Command "msbuild.exe" -ErrorAction SilentlyContinue) -eq $null)
{
Write-Error "Unable to find msbuild.exe in your PATH"
}
Write-Verbose "Executing MSBuild.exe..."
$result = (msbuild.exe $SourceCodeBaseDir\fellow\SRC\WIN32\MSVC\WinFellow.vcxproj /t:"Clean;Build" /p:Configuration=$FELLOWBUILDPROFILE /p:Platform=$FELLOWPLATFORM /fl /flp:logfile=$MSBuildLog)

If($LastExitCode -ne 0)
{
ii $MSBuildLog
Pop-Location
Write-Error "ERROR executing MSBuild, opening logfile '$MSBuildLog'."
}
If($LastExitCode -ne 0)
{
ii $MSBuildLog
Pop-Location
Write-Error "ERROR executing MSBuild, opening logfile '$MSBuildLog'."
}

Write-Verbose "Checking file version of file WinFellow\fellow\SRC\Win32\MSVC\$FELLOWBUILDPROFILE\WinFellow.exe..."
$FELLOWVERSION = (Get-Item $SourceCodeBaseDir\fellow\SRC\Win32\MSVC\$FELLOWBUILDPROFILE\WinFellow.exe).VersionInfo.ProductVersion
Write-Debug "Detected file version: $FELLOWVERSION"
Write-Verbose "Checking file version of file WinFellow\fellow\SRC\Win32\MSVC\$FELLOWBUILDPROFILE\WinFellow.exe..."
$FELLOWVERSION = (Get-Item $SourceCodeBaseDir\fellow\SRC\Win32\MSVC\$FELLOWBUILDPROFILE\WinFellow.exe).VersionInfo.ProductVersion
Write-Debug "Detected file version: $FELLOWVERSION"

ShowProgressIndicator 5 "Generating GPL terms..."
ShowProgressIndicator 5 "Generating GPL terms..."

Copy-Item -Force "$SourceCodeBaseDir\fellow\Docs\WinFellow\gpl-2.0.tex" "$temp"
cd $temp
$result = (pdflatex gpl-2.0.tex)
Copy-Item -Force "$SourceCodeBaseDir\fellow\Docs\WinFellow\gpl-2.0.tex" "$temp"
cd $temp
$result = (pdflatex gpl-2.0.tex)

ShowProgressIndicator 6 "Generating ChangeLog..."
ShowProgressIndicator 6 "Generating ChangeLog..."

cd $SourceCodeBaseDir
$result = (git log --date=short --pretty=format:"%h - %<(17)%an, %ad : %w(80,0,42)%s%n%w(80,42,42)%b" --invert-grep --grep="" > $temp\ChangeLog.txt)
cd $SourceCodeBaseDir
$result = (git log --date=short --pretty=format:"%h - %<(17)%an, %ad : %w(80,0,42)%s%n%w(80,42,42)%b" --invert-grep --grep="" > $temp\ChangeLog.txt)

ShowProgressIndicator 7 "Generating User Manual..."
ShowProgressIndicator 7 "Generating User Manual..."

$result = (lyx --export pdf2 "$SourceCodeBaseDir\fellow\Docs\WinFellow\WinFellow User Manual.lyx" | Out-Null)
$result = (lyx --export pdf2 "$SourceCodeBaseDir\fellow\Docs\WinFellow\WinFellow User Manual.lyx" | Out-Null)

ShowProgressIndicator 8 "Assembling release build..."
ShowProgressIndicator 8 "Assembling release build..."

$OUTPUTDIR = "$temp\WinFellow_v$FELLOWVERSION"
$result = mkdir $OUTPUTDIR -Force
$OUTPUTDIR = Resolve-Path $OUTPUTDIR
Write-Debug "Build output dir: $OUTPUTDIR"
$OUTPUTDIR = "$temp\WinFellow_v$FELLOWVERSION"
$result = mkdir $OUTPUTDIR -Force
$OUTPUTDIR = Resolve-Path $OUTPUTDIR
Write-Debug "Build output dir: $OUTPUTDIR"

Move-Item -Force "$temp\ChangeLog.txt" "$OUTPUTDIR\ChangeLog.txt"
Move-Item -Force "$SourceCodeBaseDir\fellow\Docs\WinFellow\WinFellow User Manual.pdf" "$OUTPUTDIR\WinFellow User Manual.pdf"
Move-Item -Force "$SourceCodeBaseDir\fellow\SRC\WIN32\MSVC\$FELLOWBUILDPROFILE\WinFellow.exe" "$OUTPUTDIR\WinFellow.exe"
Move-Item -Force "$SourceCodeBaseDir\fellow\SRC\WIN32\MSVC\$FELLOWBUILDPROFILE\WinFellow.pdb" "$OUTPUTDIR\WinFellow.pdb"
Copy-Item -Force "$SourceCodeBaseDir\fellow\Presets" "$OUTPUTDIR\Presets" -Recurse
Copy-Item -Force "$SourceCodeBaseDir\fellow\Utilities" "$OUTPUTDIR\Utilities" -Recurse
Copy-Item -Force "$temp\gpl-2.0.pdf" "$OUTPUTDIR\gpl-2.0.pdf"
Move-Item -Force "$temp\ChangeLog.txt" "$OUTPUTDIR\ChangeLog.txt"
Move-Item -Force "$SourceCodeBaseDir\fellow\Docs\WinFellow\WinFellow User Manual.pdf" "$OUTPUTDIR\WinFellow User Manual.pdf"
Move-Item -Force "$SourceCodeBaseDir\fellow\SRC\WIN32\MSVC\$FELLOWBUILDPROFILE\WinFellow.exe" "$OUTPUTDIR\WinFellow.exe"
Move-Item -Force "$SourceCodeBaseDir\fellow\SRC\WIN32\MSVC\$FELLOWBUILDPROFILE\WinFellow.pdb" "$OUTPUTDIR\WinFellow.pdb"
Copy-Item -Force "$SourceCodeBaseDir\fellow\Presets" "$OUTPUTDIR\Presets" -Recurse
Copy-Item -Force "$SourceCodeBaseDir\fellow\Utilities" "$OUTPUTDIR\Utilities" -Recurse
Copy-Item -Force "$temp\gpl-2.0.pdf" "$OUTPUTDIR\gpl-2.0.pdf"

Write-Verbose "Compressing release binary distribution archive..."
CD $OUTPUTDIR
Write-Debug "Release binary archive name: $TargetOutputDir\WinFellow_v$FELLOWVERSION.zip"
Write-Verbose "Compressing release binary distribution archive..."
CD $OUTPUTDIR
Write-Debug "Release binary archive name: $TargetOutputDir\WinFellow_v$FELLOWVERSION.zip"

$result = (SevenZip a -tzip "$temp\WinFellow_v$FELLOWVERSION.zip" "*" -r)
Move-Item -Force "$temp\WinFellow_v$FELLOWVERSION.zip" "$TargetOutputDir\WinFellow_v$FELLOWVERSION.zip"
$result = (SevenZip a -tzip "$temp\WinFellow_v$FELLOWVERSION.zip" "*" -r)
Move-Item -Force "$temp\WinFellow_v$FELLOWVERSION.zip" "$TargetOutputDir\WinFellow_v$FELLOWVERSION.zip"

ShowProgressIndicator 9 "Generating NSIS Installer..."
ShowProgressIndicator 9 "Generating NSIS Installer..."

$NSISDIR = Resolve-Path ("$SourceCodeBaseDir\fellow\SRC\WIN32\NSIS")
Write-Debug "NSIS dir: $NSISDIR"
cd $temp
$result = (makensis.exe /DFELLOWVERSION=$FELLOWVERSION "$NSISDIR\WinFellow.nsi" > "WinFellow.log")
Move-Item -Force "WinFellow_v${FELLOWVERSION}.exe" $TargetOutputDir
Write-Debug "NSIS installer output name: $TargetOutputDir\WinFellow_v${FELLOWVERSION}.exe"
$NSISDIR = Resolve-Path ("$SourceCodeBaseDir\fellow\SRC\WIN32\NSIS")
Write-Debug "NSIS dir: $NSISDIR"
cd $temp
$result = (makensis.exe /DFELLOWVERSION=$FELLOWVERSION "$NSISDIR\WinFellow.nsi" > "WinFellow.log")
Move-Item -Force "WinFellow_v${FELLOWVERSION}.exe" $TargetOutputDir
Write-Debug "NSIS installer output name: $TargetOutputDir\WinFellow_v${FELLOWVERSION}.exe"

cd $SourceCodeBaseDir
cd $SourceCodeBaseDir

if($FELLOWBUILDPROFILE -eq "Release")
{
ShowProgressIndicator 10 "Cleaning up unwanted parts within Git working copy..."

$result = (git status --porcelain --ignored |
Select-String '^??' |
ForEach-Object {
[Regex]::Match($_.Line, '^[^\s]*\s+(.*)$').Groups[1].Value
} |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue)
}
if($FELLOWBUILDPROFILE -eq "Release")
{
ShowProgressIndicator 10 "Cleaning up unwanted parts within Git working copy..."
$result = (git status --porcelain --ignored |
Select-String '^??' |
ForEach-Object {
[Regex]::Match($_.Line, '^[^\s]*\s+(.*)$').Groups[1].Value
} |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue)
}

ShowProgressIndicator 11 "Compressing release source code archive..."
Write-Debug "Release source code archive output name: $TargetOutputDir\WinFellow_v${FELLOWVERSION}_src.zip"
ShowProgressIndicator 11 "Compressing release source code archive..."
Write-Debug "Release source code archive output name: $TargetOutputDir\WinFellow_v${FELLOWVERSION}_src.zip"

$result = (SevenZip a -tzip "$temp\WinFellow_v${FELLOWVERSION}_src.zip" "fellow")
$result = (SevenZip a -tzip "$temp\WinFellow_v${FELLOWVERSION}_src.zip" ".git")
cd $OUTPUTDIR
$result = (SevenZip a -tzip "$temp\WinFellow_v${FELLOWVERSION}_src.zip" "gpl-2.0.pdf")
Move-Item -Force "$temp\WinFellow_v${FELLOWVERSION}_src.zip" "$TargetOutputDir\WinFellow_v${FELLOWVERSION}_src.zip"
$result = (SevenZip a -tzip "$temp\WinFellow_v${FELLOWVERSION}_src.zip" "fellow")
$result = (SevenZip a -tzip "$temp\WinFellow_v${FELLOWVERSION}_src.zip" ".git")
cd $OUTPUTDIR
$result = (SevenZip a -tzip "$temp\WinFellow_v${FELLOWVERSION}_src.zip" "gpl-2.0.pdf")
Move-Item -Force "$temp\WinFellow_v${FELLOWVERSION}_src.zip" "$TargetOutputDir\WinFellow_v${FELLOWVERSION}_src.zip"

cd $SourceCodeBaseDir
Remove-Item "$OUTPUTDIR" -Recurse -Force
cd $SourceCodeBaseDir
Remove-Item "$OUTPUTDIR" -Recurse -Force

cd $TargetOutputDir
Invoke-Item .
cd $TargetOutputDir
Invoke-Item .

Pop-Location
Pop-Location
}

exit 0
Try
{
Main
}
Catch
{
$ErrorMessage = $_.Exception | Format-List -Force | Out-String
Write-Host "[ERROR] A terminating error was encountered. See error details below." -ForegroundColor Red
Write-Host "$($_.InvocationInfo.PositionMessage)" -ForegroundColor Red
Write-Host "$ErrorMessage" -ForegroundColor Red

Pop-Location
}

0 comments on commit 4f92e58

Please sign in to comment.