Update-VersionInfo.psm1
# GIT Revision function Get-GitRepository-Version([string] $file){ try { $dir = Split-Path $file -Parent $rev = git -C "$dir" rev-list --all --count 2> $null $hash = git -C "$dir" rev-parse --short HEAD 2> $null if ([string]::IsNullOrEmpty($rev) -or [string]::IsNullOrEmpty($hash)) { Write-Warning "Not valid repository, or GIT command was not found" return $null } else{ return new-object psobject -property @{ Revision = $rev; Hash = $hash} } } catch { Write-Warning "Not valid repository, or GIT command was not found" } return $null; } # Set/Reset AssemblyInfo.cs function Set-Csharp-Project-AssemblyInfo([string] $projectFile, $version, [bool] $useRevision){ try{ #vars $majorVersion = "1" $minorVersion = "0" $buildNumber = "0" #version if($null -eq $version){ $revisionNumber = "0" $revisionHash = "0" } else{ $revisionNumber = $version.Revision $revisionHash = $version.Hash } #useRevision if($null -eq $useRevision){ $useRevision = $true } #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]+)\.(?<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"] } } if($useRevision){ Write-Verbose "Version number was determined as $majorVersion.$minorVersion.$buildNumber.$revisionNumber" $versionReplace= "[assembly: AssemblyVersion(`"$majorVersion.$minorVersion.$buildNumber.$revisionNumber`")]" } else{ Write-Verbose "Version number was used as $majorVersion.$minorVersion.$buildNumber.0" $versionReplace= "[assembly: AssemblyVersion(`"$majorVersion.$minorVersion.$buildNumber.0`")]" } # $versionInfoPattern='^\[assembly: AssemblyInformationalVersion\(\s*"(?<major>[1-9]+)\.(?<minor>[0-9]+)\.(?<build>[0-9]+)(\.[a-z0-9]+)?"\s*\)\]' (Get-Content $assemblyFileName) | Foreach-Object { if($_ -match $versionPattern) { $majorVersion=$matches["major"]; $minorVersion=$matches["minor"]; $buildNumber=$matches["build"] } } $versionInfoReplace = "[assembly: AssemblyInformationalVersion(`"$majorVersion.$minorVersion.$buildNumber.$revisionHash`")]" # replace version info (Get-Content $assemblyFileName) | ForEach-Object { $_ -replace $versionPattern,$versionReplace} | ForEach-Object { $_ -replace $versionInfoPattern,$versionInfoReplace} | Set-Content -Path $assemblyFileName -Force # report $file = Split-Path $projectFile -Leaf if($null -eq $version){ Write-Host "#### AVT RESET $majorVersion.$minorVersion.$buildNumber.$revisionNumber [$file] ####" } else{ if($useRevision){ Write-Host "#### AVT SET $majorVersion.$minorVersion.$buildNumber.$revisionNumber for $revisionHash [$file] ####" } else{ Write-Host "#### AVT SET $majorVersion.$minorVersion.$buildNumber.0 for $revisionHash [$file] ####" } } return "$majorVersion.$minorVersion.$buildNumber" } catch{ Write-Warning "Cann't set assembly info [$projectFile]." return $null } } # Set/Reset c# project VersionInfo function Set-Csharp-Project-VersionInfo([string] $projectFile, $version, [bool] $useRevision){ try{ #vars $majorVersion = "1" $minorVersion = "0" $buildNumber = "0" #version if($null -eq $version){ $revisionNumber = "0" $revisionHash = "0" } else{ $revisionNumber = $version.Revision $revisionHash = $version.Hash } #useRevision if($null -eq $useRevision){ $useRevision = $true } # find version info $versionPattern='<Version>((?<major>[1-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"] } } if($useRevision){ Write-Verbose "Version number was determined as $majorVersion.$minorVersion.$buildNumber.$revisionNumber" $versionReplace= "<Version>$majorVersion.$minorVersion.$buildNumber.$revisionNumber</Version>" } else{ Write-Verbose "Version number was used as $majorVersion.$minorVersion.$buildNumber.0" $versionReplace= "<Version>$majorVersion.$minorVersion.$buildNumber.0</Version>" } # $versionInfoPattern='<InformationalVersion>((?<major>[1-9]+)\.(?<minor>[0-9]+)\.(?<build>[0-9]+)(\.[a-z0-9]+)?)</InformationalVersion>' (Get-Content $projectFile) | Foreach-Object { if($_ -match $versionPattern) { $majorVersion=$matches["major"]; $minorVersion=$matches["minor"]; $buildNumber=$matches["build"] } } $versionInfoReplace = "<InformationalVersion>$majorVersion.$minorVersion.$buildNumber.$revisionHash</InformationalVersion>" # replace version info (Get-Content $projectFile) | ForEach-Object { $_ -replace $versionPattern,$versionReplace} | ForEach-Object { $_ -replace $versionInfoPattern,$versionInfoReplace} | Set-Content -Path $projectFile -Force # report $file = Split-Path $projectFile -Leaf if($null -eq $version){ Write-Host "#### AVT RESET $majorVersion.$minorVersion.$buildNumber.$revisionNumber [$file] ####" } else{ if($useRevision){ Write-Host "#### AVT SET $majorVersion.$minorVersion.$buildNumber.$revisionNumber for $revisionHash [$file] ####" } else{ Write-Host "#### AVT SET $majorVersion.$minorVersion.$buildNumber.0 for $revisionHash [$file] (no-revision) ####" } } return "$majorVersion.$minorVersion.$buildNumber" } catch{ Write-Warning "Cann't set version info [$projectFile]." return $null } } # Set/Reset nuspec VersionInfo function Set-Nuspec-VersionInfo([string] $nugetFile, [string] $textversion){ try{ if( ![string]::IsNullOrEmpty($textversion) ){ Write-Verbose "Update nuspec to $textversion [$(Split-Path -Leaf $nugetFile)]" $versionPattern='<version>(([1-9]+)\.([0-9]+)\.([0-9]+)([\.-][-0-9a-z]+)?)</version>' $versionReplace= "<version>$textversion</version>" $content = (Get-Content $nugetFile) | ForEach-Object { $_ -replace $versionPattern,$versionReplace} Set-Content -Path $nugetFile -Value $content -Force } } catch{ Write-Warning "Cann't set nuspec version info [$nugetFile]." } } # process c# proj function Set-Csharp-Project([string] $projectFile, $version){ # $textVersion = $null # $nugetFile = $projectFile.Replace(".csproj",".nuspec") if(Test-Path $nugetFile -PathType Leaf){ $hasNugetFile = $true } else{ $hasNugetFile = $false } $useRevision = -not $hasNugetFile # try{ [xml] $project = Get-Content $projectFile if ($project.Project.Sdk.Length -eq 0){ Write-Verbose "Using AssemblyInfo.cs [$(Split-Path $projectFile -Leaf)]" $textVersion = Set-Csharp-Project-AssemblyInfo $projectFile $version $useRevision } else{ $textVersion = Set-Csharp-Project-VersionInfo $projectFile $version $useRevision } if ($hasNugetFile -and !($null -eq $version)){ Set-Nuspec-VersionInfo $nugetFile $textVersion } } catch{ Write-Warning "Not valid project file [$projectFile]." } } # process sln file function Set-VisualStudio-Solution([string] $slnFile, $version){ 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 #process foreach($p in $projects){ Write-Verbose "Processing project: $p" $prj = Join-Path -Path $dir -ChildPath $p Set-Csharp-Project $prj $version } } catch{ Write-Warning "Not valid solution file [$slnFile]." } } <# .Synopsis Automatic Version Tool (AVT) .DESCRIPTION Update VersionInfo from GIT .EXAMPLE Update-VersionInfo set D:\Projects\project.csproj .EXAMPLE Update-VersionInfo reset D:\Projects\project.csproj .EXAMPLE Update-VersionInfo set D:\Projects\project.sln .EXAMPLE Update-VersionInfo reset D:\Projects\project.sln #> function Update-VersionInfo { [CmdletBinding()] [Alias()] [OutputType([void])] Param ( # Mode set or reset [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$false, HelpMessage="Command type.")] [ValidateSet('set','reset')] [string] $Mode, # 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 { switch ($Mode){ 'set' { # $version = Get-GitRepository-Version $File # if ($File -like '*.csproj') { Set-Csharp-Project $File $version } elseif ($File -like '*.sln') { Set-VisualStudio-Solution $File $version } else{ throw "Not supported [$File]." } } Default { if ($File -like '*.csproj') { Set-Csharp-Project $File } elseif ($File -like '*.sln') { Set-VisualStudio-Solution $File } else{ throw "Not supported [$File]." } } } } End { } } Export-ModuleMember -Function Update-VersionInfo # SIG # Begin signature block # MIIH7gYJKoZIhvcNAQcCoIIH3zCCB9sCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUF1JGpWnqYSPKVo6+EkDcaQva # dpigggUPMIIFCzCCAvOgAwIBAgICAKUwDQYJKoZIhvcNAQELBQAwgaAxCzAJBgNV # BAYTAlNLMREwDwYDVQQIEwhTbG92YWtpYTETMBEGA1UEBxMKQnJhdGlzbGF2YTET # MBEGA1UEChMKTG9tdGVjLmNvbTErMCkGA1UEAxMiTG9tdGVjLmNvbSBDZXJ0aWZp # Y2F0aW9uIEF1dGhvcml0eTEnMCUGCSqGSIb3DQEJARYYSW1yaWNoLlN6b2xpa0Bs # b210ZWMuY29tMB4XDTE5MDMyNjE1MDQ0M1oXDTMwMDEwMTAwMDAwMFowgZMxCzAJ # BgNVBAYTAlNLMREwDwYDVQQIEwhTbG92YWtpYTETMBEGA1UEBxMKQnJhdGlzbGF2 # YTETMBEGA1UEChMKTG9tdGVjLmNvbTEeMBwGA1UEAxMVTG9tdGVjLmNvbSBQb3dl # cnNoZWxsMScwJQYJKoZIhvcNAQkBFhhJbXJpY2guU3pvbGlrQGxvbXRlYy5jb20w # ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC+J+Jwh25Ha39PSBzRUNj/ # 8+iONxiknLBSTkB1DJEQ9wC6FBS/1EQvbFtn2K6huVZBQCoZzNFcXoiAked3ALIT # UoYxX+fD4Wkve947QqCXGroCFQVaBSLkPoJzWCadlXQWyDyjFu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ATCBpzCBoDELMAkGA1UEBhMCU0sxETAPBgNVBAgTCFNs # b3Zha2lhMRMwEQYDVQQHEwpCcmF0aXNsYXZhMRMwEQYDVQQKEwpMb210ZWMuY29t # MSswKQYDVQQDEyJMb210ZWMuY29tIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MScw # JQYJKoZIhvcNAQkBFhhJbXJpY2guU3pvbGlrQGxvbXRlYy5jb20CAgClMAkGBSsO # AwIaBQCgeDAYBgorBgEEAYI3AgEMMQowCKACgAChAoAAMBkGCSqGSIb3DQEJAzEM # BgorBgEEAYI3AgEEMBwGCisGAQQBgjcCAQsxDjAMBgorBgEEAYI3AgEWMCMGCSqG # SIb3DQEJBDEWBBT3UDC2/6ft4fhXOrdL371fuvuzoDANBgkqhkiG9w0BAQEFAASC # AQBquTtJVTtB8/5duL0doeqs5JJ/gDH+RUzyRnkosNziAF3GKyE6tnVtVtCY1CI7 # Za8IVgSnNdbu+Fldko43b8qwSnqu4sEt2rIutndzr4pcCbIHC11bfHbb4hmwyCdt # /vO4FHGmomizl1yVwZDcPonTbv3yQoTfb3OCntBvAyf39hU1QXn5gbvC16C16DBM # Fc0Zq4BvmKfW97aUtzo1RVmNFvoPJf9BGVZZabyFGdlIcO2yBSQ5yznGylYzg1ut # kgzMPTQ80xbIjysYIN0h9vlU2Ouav8HjBY72O+GmURaTpQ1+l7xt8n3LZwYpb1cp # iZ9gUS35fjKgFcxDs6mVIabS # SIG # End signature block |