Skip to content

Commit 8c9024a

Browse files
committed
Allow specifying the Composer version to be installed
1 parent bc58a15 commit 8c9024a

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

PhpManager/public/Install-Composer.ps1

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
.Parameter Scope
1717
Install Composer the current user only ('User' - default), or at system-level ('System').
1818
19+
.Parameter Version
20+
Specify this option to install a specific Composer version. It can be a full version (eg 1.10.16) or a major version (eg 1)
21+
1922
.Parameter NoAddToPath
2023
Specify this option to don't add the Composer install path to the PATH environment variable.
2124
@@ -31,6 +34,9 @@
3134
[Parameter(Mandatory = $false, Position = 2, HelpMessage = 'Install for current user of for any user')]
3235
[ValidateSet('User', 'System')]
3336
[string] $Scope,
37+
[Parameter(Mandatory = $false, Position = 3, HelpMessage = 'Composer version to be installed')]
38+
[ValidatePattern('^\d+(\.\d+\.\d+(\-(alpha|beta|RC)\d*)*)?$')]
39+
[string] $Version,
3440
[switch] $NoAddToPath,
3541
[switch] $NoCache
3642
)
@@ -60,6 +66,7 @@
6066
$installerUrl = 'https://getcomposer.org/installer';
6167
$installer = ''
6268
$tempPhar = ''
69+
$actualPharUrl = ''
6370
$pathCreatedHere = $false
6471
try {
6572
if ($NoCache) {
@@ -75,13 +82,39 @@
7582
$arguments += $installer
7683
$arguments += '--install-dir=' + (Split-Path -Path $tempPhar -Parent)
7784
$arguments += '--filename=' + (Split-Path -Path $tempPhar -Leaf)
85+
if ($Version -ne '') {
86+
if ($Version -match '\.') {
87+
$arguments += '--version=' + $Version
88+
} else {
89+
$actualPharUrl = "https://getcomposer.org/composer-$Version.phar"
90+
switch ($Version) {
91+
'1' {
92+
$testVersion = '1.10.16'
93+
}
94+
'2' {
95+
$testVersion = '2.0.1'
96+
}
97+
default {
98+
$testVersion = "$Version.0.1"
99+
}
100+
}
101+
$arguments += "--version=$testVersion"
102+
$arguments += '--check'
103+
}
104+
}
78105
$arguments += '2>&1'
79106
Write-Verbose "Launching Composer installer"
80107
$installerResult = & $phpVersion.ExecutablePath $arguments
81108
if ($LASTEXITCODE -ne 0) {
82109
throw $installerResult
83110
}
84-
Write-Verbose "Composer succeeded"
111+
Write-Verbose "Composer installed succeeded"
112+
if ($actualPharUrl -ne '') {
113+
Write-Verbose "Downloading Composer"
114+
Set-NetSecurityProtocolType
115+
Write-Verbose "Downloading from $actualPharUrl"
116+
Invoke-WebRequest -UseBasicParsing -Uri $actualPharUrl -OutFile $tempPhar
117+
}
85118
Write-Verbose "Installing to $Path"
86119
If (-Not(Test-Path -LiteralPath $Path)) {
87120
New-Item -ItemType Directory -Path $Path | Out-Null
@@ -108,6 +141,9 @@
108141
Set-Content -Path $destBat -Value $lines
109142
if (-not($NoAddToPath)) {
110143
Write-Verbose 'Adding to PATH'
144+
if (-not($Scope)) {
145+
$Scope = 'User'
146+
}
111147
Edit-FolderInPath -Operation Add -Path $Path -Persist $Scope -CurrentProcess
112148
}
113149
} catch {

0 commit comments

Comments
 (0)