Lib/helper_nuget.psm1

<#
    .SYNOPSIS
        Install Nuget
 
    .DESCRIPTION
        Install Nuget. This will install a stand-alone version of nuget.exe
 
    .NOTES
        Author : hillesheim@n-dimensions.de
        Version : 1.1
        DateLastChanged : 2023-11-28
#>
 

Param ( 
    [Switch] $Auto 
)

Write-Host "Load nested module 'helper_nuget' ... "; 

#region begin DECLARATION
    
    #region begin TYPES
    #endregion

    #region begin USER_VARIABLES
    #endregion

    #region begin SYSTEM_VARIABLES

    [String] $psScriptFilePath = $MyInvocation.MyCommand.Definition; 
    [String] $psScriptFolderPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent; 
    [String] $defaultUrlArtifactsCredproviderPsScript = "https://aka.ms/install-artifacts-credprovider.ps1"; 
    
    #endregion

#endregion

#region FUNCTIONS_PUBLIC


Function Install-Nuget {

    Write-Host ("Install nuget ... ") -BackgroundColor Green; 
    
    Initialize-NugetClient `
        -NugetSettings $Global:nugetSettings; 

    Get-NugetFromWeb `
        -NugetSettings $Global:nugetSettings; 

    Add-NugetEnvPath `
        -NugetSettings $Global:nugetSettings; 

}

#endregion

#region FUNCTIONS_PRIVATE

Function Initialize-NugetClient {
    Param(
        [NugetSettings] $NugetSettings 
    )

    Write-Host ( "Initialize nuget client environment ... " );    

    [String[]] $folders = @(
        $NugetSettings.NugetProgramFolder, 
        $NugetSettings.NugetPackagesInstallFolder
    )

    $folders `
        | ForEach-Object {
            Write-Host ("- Check for folder '{0}' ... " -f $_)
            If ( -not ( Test-Path $_ ) ) {                
                New-Item -Path $_ -ItemType Directory; 
            }
            Else {
                Write-Host " No action required"; 
            }
        }

}
Function Add-NugetEnvPath {
    Param ( 
        [NugetSettings] $NugetSettings
    )

    [String] $newPath = $NugetSettings.NugetProgramFolder; 

    Write-Host ( "Update env:path variable. Add '{0}'" -f $newPath ); 

    # Check if new path already exists
    [String[]] $pathItems = [Environment]::GetEnvironmentVariable("Path", "Machine").Split(";"); 
    If ( $pathItems.Contains($newPath) ) {
        Write-Host ( "The environment variable already contains the new path. Abort." ); 
        Return; 
    } Else { # Add path
        [String] $currentPathValue = [Environment]::GetEnvironmentVariable("Path", "Machine"); 
        If ( -not ( $currentPathValue.EndsWith(";") ) ) {
            $currentPathValue += ";"; 
        }
        [String] $newPathValue = $currentPathValue + $newPath + ";"; 
        [Environment]::SetEnvironmentVariable("Path", $newPathValue, "Machine"); 
    }

}

Function Get-NugetFromWeb { 
    Param ( 
        [NugetSettings] $NugetSettings
    ) 

    Write-Host ( "Going to download nuget.exe from '{0}'" ); 
    Write-Host ( "Save nuget.exe to nuget program files folder" -f $NugetSettings.NugetExeDownloadUrl, $NugetSettings.NugetProgramFolder ); 

    # Action
    Invoke-WebRequest `
        -Uri $NugetSettings.NugetExeDownloadUrl `
        -OutFile ([System.IO.Path]::Combine($NugetSettings.NugetProgramFolder, "nuget.exe")); 

    # Return Nuget.Config file paths
    Write-Host ( "'Nuget.Config' file path 1 (User) = '{0}'" -f "%appdata%\NuGet" ); 
    Write-Host ( "'Nuget.Config' file path 2 (System) = '{0}'" -f "%ProgramFiles(x86)%\NuGet\Config" ); 

}

#endregion

#region FUNCTIONS_QUEUED

# Function Add-NugetPackageSource {
# Param (
# [NugetSettings] $NugetSettings,
# [String] $NugetProgramFolder = $folders.NugetProgramFolder
# )
#
# Write-Host ( "Add nuget package source '{0}'" -f $NugetSettings.NuGetSourceName );
#
# cd $NugetProgramFolder;
#
# # Check for existing source
# [String[]] $s = `
# .\nuget.exe source `
# | Where-Object { $_ -like ( "*" + $NugetSettings.NuGetSourceName + "*" ) };
# If ( $s.Count -gt 0 ) {
# "Package source already exists. Skip.";
# Return
# }
#
# # Action
# Write-Host ( "- Nuget source properties: " );
# Write-Host ( " - Name: '{0}'" -f $NugetSettings.NuGetSourceName );
# Write-Host ( " - Source: '{0}'" -f $NugetSettings.AzArtifactFeedUrl );
# Write-Host ( " - UserName: '{0}'" -f $NugetSettings.AzArtifactUserName );
# Write-Host ( " - Password / PersonalAccessToken: '{0}'" -f $NugetSettings.AzApiKey ); # $NugetSettings.AzArtifactUserPassword );
#
# .\nuget.exe sources add `
# -Name $NugetSettings.NuGetSourceName `
# -Source $NugetSettings.AzArtifactFeedUrl `
# -UserName $NugetSettings.AzArtifactUserName `
# -Password $NugetSettings.AzApiKey;
# # $NugetSettings.AzArtifactUserPassword;
# }


#endregion

#region EXPORT

Export-ModuleMember -Function 'Install-Nuget'; 

#endregion