functions/pipeline.install-tools.txt

[CmdletBinding()]
param(
    [string]$workingPath
)
$ProgressPreference = 'SilentlyContinue'
$Global:ProgressPreference = 'SilentlyContinue' #Needed for expand archive to supress progress

function Install-ToolsPackageFromNuget {
    param($PackagePath, $package, $subPath, $version, $environmentVariablesToSet)

    Write-Host "Installing Package $package"
    $testPath = Join-Path(Join-Path $PackagePath $package) $subPath
    
    if (-not (Test-path $testPath)) {
        $nugetparams = @("install", $package, "-ExcludeVersion", "-OutputDirectory", $PackagePath)
        if ($_.version) { $nugetparams += "-version", $_.version }
        &$nugetpath $nugetparams
    }
    if ($_.env) {
        $_.env | foreach-object {
            Write-verbose "Setting $_ to $testPath"
            set-item -path "env:$_" -value $testPath
        }
    }
}

function Install-ToolFromUrl {
    param ([string] $ToolPath
        , [string] $url
        , [string] $TestFile
        , $EnvironmentSettings)
    
    if (-not ( Test-path $ToolPath)) { New-Item -ItemType Directory -Path $ToolPath | Out-Null }

    [System.Net.HttpWebResponse] $zipHttpResponse = [System.Net.WebRequest]::Create($url).GetResponse();
    $downloadZip = $false

    #Meta data file to store information about the download for future comparisons
    $MetaDataFilePath = join-path $ToolFolder "metadata.json"
    if (Test-path $MetaDataFilePath) {
        $SqlpackageMetaData = Get-Content $MetaDataFilePath -Raw | Convertfrom-json
        #Check the metadata from the URL compared with the previous download
        if ($SqlpackageMetaData.Size -ne $zipHttpResponse.ContentLength -or
            $SqlpackageMetaData.Filename -ne $zipHttpResponse.ResponseUri.Segments[-1] ) {
            $downloadZip = $true
        }
    }
    else {
        $downloadZip = $true
    }
    if ($downloadZip) {
        $toolzip = join-path $ToolPath "$toolName.zip"
        Invoke-WebRequest $url -OutFile $toolzip
        Expand-Archive -Path $toolzip -DestinationPath $ToolPath -Force
        @{Size = $zipHttpResponse.ContentLength; Filename = $zipHttpResponse.ResponseUri.Segments[-1] } | convertto-json | out-file $MetaDataFilePath
    }
    $zipHttpResponse.Close()
    $zipHttpResponse.Dispose()
    $EnvironmentSettings | ForEach-Object { Set-item env:"$_.environmentvariable" (join-path $ToolPath $_.path) }
   
    Write-Host "$toolName installed"
}

##### Install Nuget #####################################
Write-Verbose "Installing Nuget"

$nugetpath = ""
$fallbackNugetPath = "c:\ProgramData\Nuget\Nuget.exe"
if ($nugetpath -eq "") {
    if (Test-Path $fallbackNugetPath) {
        $nugetPath = $fallbackNugetPath
    }
    else {
        Write-Verbose "Downloading&Installing Nuget latest\"
        New-Item -ItemType Directory -Path "c:\ProgramData\Nuget" | Out-Null
        Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -OutFile $fallbackNugetPath
        $nugetpath = $fallbackNugetPath
    }
}
Write-Verbose "Nuget found $($env:nugetPath)"
$env:nugetPath = $nugetpath

##### Install Data Tools Msbuild #####################################

if (-not (Test-path $workingPath)) {
    Write-Verbose "Creating $workingPath"
    New-Item $workingPath -ItemType Directory -Force | Out-Null
}
$PackagePath = Resolve-Path $workingPath

Write-Verbose "Installing tools to $PackagePath "


$packages = @()
#@{package="Microsoft.Data.Tools.Msbuild";subpath="lib\net46";env="SQLDBExtensionsRefPath","SqlServerRedistPath"} `
# ,@{package="Nunit.ConsoleRunner";subpath="\tools\";env="NunitToolsPath"}

$packages | ForEach-Object { Install-ToolsPackageFromNuget -packagePath $PackagePath -package $_.package -version $_.version -subPath $_.subpath -environmentVariablesToSet $_.env }


#Install-ToolFromZipUrl -ToolFolder "$PackagePath\sqlpackage" -ToolName "sqlpackage" `
#-url "https://go.microsoft.com/fwlink/?linkid=2109019" `
#-EnvironmentSettings @{environmentvariable="sqlPackagePath";path=""}
# ,@{environmentvariable="SqlpackagePathExe";path="sqlpackage.exe"}

Function Install-PsModuleFast {
    param([string] $module
        , [version] $version)
    Write-Verbose "Loading module $Module"

    if (-not (get-module $module -ListAvailable | Where-object Version -gt $version)) {
        Write-Output " Installing module $module"
        if ($null -eq $version) {
            install-module $module -force -AllowClobber -Scope CurrentUser -SkipPublisherCheck
        }
        else {
            install-module $module -force -AllowClobber -Scope CurrentUser -SkipPublisherCheck -MinimumVersion $version
        }
    }

    if (-not (get-module $module | Where-object Version -GE $version)) {
        Write-Output " importing module $module $version"
        if ($null -eq $version ) {
            import-module $module -force
        }
        else {
            import-module $module -force -MinimumVersion $version
        }
    }
}

$PSModules = @{Module = "Pester"; Version = "4.5" }, `
             @{Module = "Microsoft.PowerApps.PowerShell" }, `
             @{Module = "Microsoft.PowerApps.Administration.PowerShell" }, `
             @{Module = "VSSetup" }
foreach ($Ps in $PSModules) {
 
    Install-PsModuleFast @PS
}

#MSBuild
#$Env:VSPath= (Get-VSSetupInstance | Sort-Object -Property InstallationVersion -Descending | Select-Object -First 1).InstallationPath
#$Env:MsbuildPath = (gci $Env:VSPath msbuild.exe -Recurse | select-object -First 1).FullName
#Write-Host "Setting MsBuildPath to $($Env:MsbuildPath)"