dev.core.nuget.psm1

Import-Module "$PSScriptRoot\dev.core.utils.psm1" -DisableNameChecking;
Import-Module "$PSScriptRoot\dev.core.http.psm1" -DisableNameChecking;

$NugetAppFolder = "$Home\AppData\Local\DevPowerShell\dev.core.nudge";
$NugetExe = "${NugetAppFolder}\nuget.exe";
$DefaultPackageFolder = "$Home\.nuget\packages";


#
# Private functions
#
function Download-NugetExe
{
    # Download latest nuget.exe if not present
    if (!(Test-Path $script:NugetExe))
    {
        $NugetExeDownloadUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe";
        Write-Verbose "Downloading nuget.exe from $NugetExeDownloadUrl...";
        Invoke-WebRequest -Uri $NugetExeDownloadUrl -OutFile $script:NugetExe;
    }
}

function CreateDirIfNotExists([string] $Directory)
{
    if (-not (Get-Item $Directory -ErrorAction SilentlyContinue))
    {
        Write-Verbose "Creating directory `"$Directory`""
        mkdir $Directory | out-null
    }
}


#
# Public functions
#

function Upgrade-Nuget
{
    [CmdletBinding()]
    param();

    # Delete the nuget.exe and download call initialize to download latest version
    Delete-Item $script:NugetExe;

    Download-NugetExe;
}

function Get-NugetPackage
{
    [CmdletBinding()]
    param([string]$Name, [string]$Version, [string]$Source);
    
    $arguments = @("list");

    if ($Name)
    {
        $arguments += $Name;
    }

    if ($Version)
    {
        $arguments += $Version;
    }

    if ($Source)
    {
        $arguments += "-Source";
        $arguments += $Source;
    }

    Write-Host "${script:NugetExe} $(${arguments} -join ' ')";
    $lines = & $script:NugetExe $arguments;

    $NameVersionRegex = [regex]"(?<n>[\w\.\d]+) (?<v>\d+(.\d+)*)";

    foreach ($line in $lines)
    {
        $match = $NameVersionRegex.Match($line);
        if ($match.Success)
        {
            $package = [PSCustomObject]@{
                Name = $match.Groups["n"].Value;
                Version = $match.Groups["v"].Value;
            };

            Write-Output $package;
        }
        else
        {
            Write-Host $line;
        }
    }    
}

function Install-NugetPackage
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)][string]$Name, 
        [Parameter(Mandatory=$true)][string]$Version, 
        [Parameter(Mandatory=$true)][string]$Directory,
        [string]$Framework,
        [ValidateSet("Lowest","HighestPatch", "HighestMinor", "Highest", "Ignore")][string]$DependencyVersion,
        [ValidateSet("nuspec","nupkg")][string[]]$PackageSaveMode,
        [string]$Source, 
        [switch]$Force);

    $PackageFolder += "$Directory\$Name.$Version";
    
    if ((Test-Path $PackageFolder -PathType Container) -and (!$Force))
    {
        Write-Verbose "The nuget package $PackageFolder is already installed, skip installation.";
        return;
    }
    
    $arguments = @("install");

    $arguments += $Name;
    
    $arguments += "-Version";
    $arguments += $Version;
    
    if ($Source)
    {
        $arguments += "-Source";
        $arguments += $Source;
    }

    if ($Framework)
    {
        $arguments += "-Framework";
        $arguments += $Framework;
    }

    if ($DependencyVersion)
    {
        $arguments += "-DependencyVersion";
        $arguments += $DependencyVersion;
    }

    if ($PackageSaveMode)
    {
        $arguments += "-PackageSaveMode";
        $arguments += ($PackageSaveMode -join ';');
    }

    $arguments += "-OutputDirectory";
    $arguments += $Directory;

    $arguments += "-NonInteractive";

    Write-Host "${script:NugetExe} $(${arguments} -join ' ')";
    $lines = & $script:NugetExe $arguments;
    foreach($line in $lines)
    {
        Write-Host $line;
    }
}


#
# Module Initialization
#

New-DirectoryIfNotExist -Path $NugetAppFolder | Out-Null;

Download-NugetExe;


Export-ModuleMember Upgrade-Nuget;
Export-ModuleMember Get-NugetPackage;
Export-ModuleMember Install-NugetPackage;