Skip to content

Commit a90e6da

Browse files
committed
feat: add links and legend
1 parent 2c4ee1b commit a90e6da

File tree

7 files changed

+75
-34
lines changed

7 files changed

+75
-34
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/gqm_gen/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Goal, Question, Metrics Generator
2+
3+
Creates a Mermaid diagram from Goal, Question, Metrics (GQM) Markdown files.
4+
5+
## Usage
6+
7+
Builds the mermaid diagram and outputs it to stdout.
8+
9+
```bash
10+
npm install
11+
npm run --silent start
12+
```

scripts/gqm_gen/gqm.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

scripts/gqm_gen/index.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
getLinks,
77
getFileLinks,
88
generateMermaidDiagram,
9-
appendToGraph,
10-
getGQMFileLinks
9+
getGQMFileLinks,
1110
} from "./index";
1211

1312
test("can get links", () => {
@@ -34,11 +33,9 @@ test("can generate mermaid diagram", () => {
3433
});
3534

3635
test("can generate mermaid diagram from file", { only: true }, () => {
37-
38-
3936
const graph = getGQMFileLinks();
4037

4138
const diagram = generateMermaidDiagram(graph);
42-
console.log(diagram)
39+
console.log(diagram);
4340
assert(diagram.indexOf("graph TB") > 1);
4441
});

scripts/gqm_gen/index.ts

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import { FileLink } from "./types";
66

77
const graph = getGQMFileLinks();
88

9+
export function getLinkUrl(linkType: LinkType, file: string) {
10+
// https://github.com/InnerSourceCommons/managing-inner-source-projects/blob/main/measuring/goals/reduce-duplication.md
11+
const measuringUrl = "https://github.com/InnerSourceCommons/managing-inner-source-projects/blob/main/measuring/";
12+
const url = `${measuringUrl}/${linkType.toLowerCase()}s/${file}`
13+
return url;
14+
}
15+
916
export function getGQMFileLinks() {
1017
const graph: Graph = {
1118
nodes: [],
@@ -31,10 +38,11 @@ export function getGQMFileLinks() {
3138
return graph;
3239
}
3340

34-
export function appendToGraph(graph: Graph, goalFileLinks: FileLink[]) {
35-
goalFileLinks.forEach((fileLink) => {
41+
export function appendToGraph(graph: Graph, fileLinks: FileLink[]) {
42+
fileLinks.forEach((fileLink) => {
3643
const node: Node = {
3744
id: fileLink.file,
45+
type: fileLink.linkType,
3846
shape: NodeShape.RECT,
3947
label: fileLink.label,
4048
};
@@ -117,35 +125,64 @@ export function getLinks(parsed: Commonmark.Node) {
117125
}
118126

119127
export function getNodeShapeSyntax(node: Node) {
128+
const nodeUrl = getLinkUrl(node.type, node.id)
129+
const nodeLabel = `<a href='${nodeUrl}'>${node.label}</a>`;
120130
switch (node.shape) {
121131
case 'rect':
122-
return `[${node.label}]`;
132+
return `[${nodeLabel}]`;
123133
case 'circ':
124-
return `((${node.label}))`;
134+
return `((${nodeLabel}))`;
125135
case 'roundrect':
126-
return `((${node.label}))`;
136+
return `((${nodeLabel}))`;
127137
case 'diamond':
128-
return `{${node.label}}`;
138+
return `{${nodeLabel}}`;
129139
default:
130-
return `[${node.label}]`;
140+
return `[${nodeLabel}]`;
131141
}
132142
}
133143

134144
export function generateMermaidDiagram(graph: Graph) {
135145
const nodes = graph.nodes;
136146
const edges = graph.edges;
137147

138-
let mermaidSyntax = "```mermaid\ngraph TB\n";
139-
148+
let mermaidSyntax = `\`\`\`mermaid\n
149+
graph LR;\n
150+
subgraph GQM[Goals, Questions, Metrics]\n
151+
`;
152+
140153
nodes.forEach((node) => {
141-
const nodeSyntax = getNodeShapeSyntax(node);
142-
mermaidSyntax += `${node.id}${nodeSyntax}\n`;
154+
const nodeSyntax = getNodeShapeSyntax(node)
155+
mermaidSyntax += ` ${node.id}${nodeSyntax}\n`
143156
});
144157

145158
edges.forEach((edge) => {
146159
const arrowSyntax: string = ArrowType.ARROW;
147160
mermaidSyntax += `${edge.from}${arrowSyntax}${edge.to}\n`;
148161
});
162+
163+
const goalsList = nodes.filter(n => n.type == LinkType.GOAL).map(n => `${n.id}`).join(',');
164+
const questionsList = nodes.filter(n => n.type == LinkType.QUESTION).map(n => `${n.id}`).join(',');
165+
const metricsList = nodes.filter(n => n.type == LinkType.METRIC).map(n => `${n.id}`).join(',');
166+
167+
mermaidSyntax += " end";
168+
mermaidSyntax += `
169+
subgraph Legend
170+
direction TB
171+
172+
goal[Goal]
173+
question[Question]
174+
metric[Metric]
175+
176+
classDef goals stroke:green,stroke-width:2px;
177+
class goal,${goalsList} goals
178+
179+
classDef questions stroke:orange,stroke-width:2px;
180+
class question,${questionsList} questions
181+
182+
classDef metrics stroke:purple,stroke-width:2px;
183+
class metric,${metricsList} metrics
184+
end
185+
`;
149186
mermaidSyntax += "\n```";
150187
return mermaidSyntax;
151188
}

scripts/gqm_gen/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export type FileLink = {
2727
};
2828
export type Node = {
2929
id: string;
30+
type: LinkType;
3031
shape: string;
3132
label: string;
3233
};

scripts/package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)