-
Notifications
You must be signed in to change notification settings - Fork 86
Bundle default PHP binary #3554
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
4 commits
Select commit
Hold shift + click to select a range
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
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,63 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { DEFAULT_PHP_VERSION } from '@studio/common/constants'; | ||
| import { getConfiguredPhpBinaryVersion } from '@studio/common/lib/php-binary-metadata'; | ||
| import { getPhpBinaryPath } from 'cli/lib/dependency-management/paths'; | ||
| import type { Migration } from '@studio/common/lib/migration'; | ||
|
|
||
| const PHP_BINARY_FILENAME = process.platform === 'win32' ? 'php.exe' : 'php'; | ||
|
|
||
| function getBundledPhpBinaryRoot(): string { | ||
| return ( | ||
| process.env.STUDIO_BUNDLED_PHP_BIN_ROOT ?? path.resolve( import.meta.dirname, '..', 'php-bin' ) | ||
| ); | ||
| } | ||
|
|
||
| function getDefaultPhpPatchVersion(): string { | ||
| return getConfiguredPhpBinaryVersion( DEFAULT_PHP_VERSION ) ?? DEFAULT_PHP_VERSION; | ||
| } | ||
|
|
||
| function getBundledDefaultPhpDir(): string { | ||
| return path.join( getBundledPhpBinaryRoot(), getDefaultPhpPatchVersion() ); | ||
| } | ||
|
|
||
| function getBundledDefaultPhpPath(): string { | ||
| return path.join( getBundledDefaultPhpDir(), PHP_BINARY_FILENAME ); | ||
| } | ||
|
|
||
| function getDefaultPhpDestinationDir(): string { | ||
| return path.dirname( getPhpBinaryPath( DEFAULT_PHP_VERSION ) ); | ||
| } | ||
|
|
||
| function bundledDefaultPhpExists(): boolean { | ||
| return fs.existsSync( getBundledDefaultPhpPath() ); | ||
| } | ||
|
|
||
| // Packaged Studio builds include the default PHP package in app resources. | ||
| // Copy it into the writable PHP cache so first native-PHP startup works offline. | ||
| export const installBundledDefaultPhp: Migration = { | ||
| needsToRun: async () => { | ||
| return bundledDefaultPhpExists() && ! fs.existsSync( getDefaultPhpDestinationDir() ); | ||
| }, | ||
| run: async () => { | ||
| const destinationDir = getDefaultPhpDestinationDir(); | ||
| fs.mkdirSync( path.dirname( destinationDir ), { recursive: true } ); | ||
| try { | ||
| fs.cpSync( getBundledDefaultPhpDir(), destinationDir, { | ||
| recursive: true, | ||
| force: true, | ||
| } ); | ||
|
|
||
| if ( process.platform !== 'win32' ) { | ||
| fs.chmodSync( getPhpBinaryPath( DEFAULT_PHP_VERSION ), 0o755 ); | ||
| } | ||
| } catch ( error ) { | ||
| fs.rmSync( destinationDir, { recursive: true, force: true } ); | ||
| console.warn( | ||
| `Warning: failed to install bundled PHP ${ DEFAULT_PHP_VERSION } package: ${ | ||
| ( error as Error ).message | ||
| }` | ||
| ); | ||
| } | ||
| }, | ||
| }; | ||
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
78 changes: 78 additions & 0 deletions
78
apps/cli/migrations/tests/06-install-bundled-default-php.test.ts
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,78 @@ | ||
| import fs from 'fs'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
| import { DEFAULT_PHP_VERSION } from '@studio/common/constants'; | ||
| import { getConfiguredPhpBinaryVersion } from '@studio/common/lib/php-binary-metadata'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { installBundledDefaultPhp } from 'cli/migrations/06-install-bundled-default-php'; | ||
|
|
||
| let configDir: string; | ||
| let bundledPhpDir: string; | ||
|
|
||
| function getBinaryName(): string { | ||
| return process.platform === 'win32' ? 'php.exe' : 'php'; | ||
| } | ||
|
|
||
| function getDefaultPhpPatchVersion(): string { | ||
| return getConfiguredPhpBinaryVersion( DEFAULT_PHP_VERSION )!; | ||
| } | ||
|
|
||
| function getBundledDefaultPhpDir(): string { | ||
| return path.join( bundledPhpDir, getDefaultPhpPatchVersion() ); | ||
| } | ||
|
|
||
| function getDefaultPhpDestinationDir(): string { | ||
| return path.join( configDir, 'php-bin', getDefaultPhpPatchVersion() ); | ||
| } | ||
|
|
||
| function writeBundledDefaultPhp(): void { | ||
| const bundledDir = getBundledDefaultPhpDir(); | ||
| fs.mkdirSync( bundledDir, { recursive: true } ); | ||
| fs.writeFileSync( path.join( bundledDir, getBinaryName() ), 'bundled php' ); | ||
| fs.writeFileSync( path.join( bundledDir, 'runtime.json' ), '{"binary":"php"}' ); | ||
| } | ||
|
|
||
| describe( 'installBundledDefaultPhp', () => { | ||
| beforeEach( () => { | ||
| configDir = fs.mkdtempSync( path.join( os.tmpdir(), 'studio-php-bin-' ) ); | ||
| bundledPhpDir = fs.mkdtempSync( path.join( os.tmpdir(), 'studio-bundled-php-bin-' ) ); | ||
| vi.stubEnv( 'DEV_CONFIG_DIR', configDir ); | ||
| vi.stubEnv( 'STUDIO_BUNDLED_PHP_BIN_ROOT', bundledPhpDir ); | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| vi.unstubAllEnvs(); | ||
| fs.rmSync( configDir, { recursive: true, force: true } ); | ||
| fs.rmSync( bundledPhpDir, { recursive: true, force: true } ); | ||
| } ); | ||
|
|
||
| it( 'does not run when bundled PHP is unavailable', async () => { | ||
| await expect( installBundledDefaultPhp.needsToRun() ).resolves.toBe( false ); | ||
| } ); | ||
|
|
||
| it( 'copies the bundled default PHP folder when the destination patch folder is missing', async () => { | ||
| writeBundledDefaultPhp(); | ||
|
|
||
| await expect( installBundledDefaultPhp.needsToRun() ).resolves.toBe( true ); | ||
| await installBundledDefaultPhp.run(); | ||
|
|
||
| expect( | ||
| fs.readFileSync( path.join( getDefaultPhpDestinationDir(), getBinaryName() ), 'utf8' ) | ||
| ).toBe( 'bundled php' ); | ||
| expect( | ||
| fs.readFileSync( path.join( getDefaultPhpDestinationDir(), 'runtime.json' ), 'utf8' ) | ||
| ).toBe( '{"binary":"php"}' ); | ||
| } ); | ||
|
|
||
| it( 'does not replace an existing destination patch folder', async () => { | ||
| writeBundledDefaultPhp(); | ||
| fs.mkdirSync( getDefaultPhpDestinationDir(), { recursive: true } ); | ||
| fs.writeFileSync( path.join( getDefaultPhpDestinationDir(), getBinaryName() ), 'existing php' ); | ||
|
|
||
| await expect( installBundledDefaultPhp.needsToRun() ).resolves.toBe( false ); | ||
|
|
||
| expect( | ||
| fs.readFileSync( path.join( getDefaultPhpDestinationDir(), getBinaryName() ), 'utf8' ) | ||
| ).toBe( 'existing php' ); | ||
| } ); | ||
| } ); |
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
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this script was there before this PR, but it strikes me now that we should maybe say that it downloads a PHP package rather than a binary, because it's not just that single binary on Windows. |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a comment that explains what this migration does.