@@ -28,37 +28,45 @@ import * as storageModule from './module';
2828import * as storageModuleContent from './module_content' ;
2929import * as storageNames from './names' ;
3030import * as storageProject from './project' ;
31+ import { ClassMethodDefBlock , Parameter , 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
3336export async function upgradeProjectIfNecessary (
34- storage : commonStorage . Storage , projectName : string ) : Promise < void > {
37+ 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+ // @ts -ignore
50+ upgradeFrom_002_to_003 ( storage , projectName , projectInfo ) ;
51+
4452 }
4553 await storageProject . saveProjectInfo ( storage , projectName ) ;
4654 }
4755}
4856
4957async function upgradeFrom_000_to_001 (
50- _storage : commonStorage . Storage ,
51- _projectName : string ,
52- projectInfo : storageProject . ProjectInfo ) : Promise < void > {
58+ _storage : commonStorage . Storage ,
59+ _projectName : string ,
60+ projectInfo : storageProject . ProjectInfo ) : Promise < void > {
5361 // Project was saved without a project.info.json file.
5462 // Nothing needs to be done to upgrade to '0.0.1';
5563 projectInfo . version = '0.0.1' ;
5664}
5765
5866async function upgradeFrom_001_to_002 (
59- storage : commonStorage . Storage ,
60- projectName : string ,
61- projectInfo : storageProject . ProjectInfo ) : Promise < void > {
67+ storage : commonStorage . Storage ,
68+ projectName : string ,
69+ projectInfo : storageProject . ProjectInfo ) : Promise < void > {
6270 // Modules were saved without private components.
6371 // The Robot's mrc_mechanism_component_holder block was saved without hidePrivateComponents.
6472 const projectFileNames : string [ ] = await storage . list (
@@ -92,3 +100,48 @@ async function upgradeFrom_001_to_002(
92100 }
93101 projectInfo . version = '0.0.2' ;
94102}
103+
104+ async function upgradeFrom_002_to_003 (
105+ storage : commonStorage . Storage ,
106+ projectName : string ,
107+ projectInfo : storageProject . ProjectInfo ) : Promise < void > {
108+ // Opmodes had robot as a parameter to init method
109+ const projectFileNames : string [ ] = await storage . list (
110+ storageNames . makeProjectDirectoryPath ( projectName ) ) ;
111+
112+ for ( const projectFileName of projectFileNames ) {
113+ const modulePath = storageNames . makeFilePath ( projectName , projectFileName ) ;
114+
115+ if ( storageNames . getModuleType ( modulePath ) === storageModule . ModuleType . OPMODE ) {
116+ let moduleContentText = await storage . fetchFileContentText ( modulePath ) ;
117+ const moduleContent = storageModuleContent . parseModuleContentText ( moduleContentText ) ;
118+ let blocks = moduleContent . getBlocks ( ) ;
119+
120+ // Create a temporary workspace to upgrade the blocks
121+ const headlessWorkspace = new Blockly . Workspace ( ) ;
122+ try {
123+ Blockly . serialization . workspaces . load ( blocks , headlessWorkspace ) ;
124+
125+ // Find and modify init method blocks to remove robot parameter
126+ const allBlocks = headlessWorkspace . getAllBlocks ( ) ;
127+ for ( const block of allBlocks ) {
128+ if ( block . type === MRC_CLASS_METHOD_DEF_BLOCK_NAME &&
129+ block . getFieldValue ( 'NAME' ) === 'init' ) {
130+ // Remove robot parameter from init method
131+ const methodBlock = block as ClassMethodDefBlock ;
132+ let filteredParams : Parameter [ ] = methodBlock . mrcParameters . filter ( param => param . name !== 'robot' ) ;
133+ methodBlock . mrcParameters = filteredParams ;
134+ }
135+ }
136+ blocks = Blockly . serialization . workspaces . save ( headlessWorkspace ) ;
137+ } finally {
138+ headlessWorkspace . dispose ( ) ;
139+ }
140+
141+ moduleContent . setBlocks ( blocks ) ;
142+ moduleContentText = moduleContent . getModuleContentText ( ) ;
143+ await storage . saveFile ( modulePath , moduleContentText ) ;
144+ }
145+ }
146+ projectInfo . version = '0.0.3' ;
147+ }
0 commit comments