@@ -28,19 +28,26 @@ import * as storageModule from './module';
28
28
import * as storageModuleContent from './module_content' ;
29
29
import * as storageNames from './names' ;
30
30
import * as storageProject from './project' ;
31
+ import { ClassMethodDefBlock , BLOCK_NAME as MRC_CLASS_METHOD_DEF_BLOCK_NAME } from '../blocks/mrc_class_method_def' ;
31
32
33
+ export const NO_VERSION = '0.0.0' ;
34
+ export const CURRENT_VERSION = '0.0.3' ;
32
35
33
36
export async function upgradeProjectIfNecessary (
34
37
storage : commonStorage . Storage , projectName : string ) : Promise < void > {
35
38
const projectInfo = await storageProject . fetchProjectInfo ( storage , projectName ) ;
36
- if ( semver . lt ( projectInfo . version , storageProject . CURRENT_VERSION ) ) {
39
+ if ( semver . lt ( projectInfo . version , CURRENT_VERSION ) ) {
37
40
switch ( projectInfo . version ) {
38
41
// @ts -ignore
39
42
case '0.0.0' :
40
43
upgradeFrom_000_to_001 ( storage , projectName , projectInfo )
41
- // Intentional fallthrough
44
+ // Intentional fallthrough
45
+ // @ts -ignore
42
46
case '0.0.1' :
43
47
upgradeFrom_001_to_002 ( storage , projectName , projectInfo ) ;
48
+ case '0.0.2' :
49
+ upgradeFrom_002_to_003 ( storage , projectName , projectInfo ) ;
50
+
44
51
}
45
52
await storageProject . saveProjectInfo ( storage , projectName ) ;
46
53
}
@@ -92,3 +99,41 @@ async function upgradeFrom_001_to_002(
92
99
}
93
100
projectInfo . version = '0.0.2' ;
94
101
}
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