functions/Set/Set-ProjVersion.ps1
function Set-ProjVersion { param( [Parameter(Mandatory = $true)] [string]$ProjectDirectory, [Parameter(Mandatory = $true, ParameterSetName = 'FullVersion')] [Version]$FullVersion, [Parameter(Mandatory = $true, ParameterSetName = 'Revision')] [int]$Revision, [Parameter(Mandatory = $false)] [bool]$SetFileVersion = $true, [Parameter(Mandatory = $false)] [bool]$SetAssemblyVersion = $true, [Parameter(Mandatory = $false)] [bool]$SetPackageVersion = $true, [Parameter(Mandatory = $false)] [string[]]$Exclude ) $projectFiles = Get-ChildItem $ProjectDirectory -Filter '*.csproj' | Where-Object { $_.Name -notin $Exclude } foreach ($projectFile in $projectFiles) { $xml = New-Object System.Xml.XmlDocument $xml.Load($projectFile.FullName) # Check if project is .Net Core if ($null -ne $xml.Project.Attributes['Sdk']) { # .NET Core switch ($PsCmdlet.ParameterSetName) { 'FullVersion' { $newVersion = Convert-VersionToString $FullVersion if ($SetAssemblyVersion) { Write-Host "Setting AssemblyVersion $newVersion for $($projectFile.FullName)" $assemblyVersionNode = $xml.SelectSingleNode('//Project/PropertyGroup/AssemblyVersion') if ($null -eq $assemblyVersionNode) { $child = $xml.CreateElement("AssemblyVersion") $xml.Project.PropertyGroup.AppendChild($child) | Out-Null } foreach($propGroup in $xml.Project.PropertyGroup){ $propGroup.AssemblyVersion = $newVersion break } } if ($SetFileVersion) { Write-Host "Setting FileVersion $newVersion for $($projectFile.FullName)" $fileVersionNode = $xml.SelectSingleNode('//Project/PropertyGroup/FileVersion') if ($null -eq $fileVersionNode) { $child = $xml.CreateElement("FileVersion") $xml.Project.PropertyGroup.AppendChild($child) | Out-Null } foreach($propGroup in $xml.Project.PropertyGroup){ $propGroup.FileVersion = $newVersion break } } if ($SetPackageVersion) { Write-Host "Setting Version $newVersion for $($projectFile.FullName)" $versionNode = $xml.SelectSingleNode('//Project/PropertyGroup/Version') if ($null -eq $versionNode) { $child = $xml.CreateElement("Version") $xml.Project.PropertyGroup.AppendChild($child) | Out-Null } foreach($propGroup in $xml.Project.PropertyGroup){ $propGroup.Version = $newVersion break } } } 'Revision' { if ($SetAssemblyVersion) { $asmVersion = [Version]$xml.Project.PropertyGroup.AssemblyVersion $newVersion = Get-NewVersion $asmVersion $Revision Write-Host "Setting AssemblyVersion $newVersion for $($projectFile.FullName)" foreach($propGroup in $xml.Project.PropertyGroup){ $propGroup.AssemblyVersion = $newVersion break } } if ($SetFileVersion) { $fileVersion = [Version]$xml.Project.PropertyGroup.FileVersion $newVersion = Get-NewVersion $fileVersion $Revision Write-Host "Setting FileVersion $newVersion for $($projectFile.FullName)" foreach($propGroup in $xml.Project.PropertyGroup){ $propGroup.FileVersion = $newVersion break } } if ($SetPackageVersion) { $version = [Version]$xml.Project.PropertyGroup.Version $newVersion = Get-NewVersion $version $Revision Write-Host "Setting Version $newVersion for $($projectFile.FullName)" foreach($propGroup in $xml.Project.PropertyGroup){ $propGroup.Version = $newVersion break } } } } } else { # .NET Framework switch ($PsCmdlet.ParameterSetName) { 'FullVersion' { if ($SetAssemblyVersion) { if ($xml.Project.PropertyGroup[0].ApplicationVersion) { Write-Host "Setting AssemblyVersion $FullVersion for $($projectFile.FullName)" $xml.Project.PropertyGroup[0].ApplicationVersion = (Convert-VersionToString $FullVersion) } Set-AssemblyInfoVersion -FilePath "$ProjectDirectory\Properties\AssemblyInfo.cs" -AttributeName 'AssemblyVersion' -FullVersion $FullVersion } if ($SetFileVersion) { Set-AssemblyInfoVersion -FilePath "$ProjectDirectory\Properties\AssemblyInfo.cs" -AttributeName 'AssemblyFileVersion' -FullVersion $FullVersion } } 'Revision' { if ($SetAssemblyVersion) { $appVersion = [Version]$xml.Project.PropertyGroup[0].ApplicationVersion if ($appVersion) { $newVersion = Get-NewVersion $appVersion $Revision Write-Host "Setting AssemblyVersion $newVersion for $($projectFile.FullName)" $xml.Project.PropertyGroup[0].ApplicationVersion = $newVersion } $asmVersion = Get-AssemblyInfoVersion -FilePath "$ProjectDirectory\Properties\AssemblyInfo.cs" -AttributeName 'AssemblyVersion' if ($asmVersion) { Set-AssemblyInfoVersion -FilePath "$ProjectDirectory\Properties\AssemblyInfo.cs" -AttributeName 'AssemblyVersion' -FullVersion (Get-NewVersion $asmVersion $Revision) } } if ($SetFileVersion) { $fileVersion = Get-AssemblyInfoVersion -FilePath "$ProjectDirectory\Properties\AssemblyInfo.cs" -AttributeName 'AssemblyFileVersion' if ($fileVersion) { Set-AssemblyInfoVersion -FilePath "$ProjectDirectory\Properties\AssemblyInfo.cs" -AttributeName 'AssemblyFileVersion' -FullVersion (Get-NewVersion $fileVersion $Revision) } } } } } $xml.Save($projectFile.FullName) Write-Host "$($projectFile.FullName) successfully saved" } } |