forked from microbit-foundation/cctd-ml-machine
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.ts
More file actions
152 lines (138 loc) · 4.46 KB
/
Copy pathutils.ts
File metadata and controls
152 lines (138 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* (c) 2023, Center for Computational Thinking and Design at Aarhus University and contributors
* Modifications (c) 2024, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import camelCase from "lodash.camelcase";
import upperFirst from "lodash.upperfirst";
import { DatasetEditorJsonFormat, TrainedModel } from "../model";
import { getMainScript } from "./generate-main-scripts";
import { getAutogeneratedTs, getDatasetJson } from "./generate-custom-scripts";
import { MakeCodeProject } from "@microbit/makecode-embed/react";
import { DataWindow, untitledProjectName } from "../project-utils";
export const filenames = {
mainTs: "main.ts",
mainBlocks: "main.blocks",
autogenerated: "autogenerated.ts",
metadata: "ml-metadata.json",
datasetJson: "dataset.json",
pxtJson: "pxt.json",
readme: "README.md",
};
// Exported for testing.
export const extensionName = "machine-learning";
const extensionURL = "github:microbit-foundation/pxt-microbit-ml#v1.0.14";
export const pxt = {
name: untitledProjectName,
description: "",
dependencies: {
core: "*",
microphone: "*",
radio: "*", // Needed to compile.
[extensionName]: extensionURL,
},
files: Object.values(filenames),
preferredEditor: "blocksprj",
};
export const generateProject = (
name: string,
actionState: DatasetEditorJsonFormat,
model: TrainedModel | undefined,
dataWindow: DataWindow,
projectEdited: boolean
) => {
const { data: actions } = actionState;
const useableActions = model ? actions : [];
return {
text: {
[filenames.pxtJson]: JSON.stringify({ ...pxt, name }),
[filenames.readme]: "",
[filenames.mainTs]: getMainScript(useableActions, "javascript"),
[filenames.mainBlocks]: getMainScript(useableActions, "blocks"),
...generateCustomFiles(actionState, model, dataWindow, projectEdited),
},
};
};
export const generateCustomFiles = (
actionState: DatasetEditorJsonFormat,
model: TrainedModel | undefined,
dataWindow: DataWindow,
projectEdited: boolean,
project?: MakeCodeProject
) => {
const { data: actions } = actionState;
const useableActions = model ? actions : [];
const customFiles = {
[filenames.autogenerated]: model
? getAutogeneratedTs(useableActions, model.machineCode, dataWindow)
: "",
// Save all actions to dataset.json.
[filenames.datasetJson]: getDatasetJson(actionState),
[filenames.metadata]: JSON.stringify({ projectEdited }),
};
if (!project) {
return customFiles;
}
const currentPxtJSON = project.text?.[filenames.pxtJson];
if (currentPxtJSON) {
try {
const updatedPxt = JSON.parse(currentPxtJSON) as typeof pxt;
updatedPxt.dependencies[extensionName] = extensionURL;
if (!updatedPxt.files.includes(filenames.metadata)) {
updatedPxt.files.push(filenames.metadata);
}
return {
...customFiles,
[filenames.pxtJson]: JSON.stringify(updatedPxt),
};
} catch (e) {
// If we reach this case, the project is in theory already very broken
// as it will be missing a pxt file and the extension.
}
}
};
export interface ActionName {
actionLabel: string;
actionVar: string;
}
const sanitizeActionVar = (input: string) =>
input
.replace(/[^\p{L}\p{N}_$\s]/gu, "")
.replace(/^(\s|\p{N})+/gu, "")
.trim();
const sanitizeActionLabel = (input: string) => input.replace(/"/g, "'");
export const actionNamesFromLabels = (actionLabels: string[]): ActionName[] => {
const actionNames: ActionName[] = [];
actionLabels.forEach((actionLabel, i) => {
let sanitizedLabel = sanitizeActionLabel(actionLabel);
if (!sanitizedLabel) {
sanitizedLabel = `Event`;
}
while (actionNames.map((an) => an.actionLabel).includes(sanitizedLabel)) {
sanitizedLabel += i;
}
let actionVar = upperFirst(camelCase(sanitizeActionVar(sanitizedLabel)));
if (!actionVar) {
actionVar = `Event`;
}
while (actionNames.map((an) => an.actionVar).includes(actionVar)) {
actionVar += i;
}
actionNames.push({
actionLabel: sanitizedLabel,
actionVar,
});
});
return actionNames;
};
export const hasMakeCodeMlExtension = (project: MakeCodeProject) => {
if (!project.text || !project.text[filenames.pxtJson]) {
return false;
}
const pxtJson = JSON.parse(project.text[filenames.pxtJson]) as object;
if (!("dependencies" in pxtJson)) {
return false;
}
return extensionName in (pxtJson["dependencies"] as object);
};