Skip to content

Commit

Permalink
perf: faster exec speed
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Apr 29, 2023
1 parent ff8146e commit 3e884ea
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 61 deletions.
7 changes: 5 additions & 2 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ void (async () => {
}
);

const transformations = await new Transformations(
'php'
).getTransformations();

readmeContent = readmeContent.replace(
/<!-- Transformations START -->([\s\S]*)<!-- Transformations END -->/,
() => {
Expand All @@ -69,8 +73,7 @@ void (async () => {
os.EOL +
'| -------- | ----------- |' +
os.EOL +
new Transformations('php')
.getTransformations()
transformations
.map(item => {
let row = `| ${item.key} | `;
row += item.description;
Expand Down
17 changes: 9 additions & 8 deletions src/PHPFmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand All @@ -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');
Expand All @@ -231,5 +234,3 @@ export class PHPFmt {
throw new PHPFmtIgnoreError();
}
}

export default PHPFmt;
30 changes: 13 additions & 17 deletions src/PHPFmtProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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) {
Expand All @@ -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();
}
});
}

Expand Down
64 changes: 35 additions & 29 deletions src/Transformations.ts
Original file line number Diff line number Diff line change
@@ -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<TransformationItem[]> {
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<string> {
try {
const { stdout } = await exec(
`${this.baseCmd} --help-pass ${transformationItem.key}`
);

return stdout;
} catch (err) {
return '';
}
}
}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import childProcess from 'child_process';
import util from 'util';

export const exec = util.promisify(childProcess.exec);
6 changes: 3 additions & 3 deletions test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
6 changes: 5 additions & 1 deletion testProject/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 3e884ea

Please sign in to comment.