Public/Set-NuspecRepository.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function Set-NuspecRepository
{
    <#
        .SYNOPSIS
            Sets the repository information in a Nuspec manifest.
        .DESCRIPTION
            Sets the repository information in a Nuspec manifest.
        .PARAMETER Uri
            An URI containing the URI of repository.
        .PARAMETER Type
            A string containing the type of repository.
        .Parameter Branch
            A string containing the branch.
        .PARAMETER commit
            A string containing the full commit SHA1.
        .PARAMETER Force
            A switch sepecifying whether or not to override repository if it already exists.
        .INPUTS
            System.Object
            Accepts an object containing the Name and Value as an input from the pipeline.
        .OUTPUTS
            System.Xml.XmlDocument
            Returns an XmlDocument containing the manifest.
        .NOTES
        .LINK
            Set-NuspecProperty
        .LINK
            https://docs.microsoft.com/en-us/nuget/reference/nuspec#repository
    #>

    [CmdLetBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("Url")]
        [uri] $Uri,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string] $Type,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string] $Branch,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [ValidatePattern("^[a-f0-9]{40}$")]
        [string] $Commit,
        [Parameter(Mandatory = $true)]
        [xml] $Nuspec,
        [Parameter(Mandatory = $false)]
        [switch] $Force
    )

    try
    {
        $NameSpace = New-Object System.Xml.XmlNamespaceManager($Nuspec.NameTable)
        $NameSpace.AddNamespace("ns", $Nuspec.DocumentElement.xmlns)
        
        $Repository = $nuspec.SelectSingleNode("//ns:repository", $NameSpace)
        if (!$Repository -or $Force)
        {
            if ($Repository)
            {
                Write-Verbose "Removing existing repository node."
                $Nuspec.package.metadata.RemoveChild($Repository) | Out-Null
            }
            
            $Repository = $Nuspec.CreateElement("repository", $Nuspec.package.xmlns)

            $Repository.SetAttribute("url", $Uri.AbsoluteUri)
            if ($Type) { $Repository.SetAttribute("type", $Type.ToLower()) }
            if ($Branch) { $Repository.SetAttribute("branch", $Branch) }
            if ($Commit) { $Repository.SetAttribute("commit", $Commit) }
            $Nuspec.GetElementsByTagName("metadata").AppendChild($Repository) | Out-Null
        }
        else { Write-Warning "Repository already present ($($Repository.url)), use Force switch to override." }
        $Nuspec
    }
    catch
    {
        Write-Error $_
    }
}