-
-
Notifications
You must be signed in to change notification settings - Fork 48
Add Windows setup script #403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2fbaf3b
create windows setup script, add setup script usage in README.md
headquarter8302 5cf538b
Apply suggestions from code review
headquarter8302 724d727
add script param and its check
headquarter8302 3526c16
add php setting setup functionality, more verbose logging
headquarter8302 7b2fd15
further refine script
headquarter8302 e3ffa71
clarify script in readme
headquarter8302 c9acfb3
remove .ini validation confirmation
headquarter8302 06174e9
add "[setup]" prefix to script output
headquarter8302 fe1281c
add env:path check for PHP to skip manual input, add path validation …
headquarter8302 5ba006c
use proper regex for matching setting lines, add zip extension to set…
headquarter8302 3bac9c3
function-ize composer setup
headquarter8302 6201e1e
add `include_path` warning
headquarter8302 7a4f0c0
use `$PSScriptRoot` for root directory finding, making it execution c…
headquarter8302 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
param ( | ||
[ValidateScript({ Test-Path $_ })] | ||
[string] $phpDir | ||
) | ||
|
||
# check if php exists in Path, then set it as full PHP dir | ||
if (-not [string]::IsNullOrEmpty((Get-Command php).Source)) { | ||
$phpDir = Split-Path (Get-Command php).Source | ||
$fullPhpDir = (Get-Command php).Source | ||
} | ||
|
||
# if not, then check if the phpDir arg exists | ||
elseif ([string]::IsNullOrEmpty($phpDir)) { | ||
$phpDir = Read-Host "Enter the path of your PHP installation (e.g. 'C:\Program Files\php', 'D:\php', 'C:\wamp\bin\php')" | ||
|
||
# check if given path exists | ||
if (-not (Test-Path $phpDir)) { | ||
throw "The directory '$phpDir' does not exist. Did you point to the exact file instead of the containing folder?" | ||
} | ||
|
||
$fullPhpDir = Join-Path $phpDir "php.exe" | ||
|
||
# check if php.exe is there | ||
if (-not (Test-Path $fullPhpDir)) { | ||
throw "'php.exe' was not found in '$phpDir'. Was it installed correctly?" | ||
} | ||
} | ||
|
||
Write-Host -ForegroundColor Gray "[setup] Using $fullPhpDir as PHP runner" | ||
|
||
$rootDir = Split-Path -Path (Split-Path -Path $PSScriptRoot) | ||
|
||
function Validate-Ini { | ||
Write-Host "[setup] Validating .ini file" | ||
|
||
# ungodly regexes of setting lines that are commented | ||
# bulletproof except for the most egregious | ||
$iniSettingsRegex = @( | ||
";\s*\s*extension_dir\s*=\s*(`"ext`")" | ||
";\s*\s*extension\s*=\s*(gd)\b" | ||
";\s*\s*extension\s*=\s*(intl)\b" | ||
";\s*\s*extension\s*=\s*(openssl)\b" | ||
";\s*\s*extension\s*=\s*(pdo_mysql)\b" | ||
";\s*\s*extension\s*=\s*(zip)\b" | ||
) | ||
|
||
# will change to false if the validation fails | ||
$iniValid = $true | ||
|
||
# cover edge case if `include_path` is defined | ||
$iniIncludePathExists = $false | ||
|
||
$iniFilePath = Join-Path $phpDir "php.ini" | ||
$iniFileDevPath = Join-Path $phpDir "php.ini-development" | ||
|
||
# if the .ini doesn't exist, but the dev template does | ||
if (-not (Test-Path $iniFilePath) -and (Test-Path $iniFileDevPath)) { | ||
$confirm = Read-Host "You do not have a 'php.ini' file, but you have the 'php.ini-development' file. Would you like the script to enable the development .ini? Saying [n] will exit the script [y/n]" | ||
|
||
if ($confirm -match "y") { | ||
Write-Host "[setup] Renaming 'php.ini-development' to 'php.ini'" | ||
Rename-Item -Path $iniFileDevPath -NewName "php.ini" | ||
} | ||
else { | ||
throw "Exiting since the script must use 'php.ini'. You must rename/create the file yourself" | ||
} | ||
} | ||
# if none was found, then we're inbetween a rock and a hard place | ||
elseif (-not (Test-Path $iniFilePath) -and -not (Test-Path $iniFileDevPath)) { | ||
throw "'php.ini' and 'php.ini-development' cannot be found. Was PHP installed correctly?" | ||
} | ||
|
||
$iniFile = Get-Content $iniFilePath | ||
|
||
# actual file validation starts here | ||
foreach ($line in $iniFile) { | ||
if ($line -match [regex]::new("^(include_path)", "Multiline")) { | ||
Write-Host -ForegroundColor Yellow "[setup] You have 'include_path' defined in your settings file. This will disrupt the development server. You can temporarily comment out the 'include_path' setting so that PHP can use the relative working directory path to infer locations. The script will not modify this setting as this may break other applications depending on this setting" | ||
$iniIncludePathExists = $true | ||
} | ||
foreach ($settingRegex in $iniSettingsRegex) { | ||
if ($line -match $settingRegex) { | ||
$iniValid = $false | ||
} | ||
} | ||
} | ||
|
||
# .ini is invalid, prompt to fix | ||
if ($iniValid -eq $false) { | ||
$confirm = Read-Host "[setup] Your .ini file is not suitable for running the local test environment. Would you like the script to modify the file and enable the relevant settings? The script will only modify the settings needed for the project to run and leave others unchanged, you can view the list of settings that needs to be enabled in the root README [y/n]" | ||
|
||
if ($confirm -match "y") { | ||
Write-Host "[setup] Modifying and writing settings" | ||
|
||
# read-write each line, and uncomment lines that match the regex | ||
$iniFileNew = "" | ||
foreach ($iniLine in $iniFile) { | ||
$isSettingLine = $false | ||
foreach ($settingRegex in $iniSettingsRegex) { | ||
if ($iniLine -match $settingRegex) { | ||
$isSettingLine = $true | ||
$iniFileNew += $iniLine.Replace(";", "") + "`n" | ||
break | ||
} | ||
} | ||
if (-not $isSettingLine) { | ||
$iniFileNew += $iniLine + "`n" | ||
} | ||
} | ||
|
||
Write-Host -ForegroundColor Green "[setup] Settings file modified" | ||
|
||
# the modified file hasn't been written yet, write it now, per-line | ||
Clear-Content -Path $iniFilePath | ||
foreach ($line in $iniFileNew) { | ||
Add-Content -Path $iniFilePath -Value $line | ||
} | ||
|
||
Write-Host -ForegroundColor Green "[setup] Succesfully validated and written '.ini' settings, continuing setup" | ||
} | ||
# take a chance that the install process will continue to run fine, probably not | ||
else { | ||
Write-Host -ForegroundColor Yellow "[setup] Skipping .ini file modification. You must enable the relevant settings by yourself. Refer to the README for instructions on how to enable the settings manually." | ||
Write-Host -ForegroundColor Red "[setup] There's a chance that the script will error after this" | ||
} | ||
} | ||
else { | ||
Write-Host -ForegroundColor Green "[setup] All settings are valid, continuing setup" | ||
} | ||
} | ||
|
||
Validate-Ini | ||
|
||
|
||
function Setup-Composer { | ||
# shift to root dir | ||
Set-Location $rootDir | ||
|
||
# download composer | ||
Write-Host "[setup] Dowloading composer's installer" | ||
$composerInstallerUrl = "https://getcomposer.org/installer" | ||
$composerInstallerPath = Join-Path $rootDir "composer-setup.php" | ||
Invoke-WebRequest -Uri $composerInstallerUrl -OutFile $composerInstallerPath | ||
|
||
# check if composer's installer exists | ||
if (-Not (Test-Path $composerInstallerPath)) { | ||
throw "Failed to download Composer installer" | ||
} | ||
|
||
Write-Host "[setup] Installing composer" | ||
Start-Process -FilePath $fullPhpDir -ArgumentList $composerInstallerPath -Wait -NoNewWindow | ||
|
||
Write-Host "[setup] Getting dependencies" | ||
Start-Process -FilePath $fullPhpDir -ArgumentList "composer.phar install" -Wait -NoNewWindow | ||
|
||
# cleanup composer's installer | ||
Write-Host "[setup] Cleaning up composer's installer" | ||
Remove-Item $composerInstallerPath | ||
} | ||
|
||
Setup-Composer | ||
|
||
Write-Host -ForegroundColor Green "[setup] Setup complete! Run 'php -S localhost:8000 -t ./public/' to start the local dev server" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.