Skip to content

Commit

Permalink
Automatically convert all relative paths to absolute paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
We-Gold committed Aug 7, 2024
1 parent b73063a commit 155f4c8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
29 changes: 29 additions & 0 deletions src/renderer/src/interfaces/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
29 changes: 22 additions & 7 deletions src/renderer/src/routes/BackprojectPage/BackprojectPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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']

Expand Down
27 changes: 21 additions & 6 deletions src/renderer/src/routes/SlicesPage/SlicesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CompoundEntry,
CompoundValueType,
Entry,
findPathsToType,
SliceOptionsFile
} from '@renderer/interfaces/options'
import { useContext, useEffect, useState } from 'react'
Expand Down Expand Up @@ -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']

Expand Down

0 comments on commit 155f4c8

Please sign in to comment.