add-type-Noveris.Version.psm1

<#
#>


$ErrorActionPreference = "Stop"

Set-StrictMode -Version 2.0

Add-Type -EA SilentlyContinue @'
    public class VersionStore
    {
        public int Major;
        public int Minor;
        public int Patch;
        public int Build;
 
        public VersionStore()
        {
            this.Major = 0;
            this.Minor = 0;
            this.Patch = 0;
            this.Build = 0;
        }
 
        public VersionStore(VersionStore store)
        {
            this.Major = store.Major;
            this.Minor = store.Minor;
            this.Patch = store.Patch;
            this.Build = store.Build;
        }
 
        public string SemVer {
            get {
                return string.Format("{0}.{1}.{2}", this.Major, this.Minor, this.Patch);
            }
        }
 
        public string FullVer {
            get {
                return string.Format("{0}.{1}.{2}.{3}", this.Major, this.Minor, this.Patch, this.Build);
            }
        }
 
        public override string ToString()
        {
            return string.Format("{0}.{1}.{2}", this.Major, this.Minor, this.Patch);
        }
    }
'@


Function ConvertTo-VersionStore
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline)]
        [AllowEmptyString()]
        [string]$VersionSource,

        [Parameter(Mandatory=$false)]
        [switch]$Strict = $false,

        [Parameter(Mandatory=$false)]
        [switch]$AddMissingBuildNumber = $false,

        [Parameter(Mandatory=$false)]
        [switch]$ForceBuildGeneration = $false
    )

    process
    {
        $MinDate = New-Object DateTime -ArgumentList 1970, 1, 1
        $source = $VersionSource

        # Ignore empty version sources (or fail, if strict)
        if ([string]::IsNullOrEmpty($source))
        {
            if ($Strict)
            {
                throw New-Object ArgumentException -ArgumentList "Version supplied (${source}) is not formatted correctly"
            }

            return
        }

        # Strip any refs/tags/ reference at the beginning of the version source
        $tagBranch = "refs/tags/"
        if ($source.StartsWith($tagBranch))
        {
            $source = $source.Substring($tagBranch.Length)
        }

        $version = New-Object VersionStore
        
        # Check for three number version format (Major.Minor.Patch)
        if ($source -match "^[v]*([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$")
        {
            $major = [Convert]::ToInt64($Matches[1])
            $minor = [Convert]::ToInt64($Matches[2])
            $patch = [Convert]::ToInt64($Matches[3])
            $build = [Convert]::ToInt64($Matches[4])

            $version.Major = $major
            $version.Minor = $minor
            $version.Patch = $patch
            $version.Build = $build

            if ($ForceBuildGeneration)
            {
                $version.Build = [Int64]([DateTime]::Now - $MinDate).TotalDays
            }

            $version
            return
        }
        # Check for four number version format (Major.Minor.Patch.Build)
        elseif ($source -match "^[v]*([0-9]+)\.([0-9]+)\.([0-9]+)$")
        {
            $major = [Convert]::ToInt64($Matches[1])
            $minor = [Convert]::ToInt64($Matches[2])
            $patch = [Convert]::ToInt64($Matches[3])

            $version.Major = $major
            $version.Minor = $minor
            $version.Patch = $patch

            $version.Build = 0
            if ($AddMissingBuildNumber -or $ForceBuildGeneration)
            {
                $version.Build = [Int64]([DateTime]::Now - $MinDate).TotalDays
            }

            $version
            return
        }

        # Couldn't identify a usable version format
        if ($Strict)
        {
            throw New-Object ArgumentException -ArgumentList "Version supplied (${source}) is not formatted correctly"
        }
    }
}