Skip to content

Commit

Permalink
Compiler GUI (mark-wiemer-org#251)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Wiemer <7833360+mark-wiemer@users.noreply.github.com>
  • Loading branch information
kyklish and mark-wiemer committed Oct 15, 2022
1 parent ba98bee commit 6dc1db6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 13 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"category": "AHK++",
"icon": "./image/build.svg"
},
{
"command": "ahk++.compilerGui",
"title": "Compile AHK Script (GUI)",
"category": "AHK++"
},
{
"command": "ahk++.debug",
"title": "Debug AHK Script",
Expand Down Expand Up @@ -271,6 +276,11 @@
"when": "editorLangId == ahk",
"group": "navigation@1"
},
{
"command": "ahk++.compilerGui",
"when": "editorLangId == ahk",
"group": "navigation@1"
},
{
"command": "ahk++.run",
"when": "editorLangId == ahk",
Expand Down
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export function activate(context: vscode.ExtensionContext) {
),
TemplateService.createEditorListener(),
vscode.commands.registerCommand('ahk++.compile', () =>
RunnerService.compile(),
RunnerService.compile(false),
),
vscode.commands.registerCommand('ahk++.compilerGui', () =>
RunnerService.compile(true),
),
vscode.commands.registerCommand('ahk++.debug', () =>
RunnerService.startDebugger(),
Expand Down
52 changes: 40 additions & 12 deletions src/service/runnerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as vscode from 'vscode';
import { FileManager, FileModel } from '../common/fileManager';
import { ConfigKey, Global } from '../common/global';
import { Process } from '../common/processWrapper';
import * as fs from 'fs'; // In NodeJS: 'const fs = require('fs')'

export class RunnerService {
/** Runs the editor selection as a standalone script. */
Expand Down Expand Up @@ -49,28 +50,55 @@ export class RunnerService {
});
}

/**
* Build compile command
* @param compilePath Compiler path
* @param scriptPath Script path
* @param showGui Flag to show compiler GUI
* @returns Compile command
*/
public static compileCommand(
compilePath: string,
scriptPath: string,
showGui: boolean,
) {
if (!compilePath || !scriptPath) {
return '';
}
const pos = scriptPath.lastIndexOf('.');
const exePath =
scriptPath.substring(0, pos < 0 ? scriptPath.length : pos) + '.exe';
const guiKey = showGui ? ' /gui' : '';
const compileCommand = `"${compilePath}"${guiKey} /in "${scriptPath}" /out "${exePath}"`;
return compileCommand;
}

/**
* Compiles current script
*/
public static async compile() {
public static async compile(showGui: boolean) {
const currentPath = vscode.window.activeTextEditor.document.uri.fsPath;
if (!currentPath) {
if (!fs.existsSync(currentPath)) {
vscode.window.showErrorMessage('Cannot compile never-saved files.');
return;
}
this.checkAndSaveActive();
const pos = currentPath.lastIndexOf('.');
const compilePath =
currentPath.substr(0, pos < 0 ? currentPath.length : pos) + '.exe';
const command = this.compileCommand(
Global.getConfig(ConfigKey.compilePath),
currentPath,
showGui,
);
if (!command) {
vscode.window.showErrorMessage('Cannot build compile command.');
return;
}
if (
await Process.exec(
`"${Global.getConfig(
ConfigKey.compilePath,
)}" /in "${currentPath}" /out "${compilePath}"`,
{ cwd: `${res(currentPath, '..')}` },
)
(await Process.exec(command, {
cwd: `${res(currentPath, '..')}`,
})) &&
!showGui
) {
vscode.window.showInformationMessage('compile success!');
vscode.window.showInformationMessage('Compile success!');
}
}

Expand Down
56 changes: 56 additions & 0 deletions src/test/suite/service/runnerService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as assert from 'assert';
import { RunnerService } from '../../../service/runnerService';

suite('RunnerService', () => {
suite('compileCommand', () => {
const compilePath = 'C:/Ahk2Exe.exe';
const scriptPath = 'C:/Untitled.ahk';
const exePath = 'C:/Untitled.exe';
// List of test data
let dataList = [
// {
// cp: , // compiler path
// sp: , // script path
// sg: , // show GUI
// rs: , // expected result
// },
{
cp: compilePath,
sp: scriptPath,
sg: false,
rs: `"${compilePath}" /in "${scriptPath}" /out "${exePath}"`,
},
{
cp: compilePath,
sp: scriptPath,
sg: true,
rs: `"${compilePath}" /gui /in "${scriptPath}" /out "${exePath}"`,
},
{
cp: '',
sp: '',
sg: false,
rs: '',
},
];
dataList.forEach((data) => {
test(
'compilePath: "' +
data.cp +
'" scriptPath: "' +
data.sp +
'" showGui: ' +
data.sg.toString() +
" => '" +
data.rs +
"'",
() => {
assert.strictEqual(
RunnerService.compileCommand(data.cp, data.sp, data.sg),
data.rs,
);
},
);
});
});
});

0 comments on commit 6dc1db6

Please sign in to comment.