From 155f4c81269c7168042b201105cd1b5b63c71d45 Mon Sep 17 00:00:00 2001 From: Weaver Goldman Date: Wed, 7 Aug 2024 17:13:06 -0400 Subject: [PATCH] Automatically convert all relative paths to absolute paths. --- src/renderer/src/interfaces/options.tsx | 29 +++++++++++++++++++ .../BackprojectPage/BackprojectPage.tsx | 29 ++++++++++++++----- .../src/routes/SlicesPage/SlicesPage.tsx | 27 +++++++++++++---- 3 files changed, 72 insertions(+), 13 deletions(-) diff --git a/src/renderer/src/interfaces/options.tsx b/src/renderer/src/interfaces/options.tsx index b27c9ea..7576ca1 100644 --- a/src/renderer/src/interfaces/options.tsx +++ b/src/renderer/src/interfaces/options.tsx @@ -209,3 +209,32 @@ export class BackprojectOptionsFile extends CompoundEntry { this.setValue(values) } } + +/** + * Finds all paths to a given type in a compound entry. + * + * @param entry The entry to search + * @param targetType The type to search for + * @returns An array of paths to the target type + */ +export function findPathsToType( + entry: CompoundEntry | Entry, + targetType: EntryValueType +): string[][] { + const queue: { node: CompoundEntry | Entry; path: string[] }[] = [{ node: entry, path: [] }] + const paths: string[][] = [] + + while (queue.length > 0) { + const { node, path } = queue.shift()! + + if (node instanceof Entry && node.type === targetType) { + paths.push(path) + } else if (node instanceof CompoundEntry) { + for (const entry of node.getEntries()) { + queue.push({ node: entry, path: [...path, entry.name] }) + } + } + } + + return paths +} diff --git a/src/renderer/src/routes/BackprojectPage/BackprojectPage.tsx b/src/renderer/src/routes/BackprojectPage/BackprojectPage.tsx index 8e94bdc..318a237 100644 --- a/src/renderer/src/routes/BackprojectPage/BackprojectPage.tsx +++ b/src/renderer/src/routes/BackprojectPage/BackprojectPage.tsx @@ -7,7 +7,8 @@ import { CompoundEntry, Entry, BackprojectOptionsFile, - CompoundValueType + CompoundValueType, + findPathsToType } from '@renderer/interfaces/options' import { useContext, useEffect, useState } from 'react' import { DirectoryContext } from '@renderer/contexts/DirectoryContext' @@ -110,13 +111,27 @@ function useBackprojectPageState(): BackprojectPageState { const optionsObject = entries[0].toObject() - // Convert relative paths to absolute paths if necessary - const outputFolder = optionsObject['output_file_folder'].startsWith('.') - ? await join(directoryPath, optionsObject['output_file_folder']) - : optionsObject['output_file_folder'] + const pathsToFilePathType = findPathsToType(entries[0], 'filePath') - // Add the absolute output folder to the options object - optionsObject['output_file_folder'] = outputFolder + // Make all file paths absolute + for (const path of pathsToFilePathType) { + let current = optionsObject + + // Traverse the object to find the entry with the path + for (let i = 0; i < path.length - 1; i++) current = current[path[i]] + + const name = path[path.length - 1] + + // Convert relative paths to absolute paths if necessary + const filePathValue = current[name].startsWith('.') + ? await join(directoryPath, current[name]) + : current[name] + + // Add the absolute path to the options object + current[name] = filePathValue + } + + const outputFolder = optionsObject['output_file_folder'] const outputName = optionsObject['output_file_name'] diff --git a/src/renderer/src/routes/SlicesPage/SlicesPage.tsx b/src/renderer/src/routes/SlicesPage/SlicesPage.tsx index 6d3c01a..654afb2 100644 --- a/src/renderer/src/routes/SlicesPage/SlicesPage.tsx +++ b/src/renderer/src/routes/SlicesPage/SlicesPage.tsx @@ -7,6 +7,7 @@ import { CompoundEntry, CompoundValueType, Entry, + findPathsToType, SliceOptionsFile } from '@renderer/interfaces/options' import { useContext, useEffect, useState } from 'react' @@ -232,13 +233,27 @@ function useSlicePageState(): SlicePageState { const optionsObject = entries[0].toObject() - // Convert relative paths to absolute paths if necessary - const outputFolder = optionsObject['output_file_folder'].startsWith('.') - ? await join(directoryPath, optionsObject['output_file_folder']) - : optionsObject['output_file_folder'] + const pathsToFilePathType = findPathsToType(entries[0], 'filePath') - // Add the absolute output folder to the options object - optionsObject['output_file_folder'] = outputFolder + // Make all file paths absolute + for (const path of pathsToFilePathType) { + let current = optionsObject + + // Traverse the object to find the entry with the path + for (let i = 0; i < path.length - 1; i++) current = current[path[i]] + + const name = path[path.length - 1] + + // Convert relative paths to absolute paths if necessary + const filePathValue = current[name].startsWith('.') + ? await join(directoryPath, current[name]) + : current[name] + + // Add the absolute path to the options object + current[name] = filePathValue + } + + const outputFolder = optionsObject['output_file_folder'] const outputName = optionsObject['output_file_name']