PoshCode Archive  Artifact [080bba070a]

Artifact 080bba070ad2968b5ff91df52b2ca64c4ce12f3ddd13c42808046be25c0b48f2:

  • File PackageManagementHelper.ps1 — part of check-in [6886848554] at 2018-06-10 14:11:07 on branch trunk — Publish a module to a local folder to get the .nupkg (user: Joel Bennett size: 1786)

# encoding: ascii
# api: powershell
# title: PackageManagementHelper
# description: Publish a module to a local folder to get the .nupkg
# version: 0.1
# type: module
# author: Joel Bennett
# license: CC0
# function: Package-Module
# x-poshcode-id: 6234
# x-archived: 2016-05-05T01:46:23
# x-published: 2016-02-25T02:46:00
#
#
#requires -module PackageManagement

function Package-Module {
    #.Synopsis
    #   Generate a .nupkg file from the specified module
    [CmdletBinding(DefaultParameterSetName="Name")]
    param(
        [Parameter(Mandatory,ParameterSetName="Name")]
        [string]$Name, 

        [Parameter(Mandatory,ParameterSetName="Path")]
        [string]$Path, 

        [string]$OutputPath=$Pwd,

        [Switch]$Force
    )

    do {
        $Repository = [GUID]::NewGuid().Guid
        $Location = Join-Path ([IO.Path]::GetTempPath()) $Repository
    } while(Test-Path $Location)
    $null = mkdir $Location -ErrorAction Stop

    Register-PSRepository -Name $Repository -Source $Location -Publish $Location -ErrorAction Stop
    if($PSCmdlet.ParameterSetName -eq "Name") {
        Publish-Module -Name $Name -Repository $Repository -NuGetApiKey (Get-Random) -ErrorAction Stop
    } else {
        Publish-Module -Path $Path -Repository $Repository -NuGetApiKey (Get-Random) -ErrorAction Stop
    }

    Unregister-PSRepository -Name $Repository

    Get-ChildItem $Location | Move-Item -Destination $OutputPath -Passthru -Force:$Force -ErrorAction SilentlyContinue -ErrorVariable MoveError

    if($MoveError) {
        Remove-Item $Location -recurse
        throw "Cannot create a file when that file already exists. Specify -Force to overwrite $OutputPath"
    }

    Remove-Item $Location -recurse
}