Skip to content

Commit

Permalink
Merge pull request #1111 from halcyon-tech/feature/env_library_setup
Browse files Browse the repository at this point in the history
`RunCommand` API to support custom library list and current library
  • Loading branch information
worksofliam authored Mar 25, 2023
2 parents 6a92e7f + d752bdf commit 32ff09a
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/api/CompileTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import { Tools } from './Tools';
import { parseFSOptions } from '../filesystems/qsys/QSysFs';
import { instance } from '../instantiate';

export interface ILELibrarySettings {
currentLibrary: string;
libraryList: string[];
}

export namespace CompileTools {
type Variables = Map<string, string>

Expand Down Expand Up @@ -167,21 +172,20 @@ export namespace CompileTools {
return string;
}

function getDefaultVariables(instance: Instance): Variables {
function getDefaultVariables(instance: Instance, librarySettings: ILELibrarySettings): Variables {
const variables: Variables = new Map;

const connection = instance.getConnection();
const config = instance.getConfig();
if (connection && config) {
variables.set(`&BUILDLIB`, config.currentLibrary);
variables.set(`&CURLIB`, config.currentLibrary);
variables.set(`\\*CURLIB`, config.currentLibrary);
variables.set(`&BUILDLIB`, librarySettings ? librarySettings.currentLibrary : config.currentLibrary);
variables.set(`&CURLIB`, librarySettings ? librarySettings.currentLibrary : config.currentLibrary);
variables.set(`\\*CURLIB`, librarySettings ? librarySettings.currentLibrary : config.currentLibrary);
variables.set(`&USERNAME`, connection.currentUser);
variables.set(`{usrprf}`, connection.currentUser);
variables.set(`&HOME`, config.homeDirectory);

const libraryList = buildLibraryList(config);
variables.set(`&LIBLC`, libraryList.join(`,`));
const libraryList = buildLibraryList(librarySettings);
variables.set(`&LIBLS`, libraryList.join(` `));

for (const variable of config.customVariables) {
Expand Down Expand Up @@ -517,9 +521,22 @@ export namespace CompileTools {
if (config && connection) {
const cwd = options.cwd;

let ileSetup: ILELibrarySettings = {
currentLibrary: config.currentLibrary,
libraryList: config.libraryList,
};

if (options.env) {
const libl: string|undefined = options.env[`&LIBL`];
const curlib: string|undefined = options.env[`&CURLIB`];

if (libl) ileSetup.libraryList = libl.split(` `);
if (curlib) ileSetup.currentLibrary = curlib;
}

let commandString = replaceValues(
options.command,
getDefaultVariables(instance)
getDefaultVariables(instance, ileSetup)
);

if (commandString.startsWith(`?`)) {
Expand All @@ -532,8 +549,8 @@ export namespace CompileTools {
const commands = commandString.split(`\n`).filter(command => command.trim().length > 0);

outputChannel.append(`\n\n`);
outputChannel.append(`Current library: ` + config.currentLibrary + `\n`);
outputChannel.append(`Library list: ` + config.libraryList.join(` `) + `\n`);
outputChannel.append(`Current library: ` + ileSetup.currentLibrary + `\n`);
outputChannel.append(`Library list: ` + ileSetup.libraryList.join(` `) + `\n`);
outputChannel.append(`Commands:\n${commands.map(command => `\t\t${command}\n`).join(``)}\n`);

const callbacks: StandardIO = {
Expand All @@ -552,7 +569,7 @@ export namespace CompileTools {
const paseVars: Variables = new Map;

// Get default variable
getDefaultVariables(instance).forEach((value: string, key: string) => {
getDefaultVariables(instance, ileSetup).forEach((value: string, key: string) => {
if ((/^[A-Za-z\&]/i).test(key)) {
paseVars.set(key.startsWith('&') ? key.substring(1) : key, value);
}
Expand All @@ -576,7 +593,7 @@ export namespace CompileTools {
case `qsh`:
commandResult = await connection.sendQsh({
command: [
...buildLiblistCommands(connection, config),
...buildLiblistCommands(connection, ileSetup),
...commands,
].join(` && `),
directory: cwd,
Expand All @@ -589,7 +606,7 @@ export namespace CompileTools {
// escape $ and # in commands
commandResult = await connection.sendQsh({
command: [
...buildLiblistCommands(connection, config),
...buildLiblistCommands(connection, ileSetup),
...commands.map(command =>
`${`system ${GlobalConfiguration.get(`logCompileOutput`) ? `` : `-s`} "${command.replace(/[$]/g, `\\$&`)}"; if [[ $? -ne 0 ]]; then exit 1; fi`}`
),
Expand Down Expand Up @@ -716,7 +733,7 @@ export namespace CompileTools {
}
}

function buildLibraryList(config: ConnectionConfiguration.Parameters): string[] {
function buildLibraryList(config: ILELibrarySettings): string[] {
//We have to reverse it because `liblist -a` adds the next item to the top always
return config.libraryList
.map(library => {
Expand All @@ -730,7 +747,7 @@ export namespace CompileTools {
}).reverse();
}

function buildLiblistCommands(connection: IBMi, config: ConnectionConfiguration.Parameters): string[] {
function buildLiblistCommands(connection: IBMi, config: ILELibrarySettings): string[] {
return [
`liblist -d ${connection.defaultUserLibraries.join(` `).replace(/\$/g, `\\$`)}`,
`liblist -c ${config.currentLibrary.replace(/\$/g, `\\$`)}`,
Expand Down

0 comments on commit 32ff09a

Please sign in to comment.