@@ -4,11 +4,11 @@ import * as path from 'path';
44import { BaseTreeItem } from './BaseTreeItem' ;
55import { IotResult , StatusResult } from './IotResult' ;
66import { IotDevice } from './IotDevice' ;
7- import { LaunchOptionsNode } from './LaunchOptionsNode' ;
87import { IotLaunchEnvironment } from './IotLaunchEnvironment' ;
98import { IotOption } from './IotOption' ;
109import { IoTHelper } from './Helper/IoTHelper' ;
1110import { launchHelper } from './Helper/launchHelper' ;
11+ import { IotConfiguration } from './Configuration/IotConfiguration' ;
1212
1313export class IotLaunch {
1414
@@ -129,7 +129,7 @@ export class IotLaunch {
129129 const idDevice :string = jsonObj . fastiotIdDevice ;
130130 const device = devices . find ( x => x . IdDevice == idDevice ) ;
131131 if ( device )
132- this . _device = device . IdDevice ;
132+ this . _device = device ;
133133 else this . _device = idDevice ;
134134 //fastiotProject
135135 this . _pathProject = jsonObj . fastiotProject ;
@@ -217,7 +217,7 @@ export class IotLaunch {
217217 result = this . GetJsonLaunch ( ) ;
218218 if ( result . Status == StatusResult . Error ) return result ;
219219 let jsonLaunch = result . returnObject ;
220- result = new IotResult ( StatusResult . Error , `Launch not found. Name : ${ this . IdLaunch } ` ) ;
220+ result = new IotResult ( StatusResult . Error , `Launch not found. IdLaunch : ${ this . IdLaunch } ` ) ;
221221 //change
222222 const launch = jsonLaunch . configurations . find ( ( x :any ) => x . fastiotIdLaunch == this . IdLaunch ) ;
223223 if ( launch ) {
@@ -226,7 +226,7 @@ export class IotLaunch {
226226 result = this . SaveLaunch ( jsonLaunch ) ;
227227 }
228228 } catch ( err : any ) {
229- result = new IotResult ( StatusResult . Error , `Rename launch. Name : ${ this . IdLaunch } ` , err ) ;
229+ result = new IotResult ( StatusResult . Error , `Rename launch. IdLaunch : ${ this . IdLaunch } ` , err ) ;
230230 }
231231 return result ;
232232 }
@@ -458,4 +458,110 @@ export class IotLaunch {
458458 return result ;
459459 }
460460
461+ public RebuildLaunch ( config :IotConfiguration , devices : Array < IotDevice > ) : IotResult {
462+ let result :IotResult ;
463+ //--------------Checks--------------
464+ //check device
465+ if ( ! this . Device ) {
466+ result = new IotResult ( StatusResult . Error , `Missing device for idLaunch: ${ this . IdLaunch } ` ) ;
467+ return result ;
468+ }
469+ if ( typeof this . Device === "string" ) {
470+ result = new IotResult ( StatusResult . Error , `Missing device for idLaunch: ${ this . IdLaunch } ` ) ;
471+ return result ;
472+ }
473+ //check project
474+ const projectMainfilePath = IoTHelper . ReverseSeparatorLinuxToWin ( `${ this . _workspaceDirectory } ${ this . PathProject } ` ) ;
475+ if ( ! fs . existsSync ( projectMainfilePath ) ) {
476+ result = new IotResult ( StatusResult . Error , `Missing project: ${ projectMainfilePath } ` ) ;
477+ return result ;
478+ }
479+ //check template
480+ const template = config . Templates . FindbyId ( this . IdTemplate ?? "non" ) ;
481+ if ( ! template ) {
482+ result = new IotResult ( StatusResult . Error , `Missing template: ${ this . IdTemplate } ` ) ;
483+ return result ;
484+ }
485+ //--------------Main--------------
486+ //get json linked Launchs
487+ result = this . GetJsonLaunch ( ) ;
488+ if ( result . Status == StatusResult . Error ) return result ;
489+ let jsonLaunch = < any > result . returnObject ;
490+ let jsonLinkedLaunchs = jsonLaunch . configurations . filter ( ( e :any ) => e . fastiotIdLaunch ) ;
491+ jsonLinkedLaunchs = jsonLinkedLaunchs . filter ( ( e :any ) => e . fastiotIdLaunch . includes ( this . IdLaunch ?. substring ( 0 , 8 ) ) ) ;
492+ //create a Message and get IotLaunch LinkedLaunchs
493+ let LinkedLaunchs :Array < IotLaunch > = [ ] ;
494+ let msg = "\n" ;
495+ let index = 1 ;
496+ jsonLinkedLaunchs . forEach ( ( item :any ) => {
497+ let launchItem = new IotLaunch ( this . _workspaceDirectory ) ;
498+ result = launchItem . FromJSON ( item , devices ) ;
499+ if ( result . Status == StatusResult . Error ) return Promise . resolve ( result ) ;
500+ LinkedLaunchs . push ( launchItem ) ;
501+ //
502+ msg = msg + `${ index } ) ${ launchItem . IdLaunch } ${ launchItem . Label ?. toString ( ) } \n` ;
503+ index ++ ;
504+ } ) ;
505+ vscode . window . showInformationMessage ( `Rebuilding Launches: ${ msg } ` ) ;
506+ //remove linked launchs
507+ LinkedLaunchs . forEach ( ( item ) => {
508+ result = item . Remove ( ) ;
509+ if ( result . Status == StatusResult . Error ) return Promise . resolve ( result ) ;
510+ } ) ;
511+ //Add Configuration Vscode
512+ let values :Map < string , string > = new Map < string , string > ( ) ;
513+ //Preparing values
514+ const baseName = path . basename ( projectMainfilePath ) ;
515+ const projectName = baseName . substring ( 0 , baseName . length - template . Attributes . ExtMainFileProj . length ) ;
516+ values . set ( "%{project.mainfile.path.full.aswindows}" , projectMainfilePath ) ;
517+ values . set ( "%{project.name}" , projectName ) ;
518+ result = template . AddConfigurationVscode ( this . Device , config , this . _workspaceDirectory , values ) ;
519+ if ( result . Status == StatusResult . Error ) return result ;
520+ const newLaunchId = result . tag ;
521+ //--------------End of process--------------
522+ //Name and env recovery
523+ result = this . GetJsonLaunch ( ) ;
524+ if ( result . Status == StatusResult . Error ) return result ;
525+ let jsonNewLaunch = result . returnObject ;
526+ //launchs
527+ index = 0 ;
528+ do {
529+ let newItemLaunch = jsonNewLaunch . configurations [ index ] ;
530+ if ( newItemLaunch )
531+ {
532+ if ( newItemLaunch . fastiotIdLaunch && newItemLaunch . fastiotIdLaunch . includes ( newLaunchId ) ) {
533+ const oldItemLaunch = jsonLinkedLaunchs . find ( ( x :any ) => x . fastiotIdLaunch . substring ( 8 ) == newItemLaunch . fastiotIdLaunch . substring ( 8 ) ) ;
534+ if ( oldItemLaunch )
535+ {
536+ //replace name
537+ newItemLaunch . name = oldItemLaunch . name ;
538+ //replace env
539+ if ( oldItemLaunch . env ) {
540+ if ( newItemLaunch . env ) {
541+ if ( JSON . stringify ( oldItemLaunch . env ) != "{}" )
542+ newItemLaunch . env = oldItemLaunch . env ;
543+ } else {
544+ newItemLaunch . push ( oldItemLaunch . env ) ;
545+ }
546+ }
547+ }
548+ }
549+ index = index + 1 ;
550+ } else break ;
551+ }
552+ while ( true )
553+ //result
554+ result = this . SaveLaunch ( jsonNewLaunch ) ;
555+ if ( result . Status == StatusResult . Error ) return result ;
556+ return result ;
557+ }
558+
559+
560+
561+
562+
563+
564+
565+
566+
461567}
0 commit comments