Skip to content

Commit 14f9b65

Browse files
authored
Pr hide robot for init (#236)
* First pass of removing robot from init in opmodes * Get rid of leftover extra ignore * Move upgrade details of mrc_class_method into the block * address review comment * Fix space issue * Fix spaces * Fix spacing * Change spaces
1 parent 003199a commit 14f9b65

File tree

4 files changed

+62
-14
lines changed

4 files changed

+62
-14
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,14 @@ const CLASS_METHOD_DEF = {
322322
this.mrcMethodId = oldIdToNewId[this.mrcMethodId];
323323
}
324324
},
325+
upgrade_002_to_003: function(this: ClassMethodDefBlock) {
326+
if (this.getFieldValue('NAME') === 'init') {
327+
// Remove robot parameter from init method
328+
const methodBlock = this as ClassMethodDefBlock;
329+
let filteredParams: Parameter[] = methodBlock.mrcParameters.filter(param => param.name !== 'robot');
330+
methodBlock.mrcParameters = filteredParams;
331+
}
332+
},
325333
};
326334

327335
/**
@@ -447,6 +455,10 @@ export const pythonFromBlock = function (
447455
}
448456
}
449457

458+
if (generator.getModuleType() === storageModule.ModuleType.OPMODE && block.mrcPythonMethodName === '__init__') {
459+
paramString = 'robot';
460+
}
461+
450462
if (params.length != 0) {
451463
block.mrcParameters.forEach((param) => {
452464
paramString += ', ' + param.name;

src/modules/opmode_start.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@
2626
"canBeCalledWithinClass": false,
2727
"canBeCalledOutsideClass": false,
2828
"returnType": "None",
29-
"params": [
30-
{
31-
"name": "robot",
32-
"type": "Robot"
33-
}
34-
],
29+
"params": [],
3530
"pythonMethodName": "__init__"
3631
},
3732
"fields": {
38-
"NAME": "init",
39-
"PARAM_robot": "robot"
33+
"NAME": "init"
4034
}
4135
},
4236
{

src/storage/project.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import * as commonStorage from './common_storage';
2525
import * as storageModule from './module';
2626
import * as storageModuleContent from './module_content';
2727
import * as storageNames from './names';
28-
import { upgradeProjectIfNecessary } from './upgrade_project';
28+
import { upgradeProjectIfNecessary, CURRENT_VERSION, NO_VERSION } from './upgrade_project';
2929

3030
// Types, constants, and functions related to projects, regardless of where the projects are stored.
3131

@@ -36,9 +36,6 @@ export type Project = {
3636
opModes: storageModule.OpMode[],
3737
};
3838

39-
const NO_VERSION = '0.0.0';
40-
export const CURRENT_VERSION = '0.0.2';
41-
4239
export type ProjectInfo = {
4340
version: string,
4441
};

src/storage/upgrade_project.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,26 @@ import * as storageModule from './module';
2828
import * as storageModuleContent from './module_content';
2929
import * as storageNames from './names';
3030
import * as storageProject from './project';
31+
import { ClassMethodDefBlock, BLOCK_NAME as MRC_CLASS_METHOD_DEF_BLOCK_NAME } from '../blocks/mrc_class_method_def';
3132

33+
export const NO_VERSION = '0.0.0';
34+
export const CURRENT_VERSION = '0.0.3';
3235

3336
export async function upgradeProjectIfNecessary(
3437
storage: commonStorage.Storage, projectName: string): Promise<void> {
3538
const projectInfo = await storageProject.fetchProjectInfo(storage, projectName);
36-
if (semver.lt(projectInfo.version, storageProject.CURRENT_VERSION)) {
39+
if (semver.lt(projectInfo.version, CURRENT_VERSION)) {
3740
switch (projectInfo.version) {
3841
// @ts-ignore
3942
case '0.0.0':
4043
upgradeFrom_000_to_001(storage, projectName, projectInfo)
41-
// Intentional fallthrough
44+
// Intentional fallthrough
45+
// @ts-ignore
4246
case '0.0.1':
4347
upgradeFrom_001_to_002(storage, projectName, projectInfo);
48+
case '0.0.2':
49+
upgradeFrom_002_to_003(storage, projectName, projectInfo);
50+
4451
}
4552
await storageProject.saveProjectInfo(storage, projectName);
4653
}
@@ -92,3 +99,41 @@ async function upgradeFrom_001_to_002(
9299
}
93100
projectInfo.version = '0.0.2';
94101
}
102+
103+
async function upgradeFrom_002_to_003(
104+
storage: commonStorage.Storage,
105+
projectName: string,
106+
projectInfo: storageProject.ProjectInfo): Promise<void> {
107+
// Opmodes had robot as a parameter to init method
108+
const projectFileNames: string[] = await storage.list(
109+
storageNames.makeProjectDirectoryPath(projectName));
110+
111+
for (const projectFileName of projectFileNames) {
112+
const modulePath = storageNames.makeFilePath(projectName, projectFileName);
113+
114+
if (storageNames.getModuleType(modulePath) === storageModule.ModuleType.OPMODE) {
115+
let moduleContentText = await storage.fetchFileContentText(modulePath);
116+
const moduleContent = storageModuleContent.parseModuleContentText(moduleContentText);
117+
let blocks = moduleContent.getBlocks();
118+
119+
// Create a temporary workspace to upgrade the blocks
120+
const headlessWorkspace = new Blockly.Workspace();
121+
try {
122+
Blockly.serialization.workspaces.load(blocks, headlessWorkspace);
123+
124+
// Method blocks need to be upgraded
125+
headlessWorkspace.getBlocksByType(MRC_CLASS_METHOD_DEF_BLOCK_NAME, false).forEach(block => {
126+
(block as ClassMethodDefBlock).upgrade_002_to_003();
127+
});
128+
blocks = Blockly.serialization.workspaces.save(headlessWorkspace);
129+
} finally {
130+
headlessWorkspace.dispose();
131+
}
132+
133+
moduleContent.setBlocks(blocks);
134+
moduleContentText = moduleContent.getModuleContentText();
135+
await storage.saveFile(modulePath, moduleContentText);
136+
}
137+
}
138+
projectInfo.version = '0.0.3';
139+
}

0 commit comments

Comments
 (0)