Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set VersionPrefix/Suffix if normal Version/AssemblyVersion not found #5

Merged
merged 1 commit into from
Apr 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 34 additions & 35 deletions VersionReaderTask/VersionReaderTask.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,65 @@ Write-Host ("Search Pattern: " + $searchPattern)
Write-Host ("Variables Prefix: " + $variablesPrefix)
Write-Host ("Build Prefix: " + $buildPrefix)

function SetBuildVariable([string]$varName, [string]$varValue)
{
function SetBuildVariable([string]$varName, [string]$varValue) {
$varName = $variablesPrefix + $varName
$versionBuild = $varValue + $buildPrefix + $Env:BUILD_BUILDID
Write-Host ("Setting variable " + $varName + " = '" + $varValue + "'")
Write-Host ("Setting variable " + $varName + "_build = '" + $versionBuild + "'")
Write-Output ("##vso[task.setvariable variable=" + $varName + ";]" + $varValue )
Write-Output ("##vso[task.setvariable variable=" + $varName + "_Build;]" + $versionBuild )
Write-Host ("Setting variable " + $varName + " = '" + $varValue + "'")
Write-Host ("Setting variable " + $varName + "_build = '" + $versionBuild + "'")
Write-Output ("##vso[task.setvariable variable=" + $varName + ";]" + $varValue )
Write-Output ("##vso[task.setvariable variable=" + $varName + "_Build;]" + $versionBuild )
}

function SetVersionVariables([xml]$xml)
{
function SetVersionVariables([xml]$xml) {
[string]$version = ([string]$xml.Project.PropertyGroup.Version).Trim()
if ($version -eq "")
{
Write-Host ("No Version property value found, checking AssemblyVersion instead")
$version = ([string]$xml.Project.PropertyGroup.AssemblyVersion).Trim()
if ($version -eq "")
{
# FAIL
Throw "No usable version found"
}
if ($version -ne "") {
Write-Host ("Version property value found with version " + $version)
SetBuildVariable "Version" $version
return
}

# set env var
SetBuildVariable "Version" $version

# check for VersionPrefix
[string]$versionPrefix = ([string]$xml.Project.PropertyGroup.VersionPrefix).Trim()
if ($versionPrefix -eq "") {
Write-Host ("No VersionPrefix value found");
}
else {
SetBuildVariable "VersionPrefix" $versionPrefix
[string]$assemblyVersion = ([string]$xml.Project.PropertyGroup.AssemblyVersion).Trim()
if ($assemblyVersion -ne "") {
Write-Host ("AssemblyVersion property value found with version " + $assemblyVersion)
SetBuildVariable "Version" $assemblyVersion
return
}

Write-Host ("No Version or AssemblyVersion property value found");

# check for VersionSuffix
[string]$versionSuffix = ([string]$xml.Project.PropertyGroup.VersionSuffix).Trim()
if ($versionSuffix -eq "") {
Write-Host ("No VersionSuffix value found");
Write-Host ("No VersionSuffix property value found");
}
else {
SetBuildVariable "VersionPrefix" $versionSuffix
}

# check for VersionPrefix
[string]$versionPrefix = ([string]$xml.Project.PropertyGroup.VersionPrefix).Trim()
if ($versionPrefix -eq "") {
Write-Host ("No VersionPrefix property value found");

# Missing all version sources is considered a failure of the task
throw "No Version sources found in project files"
}
else {
SetBuildVariable "VersionPrefix" $versionPrefix
}
}

$filesFound = Get-ChildItem -Path $searchPattern -Recurse

if ($filesFound.Count -eq 0)
{
if ($filesFound.Count -eq 0) {
Write-Warning ("No files matching pattern found.")
}

if ($filesFound.Count -gt 1)
{
Write-Warning ("Multiple assemblyinfo files found.")
if ($filesFound.Count -gt 1) {
Write-Warning ("Multiple assemblyinfo files found.")
}

foreach ($fileFound in $filesFound)
{
foreach ($fileFound in $filesFound) {
Write-Host ("Reading file: " + $fileFound)
[xml]$XmlDocument = Get-Content -Path $fileFound
SetVersionVariables($XmlDocument)
Expand Down