From 3e884ea208194461c7f87240e56b702446d1de2b Mon Sep 17 00:00:00 2001 From: kokororin Date: Sat, 29 Apr 2023 20:19:27 +0800 Subject: [PATCH] perf: faster exec speed --- scripts/generate.ts | 7 +++- src/PHPFmt.ts | 17 ++++---- src/PHPFmtProvider.ts | 30 +++++++-------- src/Transformations.ts | 64 +++++++++++++++++-------------- src/extension.ts | 2 +- src/utils.ts | 4 ++ test/extension.test.ts | 6 +-- testProject/.vscode/settings.json | 6 ++- 8 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 src/utils.ts diff --git a/scripts/generate.ts b/scripts/generate.ts index 630019d..74e37c1 100644 --- a/scripts/generate.ts +++ b/scripts/generate.ts @@ -59,6 +59,10 @@ void (async () => { } ); + const transformations = await new Transformations( + 'php' + ).getTransformations(); + readmeContent = readmeContent.replace( /([\s\S]*)/, () => { @@ -69,8 +73,7 @@ void (async () => { os.EOL + '| -------- | ----------- |' + os.EOL + - new Transformations('php') - .getTransformations() + transformations .map(item => { let row = `| ${item.key} | `; row += item.description; diff --git a/src/PHPFmt.ts b/src/PHPFmt.ts index f6119d3..a8c16e1 100644 --- a/src/PHPFmt.ts +++ b/src/PHPFmt.ts @@ -6,13 +6,13 @@ import { import path from 'path'; import fs from 'fs'; import os from 'os'; -import { execSync } from 'child_process'; import detectIndent from 'detect-indent'; import findUp from 'find-up'; import phpfmt from 'use-phpfmt'; import type { PHPFmtConfig } from './types'; import { Widget } from './Widget'; import { PHPFmtError, PHPFmtIgnoreError } from './PHPFmtError'; +import { exec } from './utils'; export class PHPFmt { private readonly widget: Widget; @@ -141,15 +141,18 @@ export class PHPFmt { } try { - const stdout = execSync( + const { stdout, stderr } = await exec( `${this.config.php_bin} -r "echo PHP_VERSION_ID;"`, execOptions ); - if (Number(stdout.toString()) < 70000) { + if (stderr) { + throw new PHPFmtError(`php_bin "${this.config.php_bin}" is invalid`); + } + if (Number(stdout.trim()) < 70000) { throw new PHPFmtError('PHP version < 7 is not supported'); } } catch (err) { - throw new PHPFmtError(`php_bin "${this.config.php_bin}" is invalid`); + throw new PHPFmtError(`Error getting php version`); } const tmpDir = os.tmpdir(); @@ -188,7 +191,7 @@ export class PHPFmt { // test whether the php file has syntax error try { - execSync(`${this.config.php_bin} -l ${tmpFileName}`, execOptions); + await exec(`${this.config.php_bin} -l ${tmpFileName}`, execOptions); } catch (err) { this.widget.addToOutput(err.message); Window.setStatusBarMessage( @@ -214,7 +217,7 @@ export class PHPFmt { this.widget.addToOutput(`Executing process: ${formatCmd}`); try { - execSync(formatCmd, execOptions); + await exec(formatCmd, execOptions); } catch (err) { this.widget.addToOutput(err.message).show(); throw new PHPFmtError('Execute phpfmt failed'); @@ -231,5 +234,3 @@ export class PHPFmt { throw new PHPFmtIgnoreError(); } } - -export default PHPFmt; diff --git a/src/PHPFmtProvider.ts b/src/PHPFmtProvider.ts index f8b6e86..7844229 100644 --- a/src/PHPFmtProvider.ts +++ b/src/PHPFmtProvider.ts @@ -14,16 +14,13 @@ import pkg from 'pjson'; import type { PHPFmt } from './PHPFmt'; import type { Widget } from './Widget'; import { Transformations } from './Transformations'; -import type { TransformationItem } from './types'; import { PHPFmtIgnoreError } from './PHPFmtError'; -export default class PHPFmtProvider { - private readonly phpfmt: PHPFmt; +export class PHPFmtProvider { private readonly widget: Widget; private readonly documentSelector: DocumentSelector; - public constructor(phpfmt: PHPFmt) { - this.phpfmt = phpfmt; + public constructor(private readonly phpfmt: PHPFmt) { this.widget = this.phpfmt.getWidget(); this.documentSelector = [ { language: 'php', scheme: 'file' }, @@ -47,13 +44,12 @@ export default class PHPFmtProvider { } public listTransformationsCommand(): Disposable { - return Commands.registerCommand('phpfmt.listTransformations', () => { + return Commands.registerCommand('phpfmt.listTransformations', async () => { const transformations = new Transformations( this.phpfmt.getConfig().php_bin ); - const transformationItems: TransformationItem[] = - transformations.getTransformations(); + const transformationItems = await transformations.getTransformations(); const items: QuickPickItem[] = []; for (const item of transformationItems) { @@ -63,15 +59,15 @@ export default class PHPFmtProvider { }); } - void Window.showQuickPick(items).then(result => { - if (typeof result !== 'undefined') { - const output = transformations.getExample({ - key: result.label, - description: result.description ?? '' - }); - this.widget.addToOutput(output).show(); - } - }); + const result = await Window.showQuickPick(items); + + if (typeof result !== 'undefined') { + const output = await transformations.getExample({ + key: result.label, + description: result.description ?? '' + }); + this.widget.addToOutput(`Transformation\n${output}`).show(); + } }); } diff --git a/src/Transformations.ts b/src/Transformations.ts index 58753ea..a5f5e08 100644 --- a/src/Transformations.ts +++ b/src/Transformations.ts @@ -1,43 +1,49 @@ import os from 'os'; -import { execSync } from 'child_process'; import phpfmt from 'use-phpfmt'; import type { TransformationItem } from './types'; +import { exec } from './utils'; export class Transformations { - private readonly phpBin: string; - - public constructor(phpBin: string) { - this.phpBin = phpBin; - } + public constructor(private readonly phpBin: string) {} private get baseCmd(): string { return `${this.phpBin} "${phpfmt.pharPath}"`; } - public getTransformations(): TransformationItem[] { - const output = execSync(`${this.baseCmd} --list-simple`).toString(); - - return output - .trim() - .split(os.EOL) - .map(v => { - const splited = v.split(' '); - - return { - key: splited[0], - description: splited - .filter((value, index) => value && index > 0) - .join(' ') - .trim() - }; - }); + public async getTransformations(): Promise { + try { + const { stdout } = await exec(`${this.baseCmd} --list-simple`); + + return stdout + .trim() + .split(os.EOL) + .map(v => { + const splited = v.split(' '); + + return { + key: splited[0], + description: splited + .filter((value, index) => value && index > 0) + .join(' ') + .trim() + }; + }); + } catch (err) { + return []; + } } - public getExample(transformationItem: TransformationItem): string { - const output = execSync( - `${this.baseCmd} --help-pass ${transformationItem.key}` - ).toString(); - - return output.trim(); + public async getExample( + transformationItem: TransformationItem + ): Promise { + try { + const { stdout } = await exec( + `${this.baseCmd} --help-pass ${transformationItem.key}` + ); + + return stdout; + } catch (err) { + return ''; + } } } diff --git a/src/extension.ts b/src/extension.ts index 70b3657..65c123a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,6 @@ import { type ExtensionContext } from 'vscode'; import { PHPFmt } from './PHPFmt'; -import PHPFmtProvider from './PHPFmtProvider'; +import { PHPFmtProvider } from './PHPFmtProvider'; export function activate(context: ExtensionContext): void { const provider = new PHPFmtProvider(new PHPFmt()); diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..8222f56 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,4 @@ +import childProcess from 'child_process'; +import util from 'util'; + +export const exec = util.promisify(childProcess.exec); diff --git a/test/extension.test.ts b/test/extension.test.ts index 105cbee..930a77c 100644 --- a/test/extension.test.ts +++ b/test/extension.test.ts @@ -33,15 +33,15 @@ suite('PHPFmt Test', () => { assert.fail(); } - const filePath: string = path.join(Workspace.rootPath, 'ugly.php'); + const filePath = path.join(Workspace.rootPath, 'ugly.php'); return Workspace.openTextDocument(filePath).then(doc => { return Window.showTextDocument(doc).then(() => Commands.executeCommand('editor.action.formatDocument').then( () => { - const stdout: Buffer = execSync( + const stdout = execSync( `php "${phpfmt.pharPath}" --psr2 --indent_with_space=4 --dry-run -o=- ${filePath}` ); - const phpfmtFormatted: string = stdout.toString(); + const phpfmtFormatted = stdout.toString(); assert.equal(doc.getText(), phpfmtFormatted); }, err => { diff --git a/testProject/.vscode/settings.json b/testProject/.vscode/settings.json index b332177..571652b 100644 --- a/testProject/.vscode/settings.json +++ b/testProject/.vscode/settings.json @@ -1,5 +1,9 @@ { "extensions.supportUntrustedWorkspaces": true, "phpfmt.psr2": true, - "phpfmt.indent_with_space": 4 + "phpfmt.indent_with_space": 4, + "phpfmt.ignore": [ + ".ctp", + ".blade.php" + ] }