Get-VersionInfo.psm1
# Get AssemblyInfo.cs VersionInfo function Get-Csharp-Project-AssemblyInfo([string] $projectFile){ try{ #vars $majorVersion = "1" $minorVersion = "0" $buildNumber = "0" #validate assemblyFileName $dir = Split-Path $projectFile -Parent $assemblyFileName = Join-Path -Path $dir -ChildPath "Properties" | Join-Path -ChildPath "Assemblyinfo.cs" # if( !(Test-Path $assemblyFileName -PathType Leaf ) ) { $msg = "Assembly info is missing [$assemblyFileName]." Write-Warning $msg throw $msg } # find version info $versionPattern='^\[assembly: AssemblyVersion\(\s*"(?<major>[1-9]+[0-9]*)\.(?<minor>[0-9]+)\.(?<build>[0-9]+)(\.[0-9]+)?"\s*\)\]' (Get-Content $assemblyFileName) | Foreach-Object { if($_ -match $versionPattern) { $majorVersion=$matches["major"]; $minorVersion=$matches["minor"]; $buildNumber=$matches["build"] } } return "$majorVersion.$minorVersion.$buildNumber" } catch{ Write-Warning "Cann't set assembly info [$projectFile]." return $null } } # Get c# project VersionInfo function Get-Csharp-Project-VersionInfo([string] $projectFile, $version){ try{ #vars $majorVersion = "1" $minorVersion = "0" $buildNumber = "0" # find version info $versionPattern='<Version>((?<major>[1-9]+[0-9]*)\.(?<minor>[0-9]+)\.(?<build>[0-9]+)(\.[0-9]+)?)</Version>' (Get-Content $projectFile) | Foreach-Object { if($_ -match $versionPattern) { $majorVersion=$matches["major"]; $minorVersion=$matches["minor"]; $buildNumber=$matches["build"] } } return "$majorVersion.$minorVersion.$buildNumber" } catch{ Write-Warning "Cann't set version info [$projectFile]." return $null } } # Process c# proj function Get-Csharp-Project([string] $projectFile){ # $textVersion = $null # try{ [xml] $project = Get-Content $projectFile if ($project.Project.Sdk.Length -eq 0){ Write-Verbose "Using AssemblyInfo.cs [$(Split-Path $projectFile -Leaf)]" $textVersion = Get-Csharp-Project-AssemblyInfo $projectFile } else{ $textVersion = Get-Csharp-Project-VersionInfo $projectFile } if ( $null -ne $textVersion){ return @{ Project=[System.IO.Path]::GetFileNameWithoutExtension($projectFile); Version = $textVersion } } } catch{ Write-Warning "Not valid project file [$projectFile]." return $null } } # Process sln file function Get-VisualStudio-Solution([string] $slnFile){ try{ # get all projects $projects = Get-Content $slnFile | Where-Object { $_ -match "^Project" } | ForEach-Object { $_ -match ".*=(.*)$" | out-null ; $matches[1] } | ForEach-Object { $_.Split(",")[1].Trim().Trim('"') } | Where-Object { $_ -match ".*\.csproj$" } # $dir = Split-Path $slnFile -Parent $version = @{} #process foreach($p in $projects){ Write-Verbose "Processing project: $p" $prj = Join-Path -Path $dir -ChildPath $p $tmp = Get-Csharp-Project $prj if($null -ne $tmp){ foreach($k in $tmp){ if(-not $version.ContainsKey($k.Project)){ $version.Add($k.Project, $k.Version) } } } } return $version } catch{ throw "Not valid solution file [$slnFile]." } } <# .Synopsis Automatic Version Tool (AVT) .DESCRIPTION Get VersionInfo .EXAMPLE Get-VersionInfo D:\Projects\project.csproj .EXAMPLE Get-VersionInfo D:\Projects\project.sln #> function Get-VersionInfo { [CmdletBinding()] [Alias()] [OutputType([hashtable])] Param ( # Project/Solution file (csproj,sln) [string] [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$false, HelpMessage="Project file.")] $File ) Begin { if( !(Test-Path $File -PathType Leaf ) ) { throw "File is missing [$File]." } } Process { if ($File -like '*.csproj') { $tmp = Get-Csharp-Project $File return @{ $tmp.Project=$tmp.Version } } elseif ($File -like '*.sln') { return Get-VisualStudio-Solution $File } else{ throw "Not supported [$File]." } } End { } } Export-ModuleMember -Function Get-VersionInfo # SIG # Begin signature block # MIIIEwYJKoZIhvcNAQcCoIIIBDCCCAACAQExDzANBglghkgBZQMEAgEFADB5Bgor # BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG # KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCD6MVrwVyLR5Kgr # aw0pjDHNUTijOK2rUhWb3nC29RL4HKCCBQ8wggULMIIC86ADAgECAgIApT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