Skip to content

Instantly share code, notes, and snippets.

@bpatra
Created July 8, 2014 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpatra/1ae1aac8c9b9508844ab to your computer and use it in GitHub Desktop.
Save bpatra/1ae1aac8c9b9508844ab to your computer and use it in GitHub Desktop.
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