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 # KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCDJDqZkv1U3HOMx # dDLkof5Wa8Lc3j48E0SL+X36erJ+VqCCBQ8wggULMIIC86ADAgECAgIApT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