Update-BinVer.ps1 (3342B)
1 <# 2 .SYNOPSIS 3 Updates the given version header file to match the version info parse from git. 4 .DESCRIPTION 5 Uses "git describe --tags" to determine the current version, then updates the given file 6 to match. See the existing version.h for the format. 7 .PARAMETER TargetFile 8 The file to update with version information. If not given, simply outputs the version info. 9 .PARAMETER BuildVersion 10 The build version to use. 11 .PARAMETER MajorMinorOverride 12 The major and minor version in the format of Major.Minor to use. 13 Or skip to skip the major and minor version override. 14 .PARAMETER OutVar 15 Output a pipeline variable with the version. 16 #> 17 param( 18 [Parameter(Mandatory=$false)] 19 [string]$TargetFile, 20 21 [Parameter(Mandatory=$false)] 22 [int]$BuildVersion = 0, 23 24 [Parameter(Mandatory=$false)] 25 [string]$MajorMinorOverride, 26 27 [switch]$OutVar 28 ) 29 30 $Local:Major = 0; 31 $Local:Minor = 0; 32 $Local:SkipMajorMinorOverride = $false; 33 34 if ($MajorMinorOverride -match "([0-9]+)\.([0-9]+)") 35 { 36 $Local:Major = $Matches[1] 37 $Local:Minor = $Matches[2] 38 } 39 elseif ($MajorMinorOverride -eq "skip") 40 { 41 $Local:SkipMajorMinorOverride = $true; 42 43 Write-Host "Major minor version override skipped" 44 } 45 else 46 { 47 $ErrorActionPreference = 'SilentlyContinue' 48 $Local:GitDescribeText = git describe --tags; 49 $ErrorActionPreference = 'Continue' 50 51 Write-Host "Git describe: $Local:GitDescribeText" 52 53 if ($Local:GitDescribeText -match "v([0-9]+)\.([0-9]+)") 54 { 55 $Local:Major = $Matches[1] 56 $Local:Minor = $Matches[2] 57 } 58 else 59 { 60 Write-Host "Describe did not match regex and major/minor weren't explicitly provided; using zeros" 61 } 62 } 63 64 if ($Local:SkipMajorMinorOverride) 65 { 66 Write-Host "Using build version only: $BuildVersion" 67 } 68 else 69 { 70 Write-Host "Using version: $Local:Major.$Local:Minor.$BuildVersion" 71 } 72 73 if ($OutVar) 74 { 75 Write-Host "##vso[task.setvariable variable=tag;isOutput=true]$Local:Major.$Local:Minor" 76 } 77 78 if (![String]::IsNullOrEmpty($TargetFile)) 79 { 80 $Local:FullPath = Resolve-Path $TargetFile 81 Write-Host "Updating file: $Local:FullPath" 82 if (Test-Path $TargetFile) 83 { 84 $Local:ResultContent = "" 85 foreach ($Local:line in [System.IO.File]::ReadLines($Local:FullPath)) 86 { 87 if (-Not($Local:SkipMajorMinorOverride) -And $Local:line.StartsWith("#define VERSION_MAJOR")) 88 { 89 $Local:ResultContent += "#define VERSION_MAJOR $Local:Major"; 90 } 91 elseif (-Not($Local:SkipMajorMinorOverride) -And $Local:line.StartsWith("#define VERSION_MINOR")) 92 { 93 $Local:ResultContent += "#define VERSION_MINOR $Local:Minor"; 94 } 95 elseif ($Local:line.StartsWith("#define VERSION_BUILD")) 96 { 97 $Local:ResultContent += "#define VERSION_BUILD $BuildVersion"; 98 } 99 else 100 { 101 $Local:ResultContent += $Local:line; 102 } 103 $Local:ResultContent += [System.Environment]::NewLine; 104 } 105 Set-Content -Path $Local:FullPath -Value $Local:ResultContent 106 } 107 else 108 { 109 Write-Error "Did not find target file: $TargetFile" 110 } 111 } 112 113 exit 0