Skip to content

Instantly share code, notes, and snippets.

@Alezis
Last active January 23, 2017 12:05
Show Gist options
  • Save Alezis/6e5cf5b167c5caf42feac6032af0462b to your computer and use it in GitHub Desktop.
Save Alezis/6e5cf5b167c5caf42feac6032af0462b to your computer and use it in GitHub Desktop.
Inject into version ChangesetID like ( 2.0.0.5556), where changesetID = "C5556". Script processes AssemblyInfo.cs and project.json...and can be improved.
function Update-VersionInFiles
{
Param
(
[Parameter(Mandatory=$false)]
[string]$SourceDir
)
Write-Host "Starting update versions of source files..."
####################################################################################
# Arrange variables
if ([string]::IsNullOrWhiteSpace($SourceDir))
{
$SourceDir = $env:BUILD_SOURCESDIRECTORY
if ([string]::IsNullOrWhiteSpace($SourceDir))
{
Write-Error-Vso "BUILD_SOURCESDIRECTORY variable is not specified"
Set-Failed-Task
return
}
}
if(-Not $(Test-Path $SourceDir))
{
Write-Error-Vso "Source directory is not reachable '" + $SourceDir + "'"
Set-Failed-Task
return;
}
if([string]::IsNullOrWhiteSpace($env:BUILD_SOURCEVERSION))
{
Write-Error-Vso "BUILD_SOURCEVERSION variable is not specified"
Set-Failed-Task
return;
}
####################################################################################
# Execute
$CommitId = ([string]$env:BUILD_SOURCEVERSION) -replace "[^0-9]+", ""
Write-Host "Executing Update-AssemblyInfoVersionFiles in path '$SourceDir'" -Verbose
<# Update assembly version in AssemblyInfo.cs #>
$AllVersionFiles = Get-ChildItem $SourceDir AssemblyInfo.cs -recurse
$regexToFindVersion = "Version\(""([0-9]+)\.([0-9]+).+"""
foreach ($file in $AllVersionFiles)
{
Write-Host "Processing " $file.FullName
(Get-Content $file.FullName) |
%{$_ -replace $regexToFindVersion, ('Version("$1.$2.0.' + $CommitId + '"') } |
Set-Content $file.FullName -Force
}
<# Update assembly version in project.json #>
$AllVersionFiles = Get-ChildItem $SourceDir project.json -recurse
$regexToFindVersion = """version""\s*\:\s*""([0-9]+)\.([0-9]+).+"""
foreach ($file in $AllVersionFiles)
{
Write-Host "Processing " $file.FullName
(Get-Content $file.FullName) |
%{$_ -replace $regexToFindVersion, ('"version":"$1.$2.0.' + $CommitId + '"') } |
Set-Content $file.FullName -Force
}
Write-Host "Completed " -ForegroundColor green
Set-Completed-Task
return
}
function Write-Warning-Vso
{
Param
(
[Parameter(Mandatory=$true)]
[string]$message
)
if (-Not $([string]::IsNullOrWhiteSpace($message)))
{
Write-Warning $message
Write-Host "##vso[task.logissue type=warning;] " + $message
}
}
function Write-Error-Vso
{
Param
(
[Parameter(Mandatory=$true)]
[string]$message
)
if (-Not $([string]::IsNullOrWhiteSpace($message)))
{
Write-Error $message
Write-Host "##vso[task.logissue type=error;] " + $message
}
}
function Set-Failed-Task
{
Write-Host "##vso[task.complete result=Failed;]Failed"
}
function Set-Completed-Task
{
Write-Host "##vso[task.complete result=Succeeded;]Succeeded"
}
##################### Executing script
Update-VersionInFiles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment