Internal/Install-Chocolatey.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

function Install-Chocolatey {
    [CmdletBinding()]
    [OutputType([bool])]
    param (
        [bool]
        $Force
    )

    if (Test-NanoServer -or Test-CoreCLR) {
        Write-Error ($LocalizedData.ChocoUnSupportedOnCoreCLR)
        return
    }
  
    $chocoExePath = Get-ChocolateyExeFilePath
    if($chocoExePath -and (Test-Path -Path $chocoExePath -PathType Leaf))
    {
        Write-Debug ($LocalizedData.ChocoAlreadyInstalled)
        return
    }

    #$shouldContinueCaption = $LocalizedData.InstallChocolateyShouldContinueCaption
    #$shouldContinueQueryMessage = $LocalizedData.InstallChocolateyShouldContinueQuery
    #if (-not ($Force -or $request.ShouldContinue($shouldContinueQueryMessage, $shouldContinueCaption))) {
    # Write-Error $LocalizedData.UserDeclinedInstallChocolatey
    # return
    #}

    try {
        # determine location of included chocolatey-package & unzip-tool
        $filePath = (Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath '..\Files') -Filter 'chocolatey.*.nupkg*' | Sort NameLastWriteTime | Select -Last 1).FullName
        $7zaExe = Join-Path -Path $PSScriptRoot -ChildPath '..\Files\7za.exe'

        # determine temp-location
        $tempFolderPath = Join-Path -Path $env:windir -ChildPath 'Temp\chocolatey'
        New-Item -Path $tempFolderPath -ItemType Directory -Force

        # unzip the package
        Write-Verbose "Extracting '$filePath' to '$tempFolderPath'..."
        Start-Process "$7zaExe" -ArgumentList "x -o`"$tempFolderPath`" -bd -y `"$filePath`"" -Wait -NoNewWindow

        #Write-Verbose 'Setting ChocolateyInstall environment variables...'
        #[Environment]::SetEnvironmentVariable('ChocolateyInstall', $chocoPath, [EnvironmentVariableTarget]::Machine)
        #$env:ChocolateyInstall = [Environment]::GetEnvironmentVariable('ChocolateyInstall','Machine')
        #Write-Verbose "Env:ChocolateyInstall has '$($env:ChocolateyInstall)'"

        # call chocolatey install
        Write-Verbose "Installing chocolatey on this machine..."
        $toolsFolder = Join-Path -Path $tempFolderPath -ChildPath 'tools'
        $installFilePath = Join-Path -Path $toolsFolder -ChildPath 'chocolateyInstall.ps1'
        & $installFilePath

        Write-Verbose 'Ensuring chocolatey commands are on the path...'
        $chocoPath = Join-Path -Path $env:ALLUSERSPROFILE -ChildPath 'Chocolatey'
        $chocoExePath = Join-Path -Path $chocoPath -ChildPath 'bin'
        $env:Path = [Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::Machine)
        if ($env:path -notlike "*$chocoExePath*") {
            $env:Path += ";$chocoExePath"
        }
        Write-Verbose "Env:Path has '$($env:Path)'"

        Write-Verbose 'Ensuring chocolatey.nupkg is in the lib folder'
        $chocoPackageFolderPath = Join-Path -Path $chocoPath -ChildPath 'lib\chocolatey'
        $chocoPackageFilePath = Join-Path -Path $chocoPackageFolderPath -ChildPath 'chocolatey.nupkg'
        if (![System.IO.Directory]::Exists($chocoPackageFolderPath)) {
            [System.IO.Directory]::CreateDirectory($chocoPackageFolderPath)
        }
        Copy-Item -Path "$filePath" -Destination "$chocoPackageFilePath" -Force -ErrorAction SilentlyContinue

        $null = choco
        Write-Verbose 'Finish installing chocolatey'
    }
    finally {
        if ($tempFolderPath -and (Test-Path $tempFolderPath)) {
            Remove-Item -Path $tempFolderPath -Recurse -Force
        }
    }
}