-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInstall-PhpenvVersion.ps1
98 lines (88 loc) · 2.76 KB
/
Install-PhpenvVersion.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<#
.SYNOPSIS
None
.DESCRIPTION
None
.NOTES
None
.INPUTS
None
.OUTPUTS
None
#>
function Install-PhpenvVersion
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0, HelpMessage = "PHP version to install.")]
[version]$Version,
[Parameter(Position = 1, HelpMessage = "PHP version to install.")]
[Alias("NTS")]
[switch]$NonThreadSafe = $False,
[Parameter(Position = 2, HelpMessage = "PHP version to install.")]
[Alias("E")]
[string]$Environment
)
Write-FunctionCallLogMessage -Invocation $MyInvocation -Parameters $PSBoundParameters
$targetPath = "$( Get-PhpenvVersionDir $Version )"
if (Test-Path("$( $targetPath )\php.exe"))
{
Write-Host "Version $( $Version ) already installed."
return
}
$useX64 = $False
$url = (Get-PhpDownloadUrl $Version $useX64 $NonThreadSafe);
Write-Verbose "Checking availability of version $( $Version ) from $( $url )."
if (!(UrlExists $url))
{
Write-Verbose "Unable to find downloadable version at $( $url )."
$url = (Get-PhpDownloadUrl $Version $useX64 $NonThreadSafe $true)
Write-Verbose "Checking availability of version $( $Version ) from $( $url )."
if (!(UrlExists $url))
{
Write-Verbose "Unable to find downloadable version at $( $url )."
Write-Host "Unable to install version $( $Version )."
return
}
}
$tempDir = Join-Path $env:TEMP "phpenv"
if (![System.IO.Directory]::Exists($tempDir))
{
[System.IO.Directory]::CreateDirectory($tempDir) | Out-Null
}
$downloadFilePath = Join-Path $tempDir "$( $Version )Install.zip"
Write-Verbose "Downloading $( $url ) to $( $downloadFilePath )"
try
{
$wc = New-Object System.Net.WebClient
$wc.Headers.Add("user-agent", "Windows-PHPEnv/1.0")
Write-Verbose "Request Headers:"
foreach ($key in $wc.Headers)
{
$value = $wc.Headers[$key];
if ($value)
{
Write-Debug " `'$key`':`'$value`'"
}
else
{
Write-Debug " `'$key`'"
}
}
Write-Verbose "Starting download of $($url)"
$wc.DownloadFile($url, $downloadFilePath)
$phpenvHome = Get-PhpenvHome
Unzip $downloadFilePath "$( Get-PhpenvVersionDir $Version )"
Remove-Item -Path $downloadFilePath
}
catch
{
if ($_.Exception.Response.StatusCode.Value__ -eq 404)
{
Write-Error "Unable to find package for version $( $Version )"
return
}
Write-Error "Failed to download package for version $( $Version ). Please try again later."
return
}
}