Skip to content

Commit f486ed3

Browse files
committed
In mrc_class_method_def.ts:
Added upgrade_002_to_003 function. In upgrade_project.ts: Modified upgradeBlocksFiles to take a predicate function. Added isAnyModuleType and isOpMode functions that can be passed to upgradeBlocksFiles as the predicate function. Modified upgradeFrom_002_to_003 to call upgradeBlocksFiles. Modified upgradeFrom_004_to_005 to pass isAnyModuleType to upgradeBlocksFiles.
1 parent 6efebe4 commit f486ed3

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,20 @@ export function getMethodNamesAlreadyOverriddenInWorkspace(
618618
});
619619
}
620620

621+
/**
622+
* Upgrades the ClassMethodDefBlocks in the given workspace from version 002 to 003.
623+
* This function should only be called when upgrading old projects.
624+
*/
625+
export function upgrade_002_to_003(workspace: Blockly.Workspace): void {
626+
// Make sure the workspace is headless.
627+
if (workspace.rendered) {
628+
throw new Error('upgrade_002_to_003 should never be called with a rendered workspace.');
629+
}
630+
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
631+
(block as ClassMethodDefBlock).upgrade_002_to_003();
632+
});
633+
}
634+
621635
/**
622636
* Upgrades the ClassMethodDefBlocks in the given workspace from version 004 to 005.
623637
* This function should only be called when upgrading old projects.

src/storage/upgrade_project.ts

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ 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, upgrade_004_to_005 } from '../blocks/mrc_class_method_def';
31+
import { upgrade_002_to_003, upgrade_004_to_005 } from '../blocks/mrc_class_method_def';
3232
import * as workspaces from '../blocks/utils/workspaces';
3333

3434
export const NO_VERSION = '0.0.0';
@@ -74,6 +74,7 @@ export async function upgradeProjectIfNecessary(
7474
async function upgradeBlocksFiles(
7575
storage: commonStorage.Storage,
7676
projectName: string,
77+
predicateFunc: (moduleType: storageModule.ModuleType) => boolean,
7778
upgradeFunc: (w: Blockly.Workspace) => void
7879
): Promise<void> {
7980
const projectFileNames: string[] = await storage.list(
@@ -82,6 +83,10 @@ async function upgradeBlocksFiles(
8283
const modulePath = storageNames.makeFilePath(projectName, projectFileName);
8384
const moduleType = storageNames.getModuleType(modulePath);
8485

86+
if (!predicateFunc(moduleType)) {
87+
continue;
88+
}
89+
8590
let moduleContentText = await storage.fetchFileContentText(modulePath);
8691
const moduleContent = storageModuleContent.parseModuleContentText(moduleContentText);
8792
let blocks = moduleContent.getBlocks();
@@ -105,6 +110,22 @@ async function upgradeBlocksFiles(
105110
}
106111
}
107112

113+
/**
114+
* Predicate function that can be passed to upgradeBlocksFiles indicating that all modules should be
115+
* upgraded.
116+
*/
117+
function isAnyModuleType(_moduleType: storageModule.ModuleType): boolean {
118+
return true;
119+
}
120+
121+
/**
122+
* Predicate function that can be passed to upgradeBlocksFiles indicating that only OpMode modules
123+
* should be upgraded.
124+
*/
125+
function isOpMode(moduleType: storageModule.ModuleType): boolean {
126+
return moduleType === storageModule.ModuleType.OPMODE;
127+
}
128+
108129
async function upgradeFrom_000_to_001(
109130
_storage: commonStorage.Storage,
110131
_projectName: string,
@@ -158,38 +179,8 @@ async function upgradeFrom_002_to_003(
158179
storage: commonStorage.Storage,
159180
projectName: string,
160181
projectInfo: storageProject.ProjectInfo): Promise<void> {
161-
// Opmodes had robot as a parameter to init method
162-
const projectFileNames: string[] = await storage.list(
163-
storageNames.makeProjectDirectoryPath(projectName));
164-
165-
for (const projectFileName of projectFileNames) {
166-
const modulePath = storageNames.makeFilePath(projectName, projectFileName);
167-
168-
if (storageNames.getModuleType(modulePath) === storageModule.ModuleType.OPMODE) {
169-
let moduleContentText = await storage.fetchFileContentText(modulePath);
170-
const moduleContent = storageModuleContent.parseModuleContentText(moduleContentText);
171-
let blocks = moduleContent.getBlocks();
172-
173-
// Create a temporary workspace to upgrade the blocks.
174-
const headlessWorkspace = workspaces.createHeadlessWorkspace(storageModule.ModuleType.ROBOT);
175-
176-
try {
177-
Blockly.serialization.workspaces.load(blocks, headlessWorkspace);
178-
179-
// Method blocks need to be upgraded
180-
headlessWorkspace.getBlocksByType(MRC_CLASS_METHOD_DEF_BLOCK_NAME, false).forEach(block => {
181-
(block as ClassMethodDefBlock).upgrade_002_to_003();
182-
});
183-
blocks = Blockly.serialization.workspaces.save(headlessWorkspace);
184-
} finally {
185-
workspaces.destroyHeadlessWorkspace(headlessWorkspace);
186-
}
187-
188-
moduleContent.setBlocks(blocks);
189-
moduleContentText = moduleContent.getModuleContentText();
190-
await storage.saveFile(modulePath, moduleContentText);
191-
}
192-
}
182+
// OpModes had robot as a parameter to init method.
183+
await upgradeBlocksFiles(storage, projectName, isOpMode, upgrade_002_to_003);
193184
projectInfo.version = '0.0.3';
194185
}
195186

@@ -207,6 +198,6 @@ async function upgradeFrom_004_to_005(
207198
projectName: string,
208199
projectInfo: storageProject.ProjectInfo): Promise<void> {
209200
// mrc_class_method_def blocks that return a value need to have returnType changed from 'Any' to ''.
210-
await upgradeBlocksFiles(storage, projectName, upgrade_004_to_005);
201+
await upgradeBlocksFiles(storage, projectName, isAnyModuleType, upgrade_004_to_005);
211202
projectInfo.version = '0.0.5';
212203
}

0 commit comments

Comments
 (0)