@@ -28,39 +28,83 @@ 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' ;
31+ import { ClassMethodDefBlock , BLOCK_NAME as MRC_CLASS_METHOD_DEF_BLOCK_NAME , upgrade_004_to_005 } from '../blocks/mrc_class_method_def' ;
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' :
52- upgradeFrom_002_to_003 ( storage , projectName , projectInfo ) ;
58+ 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 }
6272}
6373
74+ async function upgradeBlocksFiles (
75+ storage : commonStorage . Storage ,
76+ projectName : string ,
77+ upgradeFunc : ( w : Blockly . Workspace ) => void
78+ ) : Promise < void > {
79+ const projectFileNames : string [ ] = await storage . list (
80+ storageNames . makeProjectDirectoryPath ( projectName ) ) ;
81+ for ( const projectFileName of projectFileNames ) {
82+ const modulePath = storageNames . makeFilePath ( projectName , projectFileName ) ;
83+ const moduleType = storageNames . getModuleType ( modulePath ) ;
84+
85+ let moduleContentText = await storage . fetchFileContentText ( modulePath ) ;
86+ const moduleContent = storageModuleContent . parseModuleContentText ( moduleContentText ) ;
87+ let blocks = moduleContent . getBlocks ( ) ;
88+
89+ // Create a temporary workspace to upgrade the blocks.
90+ const headlessWorkspace = workspaces . createHeadlessWorkspace ( moduleType ) ;
91+
92+ try {
93+ Blockly . serialization . workspaces . load ( blocks , headlessWorkspace ) ;
94+
95+ upgradeFunc ( headlessWorkspace ) ;
96+
97+ blocks = Blockly . serialization . workspaces . save ( headlessWorkspace ) ;
98+ } finally {
99+ workspaces . destroyHeadlessWorkspace ( headlessWorkspace ) ;
100+ }
101+
102+ moduleContent . setBlocks ( blocks ) ;
103+ moduleContentText = moduleContent . getModuleContentText ( ) ;
104+ await storage . saveFile ( modulePath , moduleContentText ) ;
105+ }
106+ }
107+
64108async function upgradeFrom_000_to_001 (
65109 _storage : commonStorage . Storage ,
66110 _projectName : string ,
@@ -157,3 +201,12 @@ async function upgradeFrom_003_to_004(
157201 // from loading a project with an older version of software.
158202 projectInfo . version = '0.0.4' ;
159203}
204+
205+ async function upgradeFrom_004_to_005 (
206+ storage : commonStorage . Storage ,
207+ projectName : string ,
208+ projectInfo : storageProject . ProjectInfo ) : Promise < void > {
209+ // mrc_class_method_def blocks that return a value need to have returnType changed from 'Any' to ''.
210+ await upgradeBlocksFiles ( storage , projectName , upgrade_004_to_005 ) ;
211+ projectInfo . version = '0.0.5' ;
212+ }
0 commit comments