param($NuSpecFile = $(throw "Mandatory parameter -NuSpecFile not supplied"), $MajorVersion, $MinorVersion, $RevisionVersion) function AssignVersionValue([string]$oldValue, [string]$newValue) { if ($newValue -eq $null -or $newValue -eq "") { $oldValue } else { $newValue } } function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "") { # If a Namespace URI was not given, use the Xml document's default namespace. if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI } # In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up. [System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable) $xmlNsManager.AddNamespace("ns", $NamespaceURI) return ,$xmlNsManager # Need to put the comma before the variable name so that PowerShell doesn't convert it into an Object[]. } function Get-FullyQualifiedXmlNodePath([string]$NodePath, [string]$NodeSeparatorCharacter = '.') { return "/ns:$($NodePath.Replace($($NodeSeparatorCharacter), '/ns:'))" } function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.') { $xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter # Try and get the node, then return it. Returns $null if the node was not found. $node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager) return $node } function Get-XmlNodes([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.') { $xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI [string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter # Try and get the nodes, then return them. Returns $null if no nodes were found. $nodes = $XmlDocument.SelectNodes($fullyQualifiedNodePath, $xmlNsManager) return $nodes } function Get-XmlElementsTextValue([xml]$XmlDocument, [string]$ElementPath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.') { # Try and get the node. $node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter # If the node already exists, return its value, otherwise return null. if ($node) { return $node.InnerText } else { return $null } } function Set-XmlElementsTextValue([xml]$XmlDocument, [string]$ElementPath, [string]$TextValue, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.') { # Try and get the node. $node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter # If the node already exists, update its value. if ($node) { $node.InnerText = $TextValue } # Else the node doesn't exist yet, so create it with the given value. else { # Create the new element with the given value. $elementName = $ElementPath.Substring($ElementPath.LastIndexOf($NodeSeparatorCharacter) + 1) $element = $XmlDocument.CreateElement($elementName, $XmlDocument.DocumentElement.NamespaceURI) $textNode = $XmlDocument.CreateTextNode($TextValue) $element.AppendChild($textNode) > $null # Try and get the parent node. $parentNodePath = $ElementPath.Substring(0, $ElementPath.LastIndexOf($NodeSeparatorCharacter)) $parentNode = Get-XmlNode -XmlDocument $XmlDocument -NodePath $parentNodePath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter if ($parentNode) { $parentNode.AppendChild($element) > $null } else { throw "$parentNodePath does not exist in the xml." } } } $NuSpecFile = Resolve-Path $NuSpecFile [ xml ]$fileContents = Get-Content -Path $NuSpecFile $originalVersion = Get-XmlElementsTextValue -XmlDocument $fileContents -ElementPath "package.metadata.version" $newVersion = "1.0.0" if ($originalVersion -ne $null) { $segments=$originalVersion.Split(".") $v1="1" $v2="0" $v3="0" if ($segments.Length -gt 0) { $v1=$segments[0] } if ($segments.Length -gt 1) { $v2=$segments[1] } if ($segments.Length -eq 3) { $v3=$segments[2] } $v1 = AssignVersionValue $v1 $MajorVersion $v2 = AssignVersionValue $v2 $MinorVersion $v3 = AssignVersionValue $v3 $RevisionVersion if ($v1 -eq $null) { $v1 = 1 } if ($v2 -eq $null) { $v2 = 0 } if ($v3 -eq $null) { $v3 = 0 } $newVersion = "$v1.$v2" #Write-Host "REV: $RevisionVersion" if ($v3 -ne $null -and ($RevisionVersion -eq $null -or $RevisionVersion -eq "")) { $v3 = ($v3 -as [int]) + 1 } $newVersion = "$newVersion.$v3" } write-host "Setting Version to $newVersion" Set-XmlElementsTextValue -XmlDocument $fileContents -ElementPath "package.metadata.version" -TextValue $newVersion $fileContents.Save($NuSpecFile)