Skip to content

Commit

Permalink
Merge pull request #74 from IBM/feature/bob_keep_attrs
Browse files Browse the repository at this point in the history
Feature/bob_keep_attrs
  • Loading branch information
worksofliam authored Jul 16, 2024
2 parents 73f1c9a + 1e71aa6 commit bec2862
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 5 deletions.
59 changes: 56 additions & 3 deletions cli/src/builders/bob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class BobProject {
return list;
}

public

public createRules(): OutFiles {
let output: OutFiles = {};
const subdirs = Object.keys(this.dirTargets);
Expand All @@ -34,15 +36,66 @@ export class BobProject {

for (const subdir in this.dirTargets) {
const targets = this.dirTargets[subdir];
let lines: string[] = [];
const currentRulesFile = path.join(subdir, `Rules.mk`);
const currentFullPath = path.join(this.targets.getCwd(), currentRulesFile);
let rulesContent: string[] = [];

if (existsSync(currentFullPath)) {
rulesContent = readFileSync(currentFullPath, { encoding: `utf-8` }).split(`\n`);
}

const rulesFile = new RulesFile(subdir, rulesContent);

for (let target of targets) {
lines.push(`${target.systemName}.${target.type}: ${path.relative(subdir, target.relativePath)} ${target.deps.filter(d => d.reference !== true).map(d => `${d.systemName}.${d.type}`).join(` `)}`);
rulesFile.applyRule(target);
}

output[path.join(subdir, `Rules.mk`)] = lines.join(`\n`);
output[currentRulesFile] = rulesFile.getContent();
}

return output;
}
}

interface Rule {
ogLine: string;
target?: String,
content?: String,
isUserWritten?: boolean,
};

class RulesFile {
private parsed: Rule[] = [];
constructor(private subdir: string, lines: string[]) {
for (let line of lines) {
let currentRule: Rule = { ogLine: line };
if (line.includes(`:`)) {
const [target, content] = line.split(`:`);
currentRule.target = target.trim().toUpperCase();
currentRule.content = content.trim();
currentRule.isUserWritten = content.includes(`=`) || content.trimStart().startsWith(`#`);
}

this.parsed.push(currentRule);
}
}

applyRule(target: ILEObjectTarget) {
const objName = `${target.systemName}.${target.type}`;

const existingLine = this.parsed.find(r => r.target === objName && r.isUserWritten !== true);

const lineContent = `${path.relative(this.subdir, target.relativePath)} ${target.deps.filter(d => d.reference !== true).map(d => `${d.systemName}.${d.type}`).join(` `)}`.trimEnd();

if (existingLine) {
existingLine.ogLine = `${objName}: ${lineContent}`;
existingLine.content = lineContent;
} else {
this.parsed.push({ ogLine: `${objName}: ${lineContent}`, target: objName, content: lineContent });
}
}

getContent() {
return this.parsed.map(r => r.ogLine).join(`\n`);
}
}
58 changes: 58 additions & 0 deletions cli/test/bobWithExistingFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { assert, beforeAll, describe, expect, test } from 'vitest';

import { Targets } from '../src/targets'
import path from 'path';
import { MakeProject } from '../src/builders/make';
import { getFiles } from '../src/utils';
import { setupFixture } from './fixtures/projects';
import { scanGlob } from '../src/extensions';
import { writeFileSync } from 'fs';
import { BobProject } from '../src';

const cwd = setupFixture(`pseudo`);

// This issue was occuring when you had two files with the same name, but different extensions.

let files = getFiles(cwd, scanGlob);

describe.skipIf(files.length === 0)(`bob Rules.mk tests`, () => {
const targets = new Targets(cwd);
targets.setSuggestions({ renames: true, includes: true })

beforeAll(async () => {
targets.loadObjectsFromPaths(files);
const parsePromises = files.map(f => targets.parseFile(f));
await Promise.all(parsePromises);

expect(targets.getTargets().length).toBeGreaterThan(0);
targets.resolveBinder();
});

test(`bob to not overwrite any pre-existing rules for targets`, () => {
const project = new BobProject(targets);

const files = project.createRules();

const baseRules = files[`Rules.mk`].split(`\n`);

expect(baseRules).toBeDefined();
expect(baseRules[0]).toContain(`SUBDIRS =`);
expect(baseRules[0]).toContain(`qobjs`);
expect(baseRules[0]).toContain(`qrpglesrc`);

const qobjRules = files[`qobjs/Rules.mk`].split(`\n`);

expect(qobjRules).toBeDefined();
expect(qobjRules).toContain(`MYTHING.DTAARA:text=Hello world`);
expect(qobjRules).toContain(`MSTDSP.FILE: mstdsp.dspf`);

const qrpglesrcRules = files[`qrpglesrc/Rules.mk`].split(`\n`);

expect(qrpglesrcRules).toBeDefined();
expect(qrpglesrcRules).toContain(`TESTER.PGM: tester.pgm.rpgle MYTHING.DTAARA`);
expect(qrpglesrcRules).toContain(`TESTER.PGM: text = My program`);
expect(qrpglesrcRules).toContain(`# Other assignment`);
expect(qrpglesrcRules).toContain(`TESTER.PGM: bnddir:=MYBND`);
expect(qrpglesrcRules).toContain(`OTHER.PGM: other.pgm.sqlrpgle MSTDSP.FILE`);
});
});
2 changes: 0 additions & 2 deletions cli/test/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ describe(`CL parser`, () => {
const cl = `CRTCLPGM PGM(MYLIB/MYCL) SRCFILE(MYLIB/QCLSRC) SRCMBR(MYCL)`;
const parsed = fromCl(cl);

console.log(parsed);
expect(parsed.command).toBe(`CRTCLPGM`);
expect(parsed.parameters).toMatchObject({
pgm: `MYLIB/MYCL`,
Expand All @@ -82,7 +81,6 @@ describe(`CL parser`, () => {
const cl = `CRTCLPGM`;
const parsed = fromCl(cl);

console.log(parsed);
expect(parsed.command).toBe(`CRTCLPGM`);
expect(parsed.parameters).toMatchObject({});
});
Expand Down

0 comments on commit bec2862

Please sign in to comment.