chore: implemented kcl sample assembly unique sub dir creation

This commit is contained in:
Kevin Nadro
2025-05-20 09:50:01 -05:00
parent db82a54f27
commit 8ddbb488d6
3 changed files with 77 additions and 5 deletions

View File

@ -14,8 +14,13 @@ import { IS_ML_EXPERIMENTAL, PROJECT_ENTRYPOINT } from '@src/lib/constants'
import toast from 'react-hot-toast'
import { reportRejection } from '@src/lib/trap'
import { relevantFileExtensions } from '@src/lang/wasmUtils'
import { getStringAfterLastSeparator, webSafePathSplit } from '@src/lib/paths'
import {
getStringAfterLastSeparator,
joinOSPaths,
webSafePathSplit,
} from '@src/lib/paths'
import { FILE_EXT } from '@src/lib/constants'
import { getAllSubDirectoriesAtProjectRoot } from '@src/machines/systemIO/snapshotContext'
function onSubmitKCLSampleCreation({
sample,
@ -87,6 +92,26 @@ function onSubmitKCLSampleCreation({
},
})
} else {
/**
* When adding assemblies to an existing project create the assembly into a unique sub directory
*/
if (!isProjectNew) {
requestedFiles.forEach((requestedFile) => {
const subDirectoryName = projectPathPart
const firstLevelDirectories = getAllSubDirectoriesAtProjectRoot({
projectFolderName: requestedFile.requestedProjectName,
})
const uniqueSubDirectoryName = getUniqueProjectName(
subDirectoryName,
firstLevelDirectories
)
requestedFile.requestedProjectName = joinOSPaths(
requestedFile.requestedProjectName,
uniqueSubDirectoryName
)
})
}
/**
* Bulk create the assembly and navigate to the project
*/

View File

@ -1,4 +1,6 @@
import type { FileEntry } from '@src/lib/project'
import { systemIOActor } from '@src/lib/singletons'
import { isArray } from '@src/lib/utils'
export const folderSnapshot = () => {
const { folders } = systemIOActor.getSnapshot().context
@ -9,3 +11,52 @@ export const defaultProjectFolderNameSnapshot = () => {
const { defaultProjectFolderName } = systemIOActor.getSnapshot().context
return defaultProjectFolderName
}
// assumes /file/<encodedURIComponent>
// e.g '/file/%2Fhome%2Fkevin-nadro%2FDocuments%2Fzoo-modeling-app-projects%2Fbracket-1%2Fbracket.kcl'
/**
* From the application project directory go down to a project folder and list all the folders at that directory level
* application project directory: /home/documents/zoo-modeling-app-projects/
*
* /home/documents/zoo-modeling-app-projects/car-door/
* ├── handle
* ├── main.kcl
* └── window
*
* The two folders are handle and window
*
* @param {Object} params
* @param {string} params.projectFolderName - The name with no path information.
* @returns {FileEntry[]} An array of subdirectory names found at the root level of the specified project folder.
*/
export const getAllSubDirectoriesAtProjectRoot = ({
projectFolderName,
}: { projectFolderName: string }): FileEntry[] => {
const subDirectories: FileEntry[] = []
const { folders } = systemIOActor.getSnapshot().context
const projectFolder = folders.find((folder) => {
return folder.name === projectFolderName
})
console.log(projectFolder)
// Find the subdirectories
if (projectFolder) {
// 1st level
const children = projectFolder.children
console.log(children)
if (children) {
children.forEach((childFileOrDirectory) => {
// 2nd level
const secondLevelChild = childFileOrDirectory.children
// if secondLevelChild is null then it is a file
if (secondLevelChild && isArray(secondLevelChild)) {
// this is a directory!
subDirectories.push(childFileOrDirectory)
}
})
}
}
return subDirectories
}

View File

@ -73,10 +73,6 @@ const sharedBulkCreateWorkflow = async ({
baseDir,
}).name
console.log('why?')
console.log('override', input.override)
console.log('override', newProjectName)
console.log('override', fileName)
// Create the project around the file if newProject
await createNewProjectDirectory(
newProjectName,