Universal.psm1

[UniversalAutomation.AssemblyResolver]::Bind()
function Resolve-Variable {
    [CmdletBinding()]
    param($Name)

    $PSCmdlet.GetVariableValue($Name)
}

function Start-PSUServer {
    [CmdletBinding()]
    param(
        [Parameter()]
        [string]$ExecutablePath = (Join-Path $PWD "Universal.Server.exe"),
        [Parameter()]
        [string]$ListenAddress,
        [Parameter()]
        [int]$Port, 
        [Parameter()]
        [ScriptBlock]$Configuration
    )

    if ([UniversalAutomation.RemoteCommand]::Configuration)
    {
        & $Configuration
        return
    }

    $Command = Get-Command $ExecutablePath -ErrorAction SilentlyContinue
    if ($null -eq $Command)
    {
        $ExecutablePath = Join-Path $PSScriptRoot "Universal.Server.exe"
        $Command = Get-Command $ExecutablePath -ErrorAction SilentlyContinue
        if ($null -eq $Command)
        {
            throw 'Unable to locate the Universal Server executable. You can use Install-PSUServer the server for your platform. Use the -AddToPath parameter to add the installation directory the the PATH.'
        }
    }

    if ($ListenAddress)
    {
        $Env:Kestrel__Endpoints__HTTP__Url = $ListenAddress
    }
    elseif ($PSBoundParameters.ContainsKey("Port"))
    {
        $Env:Kestrel__Endpoints__HTTP__Url = "http://localhost:$port"
    }

    if ($Configuration)
    {
        $Env:Data__ConfigurationScript = (Get-PSCallStack | Select-Object -Last 1).ScriptName
    }

    $Process = Start-Process -FilePath $ExecutablePath -PassThru

    $Process
}

function Install-PSUServer {
    param(
        [Parameter()]
        [string]$Path,
        [Parameter()]
        [Switch]$AddToPath,
        [Parameter(ParameterSetName = "Version")]
        [string]$Version = (Get-Module Universal).Version,
        [Parameter(ParameterSetName = "Latest")]
        [Switch]$LatestVersion
    )

    if ([UniversalAutomation.RemoteCommand]::Configuration)
    {
        return
    }

    $platform = "win7-x64";
    if ($PSVersionTable.Edition -eq 'Core')
    {
        if ($IsLinux)
        {
            $platform = "linux-x64"
        }
        else 
        {
            $platform = "osx-x64"
        }
    }

    if (-not $Path) {
        $Path = Join-Path $Env:ProgramData "PowerShellUniversal"
    }

    if ($LatestVersion)
    {
        $Version = (Invoke-WebRequest https://imsreleases.blob.core.windows.net/universal/production/version.txt).Content
    }

    Invoke-WebRequest "https://imsreleases.blob.core.windows.net/universal/production/$version/Universal.$platform.$Version.zip" -OutFile ".\Universal.$version.$platform.zip"
    Expand-Archive -Path ".\Universal.$Version.$platform.zip" -DestinationPath $Path -Force
    Remove-Item ".\Universal.$Version.$platform.zip" -Force
    Get-ChildItem $Path -Recurse | Unblock-File

    if ($AddToPath)
    {
        $Scope = 'User'
        if ($platform -eq 'win-x64' -and ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
        {
            $Scope = 'Machine'
        }
    
        $envPath = [Environment]::GetEnvironmentVariable('PATH', $Scope)
        $newpath = $envPath + ";" + $Path
        [Environment]::SetEnvironmentVariable("PATH", $newpath, $Scope)
    
        $Env:Path += ";" + $newpath
    }
}