diff --git a/src/blocks/mrc_class_method_def.ts b/src/blocks/mrc_class_method_def.ts index 952ec811..feb26ab7 100644 --- a/src/blocks/mrc_class_method_def.ts +++ b/src/blocks/mrc_class_method_def.ts @@ -322,6 +322,14 @@ const CLASS_METHOD_DEF = { this.mrcMethodId = oldIdToNewId[this.mrcMethodId]; } }, + upgrade_002_to_003: function(this: ClassMethodDefBlock) { + if (this.getFieldValue('NAME') === 'init') { + // Remove robot parameter from init method + const methodBlock = this as ClassMethodDefBlock; + let filteredParams: Parameter[] = methodBlock.mrcParameters.filter(param => param.name !== 'robot'); + methodBlock.mrcParameters = filteredParams; + } + }, }; /** @@ -447,6 +455,10 @@ export const pythonFromBlock = function ( } } + if (generator.getModuleType() === storageModule.ModuleType.OPMODE && block.mrcPythonMethodName === '__init__') { + paramString = 'robot'; + } + if (params.length != 0) { block.mrcParameters.forEach((param) => { paramString += ', ' + param.name; diff --git a/src/modules/opmode_start.json b/src/modules/opmode_start.json index 979584e4..96944187 100644 --- a/src/modules/opmode_start.json +++ b/src/modules/opmode_start.json @@ -26,17 +26,11 @@ "canBeCalledWithinClass": false, "canBeCalledOutsideClass": false, "returnType": "None", - "params": [ - { - "name": "robot", - "type": "Robot" - } - ], + "params": [], "pythonMethodName": "__init__" }, "fields": { - "NAME": "init", - "PARAM_robot": "robot" + "NAME": "init" } }, { diff --git a/src/storage/project.ts b/src/storage/project.ts index 153051f1..7e616ac0 100644 --- a/src/storage/project.ts +++ b/src/storage/project.ts @@ -25,7 +25,7 @@ import * as commonStorage from './common_storage'; import * as storageModule from './module'; import * as storageModuleContent from './module_content'; import * as storageNames from './names'; -import { upgradeProjectIfNecessary } from './upgrade_project'; +import { upgradeProjectIfNecessary, CURRENT_VERSION, NO_VERSION } from './upgrade_project'; // Types, constants, and functions related to projects, regardless of where the projects are stored. @@ -36,9 +36,6 @@ export type Project = { opModes: storageModule.OpMode[], }; -const NO_VERSION = '0.0.0'; -export const CURRENT_VERSION = '0.0.2'; - export type ProjectInfo = { version: string, }; diff --git a/src/storage/upgrade_project.ts b/src/storage/upgrade_project.ts index 4cfaae75..1804d9c7 100644 --- a/src/storage/upgrade_project.ts +++ b/src/storage/upgrade_project.ts @@ -28,19 +28,26 @@ import * as storageModule from './module'; import * as storageModuleContent from './module_content'; import * as storageNames from './names'; import * as storageProject from './project'; +import { ClassMethodDefBlock, BLOCK_NAME as MRC_CLASS_METHOD_DEF_BLOCK_NAME } from '../blocks/mrc_class_method_def'; +export const NO_VERSION = '0.0.0'; +export const CURRENT_VERSION = '0.0.3'; export async function upgradeProjectIfNecessary( storage: commonStorage.Storage, projectName: string): Promise { const projectInfo = await storageProject.fetchProjectInfo(storage, projectName); - if (semver.lt(projectInfo.version, storageProject.CURRENT_VERSION)) { + if (semver.lt(projectInfo.version, CURRENT_VERSION)) { switch (projectInfo.version) { // @ts-ignore case '0.0.0': upgradeFrom_000_to_001(storage, projectName, projectInfo) - // Intentional fallthrough + // Intentional fallthrough + // @ts-ignore case '0.0.1': upgradeFrom_001_to_002(storage, projectName, projectInfo); + case '0.0.2': + upgradeFrom_002_to_003(storage, projectName, projectInfo); + } await storageProject.saveProjectInfo(storage, projectName); } @@ -92,3 +99,41 @@ async function upgradeFrom_001_to_002( } projectInfo.version = '0.0.2'; } + +async function upgradeFrom_002_to_003( + storage: commonStorage.Storage, + projectName: string, + projectInfo: storageProject.ProjectInfo): Promise { + // Opmodes had robot as a parameter to init method + const projectFileNames: string[] = await storage.list( + storageNames.makeProjectDirectoryPath(projectName)); + + for (const projectFileName of projectFileNames) { + const modulePath = storageNames.makeFilePath(projectName, projectFileName); + + if (storageNames.getModuleType(modulePath) === storageModule.ModuleType.OPMODE) { + let moduleContentText = await storage.fetchFileContentText(modulePath); + const moduleContent = storageModuleContent.parseModuleContentText(moduleContentText); + let blocks = moduleContent.getBlocks(); + + // Create a temporary workspace to upgrade the blocks + const headlessWorkspace = new Blockly.Workspace(); + try { + Blockly.serialization.workspaces.load(blocks, headlessWorkspace); + + // Method blocks need to be upgraded + headlessWorkspace.getBlocksByType(MRC_CLASS_METHOD_DEF_BLOCK_NAME, false).forEach(block => { + (block as ClassMethodDefBlock).upgrade_002_to_003(); + }); + blocks = Blockly.serialization.workspaces.save(headlessWorkspace); + } finally { + headlessWorkspace.dispose(); + } + + moduleContent.setBlocks(blocks); + moduleContentText = moduleContent.getModuleContentText(); + await storage.saveFile(modulePath, moduleContentText); + } + } + projectInfo.version = '0.0.3'; +}