Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
12 changes: 12 additions & 0 deletions src/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
};

/**
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 2 additions & 8 deletions src/modules/opmode_start.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down
5 changes: 1 addition & 4 deletions src/storage/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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,
};
Expand Down
49 changes: 47 additions & 2 deletions src/storage/upgrade_project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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);
}
Expand Down Expand Up @@ -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<void> {
// 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';
}