Skip to content

Instantly share code, notes, and snippets.

@awakecoding
Last active October 25, 2023 19:21
Show Gist options
  • Save awakecoding/82cd7f2b0121ad15a3da98817b6a391c to your computer and use it in GitHub Desktop.
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
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
}
@JustinGrote
Copy link

I think this will fail to package if you don't have the dependent packages available in your psmodulepath...

@awakecoding
Copy link
Author

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