This commit is contained in:
49lf
2024-07-31 15:31:55 -04:00
parent 53467ee954
commit 638217311b
17 changed files with 298 additions and 218 deletions

View File

@ -13,15 +13,13 @@ import {
parseAppSettings,
parseProjectSettings,
} from 'lang/wasm'
export {
parseProjectRoute,
} from 'lang/wasm'
export { parseProjectRoute } from 'lang/wasm'
const DEFAULT_HOST = 'https://api.zoo.dev'
const SETTINGS_FILE_NAME = 'settings.toml'
const PROJECT_SETTINGS_FILE_NAME = 'project.toml'
const PROJECT_FOLDER = 'zoo-modeling-app-projects'
const DEFAULT_PROJECT_KCL_FILE = "main.kcl"
const DEFAULT_PROJECT_KCL_FILE = 'main.kcl'
// List machines on the local network.
export async function listMachines(): Promise<{
@ -44,20 +42,28 @@ export async function renameProjectDirectory(
return Promise.reject(new Error(`New name for project cannot be empty`))
}
try { await window.electron.stat(projectPath) }
catch (e) {
try {
await window.electron.stat(projectPath)
} catch (e) {
if (e === 'ENOENT') {
return Promise.reject(new Error(`Path ${projectPath} is not a directory`))
return Promise.reject(new Error(`Path ${projectPath} is not a directory`))
}
}
// Make sure the new name does not exist.
const newPath = window.electron.path.join(projectPath.split('/').slice(0, -1).join('/'), newName)
const newPath = window.electron.path.join(
projectPath.split('/').slice(0, -1).join('/'),
newName
)
try {
await window.electron.stat(newPath)
// If we get here it means the stat succeeded and there's a file already
// with the same name...
return Promise.reject(new Error(`Path ${newPath} already exists, cannot rename to an existing path`))
return Promise.reject(
new Error(
`Path ${newPath} already exists, cannot rename to an existing path`
)
)
} catch (e) {
// Otherwise if it failed and the failure is "it doesnt exist" then rename it!
if (e === 'ENOENT') {
@ -143,11 +149,15 @@ export async function listProjects(
const projectPath = window.electron.path.join(projectDir, entry)
// if it's not a directory ignore.
const isDirectory = await window.electron.statIsDirectory(projectPath)
if (!isDirectory) { continue }
if (!isDirectory) {
continue
}
const project = await getProjectInfo(projectPath)
// Needs at least one file to be added to the projects list
if (project.kcl_file_count === 0) { continue }
if (project.kcl_file_count === 0) {
continue
}
projects.push(project)
}
return projects
@ -155,90 +165,104 @@ export async function listProjects(
const IMPORT_FILE_EXTENSIONS = [
// TODO Use ImportFormat enum
"stp", "glb", "fbxb", "kcl"
'stp',
'glb',
'fbxb',
'kcl',
]
const isRelevantFile = (filename: string): boolean => IMPORT_FILE_EXTENSIONS.some(
(ext) => filename.endsWith('.' + ext))
const isRelevantFile = (filename: string): boolean =>
IMPORT_FILE_EXTENSIONS.some((ext) => filename.endsWith('.' + ext))
const collectAllFilesRecursiveFrom = async (path: string) => {
// Make sure the filesystem object exists.
try { await window.electron.stat(path) }
catch (e) {
if (e === 'ENOENT') {
return Promise.reject(new Error(`Directory ${path} does not exist`))
// Make sure the filesystem object exists.
try {
await window.electron.stat(path)
} catch (e) {
if (e === 'ENOENT') {
return Promise.reject(new Error(`Directory ${path} does not exist`))
}
}
// Make sure the path is a directory.
const isPathDir = await window.electron.statIsDirectory(path)
if (!isPathDir) {
return Promise.reject(new Error(`Path ${path} is not a directory`))
}
const pathParts = path.split('/')
let entry = /* FileEntry */ {
name: pathParts.slice(-1)[0],
path,
children: [],
}
const children = []
const entries = await window.electron.readdir(path)
for (let e of entries) {
// ignore hidden files and directories (starting with a dot)
if (e.indexOf('.') === 0) {
continue
}
const ePath = window.electron.path.join(path, e)
const isEDir = await window.electron.statIsDirectory(ePath)
if (isEDir) {
const subChildren = await collectAllFilesRecursiveFrom(ePath)
children.push(subChildren)
} else {
if (!isRelevantFile(ePath)) {
continue
}
}
// Make sure the path is a directory.
const isPathDir = await window.electron.statIsDirectory(path)
if (!isPathDir) {
return Promise.reject(new Error(`Path ${path} is not a directory`))
}
const pathParts = path.split('/')
let entry = /* FileEntry */ {
name: pathParts.slice(-1)[0],
path,
children: [],
}
const children = []
const entries = await window.electron.readdir(path)
for (let e of entries) {
// ignore hidden files and directories (starting with a dot)
if (e.indexOf('.') === 0) { continue }
const ePath = window.electron.path.join(path, e)
const isEDir = await window.electron.statIsDirectory(ePath)
if (isEDir) {
const subChildren = await collectAllFilesRecursiveFrom(ePath)
children.push(subChildren)
} else {
if (!isRelevantFile(ePath)) { continue }
children.push(/* FileEntry */ {
name: e,
path: ePath,
children: undefined,
})
children.push(
/* FileEntry */ {
name: e,
path: ePath,
children: undefined,
}
)
}
}
// We don't set this to none if there are no children, because it's a directory.
entry.children = children
// We don't set this to none if there are no children, because it's a directory.
entry.children = children
return entry
return entry
}
const getDefaultKclFileForDir = async (projectDir, file) => {
// Make sure the dir is a directory.
const isFileEntryDir = await window.electron.statIsDirectory(projectDir)
if (!isFileEntryDir) {
return Promise.reject(new Error(`Path ${projectDir} is not a directory`))
}
// Make sure the dir is a directory.
const isFileEntryDir = await window.electron.statIsDirectory(projectDir)
if (!isFileEntryDir) {
return Promise.reject(new Error(`Path ${projectDir} is not a directory`))
}
let defaultFilePath = window.electron.path.join(projectDir, DEFAULT_PROJECT_KCL_FILE)
try { await window.electron.stat(defaultFilePath) }
catch (e) {
if (e === 'ENOENT') {
// Find a kcl file in the directory.
if (file.children) {
for (let entry of file.children) {
if (entry.name.endsWith(".kcl")) {
return window.electron.path.join(projectDir, entry.name)
} else if (entry.children.length > 0) {
// Recursively find a kcl file in the directory.
return getDefaultKclFileForDir(entry.path, entry)
}
}
// If we didn't find a kcl file, create one.
await window.electron.writeFile(defaultFilePath, '')
return defaultFilePath
let defaultFilePath = window.electron.path.join(
projectDir,
DEFAULT_PROJECT_KCL_FILE
)
try {
await window.electron.stat(defaultFilePath)
} catch (e) {
if (e === 'ENOENT') {
// Find a kcl file in the directory.
if (file.children) {
for (let entry of file.children) {
if (entry.name.endsWith('.kcl')) {
return window.electron.path.join(projectDir, entry.name)
} else if (entry.children.length > 0) {
// Recursively find a kcl file in the directory.
return getDefaultKclFileForDir(entry.path, entry)
}
}
// If we didn't find a kcl file, create one.
await window.electron.writeFile(defaultFilePath, '')
return defaultFilePath
}
}
}
if (!file.children) {
@ -249,70 +273,72 @@ const getDefaultKclFileForDir = async (projectDir, file) => {
}
const kclFileCount = (file /* fileEntry */) => {
let count = 0
if (file.children) {
for (let entry of file.children) {
if (entry.name.endsWith(".kcl")) {
count += 1
} else {
count += kclFileCount(entry)
}
}
let count = 0
if (file.children) {
for (let entry of file.children) {
if (entry.name.endsWith('.kcl')) {
count += 1
} else {
count += kclFileCount(entry)
}
}
}
return count
}
/// Populate the number of directories in the project.
const directoryCount = (file /* FileEntry */) => {
let count = 0
if (file.children) {
for (let entry of file.children) {
count += 1
directoryCount(entry)
}
let count = 0
if (file.children) {
for (let entry of file.children) {
count += 1
directoryCount(entry)
}
}
return count
}
export async function getProjectInfo(
projectPath: string,
): Promise<Project> {
// Check the directory.
try { await window.electron.stat(projectPath) }
catch (e) {
if (e === 'ENOENT') {
return Promise.reject(new Error(`Project directory does not exist: ${project_path}`))
}
export async function getProjectInfo(projectPath: string): Promise<Project> {
// Check the directory.
try {
await window.electron.stat(projectPath)
} catch (e) {
if (e === 'ENOENT') {
return Promise.reject(
new Error(`Project directory does not exist: ${project_path}`)
)
}
}
// Make sure it is a directory.
const projectPathIsDir = await window.electron.statIsDirectory(projectPath)
if (!projectPathIsDir) {
return Promise.reject(new Error(`Project path is not a directory: ${project_path}`))
}
// Make sure it is a directory.
const projectPathIsDir = await window.electron.statIsDirectory(projectPath)
if (!projectPathIsDir) {
return Promise.reject(
new Error(`Project path is not a directory: ${project_path}`)
)
}
let walked = await collectAllFilesRecursiveFrom(projectPath)
let default_file = await getDefaultKclFileForDir(projectPath, walked)
const metadata = await window.electron.stat(projectPath)
let walked = await collectAllFilesRecursiveFrom(projectPath)
let default_file = await getDefaultKclFileForDir(projectPath, walked)
const metadata = await window.electron.stat(projectPath)
let project = /* FileEntry */ {
...walked,
metadata,
kcl_file_count: 0,
directory_count: 0,
default_file,
}
let project = /* FileEntry */ {
...walked,
metadata,
kcl_file_count: 0,
directory_count: 0,
default_file,
}
// Populate the number of KCL files in the project.
project.kcl_file_count = kclFileCount(project)
// Populate the number of KCL files in the project.
project.kcl_file_count = kclFileCount(project)
//Populate the number of directories in the project.
project.directory_count = directoryCount(project)
//Populate the number of directories in the project.
project.directory_count = directoryCount(project)
return project
return project
}
// Write project settings file.
@ -361,18 +387,20 @@ export const getInitialDefaultDir = async () => {
return window.electron.path.join(dir, PROJECT_FOLDER)
}
export const readProjectSettingsFile = async (projectPath: string): ProjectConfiguration => {
let settingsPath = await getProjectSettingsFilePath(projectPath)
export const readProjectSettingsFile = async (
projectPath: string
): ProjectConfiguration => {
let settingsPath = await getProjectSettingsFilePath(projectPath)
// Check if this file exists.
try { await window.electron.stat(settingsPath) }
catch (e) {
if (e === 'ENOENT') {
// Return the default configuration.
return {}
}
// Check if this file exists.
try {
await window.electron.stat(settingsPath)
} catch (e) {
if (e === 'ENOENT') {
// Return the default configuration.
return {}
}
}
const configToml = await window.electron.readFile(settingsPath)
const configObj = parseProjectSettings(configToml)
@ -444,4 +472,3 @@ export const getUser = async (
})
return user
}