Last active
October 25, 2023 19:21
-
-
Save awakecoding/82cd7f2b0121ad15a3da98817b6a391c to your computer and use it in GitHub Desktop.
New-ModulePackage: create a PowerShell module nuget package without publishing it like Publish-Module does
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-ModulePackage | |
{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true,Position=0)] | |
[string] $InputPath, | |
[Parameter(Mandatory=$true,Position=1)] | |
[string] $OutputPath, | |
[string] $TempPath | |
) | |
$UniqueId = New-Guid | |
if ([string]::IsNullOrEmpty($TempPath)) { | |
$TempPath = [System.IO.Path]::GetTempPath() | |
} | |
$PSRepoName = "psrepo-$UniqueId" | |
$PSRepoPath = Join-Path $TempPath $UniqueId | |
if (-Not (Test-Path -Path $InputPath -PathType 'Container')) { | |
throw "`"$InputPath`" does not exist" | |
} | |
$PSModulePath = $InputPath | |
$PSManifestFile = $(@(Get-ChildItem -Path $PSModulePath -Depth 1 -Filter "*.psd1")[0]).FullName | |
$PSManifest = Import-PowerShellDataFile -Path $PSManifestFile | |
$PSModuleName = $(Get-Item $PSManifestFile).BaseName | |
$PSModuleVersion = $PSManifest.ModuleVersion | |
$PSModulePrerelease = $PSManifest.PrivateData.PSData.Prerelease | |
# https://docs.microsoft.com/en-us/nuget/concepts/package-versioning#normalized-version-numbers | |
$NugetVersion = $PSModuleVersion -Replace "^(\d+)\.(\d+)\.(\d+)(\.0)$", "`$1.`$2.`$3" | |
$NugetVersion = (@($NugetVersion, $PSModulePrerelease) | Where-Object { $_ }) -Join '-' | |
New-Item -Path $PSRepoPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null | |
$Params = @{ | |
Name = $PSRepoName; | |
SourceLocation = $PSRepoPath; | |
PublishLocation = $PSRepoPath; | |
InstallationPolicy = "Trusted"; | |
} | |
Register-PSRepository @Params | Out-Null | |
$OutputFileName = "${PSModuleName}.${NugetVersion}.nupkg" | |
$PSModulePackage = Join-Path $PSRepoPath $OutputFileName | |
Remove-Item -Path $PSModulePackage -ErrorAction 'SilentlyContinue' | |
Publish-Module -Path $PSModulePath -Repository $PSRepoName | |
Unregister-PSRepository -Name $PSRepoName | Out-Null | |
New-Item -Path $OutputPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null | |
$OutputFile = Join-Path $OutputPath $OutputFileName | |
Copy-Item $PSModulePackage $OutputFile | |
Remove-Item $PSmodulePackage | |
Remove-Item -Path $PSRepoPath | |
$OutputFile | |
} |
probably, it was only meant to replace Publish-Module to produce the .nupkg without publishing it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this will fail to package if you don't have the dependent packages available in your psmodulepath...