1
+ function New-PSMDModuleNugetPackage
2
+ {
3
+ <#
4
+ . SYNOPSIS
5
+ Creates a nuget package from a PowerShell module.
6
+
7
+ . DESCRIPTION
8
+ This function will take a module and wrap it into a nuget package.
9
+ This is accomplished by creating a temporary local filesystem repository and using the PowerShellGet module to do the actual writing.
10
+
11
+ Note:
12
+ - Requires PowerShellGet module
13
+ - Dependencies must be built first to the same folder
14
+
15
+ . PARAMETER ModulePath
16
+ Path to the PowerShell module you are creating a Nuget package from
17
+
18
+ . PARAMETER PackagePath
19
+ Path where the package file will be copied.
20
+
21
+ . PARAMETER EnableException
22
+ Replaces user friendly yellow warnings with bloody red exceptions of doom!
23
+ Use this if you want the function to throw terminating errors you want to catch.
24
+
25
+ . EXAMPLE
26
+ New-PSMDModuleNugetPackage -PackagePath 'c:\temp\package' -ModulePath .\DBOps
27
+
28
+ Packages the module stored in .\DBOps and stores the nuget file in 'c:\temp\package'
29
+
30
+ . NOTES
31
+ Author: Mark Wilkinson
32
+ Editor: Friedrich Weinmann
33
+ #>
34
+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseShouldProcessForStateChangingFunctions" , " " )]
35
+ [CmdletBinding ()]
36
+ param (
37
+ [Parameter (mandatory = $true , ValueFromPipeline = $true , ValueFromPipelineByPropertyName = $true )]
38
+ [Alias (' ModuleBase' )]
39
+ [string []]
40
+ $ModulePath ,
41
+
42
+ [string ]
43
+ $PackagePath = (Get-PSFConfigValue - FullName ' PSModuleDevelopment.Package.Path' - Fallback " $env: TEMP " ),
44
+
45
+ [switch ]
46
+ $EnableException
47
+ )
48
+
49
+ begin
50
+ {
51
+ Write-PSFMessage - Level InternalComment - Message " Bound Parameters: $ ( $PSBoundParameters.Keys -join " ," ) "
52
+
53
+ # region Input validation and prerequisites check
54
+ try
55
+ {
56
+ $null = Get-Command Publish-Module - ErrorAction Stop
57
+ $null = Get-Command Register-PSRepository - ErrorAction Stop
58
+ $null = Get-Command Unregister-PSRepository - ErrorAction Stop
59
+ }
60
+ catch
61
+ {
62
+ $paramStopPSFFunction = @ {
63
+ Message = " Failed to detect the PowerShellGet module! The module is required in order to execute this function."
64
+ EnableException = $EnableException
65
+ Category = ' NotInstalled'
66
+ ErrorRecord = $_
67
+ OverrideExceptionMessage = $true
68
+ Tag = ' fail' , ' validation' , ' prerequisites' , ' module'
69
+ }
70
+ Stop-PSFFunction @paramStopPSFFunction
71
+ return
72
+ }
73
+
74
+ if (-not (Test-Path $PackagePath ))
75
+ {
76
+ Write-PSFMessage - Level Verbose - Message " Creating path: $PackagePath " - Tag ' begin' , ' create' , ' path'
77
+ try { $null = New-Item - Path $PackagePath - ItemType Directory - Force - ErrorAction Stop }
78
+ catch
79
+ {
80
+ Stop-PSFFunction - Message " Failed to create output path: $PackagePath " - ErrorRecord $_ - EnableException $EnableException - Tag ' fail' , ' bgin' , ' create' , ' path'
81
+ return
82
+ }
83
+ }
84
+ # endregion Input validation and prerequisites check
85
+
86
+ # region Prepare local Repository
87
+ try
88
+ {
89
+ $paramRegisterPSRepository = @ {
90
+ Name = ' PSModuleDevelopment_TempLocalRepository'
91
+ PublishLocation = $PackagePath
92
+ SourceLocation = $PackagePath
93
+ InstallationPolicy = ' Trusted'
94
+ ErrorAction = ' Stop'
95
+ }
96
+
97
+ Register-PSRepository @paramRegisterPSRepository
98
+ }
99
+ catch
100
+ {
101
+ Stop-PSFFunction - Message " Failed to create temporary PowerShell Repository" - ErrorRecord $_ - EnableException $EnableException - Tag ' fail' , ' bgin' , ' create' , ' path'
102
+ return
103
+ }
104
+ # endregion Prepare local Repository
105
+ }
106
+ process
107
+ {
108
+ if (Test-PSFFunctionInterrupt ) { return }
109
+ # region Process Paths
110
+ foreach ($Path in $ModulePath )
111
+ {
112
+ Write-PSFMessage - Level VeryVerbose - Message " Starting to package: $Path " - Tag ' progress' , ' developer' - Target $Path
113
+
114
+ if (-not (Test-Path $Path ))
115
+ {
116
+ Stop-PSFFunction - Message " Path not found: $Path " - EnableException $EnableException - Category InvalidArgument - Tag ' progress' , ' developer' , ' fail' - Target $Path - Continue
117
+ }
118
+
119
+ try { Publish-Module - Path $Path - Repository ' PSModuleDevelopment_TempLocalRepository' - ErrorAction Stop - Force }
120
+ catch
121
+ {
122
+ Stop-PSFFunction - Message " Failed to publish module: $Path " - EnableException $EnableException - ErrorRecord $_ - Tag ' progress' , ' developer' , ' fail' - Target $Path - Continue
123
+ }
124
+
125
+ Write-PSFMessage - Level Verbose - Message " Finished processing: $Path " - Tag ' progress' , ' developer' - Target $Path
126
+ }
127
+ # endregion Process Paths
128
+ }
129
+ end
130
+ {
131
+ Unregister-PSRepository - Name ' PSModuleDevelopment_TempLocalRepository' - ErrorAction Ignore
132
+ if (Test-PSFFunctionInterrupt ) { return }
133
+ }
134
+ }
0 commit comments