Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/document/DocumentManager.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { createContext } from "react";
import StateStore, { IStateStore } from "./DocumentModel";
import {
dialog,
fs,
invoke,
path,
window as tauriWindow,
} from "@tauri-apps/api";
import { dialog, invoke, path, window as tauriWindow } from "@tauri-apps/api";
import { listen, TauriEvent, Event } from "@tauri-apps/api/event";
import { v4 as uuidv4 } from "uuid";
import { VERSIONS, validate, SAVE_FILE_VERSION } from "./DocumentSpecTypes";
import { applySnapshot, getRoot, onPatch } from "mobx-state-tree";
import { autorun, reaction, toJS } from "mobx";
import { validate } from "./DocumentSpecTypes";
import { applySnapshot, getSnapshot } from "mobx-state-tree";
import { reaction } from "mobx";
import { toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.min.css";
import hotkeys from "hotkeys-js";
import { resourceLimits } from "worker_threads";

type OpenFileEventPayload = {
adjacent_gradle: boolean;
Expand Down Expand Up @@ -61,11 +54,20 @@ export class DocumentManager {
} else if (payload.contents === undefined) {
throw "Unable to read file";
} else {
this.model.uiState.setSaveFileName(payload.name);
this.model.uiState.setSaveFileDir(payload.dir);
this.model.uiState.setIsGradleProject(payload.adjacent_gradle);

await this.openFromContents(payload.contents);
let oldDocument = getSnapshot(this.model.document);
let saveName = payload.name;
let saveDir = payload.dir;
let adjacent_gradle = payload.adjacent_gradle;
await this.openFromContents(payload.contents)
.catch((err) => {
applySnapshot(this.model.document, oldDocument);
throw `Internal parsing error: ${err}`;
})
.then(() => {
this.model.uiState.setSaveFileName(saveName);
this.model.uiState.setSaveFileDir(saveDir);
this.model.uiState.setIsGradleProject(adjacent_gradle);
});
}
}

Expand Down
70 changes: 34 additions & 36 deletions src/document/DocumentSpecTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* DO NOT CHANGE the following import block! It should remain as a copy-paste example */
import type {
SavedDocument as v0_0_0,
SavedDocument,
Expand Down Expand Up @@ -32,12 +31,16 @@ import {
SAVE_FILE_VERSION as v0_2_Version,
} from "./previousSpecs/v0_2";
import v0_2_Schema from "./previousSpecs/v0.2.json";
import { SAVE_FILE_VERSION } from "./previousSpecs/v0_2";
import Ajv from "ajv";
import { ROBOT_CONFIG_DEFAULTS } from "./RobotConfigStore";

// Paste new version import blocks above this line.
// Import SAVE_FILE_VERSION, SavedDocument and only the other types needed for the upgrader functions.
// Also import the new schema.

// No need to change the below two imports when adding new versions.
import Ajv from "ajv";
import { ROBOT_CONFIG_DEFAULTS } from "./RobotConfigStore";
// Update the import path in the below to point to a particular version as current
import { SAVE_FILE_VERSION } from "./previousSpecs/v0_2";
export type {
SavedDocument,
SavedTrajectorySample,
Expand All @@ -50,6 +53,8 @@ export type {
} from "./previousSpecs/v0_2";
export { SAVE_FILE_VERSION } from "./previousSpecs/v0_2";

const ajv = new Ajv();

export let VERSIONS = {
"v0.0.0": {
up: (document: any): v0_0_1 => {
Expand All @@ -73,10 +78,7 @@ export let VERSIONS = {
}
return updated;
},
validate: (document: v0_0_0): boolean => {
const ajv = new Ajv();
return ajv.validate(v0_0_0_Schema, document);
},
schema: v0_0_0_Schema,
},
"v0.0.1": {
up: (document: any): v0_1 => {
Expand All @@ -103,21 +105,9 @@ export let VERSIONS = {
}
return updated;
},
validate: (document: v0_0_1): boolean => {
const ajv = new Ajv();
return ajv.validate(v0_0_1_Schema, document);
},
schema: v0_0_1_Schema,
},
"v0.1": {
up: (document: any): v0_1 => {
return document as v0_1;
},
validate: (document: v0_1): boolean => {
const ajv = new Ajv();
return ajv.validate(v0_1_Schema, document);
},
},
"v0.1.1": {
up: (document: any): v0_1_1 => {
document = document as v0_1;
let updated: v0_1_1 = {
Expand All @@ -137,12 +127,9 @@ export let VERSIONS = {
}
return updated;
},
validate: (document: v0_1_1): boolean => {
const ajv = new Ajv();
return ajv.validate(v0_1_1_Schema, document);
},
schema: v0_1_Schema,
},
"v0.1.2": {
"v0.1.1": {
up: (document: any): v0_1_2 => {
document = document as v0_1_1;
let updated: v0_1_2 = {
Expand All @@ -164,12 +151,9 @@ export let VERSIONS = {
}
return updated;
},
validate: (document: v0_1_2): boolean => {
const ajv = new Ajv();
return ajv.validate(v0_1_2_Schema, document);
},
schema: v0_1_1_Schema,
},
"v0.2": {
"v0.1.2": {
up: (document: any): v0_2 => {
document = document as v0_1_2;
let robotConfiguration: v0_2_Config = {
Expand All @@ -185,11 +169,25 @@ export let VERSIONS = {
};
return updated;
},
validate: (document: v0_2): boolean => {
const ajv = new Ajv();
return ajv.validate(v0_2_Schema, document);
},
schema: v0_1_2_Schema,
},
"v0.2": {
up: (document: any): v0_2 => document,
schema: v0_2_Schema,
},
/**
* For developers adding new document versions-Keep this comment at the end of the list.
*
* CURRENT_VERSION refers to the version before the one being added.
*
* Step 1: Write the upgrader function from CURRENT_VERSION to ADDED_VERSION.
* Step 2: Replace VERSIONS[CURRENT_VERSION].up with your upgrader function.
* To reiterate, each version's upgrader function upgrades a document of that version to the next version,
* except for the latest version's upgrader, which returns the document unchanged.
* Step 3: write a "no-op" upgrader function for ADDED_VERSION. This function should never actually run,
* so this is mostly a matter of matching the type signature without modifying the document
* Step 4: set the `schema` property for your new version to the imported schema from above.
*/
};

export let updateToCurrent = (document: { version: string }): SavedDocument => {
Expand All @@ -213,7 +211,7 @@ export let updateToCurrent = (document: { version: string }): SavedDocument => {

export let validate = (document: { version: string }): boolean => {
if (document.version in VERSIONS) {
return VERSIONS[document.version].validate(document);
return ajv.validate(VERSIONS[document.version].schema, document);
} else {
return false;
}
Expand Down