Instantly share code, notes, and snippets.

Embed
What would you like to do?
Details of the functions used in the PatchAssemblyInfo PSake task
function GetRevisionNumber {
$major = "1"
$minor = "0"
#we use the environment variable BUILD_NUMBER provided by teamcity, use -1 is not found to test locally the process
$patch= if ([string]::IsNullOrEmpty($env:BUILD_NUMBER)) { "-1" } else { $env:BUILD_NUMBER }
$major + "." + $minor + "." + $patch
}
function PatchFile ([string] $pattern, [string] $replaceString, [string] $fullPath){
if(!(Test-Path $fullPath)) { #always test-path for build scripts
throw "cannot find the path at " + $fullPath
}
$matchCount = 0
$regex = New-Object -TypeName "System.Text.RegularExpressions.Regex" -ArgumentList $pattern
(Get-Content $fullPath -Encoding UTF8) | Foreach-Object {
if($regex.IsMatch($_)){
$matchCount = $matchCount + 1
}
$regex.Replace($_,$replaceString)
} | Set-Content $fullPath -Encoding UTF8
if($matchCount -eq 0){ #check that some lines has been patched by the function
throw "no line found for patching " + $fullPath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment