Skip to content

Commit

Permalink
Merge pull request #42 from IBM/feature/handle_metadata
Browse files Browse the repository at this point in the history
Feature/handle_metadata
  • Loading branch information
worksofliam authored Feb 2, 2024
2 parents 3f58deb + 9efc409 commit c611188
Show file tree
Hide file tree
Showing 18 changed files with 719 additions and 38 deletions.
102 changes: 102 additions & 0 deletions cli/src/builders/make/customRules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { warningOut } from "../../cli";
import { ObjectType, Targets } from "../../targets";
import { getFiles } from "../../utils";
import { readFileSync } from "fs";
import * as path from "path";
import { MakeProject } from ".";

/**
* Scan for all rules.mk files and read attributes and custom
* dependencies into the targets.
*/
export function readAllRules(targets: Targets, project: MakeProject) {
// getFiles is case insensitive
const rulesFiles = getFiles(targets.getCwd(), `**/rules.mk`);

const settings = project.getSettings();

for (const rulesFile of rulesFiles) {
const relative = targets.getRelative(rulesFile);

try {
const content = readFileSync(rulesFile, { encoding: `utf-8` });
const lines = content.split(`\n`);

for (const line of lines) {
if (line.trim().length === 0) continue; // Skip empty lines
if (line[0] === `#`) continue; // Skip comments
if (line[0] === ` ` || line[0] === `\t`) continue; // Skip lines that start with a space or tab

const nameSplit = line.indexOf(`:`);
if (nameSplit < 0) continue;

const name = line.substring(0, nameSplit).trim().toUpperCase();
const value = line.substring(nameSplit + 1).trim();

let assignmentSplit = -1;
let assignmentSplitLength = 0;

// Assignment can be with multiple operators
const assignmentOps = [`:=`, `=`];
for (const op of assignmentOps) {
assignmentSplit = value.indexOf(op);
if (assignmentSplit >= 0) {
assignmentSplitLength = op.length;
break;
}
}

if (assignmentSplit >= 0) {
// If there is an assignment value, this means we're
// setting a compile parameter on a specific object
const key = value.substring(0, assignmentSplit).trim().toLowerCase();
const val = value.substring(assignmentSplit + assignmentSplitLength).trim();

if (!settings.objectAttributes[name]) settings.objectAttributes[name] = {};

settings.objectAttributes[name][key] = val;

} else {
// Otherwise, we're overriding the deps with hardcoded deps

const nameParts = name.split(`.`);
const targetName = nameParts[0];
const targetType = nameParts[1] as ObjectType;

if (targetName && targetType) {
const currentTarget = targets.getTarget({systemName: targetName, type: targetType});

if (currentTarget) {
// We set this to empty since we're overriding them
currentTarget.deps = [];

const parts = value.split(` `).map(p => p.trim()).filter(p => p.length > 0);

// We always skip the first because ibmi-bob wants the first entry to be the source name.
for (let i = 1; i < parts.length; i++) {
const part = parts[i];
const partSplit = part.split(`.`);
const objName = partSplit[0];
const objType = partSplit[1] as ObjectType;

if (objName && objType) {
const obj = targets.searchForObject({systemName: objName, type: objType}, undefined);

if (obj) {
currentTarget.deps.push(obj);
} else {
warningOut(`make: Failed to find '${part}' in '${relative}'`);
}
}
}
}
}

}
}

} catch (e) {
warningOut(`make: Failed to read ${relative}.`);
}
}
}
35 changes: 35 additions & 0 deletions cli/src/builders/make/folderSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as path from "path";
import { getFiles } from "../../utils";
import { readFileSync } from "fs";
import { warningOut } from "../../cli";

export interface FolderOptions {
version?: "0.0.1",
build?: {
// objlib?: string, We don't support objlib
tgtCcsid?: string
}
}

/**
* Fetch the folder specific settings for a project
*/
export function getFolderOptions(cwd: string) {
// Then fetch the directory specific settings
const ibmiFiles = getFiles(cwd, `**/.ibmi.json`);

let folderSettings: { [key: string]: FolderOptions } = {};

for (const ibmiFile of ibmiFiles) {
const relative = path.relative(cwd, ibmiFile);
const folder = path.dirname(relative);

try {
folderSettings[folder] = JSON.parse(readFileSync(ibmiFile, { encoding: `utf-8` }));
} catch (e) {
warningOut(`make: Failed to read ${relative}.`);
}
}

return folderSettings;
}
Loading

0 comments on commit c611188

Please sign in to comment.