Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion src/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const BLOCK_NAME = 'mrc_class_method_def';

export const FIELD_METHOD_NAME = 'NAME';

type Parameter = {
export type Parameter = {
name: string,
type?: string,
};
Expand Down Expand Up @@ -447,6 +447,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
70 changes: 61 additions & 9 deletions src/storage/upgrade_project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,44 @@ import * as storageModule from './module';
import * as storageModuleContent from './module_content';
import * as storageNames from './names';
import * as storageProject from './project';
import { ClassMethodDefBlock, Parameter, 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> {
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);
}
}

async function upgradeFrom_000_to_001(
_storage: commonStorage.Storage,
_projectName: string,
projectInfo: storageProject.ProjectInfo): Promise<void> {
_storage: commonStorage.Storage,
_projectName: string,
projectInfo: storageProject.ProjectInfo): Promise<void> {
// Project was saved without a project.info.json file.
// Nothing needs to be done to upgrade to '0.0.1';
projectInfo.version = '0.0.1';
}

async function upgradeFrom_001_to_002(
storage: commonStorage.Storage,
projectName: string,
projectInfo: storageProject.ProjectInfo): Promise<void> {
storage: commonStorage.Storage,
projectName: string,
projectInfo: storageProject.ProjectInfo): Promise<void> {
// Modules were saved without private components.
// The Robot's mrc_mechanism_component_holder block was saved without hidePrivateComponents.
const projectFileNames: string[] = await storage.list(
Expand Down Expand Up @@ -92,3 +99,48 @@ 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);

// Find and modify init method blocks to remove robot parameter
const allBlocks = headlessWorkspace.getAllBlocks();
for (const block of allBlocks) {
if (block.type === MRC_CLASS_METHOD_DEF_BLOCK_NAME &&
block.getFieldValue('NAME') === 'init') {
// Remove robot parameter from init method
const methodBlock = block as ClassMethodDefBlock;
let filteredParams: Parameter[] = methodBlock.mrcParameters.filter(param => param.name !== 'robot');
methodBlock.mrcParameters = filteredParams;
}
}
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';
}