@@ -28,37 +28,45 @@ 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 , Parameter , 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
- storage : commonStorage . Storage , projectName : string ) : Promise < void > {
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
+ // @ts -ignore
50
+ upgradeFrom_002_to_003 ( storage , projectName , projectInfo ) ;
51
+
44
52
}
45
53
await storageProject . saveProjectInfo ( storage , projectName ) ;
46
54
}
47
55
}
48
56
49
57
async 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 > {
53
61
// Project was saved without a project.info.json file.
54
62
// Nothing needs to be done to upgrade to '0.0.1';
55
63
projectInfo . version = '0.0.1' ;
56
64
}
57
65
58
66
async 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 > {
62
70
// Modules were saved without private components.
63
71
// The Robot's mrc_mechanism_component_holder block was saved without hidePrivateComponents.
64
72
const projectFileNames : string [ ] = await storage . list (
@@ -92,3 +100,48 @@ async function upgradeFrom_001_to_002(
92
100
}
93
101
projectInfo . version = '0.0.2' ;
94
102
}
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