Public/Publish-PSModule.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<#
.SYNOPSIS
Function to publish modules to a PowerShell Repository.
 
.DESCRIPTION
Function to publish PowerShell Modules to a PowerShell Repository, NuGet and SMB
supported.
 
.PARAMETER RepositoryName
Name of the PowerShell repository.
 
.PARAMETER RepositoryPath
Path to the PowerShell repository (if not PSGallery). Format: \\path\to\repo
 
.PARAMETER ApiKey
NuGet API key to the repository/feed.
 
.PARAMETER ModuleName
Name of the module to publish.
 
.PARAMETER ModulePath
File path to the modules manifest file (.psd1).
 
.PARAMETER BuildNumber
The build number of the module (minor version).
 
.PARAMETER Properties
Hashtable containing additional properties:
 
    ReleaseNotes = <String>
    Tags = <Array>
    LicenseUri = <String>
    IconUri = <String>
    ProjectUri = <String>
 
.NOTES
General notes
#>

function Publish-PSModule {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [string]
        $RepositoryName,
        [Parameter(Mandatory = $false)]
        [string]
        $RepositoryPath,
        [Parameter(Mandatory = $false)]
        [string]
        $ApiKey,
        [Parameter(Mandatory = $true)]
        [string]
        $ModuleName,
        [Parameter(Mandatory = $true)]
        [string]
        $ModulePath,
        [Parameter(Mandatory = $true)]
        [int]
        $BuildNumber,
        [Parameter(Mandatory = $false)]
        [AllowNull()]
        [hashtable]
        $Properties
    )

    process {
        # Default Official PSGallery.
        $psGalleryName = 'PSGallery'

        # Test if the repository is registered.
        Write-Verbose ("Checking if Repository: {0} is registered." -f $RepositoryName)
        if (!(Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue)) {
            # Check if the Network Path exists.
            if(!(Test-Path $RepositoryPath)) {
                throw "The path does not exist. Please connect to the share."
            } else {
                # Register the Repository.
                if ($RepositoryName -ne $psGalleryName) {
                    Write-Verbose("Registering Repository: {0}." -f $RepositoryName)
                    
                    $registerParams = @{
                        Name = $RepositoryName
                        SourceLocation = $RepositoryPath
                        PublishLocation = $RepositoryPath
                        InstallationPolicy = "Trusted"
                    }

                    Register-PSRepository @registerParams 
                }
            }
        }

        # Update existing manifest.
        Write-Verbose("Checking if Module: {0} is registered." -f $ModuleName)
        if (Find-Module -Repository $RepositoryName -Name $ModuleName -ErrorAction SilentlyContinue) {
            Write-Verbose ("Updating Manifest for: {0}." -f $ModuleName)
            $version = (Get-Module -FullyQualifiedName $ModulePath -ListAvailable).Version | Select-Object Major, Minor
            $newVersion = New-Object Version -ArgumentList $version.major, $version.minor, $BuildNumber
            Update-ModuleManifest -Path $ModulePath -ModuleVersion $newVersion
        }

        # Publish Module.
        Write-Verbose ("Publishing Module: {0}." -f $ModuleName)

        $publishParams = @{
            Path = (Merge-Path ".", $ModuleName)
        }
        # Determine type of publish.
        if ($RepositoryName -eq $psGalleryName) {
            if ([string]::IsNullOrEmpty($ApiKey)) {
                throw("Please pass on a NuGet API key to deploy to the PSGallery.")
            } else {
                $publishParams.Add("NuGetApiKey", $ApiKey)
            }
        } else {
            $publishParams.Add("Repository", $RepositoryName)
        }

        # Adding additional parameters to the publishing.
        if ($Properties) {
            if ($Properties["ReleaseNotes"]) { 
                $publishParams.Add("ReleaseNotes", $Properties["ReleaseNotes"]) 
            }

            if ($Properties["Tags"]) {
                
                if ($Properties["Tags"].GetType().Name -eq "String") {
                    $Properties["Tags"] = $Properties["Tags"].Split(',')
                }
                
                $publishParams.Add("Tags", $Properties["Tags"]) 
            }

            if ($Properties["LicenseUri"]) {
                $publishParams.Add("LicenseUri", $Properties["LicenseUri"])
            }

            if ($Properties["IconUri"]) {
                $publishParams.Add("IconUri", $Properties["IconUri"])
            }

            if ($Properties["ProjectUri"]) {
                $publishParams.Add("ProjectUri", $Properties["ProjectUri"])
            }
        }

        try {
            Publish-Module @publishParams
        } catch [System.Exception] {
            throw($_.Exception)
        }
    }
}