Public/PublishLocalPackage.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
. $PSScriptRoot/../Private/PrepareLocalEnvironment.ps1
. $PSScriptRoot/../Private/ExtractCsprojMetaData.ps1
. $PSScriptRoot/../Private/CreatePackageToLocalFeed.ps1
. $PSScriptRoot/../Private/UpdateGlobalPackageCache.ps1

<#
    .SYNOPSIS
    Packs your C# project, publish it to your local NuGet feed and updates your global-package cache.
 
    .Parameter PackageProjectPath
    The path to your package project (default = current location).
 
    .PARAMETER LocalFeedName
    The name for your local NuGet feed (default = Local NuGet feed).
 
    .PARAMETER LocalFeedPath
    The path to your local NuGet directory (default = $HOME/localnugetfeed).
 
    .DESCRIPTION
    After applying changes to your .NET NuGet package using `Publish-LocalPackage` will let you consume these changes instantly in another project.
 
    Procedure:
    1. Creates a directory for the local NuGet path under <LocalFeedPath>.
    2. Adds the directory as a NuGet source with the name <LocalFeedName>.
    3. Packs the C# project of the <PackageProjectDirectory> directory and puts the output into the local NuGet source.
    4. Purges local global-package cache of a maybe existing package version.
    5. Updates the global-package cache with the new package.
 
    .EXAMPLE
    Publish-LocalPackage
 
    .EXAMPLE
    Publish-LocalPackage -PackageProjectPath C:\dev\my-package-project
 
    .EXAMPLE
    Publish-LocalPackage -LocalFeedName "Fancy feed name" -LocalFeedPath C:\somedir\my-local-nuget-feed#
 
    .LINK
    PS Gallery page: https://www.powershellgallery.com/packages/LocalNuGetHelper
#>

function Publish-LocalPackage {
    [CmdletBinding()]
    param (
        [Parameter()]
        [String]
        $PackageProjectPath = $(Get-Location),

        [Parameter()]
        [String]
        $LocalFeedName = "Local NuGet feed",

        [Parameter()]
        [String]
        $LocalFeedPath = $(Join-Path $HOME "localnugetfeed")
    )

    $dotnetVersion = dotnet --version
    if (($null -eq $dotnetVersion) -or ($LASTEXITCODE -ne 0)) {
        Write-Error "No dotnet CLI available." -ErrorAction Stop
    }

    if ($PackageProjectPath -eq "") {
        Write-Error "No package project path provided." -ErrorAction Stop
    }

    if ($LocalFeedName -eq "") {
        Write-Error "No local feed name provided." -ErrorAction Stop
    }

    if ($LocalFeedPath -eq "") {
        Write-Error "No local feed path provided." -ErrorAction Stop
    }

    PrepareLocalEnvironment

    ($packageName, $packageVersion) = ExtractCsprojMetaData

    CreatePackageToLocalFeed

    UpdateGlobalPackageCache $packageName $packageVersion

    Write-Host "✓ Done" -ForegroundColor Green
    return
}