Created
July 22, 2014 20:33
-
-
Save nnieslan/c631add56c5f7f3e7d6e to your computer and use it in GitHub Desktop.
Example Dynamic PowerShell MSBuild invocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Displays a formated string message with color-coding and borders | |
.PARAMETER message | |
The message to display | |
#> | |
function write-header | |
{ | |
param | |
( | |
[string]$message | |
) | |
if($Host.Name -eq "ConsoleHost") | |
{ | |
Write-Host -ForegroundColor Yellow "---------------------------------------------------------------------" | |
Write-Host -ForegroundColor White "" | |
Write-Host -ForegroundColor Green $message | |
Write-Host -ForegroundColor White "" | |
Write-Host -ForegroundColor Yellow "---------------------------------------------------------------------" | |
} | |
} | |
<# | |
.SYNOPSIS | |
Displays the message passed in to the host or log if possible. | |
.PARAMETER message | |
The message to display | |
#> | |
function write-message | |
{ | |
param | |
( | |
[string]$message, | |
[string]$color = "Green" | |
) | |
if($Host.Name -eq "ConsoleHost") | |
{ | |
Write-Host -ForegroundColor $color $message | |
} | |
} | |
<# | |
.SYNOPSIS | |
Executes the MSBUILD project file passed in with the | |
targets and properties defined. | |
.PARAMETER project | |
The project file to process | |
.PARAMETER targets | |
The Target to execute on the project | |
.PARAMETER config | |
The configuration to assign as a property | |
.PARAMETER bindir | |
The bin location to place output under in configuration subfolder | |
.PARAMETER toolsVersion | |
The .NET tools version to use when building | |
.PARAMETER consoleLogLevel | |
The MSBuild log level to use | |
.PARAMETER forceCodeAnalyis | |
Switch allowing enforcement of Code Analysis | |
.PARAMETER skipCodeAnalyis | |
Switch allowing forcible omission of Code Analysis | |
#> | |
function build-project | |
{ | |
param | |
( | |
[string] $project = $(throw 'A MSBuild project file is required'), | |
[string] $targets = $(throw 'A target(s) such as Clean or Rebuild is required'), | |
[string] $config = $(throw 'A configuration to build is required'), | |
[string] $bindir = $(throw 'A output bin directory for the build is required.'), | |
[string] $toolsVersion = $('4.0'), | |
[string] $consoleLogLevel = 'normal', | |
[switch] $skipCodeAnalysis, | |
[switch] $buildReferences, | |
[switch] $binplaceIntoConfig | |
) | |
$outdir = $bindir | |
if($binplaceIntoConfig) { $outdir = Join-Path -path $outdir -childpath "$config\\" } | |
#from here down, we are concatenating a large set of switches to pass MSBuild. | |
$fileloglevel = $consoleLogLevel; | |
if($fileloglevel -eq 'minimal'){ $fileloglevel = 'normal'} | |
$console_logging_switch = " /clp:Summary"";""Verbosity=$consoleLogLevel" | |
$file_logging_switch = " /flp:Summary"";""Append"";""LogFile=build.$config.log"";""verbosity=$fileloglevel" | |
$file_errlog_switch = " /flp1:Append"";""LogFile=build.$config.err"";""errorsonly" | |
$file_wrnlog_switch = " /flp2:Append"";""LogFile=build.$config.wrn"";""warningsonly" | |
$logging_switch = $console_logging_switch + $file_logging_switch + $file_errlog_switch + $file_wrnlog_switch | |
$targets_switch = " /t:$targets" | |
$outdir_switch = " /p:OutDir=""$outdir""" | |
$config_switch = " /p:Configuration=""$config""" | |
#optionally set some flags based on incoming switches. | |
if($skipCodeAnalysis){ $config_switch = " $config_switch /p:RunCodeAnalysis=false " } | |
if(-not $buildReferences){ $config_switch = " $config_switch /p:BuildProjectReferences=false " } | |
#write out the built switches for debugging purposes. | |
write-header -message " Logging parameters: `r`n $logging_switch" | |
write-header -message " Targets parameters: `r`n $targets_switch" | |
write-header -message " OutDir parameters: `r`n $outdir_switch" | |
write-header -message " Configuration parameters: `r`n $config_switch" | |
$options = $outdir_switch + $config_switch + $logging_switch + $targets_switch | |
write-header -message " MSBuild command: `r`n $options" | |
write-header -message " Building $project ..."; | |
#build final command and execute it. | |
$cmd = "msbuild $project $options" | |
Invoke-Expression $cmd | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment