setup-runtime.ps1 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. param (
  2. [string] ${action}
  3. )
  4. function AddToPath {
  5. param (
  6. [string]$pathToAdd
  7. )
  8. $currentPath = [System.Environment]::GetEnvironmentVariable('Path', 'User')
  9. if ($currentPath -notlike "*$pathToAdd*") {
  10. $newPath = $currentPath + ";$pathToAdd"
  11. [System.Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
  12. Write-Host "Added '$pathToAdd' to Path."
  13. Write-Host "To remove path, use: " -NoNewline
  14. Write-Host "bin/setup-runtime remove-path" -ForegroundColor Cyan
  15. } else {
  16. Write-Host "Path already exists."
  17. }
  18. }
  19. function RemoveFromPath {
  20. param (
  21. [string]$pathToRemove
  22. )
  23. $currentPath = [System.Environment]::GetEnvironmentVariable('Path', 'User')
  24. if ($currentPath -like "*$pathToRemove*") {
  25. $newPath = $currentPath -replace [regex]::Escape(';' + $pathToRemove), ''
  26. [System.Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
  27. Write-Host "Removed Path '$pathToRemove'"
  28. } else {
  29. Write-Host "Path '$pathToRemove' not in Path"
  30. }
  31. }
  32. # working dir
  33. $WorkingDir = (Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Definition))
  34. if ($action -eq 'add-path') {
  35. AddToPath ($WorkingDir + '\runtime')
  36. exit 0
  37. } elseif ($action -eq 'remove-path') {
  38. RemoveFromPath ($WorkingDir + '\runtime')
  39. exit 0
  40. } elseif (-not($action -eq '')) {
  41. Write-Host ("Invalid action: " + $action) -ForegroundColor Red
  42. exit 1
  43. }
  44. # get php 8.1 specific version
  45. $API = (Invoke-WebRequest -Uri "https://www.php.net/releases/index.php?json&version=8.1") | ConvertFrom-Json
  46. # php windows download
  47. $PHPRuntimeUrl = "https://windows.php.net/downloads/releases/php-" + $API.version + "-Win32-vs16-x64.zip"
  48. $ComposerUrl = "https://getcomposer.org/download/latest-stable/composer.phar"
  49. # create dir
  50. New-Item -Path "downloads" -ItemType Directory -Force | Out-Null
  51. # download php
  52. if (-not(Test-Path "downloads\php.zip"))
  53. {
  54. Write-Host "Downloading PHP ..."
  55. Invoke-WebRequest $PHPRuntimeUrl -OutFile "downloads\php.zip"
  56. }
  57. # extract php
  58. New-Item -Path "runtime" -ItemType Directory -Force | Out-Null
  59. Write-Host "Extracting php.zip ..."
  60. Expand-Archive -Path "downloads/php.zip" -DestinationPath "runtime" -Force
  61. # make php.ini
  62. Move-Item -Path "runtime\php.ini-production" -Destination "runtime\php.ini" -Force
  63. $OriginINI = Get-Content -Path "runtime\php.ini" -Raw
  64. $OriginINI = $OriginINI -replace ';extension=openssl', 'extension=openssl'
  65. $OriginINI = $OriginINI -replace ';extension=curl', 'extension=curl'
  66. $OriginINI = $OriginINI -replace ';extension=mbstring', 'extension=mbstring'
  67. $OriginINI = $OriginINI -replace ';extension=sodium', 'extension=sodium'
  68. $OriginINI = $OriginINI -replace ';extension_dir = "./"', ('extension_dir = "' + (Split-Path -Parent $MyInvocation.MyCommand.Definition) + '\..\runtime\ext"')
  69. $OriginINI | Set-Content -Path "runtime\php.ini"
  70. # download composer
  71. if (-not(Test-Path "runtime\composer.phar"))
  72. {
  73. Write-Host "Downloading composer ..."
  74. Invoke-WebRequest $ComposerUrl -OutFile "downloads\composer.phar"
  75. Move-Item -Path "downloads\composer.phar" -Destination "runtime\composer.phar" -Force
  76. }
  77. # create runtime\composer.ps1
  78. $ComposerContent = '
  79. $WorkingDir = (Split-Path -Parent $MyInvocation.MyCommand.Definition)
  80. & ($WorkingDir + "\php.exe") (Join-Path $WorkingDir "\composer.phar") @args
  81. '
  82. $ComposerContent | Set-Content -Path 'runtime\composer.ps1' -Encoding UTF8
  83. Write-Host "Successfully downloaded PHP and Composer !" -ForegroundColor Green
  84. Write-Host "Use static-php-cli: " -NoNewline
  85. Write-Host "bin/spc" -ForegroundColor Cyan
  86. Write-Host "Use php: " -NoNewline
  87. Write-Host "runtime/php" -ForegroundColor Cyan
  88. Write-Host "Use composer: " -NoNewline
  89. Write-Host "runtime/composer" -ForegroundColor Cyan
  90. Write-Host ""
  91. Write-Host "Don't forget installing composer dependencies '" -NoNewline
  92. Write-Host "runtime/composer install" -ForegroundColor Cyan -NoNewline
  93. Write-Host "' before using static-php-cli !"
  94. Write-Host ""
  95. Write-Host "If you want to use this PHP for quality tools (like phpstan, php-cs-fixer) or other project,"
  96. Write-Host "or use PHP, Composer as system executable,"
  97. Write-Host "use '" -NoNewline
  98. Write-Host "bin/setup-runtime add-path" -ForegroundColor Cyan -NoNewline
  99. Write-Host "' to add runtime dir in Path."