-
Notifications
You must be signed in to change notification settings - Fork 271
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
Blueprints: wp-cli step #1017
Merged
Merged
Blueprints: wp-cli step #1017
Changes from all commits
Commits
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 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 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 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 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
60 changes: 60 additions & 0 deletions
60
packages/playground/blueprints/src/lib/steps/wp-cli.spec.ts
This file contains 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,60 @@ | ||
import { NodePHP } from '@php-wasm/node'; | ||
import { splitShellCommand, wpCLI } from './wp-cli'; | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { unzip } from './unzip'; | ||
import { getWordPressModule } from '@wp-playground/wordpress'; | ||
|
||
const phpVersion = '8.0'; | ||
describe('Blueprint step wpCLI', () => { | ||
let php: NodePHP; | ||
|
||
beforeEach(async () => { | ||
php = await NodePHP.load(phpVersion, { | ||
requestHandler: { | ||
documentRoot: '/wordpress', | ||
}, | ||
}); | ||
php.setSapiName('cli'); | ||
await unzip(php, { | ||
zipFile: await getWordPressModule(), | ||
extractToPath: '/wordpress', | ||
}); | ||
const wpCliPath = join(__dirname, '../../test/wp-cli.phar'); | ||
php.writeFile('/tmp/wp-cli.phar', readFileSync(wpCliPath)); | ||
}); | ||
|
||
it('should run wp-cli commands', async () => { | ||
const result = await wpCLI(php, { | ||
command: | ||
"wp post create --post_title='Test post' --post_excerpt='Some content' --no-color", | ||
}); | ||
expect(result.text).toMatch(/Success: Created post/); | ||
}); | ||
}); | ||
|
||
describe('splitShellCommand', () => { | ||
it('Should split a shell command into an array', () => { | ||
const command = | ||
'wp post create --post_title="Test post" --post_excerpt="Some content"'; | ||
const result = splitShellCommand(command); | ||
expect(result).toEqual([ | ||
'wp', | ||
'post', | ||
'create', | ||
'--post_title=Test post', | ||
'--post_excerpt=Some content', | ||
]); | ||
}); | ||
|
||
it('Should treat multiple spaces as a single space', () => { | ||
const command = 'ls --wordpress --playground --is-great'; | ||
const result = splitShellCommand(command); | ||
expect(result).toEqual([ | ||
'ls', | ||
'--wordpress', | ||
'--playground', | ||
'--is-great', | ||
]); | ||
}); | ||
}); |
This file contains 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,138 @@ | ||
import { PHPResponse } from '@php-wasm/universal'; | ||
import { StepHandler } from '.'; | ||
import { phpVar } from '@php-wasm/util'; | ||
|
||
/** | ||
* @inheritDoc wpCLI | ||
* @hasRunnableExample | ||
* @example | ||
* | ||
* <code> | ||
* { | ||
* "step": "wpCLI", | ||
* "command": "wp post create --post_title='Test post' --post_excerpt='Some content'" | ||
* } | ||
* </code> | ||
*/ | ||
export interface WPCLIStep { | ||
/** The step identifier. */ | ||
step: 'wp-cli'; | ||
/** The WP CLI command to run. */ | ||
command: string | string[]; | ||
/** wp-cli.phar path */ | ||
wpCliPath?: string; | ||
} | ||
|
||
/** | ||
* Runs PHP code. | ||
*/ | ||
export const wpCLI: StepHandler<WPCLIStep, Promise<PHPResponse>> = async ( | ||
playground, | ||
{ command, wpCliPath = '/tmp/wp-cli.phar' } | ||
) => { | ||
if (!(await playground.fileExists(wpCliPath))) { | ||
throw new Error(`wp-cli.phar not found at ${wpCliPath}`); | ||
} | ||
|
||
let args: string[]; | ||
if (typeof command === 'string') { | ||
command = command.trim(); | ||
args = splitShellCommand(command); | ||
} else { | ||
args = command; | ||
} | ||
|
||
const cmd = args.shift(); | ||
if (cmd !== 'wp') { | ||
throw new Error(`The first argument must be "wp".`); | ||
} | ||
|
||
await playground.writeFile('/tmp/stdout', ''); | ||
await playground.writeFile('/tmp/stderr', ''); | ||
await playground.writeFile( | ||
'/wordpress/run-cli.php', | ||
`<?php | ||
// Set up the environment to emulate a shell script | ||
// call. | ||
|
||
// Set SHELL_PIPE to 0 to ensure WP-CLI formats | ||
// the output as ASCII tables. | ||
// @see https://github.com/wp-cli/wp-cli/issues/1102 | ||
putenv( 'SHELL_PIPE=0' ); | ||
|
||
// Set the argv global. | ||
$GLOBALS['argv'] = array_merge([ | ||
"/tmp/wp-cli.phar", | ||
"--path=/wordpress" | ||
], ${phpVar(args)}); | ||
|
||
// Provide stdin, stdout, stderr streams outside of | ||
// the CLI SAPI. | ||
define('STDIN', fopen('php://stdin', 'rb')); | ||
define('STDOUT', fopen('php://stdout', 'wb')); | ||
define('STDERR', fopen('/tmp/stderr', 'wb')); | ||
|
||
require( ${phpVar(wpCliPath)} ); | ||
` | ||
); | ||
|
||
const result = await playground.run({ | ||
scriptPath: '/wordpress/run-cli.php', | ||
}); | ||
|
||
if (result.errors) { | ||
throw new Error(result.errors); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
/** | ||
* Naive shell command parser. | ||
* Ensures that commands like `wp option set blogname "My blog name"` are split into | ||
* `['wp', 'option', 'set', 'blogname', 'My blog name']` instead of | ||
* `['wp', 'option', 'set', 'blogname', 'My', 'blog', 'name']`. | ||
* | ||
* @param command | ||
* @returns | ||
*/ | ||
export function splitShellCommand(command: string) { | ||
const MODE_NORMAL = 0; | ||
const MODE_IN_QUOTE = 1; | ||
|
||
let mode = MODE_NORMAL; | ||
let quote = ''; | ||
|
||
const parts: string[] = []; | ||
let currentPart = ''; | ||
for (let i = 0; i < command.length; i++) { | ||
const char = command[i]; | ||
if (mode === MODE_NORMAL) { | ||
if (char === '"' || char === "'") { | ||
mode = MODE_IN_QUOTE; | ||
quote = char; | ||
} else if (char.match(/\s/)) { | ||
if (currentPart) { | ||
parts.push(currentPart); | ||
} | ||
currentPart = ''; | ||
} else { | ||
currentPart += char; | ||
} | ||
} else if (mode === MODE_IN_QUOTE) { | ||
if (char === '\\') { | ||
i++; | ||
currentPart += command[i]; | ||
} else if (char === quote) { | ||
mode = MODE_NORMAL; | ||
quote = ''; | ||
} else { | ||
currentPart += char; | ||
} | ||
} | ||
} | ||
if (currentPart) { | ||
parts.push(currentPart); | ||
} | ||
return parts; | ||
} |
Binary file not shown.
Oops, something went wrong.
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 only trigger this request if
wp-cli.phar
doesn't yet exist in VFS, or else running this Blueprint step inwp-now
will always triggerfetch()
.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.
The step works on the web. Let's figure out node.js in a follow-up PR.