Skip to content

Commit bec2862

Browse files
authored
Merge pull request #74 from IBM/feature/bob_keep_attrs
Feature/bob_keep_attrs
2 parents 73f1c9a + 1e71aa6 commit bec2862

File tree

3 files changed

+114
-5
lines changed

3 files changed

+114
-5
lines changed

cli/src/builders/bob.ts

+56-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export class BobProject {
2626
return list;
2727
}
2828

29+
public
30+
2931
public createRules(): OutFiles {
3032
let output: OutFiles = {};
3133
const subdirs = Object.keys(this.dirTargets);
@@ -34,15 +36,66 @@ export class BobProject {
3436

3537
for (const subdir in this.dirTargets) {
3638
const targets = this.dirTargets[subdir];
37-
let lines: string[] = [];
39+
const currentRulesFile = path.join(subdir, `Rules.mk`);
40+
const currentFullPath = path.join(this.targets.getCwd(), currentRulesFile);
41+
let rulesContent: string[] = [];
42+
43+
if (existsSync(currentFullPath)) {
44+
rulesContent = readFileSync(currentFullPath, { encoding: `utf-8` }).split(`\n`);
45+
}
46+
47+
const rulesFile = new RulesFile(subdir, rulesContent);
3848

3949
for (let target of targets) {
40-
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(` `)}`);
50+
rulesFile.applyRule(target);
4151
}
4252

43-
output[path.join(subdir, `Rules.mk`)] = lines.join(`\n`);
53+
output[currentRulesFile] = rulesFile.getContent();
4454
}
4555

4656
return output;
4757
}
58+
}
59+
60+
interface Rule {
61+
ogLine: string;
62+
target?: String,
63+
content?: String,
64+
isUserWritten?: boolean,
65+
};
66+
67+
class RulesFile {
68+
private parsed: Rule[] = [];
69+
constructor(private subdir: string, lines: string[]) {
70+
for (let line of lines) {
71+
let currentRule: Rule = { ogLine: line };
72+
if (line.includes(`:`)) {
73+
const [target, content] = line.split(`:`);
74+
currentRule.target = target.trim().toUpperCase();
75+
currentRule.content = content.trim();
76+
currentRule.isUserWritten = content.includes(`=`) || content.trimStart().startsWith(`#`);
77+
}
78+
79+
this.parsed.push(currentRule);
80+
}
81+
}
82+
83+
applyRule(target: ILEObjectTarget) {
84+
const objName = `${target.systemName}.${target.type}`;
85+
86+
const existingLine = this.parsed.find(r => r.target === objName && r.isUserWritten !== true);
87+
88+
const lineContent = `${path.relative(this.subdir, target.relativePath)} ${target.deps.filter(d => d.reference !== true).map(d => `${d.systemName}.${d.type}`).join(` `)}`.trimEnd();
89+
90+
if (existingLine) {
91+
existingLine.ogLine = `${objName}: ${lineContent}`;
92+
existingLine.content = lineContent;
93+
} else {
94+
this.parsed.push({ ogLine: `${objName}: ${lineContent}`, target: objName, content: lineContent });
95+
}
96+
}
97+
98+
getContent() {
99+
return this.parsed.map(r => r.ogLine).join(`\n`);
100+
}
48101
}

cli/test/bobWithExistingFiles.test.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { assert, beforeAll, describe, expect, test } from 'vitest';
2+
3+
import { Targets } from '../src/targets'
4+
import path from 'path';
5+
import { MakeProject } from '../src/builders/make';
6+
import { getFiles } from '../src/utils';
7+
import { setupFixture } from './fixtures/projects';
8+
import { scanGlob } from '../src/extensions';
9+
import { writeFileSync } from 'fs';
10+
import { BobProject } from '../src';
11+
12+
const cwd = setupFixture(`pseudo`);
13+
14+
// This issue was occuring when you had two files with the same name, but different extensions.
15+
16+
let files = getFiles(cwd, scanGlob);
17+
18+
describe.skipIf(files.length === 0)(`bob Rules.mk tests`, () => {
19+
const targets = new Targets(cwd);
20+
targets.setSuggestions({ renames: true, includes: true })
21+
22+
beforeAll(async () => {
23+
targets.loadObjectsFromPaths(files);
24+
const parsePromises = files.map(f => targets.parseFile(f));
25+
await Promise.all(parsePromises);
26+
27+
expect(targets.getTargets().length).toBeGreaterThan(0);
28+
targets.resolveBinder();
29+
});
30+
31+
test(`bob to not overwrite any pre-existing rules for targets`, () => {
32+
const project = new BobProject(targets);
33+
34+
const files = project.createRules();
35+
36+
const baseRules = files[`Rules.mk`].split(`\n`);
37+
38+
expect(baseRules).toBeDefined();
39+
expect(baseRules[0]).toContain(`SUBDIRS =`);
40+
expect(baseRules[0]).toContain(`qobjs`);
41+
expect(baseRules[0]).toContain(`qrpglesrc`);
42+
43+
const qobjRules = files[`qobjs/Rules.mk`].split(`\n`);
44+
45+
expect(qobjRules).toBeDefined();
46+
expect(qobjRules).toContain(`MYTHING.DTAARA:text=Hello world`);
47+
expect(qobjRules).toContain(`MSTDSP.FILE: mstdsp.dspf`);
48+
49+
const qrpglesrcRules = files[`qrpglesrc/Rules.mk`].split(`\n`);
50+
51+
expect(qrpglesrcRules).toBeDefined();
52+
expect(qrpglesrcRules).toContain(`TESTER.PGM: tester.pgm.rpgle MYTHING.DTAARA`);
53+
expect(qrpglesrcRules).toContain(`TESTER.PGM: text = My program`);
54+
expect(qrpglesrcRules).toContain(`# Other assignment`);
55+
expect(qrpglesrcRules).toContain(`TESTER.PGM: bnddir:=MYBND`);
56+
expect(qrpglesrcRules).toContain(`OTHER.PGM: other.pgm.sqlrpgle MSTDSP.FILE`);
57+
});
58+
});

cli/test/environment.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ describe(`CL parser`, () => {
6969
const cl = `CRTCLPGM PGM(MYLIB/MYCL) SRCFILE(MYLIB/QCLSRC) SRCMBR(MYCL)`;
7070
const parsed = fromCl(cl);
7171

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

85-
console.log(parsed);
8684
expect(parsed.command).toBe(`CRTCLPGM`);
8785
expect(parsed.parameters).toMatchObject({});
8886
});

0 commit comments

Comments
 (0)