@@ -32,30 +32,40 @@ import { ClassMethodDefBlock, BLOCK_NAME as MRC_CLASS_METHOD_DEF_BLOCK_NAME } fr
3232import * as workspaces from '../blocks/utils/workspaces' ;
3333
3434export const NO_VERSION = '0.0.0' ;
35- export const CURRENT_VERSION = '0.0.4 ' ;
35+ export const CURRENT_VERSION = '0.0.5 ' ;
3636
3737export async function upgradeProjectIfNecessary (
3838 storage : commonStorage . Storage , projectName : string ) : Promise < void > {
3939 const projectInfo = await storageProject . fetchProjectInfo ( storage , projectName ) ;
4040 if ( semver . lt ( projectInfo . version , CURRENT_VERSION ) ) {
4141 switch ( projectInfo . version ) {
42+ default :
43+ throw new Error ( 'Unrecognized project version: ' + projectInfo . version ) ;
44+
45+ // Intentional fallthrough after case '0.0.0'.
4246 // @ts -ignore
4347 case '0.0.0' :
4448 upgradeFrom_000_to_001 ( storage , projectName , projectInfo )
45- // Intentional fallthrough
49+
50+ // Intentional fallthrough after case '0.0.1'
4651 // @ts -ignore
4752 case '0.0.1' :
4853 upgradeFrom_001_to_002 ( storage , projectName , projectInfo ) ;
49- // Intentional fallthrough
54+
55+ // Intentional fallthrough after case '0.0.2'
5056 // @ts -ignore
5157 case '0.0.2' :
5258 upgradeFrom_002_to_003 ( storage , projectName , projectInfo ) ;
59+
60+ // Intentional fallthrough after case '0.0.3'
61+ // @ts -ignore
5362 case '0.0.3' :
5463 upgradeFrom_003_to_004 ( storage , projectName , projectInfo ) ;
55- break ;
56- default :
57- throw new Error ( 'Unrecognized project version: ' + projectInfo . version ) ;
5864
65+ // Intentional fallthrough after case '0.0.4'
66+ // @ts -ignore
67+ case '0.0.4' :
68+ upgradeFrom_004_to_005 ( storage , projectName , projectInfo ) ;
5969 }
6070 await storageProject . saveProjectInfo ( storage , projectName ) ;
6171 }
@@ -157,3 +167,39 @@ async function upgradeFrom_003_to_004(
157167 // from loading a project with an older version of software.
158168 projectInfo . version = '0.0.4' ;
159169}
170+
171+ async function upgradeFrom_004_to_005 (
172+ storage : commonStorage . Storage ,
173+ projectName : string ,
174+ projectInfo : storageProject . ProjectInfo ) : Promise < void > {
175+ // mrc_class_method_def blocks that return a value need to have returnType changed from 'Any' to ''.
176+ const projectFileNames : string [ ] = await storage . list (
177+ storageNames . makeProjectDirectoryPath ( projectName ) ) ;
178+ for ( const projectFileName of projectFileNames ) {
179+ const modulePath = storageNames . makeFilePath ( projectName , projectFileName ) ;
180+
181+ let moduleContentText = await storage . fetchFileContentText ( modulePath ) ;
182+ const moduleContent = storageModuleContent . parseModuleContentText ( moduleContentText ) ;
183+ let blocks = moduleContent . getBlocks ( ) ;
184+
185+ // Create a temporary workspace to upgrade the blocks.
186+ const headlessWorkspace = workspaces . createHeadlessWorkspace ( storageModule . ModuleType . ROBOT ) ;
187+
188+ try {
189+ Blockly . serialization . workspaces . load ( blocks , headlessWorkspace ) ;
190+
191+ // Method blocks need to be upgraded
192+ headlessWorkspace . getBlocksByType ( MRC_CLASS_METHOD_DEF_BLOCK_NAME , false ) . forEach ( block => {
193+ ( block as ClassMethodDefBlock ) . upgrade_004_to_005 ( ) ;
194+ } ) ;
195+ blocks = Blockly . serialization . workspaces . save ( headlessWorkspace ) ;
196+ } finally {
197+ workspaces . destroyHeadlessWorkspace ( headlessWorkspace ) ;
198+ }
199+
200+ moduleContent . setBlocks ( blocks ) ;
201+ moduleContentText = moduleContent . getModuleContentText ( ) ;
202+ await storage . saveFile ( modulePath , moduleContentText ) ;
203+ }
204+ projectInfo . version = '0.0.5' ;
205+ }
0 commit comments