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
31 changes: 28 additions & 3 deletions src/makecode/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import upperFirst from "lodash.upperfirst";
import { DatasetEditorJsonFormat, GestureData } from "../model";
import { getMainScript } from "./generate-main-scripts";
import { getAutogeneratedTs, getDatasetJson } from "./generate-custom-scripts";
import { Project } from "@microbit/makecode-embed/react";

export const filenames = {
mainTs: "main.ts",
Expand All @@ -15,14 +16,17 @@ export const filenames = {
readme: "README.md",
};

const extensionName = "machine-learning";
const extensionURL = "github:microbit-foundation/pxt-microbit-ml#v0.4.3";

const pxt = {
name: "Untitled",
description: "",
dependencies: {
core: "*",
microphone: "*",
radio: "*", // Needed to compile.
"machine-learning": "github:microbit-foundation/pxt-microbit-ml#v0.4.3",
[extensionName]: extensionURL,
},
files: Object.values(filenames),
preferredEditor: "blocksprj",
Expand Down Expand Up @@ -56,17 +60,38 @@ export const generateProject = (

export const generateCustomFiles = (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there tests for this generation? It feels like it should be pretty straightforward to test the generation (maybe snapshot testing if there's too much output?).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There aren't at the moment, but yes, should be easy to add.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this to help folks move forward with projects that need this version bump, but please can you look at test coverage for project generation next?

gestureState: DatasetEditorJsonFormat,
model: LayersModel | undefined
model: LayersModel | undefined,
project?: Project
) => {
const { data: gestures } = gestureState;
const useableGestures = model ? gestures : [];
return {

const customFiles = {
[filenames.autogenerated]: model
? getAutogeneratedTs(useableGestures, model)
: "",
// Save all gestures to dataset.json.
[filenames.datasetJson]: getDatasetJson(gestureState),
};

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;
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const updateProject = (
text: {
...project.text,
...(projectEdited
? generateCustomFiles(gestureData, model)
? generateCustomFiles(gestureData, model, project)
: generateProject(gestureData, model).text),
},
};
Expand Down
Loading