@@ -74,6 +74,8 @@ export async function upgradeProjectIfNecessary(
7474async function upgradeBlocksFiles (
7575 storage : commonStorage . Storage ,
7676 projectName : string ,
77+ preupgradePredicate : ( moduleType : storageModule . ModuleType ) => boolean ,
78+ preupgradeFunc : ( moduleContentText : string ) => string ,
7779 upgradePredicate : ( moduleType : storageModule . ModuleType ) => boolean ,
7880 upgradeFunc : ( w : Blockly . Workspace ) => void
7981) : Promise < void > {
@@ -85,6 +87,10 @@ async function upgradeBlocksFiles(
8587 const originalModuleContentText = await storage . fetchFileContentText ( modulePath ) ;
8688 let moduleContentText = originalModuleContentText ;
8789
90+ if ( preupgradePredicate ( moduleType ) ) {
91+ moduleContentText = preupgradeFunc ( moduleContentText ) ;
92+ }
93+
8894 if ( upgradePredicate ( moduleType ) ) {
8995 const moduleContent = storageModuleContent . parseModuleContentText ( moduleContentText ) ;
9096 let blocks = moduleContent . getBlocks ( ) ;
@@ -126,6 +132,29 @@ function isOpMode(moduleType: storageModule.ModuleType): boolean {
126132 return moduleType === storageModule . ModuleType . OPMODE ;
127133}
128134
135+ /**
136+ * Predicate function that can be passed to upgradeBlocksFiles indicating that only Robot modules
137+ * should be affected.
138+ */
139+ function isRobot ( moduleType : storageModule . ModuleType ) : boolean {
140+ return moduleType === storageModule . ModuleType . ROBOT ;
141+ }
142+
143+ /**
144+ * Predicate function that can be passed to upgradeBlocksFiles indicating that no modules should be
145+ * affected.
146+ */
147+ function noModuleTypes ( _moduleType : storageModule . ModuleType ) : boolean {
148+ return false ;
149+ }
150+
151+ /**
152+ * Preupgrade function that makes no changes to moduleContentText.
153+ */
154+ function noPreupgrade ( moduleContentText : string ) : string {
155+ return moduleContentText ;
156+ }
157+
129158async function upgradeFrom_000_to_001 (
130159 _storage : commonStorage . Storage ,
131160 _projectName : string ,
@@ -141,37 +170,10 @@ async function upgradeFrom_001_to_002(
141170 projectInfo : storageProject . ProjectInfo ) : Promise < void > {
142171 // Modules were saved without private components.
143172 // The Robot's mrc_mechanism_component_holder block was saved without hidePrivateComponents.
144- const projectFileNames : string [ ] = await storage . list (
145- storageNames . makeProjectDirectoryPath ( projectName ) ) ;
146- for ( const projectFileName of projectFileNames ) {
147- const modulePath = storageNames . makeFilePath ( projectName , projectFileName ) ;
148- let moduleContentText = await storage . fetchFileContentText ( modulePath ) ;
149-
150- // Add private components to the module content.
151- moduleContentText = storageModuleContent . addPrivateComponents ( moduleContentText ) ;
152-
153- if ( storageNames . getModuleType ( modulePath ) === storageModule . ModuleType . ROBOT ) {
154- // If this module is the robot, hide the private components part of the
155- // mrc_mechanism_component_holder block.
156- const moduleContent = storageModuleContent . parseModuleContentText ( moduleContentText ) ;
157- let blocks = moduleContent . getBlocks ( ) ;
158-
159- // Create a temporary workspace to upgrade the blocks.
160- const headlessWorkspace = workspaces . createHeadlessWorkspace ( storageModule . ModuleType . ROBOT ) ;
161-
162- try {
163- Blockly . serialization . workspaces . load ( blocks , headlessWorkspace ) ;
164- mechanismComponentHolder . hidePrivateComponents ( headlessWorkspace ) ;
165- blocks = Blockly . serialization . workspaces . save ( headlessWorkspace ) ;
166- } finally {
167- workspaces . destroyHeadlessWorkspace ( headlessWorkspace ) ;
168- }
169- moduleContent . setBlocks ( blocks ) ;
170- moduleContentText = moduleContent . getModuleContentText ( ) ;
171- }
172-
173- await storage . saveFile ( modulePath , moduleContentText ) ;
174- }
173+ await upgradeBlocksFiles (
174+ storage , projectName ,
175+ anyModuleType , storageModuleContent . addPrivateComponents ,
176+ isRobot , mechanismComponentHolder . hidePrivateComponents ) ;
175177 projectInfo . version = '0.0.2' ;
176178}
177179
@@ -180,7 +182,10 @@ async function upgradeFrom_002_to_003(
180182 projectName : string ,
181183 projectInfo : storageProject . ProjectInfo ) : Promise < void > {
182184 // OpModes had robot as a parameter to init method.
183- await upgradeBlocksFiles ( storage , projectName , isOpMode , upgrade_002_to_003 ) ;
185+ await upgradeBlocksFiles (
186+ storage , projectName ,
187+ noModuleTypes , noPreupgrade ,
188+ isOpMode , upgrade_002_to_003 ) ;
184189 projectInfo . version = '0.0.3' ;
185190}
186191
@@ -198,6 +203,9 @@ async function upgradeFrom_004_to_005(
198203 projectName : string ,
199204 projectInfo : storageProject . ProjectInfo ) : Promise < void > {
200205 // mrc_class_method_def blocks that return a value need to have returnType changed from 'Any' to ''.
201- await upgradeBlocksFiles ( storage , projectName , anyModuleType , upgrade_004_to_005 ) ;
206+ await upgradeBlocksFiles (
207+ storage , projectName ,
208+ noModuleTypes , noPreupgrade ,
209+ anyModuleType , upgrade_004_to_005 ) ;
202210 projectInfo . version = '0.0.5' ;
203211}
0 commit comments