Skip to content

Commit 193dc24

Browse files
committed
Moved logic to parseScreenTree and use resolveMacros csweblib function
1 parent 57aa4aa commit 193dc24

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

src/utils/parser.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TreeViewBaseItem } from "@mui/x-tree-view";
22
import { FileIDs, FileMetadata, MacroMap, Macros } from "../store";
33
import {
44
CsWebLibHttpResponseError,
5+
resolveMacros,
56
httpRequest
67
} from "@diamondlightsource/cs-web-lib";
78

@@ -87,6 +88,19 @@ export async function parseScreenTree(
8788
// Now we have the initial fileMap with unique file entries, update fileMap with macros for duplicate files.
8889
RecursiveAppendDuplicateFileMacros(json.children, fileMap);
8990

91+
// Substitute macros in fileMap
92+
Object.keys(fileMap).forEach(key => {
93+
const entry = fileMap[key];
94+
95+
if (entry.macros && entry.macros.length > 0 && entry.file) {
96+
// Use the first macro set
97+
const macros = entry.macros[0];
98+
99+
// Use resolveMacros
100+
entry.file = resolveMacros(entry.file, macros);
101+
}
102+
});
103+
90104
const parentScreen: TreeViewBaseItem = {
91105
id: guid,
92106
label: topLevelScreen,
@@ -176,24 +190,14 @@ export const RecursiveAppendDuplicateFileMacros = (
176190
}
177191
for (const sibling of jsonSiblings) {
178192
if (sibling.macros) {
179-
// TEMP?
180-
addMColon(sibling.macros);
181-
182-
// Substitute macros into the filename
183-
const filename =
184-
sibling.file && sibling.macros
185-
? substituteMacrosIntoFilename(sibling.file, sibling.macros)
186-
: sibling.file;
187193

188194
// Match against unresolved filename
189195
const matchingFileKey = Object.keys(fileMap).find(
190196
key => fileMap[key].file === sibling.file
191197
);
192198
if (matchingFileKey) {
193-
// Replace with resolved filename
194-
fileMap[matchingFileKey].file = filename;
195199

196-
if (sibling.duplicate) {
200+
if (matchingFileKey && sibling.duplicate) {
197201
// Attach macros
198202
fileMap[matchingFileKey].macros = fileMap[matchingFileKey].macros
199203
? [...fileMap[matchingFileKey].macros, sibling.macros]
@@ -229,23 +233,4 @@ export const buildUrlId = (
229233
}
230234
const urlId = `${idPrefix}${idPrefix === "" ? "" : "+"}${fileLabel}`;
231235
return { urlId, fileLabel };
232-
};
233-
234-
// TEMP? add colon to M macro
235-
const addMColon = (macros: any) => {
236-
if (macros.M && !macros.M.startsWith(":")) {
237-
macros.M = ":" + macros.M;
238-
}
239-
};
240-
241-
// Substitute macros into a filename
242-
const substituteMacrosIntoFilename = (
243-
filename: string,
244-
macros: any
245-
): string => {
246-
let resolvedFilename = filename;
247-
Object.entries(macros).forEach(([key, value]) => {
248-
resolvedFilename = resolvedFilename.replace(`$(${key})`, value as string);
249-
});
250-
return resolvedFilename;
251-
};
236+
};

0 commit comments

Comments
 (0)