Skip to content

Commit 974e4c2

Browse files
authored
Merge pull request #2481 from codefori/fix/variables_in_ui
2 parents 79f5720 + e31ab07 commit 974e4c2

File tree

3 files changed

+210
-172
lines changed

3 files changed

+210
-172
lines changed

src/api/CompileTools.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ILELibrarySettings {
1010

1111
export namespace CompileTools {
1212
export const NEWLINE = `\r\n`;
13+
export const DID_NOT_RUN = -123;
1314

1415
function expandVariables(variables: Variable) {
1516
for (const key in variables) {
@@ -48,10 +49,15 @@ export namespace CompileTools {
4849
}
4950
}
5051

52+
interface RunCommandEvents {
53+
writeEvent?: (content: string) => void;
54+
commandConfirm?: (command: string) => Promise<string>;
55+
}
56+
5157
/**
5258
* Execute a command
5359
*/
54-
export async function runCommand(connection: IBMi, options: RemoteCommand, writeEvent?: (content: string) => void): Promise<CommandResult> {
60+
export async function runCommand(connection: IBMi, options: RemoteCommand, events: RunCommandEvents = {}): Promise<CommandResult> {
5561
const config = connection.getConfig();
5662
if (config && connection) {
5763
const cwd = options.cwd;
@@ -75,26 +81,30 @@ export namespace CompileTools {
7581
variables
7682
);
7783

84+
if (events.commandConfirm) {
85+
commandString = await events.commandConfirm(commandString);
86+
}
87+
7888
if (commandString) {
7989
const commands = commandString.split(`\n`).filter(command => command.trim().length > 0);
8090

81-
if (writeEvent) {
91+
if (events.writeEvent) {
8292
if (options.environment === `ile` && !options.noLibList) {
83-
writeEvent(`Current library: ` + ileSetup.currentLibrary + NEWLINE);
84-
writeEvent(`Library list: ` + ileSetup.libraryList.join(` `) + NEWLINE);
93+
events.writeEvent(`Current library: ` + ileSetup.currentLibrary + NEWLINE);
94+
events.writeEvent(`Library list: ` + ileSetup.libraryList.join(` `) + NEWLINE);
8595
}
8696
if (options.cwd) {
87-
writeEvent(`Working directory: ` + options.cwd + NEWLINE);
97+
events.writeEvent(`Working directory: ` + options.cwd + NEWLINE);
8898
}
89-
writeEvent(`Commands:\n${commands.map(command => `\t${command}\n`).join(``)}` + NEWLINE);
99+
events.writeEvent(`Commands:\n${commands.map(command => `\t${command}\n`).join(``)}` + NEWLINE);
90100
}
91101

92-
const callbacks: StandardIO = writeEvent ? {
102+
const callbacks: StandardIO = events.writeEvent ? {
93103
onStdout: (data) => {
94-
writeEvent(data.toString().replaceAll(`\n`, NEWLINE));
104+
events.writeEvent!(data.toString().replaceAll(`\n`, NEWLINE));
95105
},
96106
onStderr: (data) => {
97-
writeEvent(data.toString().replaceAll(`\n`, NEWLINE));
107+
events.writeEvent!(data.toString().replaceAll(`\n`, NEWLINE));
98108
}
99109
} : {};
100110

@@ -148,18 +158,19 @@ export namespace CompileTools {
148158

149159
commandResult.command = commandString;
150160
return commandResult;
161+
162+
} else {
163+
return {
164+
code: DID_NOT_RUN,
165+
command: options.command,
166+
stdout: ``,
167+
stderr: `Command execution failed. (No command)`,
168+
};
151169
}
152170
}
153171
else {
154172
throw new Error("Please connect to an IBM i");
155173
}
156-
157-
return {
158-
code: 1,
159-
command: options.command,
160-
stdout: ``,
161-
stderr: `Command execution failed. (Internal)`,
162-
};
163174
}
164175

165176
function buildLibraryList(config: ILELibrarySettings): string[] {

src/api/tests/suites/connection.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Tools } from '../../Tools';
44
import { CONNECTION_TIMEOUT, disposeConnection, newConnection } from '../connection';
55
import IBMi from '../../IBMi';
66
import { getJavaHome } from '../../configuration/DebugConfiguration';
7+
import { CompileTools } from '../../CompileTools';
78

89
describe(`connection tests`, {concurrent: true}, () => {
910
let connection: IBMi
@@ -218,6 +219,25 @@ describe(`connection tests`, {concurrent: true}, () => {
218219
expect(qtempIndex < qsysincIndex).toBeTruthy();
219220
});
220221

222+
it('runCommand (ILE, variable expansion)', async () => { const config = connection.getConfig();
223+
224+
const result = await CompileTools.runCommand(connection,
225+
{
226+
command: `CRTDTAARA DTAARA(&SCOOBY/TEST) TYPE(*CHAR) LEN(10)`,
227+
environment: `ile`,
228+
env: {'&SCOOBY': `QTEMP`},
229+
},
230+
{
231+
commandConfirm: async (command) => {
232+
expect(command).toBe(`CRTDTAARA DTAARA(QTEMP/TEST) TYPE(*CHAR) LEN(10)`);
233+
return command;
234+
}
235+
}
236+
);
237+
238+
expect(result?.code).toBe(0);
239+
});
240+
221241
it('withTempDirectory and countFiles', async () => { const content = connection.getContent()!;
222242
let temp;
223243

0 commit comments

Comments
 (0)