@@ -28,19 +28,26 @@ import * as storageModule from './module';
2828import * as storageModuleContent from './module_content' ;
2929import * as storageNames from './names' ;
3030import * 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
3336export 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