Skip to content

Commit bf96e0f

Browse files
committed
Launch: separating logic from Node display
1 parent 795a9d2 commit bf96e0f

18 files changed

+359
-825
lines changed

src/BaseTreeItemNode.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as vscode from 'vscode';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
5+
export abstract class BaseTreeItemNode extends vscode.TreeItem {
6+
public abstract Parent: BaseTreeItemNode| any| undefined;
7+
public abstract Childs: Array<BaseTreeItemNode| any>;
8+
9+
constructor(
10+
label: string,
11+
description: string| undefined,
12+
tooltip: string | vscode.MarkdownString | undefined,
13+
collapsibleState: vscode.TreeItemCollapsibleState,
14+
){
15+
super(label, collapsibleState);
16+
this.description = description;
17+
//tooltip Markdown
18+
this.tooltip = tooltip;
19+
}
20+
}

src/Configuration/IotConfigurationFolder.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export class IotConfigurationFolder {
2828
return this.ApplicationData+"\\tmp";}
2929
public get Schemas(): string {
3030
return this.Extension+"\\schemas";}
31+
public get WorkspaceFolder(): string| undefined {
32+
return IoTHelper.GetWorkspaceFolder();}
3133

3234
constructor(
3335
applicationDataPath: string,

src/IotItemTree.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import {IotDeviceAccount} from './IotDeviceAccount';
77
import {IotDeviceInformation} from './IotDeviceInformation';
88
import {IotDevicePackage} from './IotDevicePackage';
99
import {LaunchNode} from './LaunchNode';
10-
import {LaunchOptionsNode} from './LaunchOptionsNode';
1110

1211
export class IotItemTree extends BaseTreeItem {
1312
public Parent: IotDevice| IotDeviceAccount| IotDeviceInformation| IotItemTree|
14-
IotDevicePackage| LaunchNode| LaunchOptionsNode;
13+
IotDevicePackage| LaunchNode;
1514
public Childs: Array<IotItemTree>=[];
1615
public Device: IotDevice;
1716

@@ -21,7 +20,7 @@ export class IotItemTree extends BaseTreeItem {
2120
tooltip: string | vscode.MarkdownString | undefined,
2221
collapsibleState: vscode.TreeItemCollapsibleState,
2322
parent: IotDevice| IotDeviceAccount| IotDeviceInformation| IotItemTree|
24-
IotDevicePackage| LaunchNode| LaunchOptionsNode,
23+
IotDevicePackage| LaunchNode,
2524
device: IotDevice
2625
){
2726
super(label,description,tooltip,collapsibleState);

src/IotLaunch.ts

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import * as path from 'path';
44
import {BaseTreeItem} from './BaseTreeItem';
55
import {IotResult,StatusResult } from './IotResult';
66
import {IotDevice} from './IotDevice';
7-
import {LaunchOptionsNode} from './LaunchOptionsNode';
87
import {IotLaunchEnvironment} from './IotLaunchEnvironment';
98
import {IotOption} from './IotOption';
109
import {IoTHelper} from './Helper/IoTHelper';
1110
import {launchHelper} from './Helper/launchHelper';
11+
import {IotConfiguration} from './Configuration/IotConfiguration';
1212

1313
export 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
}

src/LaunchEnvironmentNode.ts

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)