Skip to content

Commit

Permalink
Merge pull request #377 from ShravanSunder/feature/tauri-paths
Browse files Browse the repository at this point in the history
fixes LUNA-218 add path to tauri project metadata
  • Loading branch information
abrenneke authored Mar 25, 2024
2 parents 238e0a4 + 2137b3f commit cdfa7ee
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 35 deletions.
66 changes: 38 additions & 28 deletions packages/app/src/hooks/useSaveProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,23 @@ export function useSaveProject() {
saving = toast.info('Saving project');
}, 500);

await ioProvider.saveProjectDataNoPrompt(newProject, { testSuites }, loadedProject.path);
try {
await ioProvider.saveProjectDataNoPrompt(newProject, { testSuites }, loadedProject.path);

if (saving != null) {
toast.dismiss(saving);
}
clearTimeout(savingTimeout);
if (saving != null) {
toast.dismiss(saving);
}
clearTimeout(savingTimeout);

toast.success('Project saved');
setLoadedProject({
loaded: true,
path: loadedProject.path,
});
toast.success('Project saved');
setLoadedProject({
loaded: true,
path: loadedProject.path,
});
} catch (cause) {
clearTimeout(savingTimeout);
toast.error('Failed to save project');
}
}

async function saveProjectAs() {
Expand All @@ -57,26 +62,31 @@ export function useSaveProject() {
saving = toast.info('Saving project');
}, 500);

const filePath = await ioProvider.saveProjectData(newProject, { testSuites });
try {
const filePath = await ioProvider.saveProjectData(newProject, { testSuites });

if (saving != null) {
toast.dismiss(saving);
}
clearTimeout(savingTimeout);
if (saving != null) {
toast.dismiss(saving);
}
clearTimeout(savingTimeout);

if (filePath) {
toast.success('Project saved');
setLoadedProject({
loaded: true,
path: filePath,
});
setOpenedProjects((projects) => ({
...projects,
[project.metadata.id]: {
project,
fsPath: filePath,
},
}));
if (filePath) {
toast.success('Project saved');
setLoadedProject({
loaded: true,
path: filePath,
});
setOpenedProjects((projects) => ({
...projects,
[project.metadata.id]: {
project,
fsPath: filePath,
},
}));
}
} catch (cause) {
clearTimeout(savingTimeout);
toast.error('Failed to save project');
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/io/TauriIOProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class TauriIOProvider implements IOProvider {

async loadProjectDataNoPrompt(path: string): Promise<{ project: Project; testData: TrivetData }> {
const data = await readTextFile(path);
const [projectData, attachedData] = deserializeProject(data);
const [projectData, attachedData] = deserializeProject(data, path);

const trivetData = attachedData.trivet
? deserializeTrivetData(attachedData.trivet as SerializedTrivetData)
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/model/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Project = {
title: string;
description: string;
mainGraphId?: GraphId;
path?: string;
};

plugins?: PluginLoadSpec[];
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/utils/serialization/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ export function serializeProject(project: Project, attachedData?: AttachedData):
return projectV4Serializer(project, attachedData);
}

export function deserializeProject(serializedProject: unknown): [Project, AttachedData] {
export function deserializeProject(serializedProject: unknown, path: string | null = null): [Project, AttachedData] {
try {
return projectV4Deserializer(serializedProject);
const result = projectV4Deserializer(serializedProject);
if (path !== null)
result[0].metadata.path = path;
return result;
} catch (err) {
if (err instanceof yaml.YAMLError) {
yamlProblem(err);
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/utils/serialization/serialization_v4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,16 @@ export function graphV4Deserializer(data: unknown): NodeGraph {
}

export function projectV4Serializer(project: Project, attachedData?: AttachedData): unknown {
const filteredProject = {
...project,
metadata: {
...project.metadata,
path: undefined,
}
}

// Make sure all data is ordered deterministically first
const stabilized = JSON.parse(stableStringify(toSerializedProject(project, attachedData)));
const stabilized = JSON.parse(stableStringify(toSerializedProject(filteredProject, attachedData)));

const serialized = yaml.stringify(
{
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export async function loadTestGraphInProcessor(graphName: string) {

export async function loadProjectFromFile(path: string): Promise<Project> {
const content = await readFile(path, { encoding: 'utf8' });
return loadProjectFromString(content);
return loadProjectFromString(content, path);
}

export function loadProjectFromString(content: string): Project {
const [project] = deserializeProject(content);
export function loadProjectFromString(content: string, path: string | null = null): Project {
const [project] = deserializeProject(content, path);
return project;
}

Expand Down

0 comments on commit cdfa7ee

Please sign in to comment.