PowerShell/tools/Xml/Xml.psm1
Bram Crielaard 7031954669 Make install scripts more consistent over different operating systems (#9071)
I noticed a couple of inconsistencies when reading through the install bash scripts. 

- Make documentation for switches consistent over all files.
- Replace all `sed` implementations of `lowercase` with a more maintainable `tr` implementation.
- Set the `OS` variable in every install script, making it so previously unused checks are actually used.
- Exit with a non-zero exit code when the script reaches an illegal state.


## PR Context

A lot of people, including myself, read the install scripts before executing them. While doing so I noticed they contained inconsistencies. For example, certain flags you can pass to the install script were either undocumented or had an incorrect description. This PR fixes some of these inconsistencies, which should make them easier to maintain and easier to read.

Co-authored-by: Travis Plunk <github@ez13.net>
2019-03-08 13:00:04 -08:00

102 lines
2.2 KiB
PowerShell

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Adds an attribute to a XmlElement
function New-XmlAttribute
{
param(
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[object]$Value,
[Parameter(Mandatory)]
[System.Xml.XmlElement]$Element,
[Parameter(Mandatory)]
[System.Xml.XmlDocument]$XmlDoc
)
$attribute = $XmlDoc.CreateAttribute($Name)
$attribute.Value = $value
$null = $Element.Attributes.Append($attribute)
}
# Adds an XmlElement to an XmlNode
# Returns the new Element
function New-XmlElement
{
param(
[Parameter(Mandatory)]
[string]$LocalName,
[Parameter(Mandatory)]
[System.Xml.XmlDocument]$XmlDoc,
[Parameter(Mandatory)]
[System.Xml.XmlNode]$Node,
[Switch]$PassThru,
[string]$NamespaceUri
)
if($NamespaceUri)
{
$newElement = $XmlDoc.CreateElement($LocalName, $NamespaceUri)
}
else
{
$newElement = $XmlDoc.CreateElement($LocalName)
}
$null = $Node.AppendChild($newElement)
if($PassThru.IsPresent)
{
return $newElement
}
}
# Removes an XmlElement and its parent if it is empty
function Remove-XmlElement
{
param(
[Parameter(Mandatory)]
[System.Xml.XmlElement]$Element,
[Switch]$RemoveEmptyParents
)
$parentNode = $Element.ParentNode
$null = $parentNode.RemoveChild($Element)
if(!$parentNode.HasChildNodes -and $RemoveEmptyParent.IsPresent)
{
Remove-XmlElement -Element $parentNode -RemoveEmptyParents
}
}
# Get a node by XPath
# Returns null if the node is not found
function Get-XmlNodeByXPath
{
param(
[Parameter(Mandatory)]
[System.Xml.XmlDocument]
$XmlDoc,
[System.Xml.XmlNamespaceManager]
$XmlNsManager,
[Parameter(Mandatory)]
[string]
$XPath
)
if($XmlNsManager)
{
return $XmlDoc.SelectSingleNode($XPath,$XmlNsManager)
}
else
{
return $XmlDoc.SelectSingleNode($XPath)
}
}
Export-ModuleMember -Function @(
'Get-XmlNodeByXPath'
'Remove-XmlElement'
'New-XmlElement'
'New-XmlAttribute'
)