Skip to content

Instantly share code, notes, and snippets.

@trashtoy
Last active July 27, 2016 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trashtoy/6d6054d39293df1df3ff473765a6d1b4 to your computer and use it in GitHub Desktop.
Save trashtoy/6d6054d39293df1df3ff473765a6d1b4 to your computer and use it in GitHub Desktop.
Using composer from PowerShell
function Download-Installer {
param(
[string] $tmp,
[int] $retry = 5
)
$installerPath = $tmp + "\composer-installer.php"
if (Test-Path $installerPath) {
return $installerPath
}
if (!(Test-Path -PathType container $tmp)) {
mkdir -Force $tmp
}
Write-Host "Downloading composer installer ..."
$installerURL = "https://getcomposer.org/installer"
Invoke-WebRequest $installerURL -OutFile $installerPath
if ($retry -gt 0) {
$next = $retry - 1
return (Download-Installer $tmp $next)
}
throw "Failed to download $installerURL"
}
function Install-Composer {
param (
[string] $tmp,
[string] $dir,
[int] $retry = 3
)
if (!(Get-Command -Name PHP 2>&1)) {
throw "Please install PHP into your computer (Cannot find 'PHP' from PATH)"
}
$phar = $dir + "\composer.phar"
if (Test-Path $phar) {
return $phar
}
$installerPath = (Download-Installer $tmp)
$installerLog = $tmp + "\composer-installer.log"
Write-Host "Installing composer.phar ..."
Push-Location $dir
php $installerPath > $installerLog
Pop-Location
if (Test-Path $phar) {
return $phar
} else {
# Seems that $installPath is broken
Write-Host -ForegroundColor Yellow "[NOTICE]"
Write-Host "Composer installer seems to be broken."
Remove-Item $installerPath
$next = $retry - 1
if ($next -gt 0) {
return (Install-Composer $tmp $dir $next)
}
throw "Failed to install composer.phar"
}
}
if (Test-Path $env:TMP) {
$tmp = $env:TMP
} else {
$tmp = "C:\Temp"
}
$dir = (Split-Path $MyInvocation.MyCommand.Path)
try {
$phar = (Install-Composer $tmp $dir)
php $phar $args
} catch {
Write-Host -ForegroundColor Red "[ERROR]"
Write-Host $Error[0].Exception.Message
Write-Host
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment