Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 36 additions & 8 deletions src/blocks/mrc_mechanism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ export const FIELD_TYPE = 'TYPE';
type Parameter = {
name: string,
type: string,
componentId?: string,
componentPortsIndex?: number, // The zero-based number when iterating through component.ports.
};

type MechanismExtraState = {
mechanismModuleId?: string,
mechanismId?: string,
importModule?: string,
parameters?: Parameter[],
parametersHaveComponentInformation?: boolean,
}

const WARNING_ID_NOT_IN_HOLDER = 'not in holder';
Expand All @@ -66,6 +69,7 @@ interface MechanismMixin extends MechanismMixinType {
mrcMechanismId: string,
mrcImportModule: string,
mrcParameters: Parameter[],
mrcParametersHaveComponentInformation: boolean,

/**
* mrcHasNotInHolderWarning is set to true if we set the NOT_IN_HOLDER warning text on the block.
Expand All @@ -83,6 +87,7 @@ const MECHANISM = {
* Block initialization.
*/
init: function (this: MechanismBlock): void {
this.mrcParametersHaveComponentInformation = false;
this.mrcHasNotInHolderWarning = false;
this.setStyle(MRC_STYLE_MECHANISMS);
const nameField = new Blockly.FieldTextInput('')
Expand All @@ -102,13 +107,11 @@ const MECHANISM = {
const extraState: MechanismExtraState = {
mechanismModuleId: this.mrcMechanismModuleId,
mechanismId: this.mrcMechanismId,
parametersHaveComponentInformation: this.mrcParametersHaveComponentInformation,
};
extraState.parameters = [];
this.mrcParameters.forEach((arg) => {
extraState.parameters!.push({
name: arg.name,
type: arg.type,
});
extraState.parameters!.push({...arg});
});
if (this.mrcImportModule) {
extraState.importModule = this.mrcImportModule;
Expand All @@ -125,12 +128,11 @@ const MECHANISM = {
this.mrcParameters = [];
if (extraState.parameters) {
extraState.parameters.forEach((arg) => {
this.mrcParameters.push({
name: arg.name,
type: arg.type,
});
this.mrcParameters.push({...arg});
});
}
this.mrcParametersHaveComponentInformation = (extraState.parametersHaveComponentInformation == undefined)
? false : extraState.parametersHaveComponentInformation;
this.updateBlock_();
},
/**
Expand Down Expand Up @@ -289,13 +291,18 @@ const MECHANISM = {
}
this.mrcParameters = [];
components.forEach(component => {
let componentPortsIndex = 0;
for (const port in component.ports) {
this.mrcParameters.push({
name: port,
type: component.ports[port],
componentId: component.componentId,
componentPortsIndex,
});
componentPortsIndex++;
}
});
this.mrcParametersHaveComponentInformation = true;
this.updateBlock_();
} else {
// Did not find the mechanism.
Expand Down Expand Up @@ -324,6 +331,12 @@ const MECHANISM = {
this.mrcMechanismId = oldIdToNewId[this.mrcMechanismId];
}
},
upgrade_005_to_006: function(this: MechanismBlock) {
// At the time when this upgrade method is called, we can't look up the component information
// for each parameter. Instead, we just mark this block as not having the information. Later,
// in the checkMechanism method, we will retrieve the component information for each parameter.
this.mrcParametersHaveComponentInformation = false;
},
};

export const setup = function () {
Expand Down Expand Up @@ -359,17 +372,22 @@ export function createMechanismBlock(
mechanismModuleId: mechanism.moduleId,
importModule: snakeCaseName,
parameters: [],
parametersHaveComponentInformation: true,
};
const inputs: {[key: string]: any} = {};
let i = 0;
components.forEach(component => {
let componentPortsIndex = 0;
for (const port in component.ports) {
const parameterType = component.ports[port];
extraState.parameters?.push({
name: port,
type: parameterType,
componentId: component.componentId,
componentPortsIndex,
});
inputs['ARG' + i] = createPort(parameterType);
componentPortsIndex++;
i++;
}
});
Expand All @@ -378,3 +396,13 @@ export function createMechanismBlock(
fields[FIELD_TYPE] = mechanism.className;
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, inputs);
}

/**
* Upgrades the MechanismBlocks in the given workspace from version 005 to 006.
* This function should only be called when upgrading old projects.
*/
export function upgrade_005_to_006(workspace: Blockly.Workspace): void {
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
(block as MechanismBlock).upgrade_005_to_006();
});
}
20 changes: 19 additions & 1 deletion src/storage/upgrade_project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ import * as storageNames from './names';
import * as storageProject from './project';
import { upgrade_001_to_002 } from '../blocks/mrc_mechanism_component_holder';
import { upgrade_002_to_003, upgrade_004_to_005 } from '../blocks/mrc_class_method_def';
import { upgrade_005_to_006 } from '../blocks/mrc_mechanism';
import * as workspaces from '../blocks/utils/workspaces';

export const NO_VERSION = '0.0.0';
export const CURRENT_VERSION = '0.0.5';
export const CURRENT_VERSION = '0.0.6';

export async function upgradeProjectIfNecessary(
storage: commonStorage.Storage, projectName: string): Promise<void> {
Expand Down Expand Up @@ -66,6 +67,11 @@ export async function upgradeProjectIfNecessary(
// @ts-ignore
case '0.0.4':
upgradeFrom_004_to_005(storage, projectName, projectInfo);

// Intentional fallthrough after case '0.0.5'
// @ts-ignore
case '0.0.5':
upgradeFrom_005_to_006(storage, projectName, projectInfo);
}
await storageProject.saveProjectInfo(storage, projectName);
}
Expand Down Expand Up @@ -212,3 +218,15 @@ async function upgradeFrom_004_to_005(
anyModuleType, upgrade_004_to_005);
projectInfo.version = '0.0.5';
}

async function upgradeFrom_005_to_006(
storage: commonStorage.Storage,
projectName: string,
projectInfo: storageProject.ProjectInfo): Promise<void> {
// mrc_mechanism blocks may have component information for each parameter.
await upgradeBlocksFiles(
storage, projectName,
noModuleTypes, noPreupgrade,
anyModuleType, upgrade_005_to_006);
projectInfo.version = '0.0.6';
}