Skip to content

Commit 83bcb1d

Browse files
committed
In upgrade_project.ts:
Incremented version to 0.0.6. Added upgradeFrom_005_to_006. In mrc_mechanism.ts: Store component information for each parameter.
1 parent 66ed284 commit 83bcb1d

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/blocks/mrc_mechanism.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ export const FIELD_TYPE = 'TYPE';
4848
type Parameter = {
4949
name: string,
5050
type: string,
51+
componentId?: string,
52+
componentPortsIndex?: number, // The zero-based number when iterating through component.ports.
5153
};
5254

5355
type MechanismExtraState = {
5456
mechanismModuleId?: string,
5557
mechanismId?: string,
5658
importModule?: string,
5759
parameters?: Parameter[],
60+
parametersHaveComponentInformation?: boolean,
5861
}
5962

6063
const WARNING_ID_NOT_IN_HOLDER = 'not in holder';
@@ -66,6 +69,7 @@ interface MechanismMixin extends MechanismMixinType {
6669
mrcMechanismId: string,
6770
mrcImportModule: string,
6871
mrcParameters: Parameter[],
72+
mrcParametersHaveComponentInformation: boolean,
6973

7074
/**
7175
* mrcHasNotInHolderWarning is set to true if we set the NOT_IN_HOLDER warning text on the block.
@@ -83,6 +87,7 @@ const MECHANISM = {
8387
* Block initialization.
8488
*/
8589
init: function (this: MechanismBlock): void {
90+
this.mrcParametersHaveComponentInformation = false;
8691
this.mrcHasNotInHolderWarning = false;
8792
this.setStyle(MRC_STYLE_MECHANISMS);
8893
const nameField = new Blockly.FieldTextInput('')
@@ -102,13 +107,11 @@ const MECHANISM = {
102107
const extraState: MechanismExtraState = {
103108
mechanismModuleId: this.mrcMechanismModuleId,
104109
mechanismId: this.mrcMechanismId,
110+
parametersHaveComponentInformation: this.mrcParametersHaveComponentInformation,
105111
};
106112
extraState.parameters = [];
107113
this.mrcParameters.forEach((arg) => {
108-
extraState.parameters!.push({
109-
name: arg.name,
110-
type: arg.type,
111-
});
114+
extraState.parameters!.push({...arg});
112115
});
113116
if (this.mrcImportModule) {
114117
extraState.importModule = this.mrcImportModule;
@@ -125,12 +128,11 @@ const MECHANISM = {
125128
this.mrcParameters = [];
126129
if (extraState.parameters) {
127130
extraState.parameters.forEach((arg) => {
128-
this.mrcParameters.push({
129-
name: arg.name,
130-
type: arg.type,
131-
});
131+
this.mrcParameters.push({...arg});
132132
});
133133
}
134+
this.mrcParametersHaveComponentInformation = (extraState.parametersHaveComponentInformation == undefined)
135+
? false : extraState.parametersHaveComponentInformation;
134136
this.updateBlock_();
135137
},
136138
/**
@@ -289,13 +291,18 @@ const MECHANISM = {
289291
}
290292
this.mrcParameters = [];
291293
components.forEach(component => {
294+
let componentPortsIndex = 0;
292295
for (const port in component.ports) {
293296
this.mrcParameters.push({
294297
name: port,
295298
type: component.ports[port],
299+
componentId: component.componentId,
300+
componentPortsIndex,
296301
});
302+
componentPortsIndex++;
297303
}
298304
});
305+
this.mrcParametersHaveComponentInformation = true;
299306
this.updateBlock_();
300307
} else {
301308
// Did not find the mechanism.
@@ -324,6 +331,12 @@ const MECHANISM = {
324331
this.mrcMechanismId = oldIdToNewId[this.mrcMechanismId];
325332
}
326333
},
334+
upgrade_005_to_006: function(this: MechanismBlock) {
335+
// At the time when this upgrade method is called, we can't look up the component information
336+
// for each parameter. Instead, we just mark this block as not having the information. Later,
337+
// in the checkMechanism method, we will retrieve the component information for each parameter.
338+
this.mrcParametersHaveComponentInformation = false;
339+
},
327340
};
328341

329342
export const setup = function () {
@@ -359,17 +372,22 @@ export function createMechanismBlock(
359372
mechanismModuleId: mechanism.moduleId,
360373
importModule: snakeCaseName,
361374
parameters: [],
375+
parametersHaveComponentInformation: true,
362376
};
363377
const inputs: {[key: string]: any} = {};
364378
let i = 0;
365379
components.forEach(component => {
380+
let componentPortsIndex = 0;
366381
for (const port in component.ports) {
367382
const parameterType = component.ports[port];
368383
extraState.parameters?.push({
369384
name: port,
370385
type: parameterType,
386+
componentId: component.componentId,
387+
componentPortsIndex,
371388
});
372389
inputs['ARG' + i] = createPort(parameterType);
390+
componentPortsIndex++;
373391
i++;
374392
}
375393
});
@@ -378,3 +396,13 @@ export function createMechanismBlock(
378396
fields[FIELD_TYPE] = mechanism.className;
379397
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, inputs);
380398
}
399+
400+
/**
401+
* Upgrades the MechanismBlocks in the given workspace from version 005 to 006.
402+
* This function should only be called when upgrading old projects.
403+
*/
404+
export function upgrade_005_to_006(workspace: Blockly.Workspace): void {
405+
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
406+
(block as MechanismBlock).upgrade_005_to_006();
407+
});
408+
}

src/storage/upgrade_project.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ import * as storageNames from './names';
2929
import * as storageProject from './project';
3030
import { upgrade_001_to_002 } from '../blocks/mrc_mechanism_component_holder';
3131
import { upgrade_002_to_003, upgrade_004_to_005 } from '../blocks/mrc_class_method_def';
32+
import { upgrade_005_to_006 } from '../blocks/mrc_mechanism';
3233
import * as workspaces from '../blocks/utils/workspaces';
3334

3435
export const NO_VERSION = '0.0.0';
35-
export const CURRENT_VERSION = '0.0.5';
36+
export const CURRENT_VERSION = '0.0.6';
3637

3738
export async function upgradeProjectIfNecessary(
3839
storage: commonStorage.Storage, projectName: string): Promise<void> {
@@ -66,6 +67,11 @@ export async function upgradeProjectIfNecessary(
6667
// @ts-ignore
6768
case '0.0.4':
6869
upgradeFrom_004_to_005(storage, projectName, projectInfo);
70+
71+
// Intentional fallthrough after case '0.0.5'
72+
// @ts-ignore
73+
case '0.0.5':
74+
upgradeFrom_005_to_006(storage, projectName, projectInfo);
6975
}
7076
await storageProject.saveProjectInfo(storage, projectName);
7177
}
@@ -212,3 +218,15 @@ async function upgradeFrom_004_to_005(
212218
anyModuleType, upgrade_004_to_005);
213219
projectInfo.version = '0.0.5';
214220
}
221+
222+
async function upgradeFrom_005_to_006(
223+
storage: commonStorage.Storage,
224+
projectName: string,
225+
projectInfo: storageProject.ProjectInfo): Promise<void> {
226+
// mrc_mechanism blocks may have component information for each parameter.
227+
await upgradeBlocksFiles(
228+
storage, projectName,
229+
noModuleTypes, noPreupgrade,
230+
anyModuleType, upgrade_005_to_006);
231+
projectInfo.version = '0.0.6';
232+
}

0 commit comments

Comments
 (0)