Skip to content

Commit

Permalink
Static asset generation (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
siefkenj authored Sep 14, 2023
1 parent 00ddadd commit 5d2a437
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 21 deletions.
62 changes: 56 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"./packages/utils",
"./packages/parser",
"./packages/doenetml-worker",
"./packages/static-assets",
"./packages/virtual-keyboard",
"./packages/codemirror",
"./packages/doenetml",
Expand All @@ -31,10 +32,10 @@
"jsdom": "^22.1.0",
"prettier": "^3.0.2",
"typescript": "^5.2.2",
"vite-node": "^0.34.4",
"vitest": "^0.34.3"
},
"prettier": {
"tabWidth": 4
},
"dependencies": {}
}
}
2 changes: 1 addition & 1 deletion packages/codemirror/src/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { completeFromSchema } from "@codemirror/lang-xml";
import { parser } from "@doenet/parser";
// @ts-ignore
import doenetSchema from "../../doenetml/src/Core/doenetSchema.json";
import doenetSchema from "@doenet/static-assets/doenet-schema.json";

export function CodeMirror({
setInternalValueTo,
Expand Down
1 change: 0 additions & 1 deletion packages/doenetml/src/Core/doenetSchema.json

This file was deleted.

24 changes: 24 additions & 0 deletions packages/static-assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@doenet/static-assets",
"type": "module",
"description": "Static JSON blobs for DoenetML components",
"version": "1.0.0",
"license": "AGPL-3.0-or-later",
"homepage": "https://github.com/Doenet/DoenetML#readme",
"private": false,
"repository": "github:Doenet/DoenetML",
"files": [
"/dist"
],
"exports": {
"./*json": {
"import": "./dist/*json"
}
},
"scripts": {
"test": "echo \"No tests \"",
"build": "npm run build:pre && npm run build:schema",
"build:pre": "rm -rf dist; mkdir dist",
"build:schema": "vite-node ./scripts/generate-schema.ts"
}
}
13 changes: 13 additions & 0 deletions packages/static-assets/scripts/generate-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as fs from "node:fs/promises";
import { getSchema } from "./get-schema";

const destUrl = new URL("../dist/doenet-schema.json", import.meta.url);

const schema = getSchema();
console.log(
"Writing",
Object.keys(schema.elements).length,
"schema items to",
destUrl.pathname,
);
await fs.writeFile(destUrl.pathname, JSON.stringify(schema, null, 4));
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
import createComponentInfoObjects from "./componentInfoObjects";
import createComponentInfoObjects from "../../doenetml/src/Core/utils/componentInfoObjects";

// Create schema of DoenetML by extracting component, attributes and children
// from component classes.
// The results are currently just stringified and printed out,
// and then manually copied into src/Core/doenetSchema.json.
// CodeMirrror.jsx reads in the json file to form its autocompletion scheme.

// For now, we just run getSchema() manually, then copy and paste the output
// to doenetSchema.json.
// CodeMirror.jsx reads in the json file to form its autocompletion scheme.

type AttributeObject = {
createPrimitiveOfType: string;
createStateVariable: string;
createComponentOfType: string;
defaultValue: unknown;
public: boolean;
excludeFromSchema: boolean;
validValues?: unknown[];
};

type ComponentClass = {
componentType: string;
renderChildren: boolean;
canDisplayChildErrors: boolean;
includeBlankStringChildren: boolean;
returnChildGroups: () => {
group: string;
componentTypes: string[];
excludeFromSchema?: boolean;
}[];
returnStateVariableDefinitions: () => Record<string, unknown>;
excludeFromSchema: boolean;
allowInSchemaAsComponent?: string[];
acceptTarget: boolean;
createAttributesObject: () => Record<string, AttributeObject>;
inSchemaOnlyInheritAs: string[];
getAdapterComponentType: (...args: any[]) => string;
numAdapters: number;
additionalSchemaChildren?: string[];
assignNamesToReplacements: boolean;
};

interface ComponentInfoObjects
extends ReturnType<typeof createComponentInfoObjects> {
allComponentClasses: Record<string, ComponentClass>;
}

export function getSchema() {
let componentInfoObjects = createComponentInfoObjects();
let componentInfoObjects =
createComponentInfoObjects() as ComponentInfoObjects;
let componentClasses = componentInfoObjects.allComponentClasses;

// If a component class has static variable excludeFromSchema set,
Expand All @@ -22,7 +55,7 @@ export function getSchema() {
}
}

let inheritedOrAdaptedTypes = {};
let inheritedOrAdaptedTypes: Record<string, string[]> = {};

for (let type1 in componentClasses) {
let inherited = [];
Expand Down Expand Up @@ -121,7 +154,9 @@ export function getSchema() {
// one can add a excludeFromSchema to an attribute definition
// to keep it from showing up in the schema
if (!attrDef.excludeFromSchema) {
let attrSpec = { name: attrName };
let attrSpec: { name: string; values?: unknown[] } = {
name: attrName,
};

if (attrDef.validValues) {
attrSpec.values = attrDef.validValues;
Expand Down Expand Up @@ -191,13 +226,17 @@ export function getSchema() {
}

// For now, we're just copying these schema from this console output
console.log(JSON.stringify({ elements }));
return { elements };
}

function checkIfInheritOrAdapt({
startingType,
destinationType,
componentInfoObjects,
}: {
startingType: string;
destinationType: string;
componentInfoObjects: ComponentInfoObjects;
}) {
let startingClass = componentInfoObjects.allComponentClasses[startingType];

Expand Down
17 changes: 17 additions & 0 deletions packages/static-assets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist/",
"rootDir": "./"
},
"include": ["./**/*.ts", "./**/*.tsx", "./src/generated-assets/*.js"],
"exclude": [
"./**/*.test.ts",
"./**/*.stub.ts",
"node_modules",
"**/tests/",
"**/dist/**/*",
"./vite.config.ts"
],
"references": []
}

0 comments on commit 5d2a437

Please sign in to comment.