Skip to content

Commit

Permalink
Merge pull request #2481 from codefori/fix/variables_in_ui
Browse files Browse the repository at this point in the history
  • Loading branch information
worksofliam authored Jan 26, 2025
2 parents 79f5720 + e31ab07 commit 974e4c2
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 172 deletions.
43 changes: 27 additions & 16 deletions src/api/CompileTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ILELibrarySettings {

export namespace CompileTools {
export const NEWLINE = `\r\n`;
export const DID_NOT_RUN = -123;

function expandVariables(variables: Variable) {
for (const key in variables) {
Expand Down Expand Up @@ -48,10 +49,15 @@ export namespace CompileTools {
}
}

interface RunCommandEvents {
writeEvent?: (content: string) => void;
commandConfirm?: (command: string) => Promise<string>;
}

/**
* Execute a command
*/
export async function runCommand(connection: IBMi, options: RemoteCommand, writeEvent?: (content: string) => void): Promise<CommandResult> {
export async function runCommand(connection: IBMi, options: RemoteCommand, events: RunCommandEvents = {}): Promise<CommandResult> {
const config = connection.getConfig();
if (config && connection) {
const cwd = options.cwd;
Expand All @@ -75,26 +81,30 @@ export namespace CompileTools {
variables
);

if (events.commandConfirm) {
commandString = await events.commandConfirm(commandString);
}

if (commandString) {
const commands = commandString.split(`\n`).filter(command => command.trim().length > 0);

if (writeEvent) {
if (events.writeEvent) {
if (options.environment === `ile` && !options.noLibList) {
writeEvent(`Current library: ` + ileSetup.currentLibrary + NEWLINE);
writeEvent(`Library list: ` + ileSetup.libraryList.join(` `) + NEWLINE);
events.writeEvent(`Current library: ` + ileSetup.currentLibrary + NEWLINE);
events.writeEvent(`Library list: ` + ileSetup.libraryList.join(` `) + NEWLINE);
}
if (options.cwd) {
writeEvent(`Working directory: ` + options.cwd + NEWLINE);
events.writeEvent(`Working directory: ` + options.cwd + NEWLINE);
}
writeEvent(`Commands:\n${commands.map(command => `\t${command}\n`).join(``)}` + NEWLINE);
events.writeEvent(`Commands:\n${commands.map(command => `\t${command}\n`).join(``)}` + NEWLINE);
}

const callbacks: StandardIO = writeEvent ? {
const callbacks: StandardIO = events.writeEvent ? {
onStdout: (data) => {
writeEvent(data.toString().replaceAll(`\n`, NEWLINE));
events.writeEvent!(data.toString().replaceAll(`\n`, NEWLINE));
},
onStderr: (data) => {
writeEvent(data.toString().replaceAll(`\n`, NEWLINE));
events.writeEvent!(data.toString().replaceAll(`\n`, NEWLINE));
}
} : {};

Expand Down Expand Up @@ -148,18 +158,19 @@ export namespace CompileTools {

commandResult.command = commandString;
return commandResult;

} else {
return {
code: DID_NOT_RUN,
command: options.command,
stdout: ``,
stderr: `Command execution failed. (No command)`,
};
}
}
else {
throw new Error("Please connect to an IBM i");
}

return {
code: 1,
command: options.command,
stdout: ``,
stderr: `Command execution failed. (Internal)`,
};
}

function buildLibraryList(config: ILELibrarySettings): string[] {
Expand Down
20 changes: 20 additions & 0 deletions src/api/tests/suites/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Tools } from '../../Tools';
import { CONNECTION_TIMEOUT, disposeConnection, newConnection } from '../connection';
import IBMi from '../../IBMi';
import { getJavaHome } from '../../configuration/DebugConfiguration';
import { CompileTools } from '../../CompileTools';

describe(`connection tests`, {concurrent: true}, () => {
let connection: IBMi
Expand Down Expand Up @@ -218,6 +219,25 @@ describe(`connection tests`, {concurrent: true}, () => {
expect(qtempIndex < qsysincIndex).toBeTruthy();
});

it('runCommand (ILE, variable expansion)', async () => { const config = connection.getConfig();

const result = await CompileTools.runCommand(connection,
{
command: `CRTDTAARA DTAARA(&SCOOBY/TEST) TYPE(*CHAR) LEN(10)`,
environment: `ile`,
env: {'&SCOOBY': `QTEMP`},
},
{
commandConfirm: async (command) => {
expect(command).toBe(`CRTDTAARA DTAARA(QTEMP/TEST) TYPE(*CHAR) LEN(10)`);
return command;
}
}
);

expect(result?.code).toBe(0);
});

it('withTempDirectory and countFiles', async () => { const content = connection.getContent()!;
let temp;

Expand Down
Loading

0 comments on commit 974e4c2

Please sign in to comment.