Skip to content

Commit 7ec4c33

Browse files
committed
Code review of IotTemplateCollection
1 parent d5aaca0 commit 7ec4c33

File tree

5 files changed

+47
-49
lines changed

5 files changed

+47
-49
lines changed

src/Configuration/IotConfiguration.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class IotConfiguration {
169169
this._contextUI.Output("Updating system templates");
170170
if(force||(this.IsUpdateTemplates&&(TimeHasPassedHours>=this.UpdateIntervalTemplatesHours))){
171171
result=await this.Templates.UpdateSystemTemplate(url,this.Folder.Temp);
172-
this._contextUI.Output(result.toString());
172+
this._contextUI.Output(result);
173173
//timestamp of last update
174174
if(result.Status==StatusResult.Ok){
175175
vscode.workspace.getConfiguration().update('fastiot.template.lastupdate',<number>dateNow,true);
@@ -182,7 +182,6 @@ export class IotConfiguration {
182182
this._contextUI.Output(endMsg);
183183
this._contextUI.Output("----------------------------------");
184184
progress.report({ message: "Templates loaded" , increment: 20 }); //100
185-
await IoTHelper.Sleep(1000);
186185
resolve(endMsg);
187186
//end
188187
});

src/Entity/EntityBase.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
2525
protected _validationErrors:Array<string>=[];
2626
public get ValidationErrors(): Array<string> {
2727
return this._validationErrors;}
28+
29+
public get ValidationErrorsToString(): string {
30+
let msg=`Validation messages:`;
31+
let index=1;
32+
this._validationErrors.forEach((item) => {
33+
msg=`${msg}\n${index}. ${item}`;
34+
index++;
35+
});
36+
return msg;}
2837
//public Attributes: EntityBaseAttribute|undefined;
2938
public Attributes: T;
3039
public Type:EntityType=EntityType.none;
@@ -84,7 +93,7 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
8493
//replace fields
8594
const fileName=this._descFilePath.substring(this.ParentDir.length+1);
8695
this._descFilePath= path.join(destDir, fileName);
87-
result = new IotResult(StatusResult.Ok,undefined,undefined);
96+
result = new IotResult(StatusResult.Ok,`${this._entityIntLabel}. ${this.ParentDir} folder successfully moved to ${destDir} folder`);
8897
} catch (err: any){
8998
result = new IotResult(StatusResult.Error,`Unable to move ${this._entityIntLabel} from folder ${this.ParentDir} to folder ${destDir}`,err);
9099
}
@@ -128,6 +137,7 @@ export abstract class EntityBase<T extends EntityBaseAttribute> {
128137
let result:IotResult;
129138
const fileZipPath=`${this.RecoverySourcePath}\\${this.ParentNameDir}.zip`;
130139
result= IoTHelper.UnpackFromZip(fileZipPath,path.dirname(this.ParentDir));
140+
if(result.Status==StatusResult.Error) result.AddMessage("Template restore error");
131141
//result
132142
return result;
133143
}

src/Entity/EntityCollection.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,6 @@ export abstract class EntityCollection <A extends EntityBaseAttribute, T extends
7171
this._data.clear();
7272
}
7373

74-
protected LogValidationErrors(validationErrors:Array<string>) {
75-
this.ContextUI.Output(`Validation messages:`);
76-
let index=1;
77-
validationErrors.forEach((item) => {
78-
this.ContextUI.Output(`${index}. ${item}`);
79-
index++;
80-
});
81-
}
82-
8374
public IsCompatible1(value:T):boolean
8475
{
8576
const forVersionExt=value.Attributes.ForVersionExt;

src/IotResult.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ export class IotResult {
4545
*/
4646

4747
public toString():string{
48+
let msg=``;
4849
//Status
49-
let msg=`[${this.Status.toString().toUpperCase()}]`;
50+
if(this.Status!=StatusResult.None) msg=`[${this.Status.toString().toUpperCase()}] `;
5051
//Message
51-
if(this.Message) msg=`${msg} ${this.Message}.`;
52+
if(this.Message) msg=`${msg}${this.Message}.`;
5253
//SystemMessage
5354
if(this.SystemMessage) msg=`${msg}\nSystem message: ${this.SystemMessage}`;
5455
return msg;

src/Templates/IotTemplateCollection.ts

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute
2525
{
2626
this.ContextUI.Output("Loading system templates");
2727
const type=EntityType.system;
28-
//
2928
const path=`${this.BasePath}\\${type}`;
3029
const result = await this.LoadFromFolder(path,type,this.RecoverySourcePath);
31-
this.ContextUI.Output(result.toString());
30+
this.ContextUI.Output(result);
3231
}
3332

3433
public async LoadTemplatesUser():Promise<void>
@@ -38,7 +37,7 @@ export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute
3837
//
3938
const path=`${this.BasePath}\\${type}`;
4039
const result = await this.LoadFromFolder(path,type,undefined);
41-
this.ContextUI.Output(result.toString());
40+
this.ContextUI.Output(result);
4241
}
4342

4443
public async LoadTemplatesCommunity():Promise<void>
@@ -53,7 +52,8 @@ export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute
5352

5453
protected async LoadFromFolder(pathFolder:string, type:EntityType,recoverySourcePath:string|undefined):Promise<IotResult>
5554
{
56-
let result= new IotResult(StatusResult.Ok, undefined,undefined);
55+
let result:IotResult;
56+
const templatesCountBegin=this.Count;
5757
//Recovery
5858
let recovery = new IotTemplateRecovery(type);
5959
if(type==EntityType.system&&recoverySourcePath)
@@ -66,7 +66,7 @@ export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute
6666
//ckeck
6767
if (listFolders.length==0)
6868
{
69-
result=new IotResult(StatusResult.Ok,`${pathFolder} folder is empty. There are no templates to load.`);
69+
result=new IotResult(StatusResult.Ok,`${pathFolder} folder is empty. There are no templates to load`);
7070
return Promise.resolve(result);
7171
}
7272
//checking all folders
@@ -81,11 +81,8 @@ export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute
8181
this.ContextUI.Output(`Template recovery: ${path.dirname(filePath)}`);
8282
result= template.Recovery();
8383
if(result.Status==StatusResult.Ok)
84-
{
85-
template.Init(type,filePath,recoverySourcePath);
86-
}else{
87-
this.ContextUI.Output(`Error. Template restore error. ${result.Message}. ${result.SystemMessage}`);
88-
}
84+
template.Init(type,filePath,recoverySourcePath);
85+
else this.ContextUI.Output(result);
8986
}
9087
//main
9188
if(template.IsValid)
@@ -103,48 +100,51 @@ export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute
103100
}
104101
case ContainsType.yesVersionSmaller: {
105102
this.Update(template.Attributes.Id,template);
106-
this.ContextUI.Output(`Template updated: ${filePath}`);
103+
this.ContextUI.Output(`Template updated: [${template.Attributes.Id}] ${template.ParentDir}`);
107104
break;
108105
}
109-
default: {
110-
//statements;
106+
default: {
107+
this.ContextUI.Output(`Adding a template was skipped because already in the collection: [${template.Attributes.Id}] ${template.ParentDir}`);
111108
break;
112109
}
113110
}
114111
}else{
115-
this.ContextUI.Output(`Error. The template ${template.DescriptionFilePath} is for a newer version of the extension.` +
112+
this.ContextUI.Output(`[ERROR] The template ${template.DescriptionFilePath} is for a newer version of the extension.` +
116113
`Update the extension.`);
117114
}
118115
}else{
119-
this.ContextUI.Output(`Error. The template ${template.DescriptionFilePath} has not been validated`);
120-
this.LogValidationErrors(template.ValidationErrors);
116+
this.ContextUI.Output(`[ERROR] The template ${template.DescriptionFilePath} has not been validated.`);
117+
this.ContextUI.Output(template.ValidationErrorsToString);
121118
//delete system template
122119
if(type==EntityType.system) {
123120
result= template.Remove();
124-
this.ContextUI.Output(`${result.Status}. ${result.Message}. ${result.SystemMessage}`);
121+
this.ContextUI.Output(result);
125122
}
126123
}
127124
});
128125
//result
129-
if(this.Count>0){
130-
result= new IotResult(StatusResult.Ok,`Loading templates from ${pathFolder} folder successfully completed`,undefined);
126+
if((this.Count-templatesCountBegin)>0){
127+
result= new IotResult(StatusResult.Ok,`Loading templates from ${pathFolder} folder successfully completed`);
131128
}else{
132-
result= new IotResult(StatusResult.Error,` No template was loaded from the ${pathFolder} folder`,undefined);
129+
result= new IotResult(StatusResult.None,`No template was loaded from the ${pathFolder} folder`);
133130
}
134131
return Promise.resolve(result);
135132
}
136133

137134
public async UpdateSystemTemplate(url:string,tempPath:string):Promise<IotResult>
138135
{
139136
const result = await this.UpdateTemplate(url,EntityType.system,tempPath);
137+
if(result.Status==StatusResult.Error)
138+
result.AddMessage(`Error updating system templates`);
140139
return result;
141140
}
142141

143142
public async UpdateTemplate(url:string,type:EntityType,tempPath:string):Promise<IotResult>
144143
{
145144
const destPath=`${this.BasePath}\\${type}`;
146145
let downloader = new IotTemplateDownloader();
147-
let result= new IotResult(StatusResult.None,undefined,undefined);
146+
let result:IotResult;
147+
this.ContextUI.Output(`Downloading a list of templates to update: ${url}`);
148148
result= await downloader.GetDownloadListTemplate(url);
149149
if(result.Status==StatusResult.Error) return Promise.resolve(result);
150150
this.ContextUI.Output(`List of templates loaded ${url}`);
@@ -176,16 +176,15 @@ export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute
176176
if(template.IsValid)
177177
{
178178
result=template.Move(path.join(destPath, template.Attributes.Id));
179-
if(result.Status==StatusResult.Error)
180-
{
181-
this.ContextUI.Output(`Error. The template ${template.DescriptionFilePath}. ${result.Message}. ${result.SystemMessage}`);
179+
if(result.Status==StatusResult.Error) {
180+
this.ContextUI.Output(result);
182181
break;
183182
}
184183
this.Add(template.Attributes.Id,template);
185-
this.ContextUI.Output(`Template added: [${template.Attributes.Id}] ${template.ParentDir}`);
184+
this.ContextUI.Output(`Template added/updated: [${template.Attributes.Id}] ${template.ParentDir}`);
186185
} else {
187-
this.ContextUI.Output(`Error. The template ${template.DescriptionFilePath} has not been validated`);
188-
this.LogValidationErrors(template.ValidationErrors);
186+
this.ContextUI.Output(`[ERROR] The template ${template.DescriptionFilePath} has not been validated`);
187+
this.ContextUI.Output(template.ValidationErrorsToString);
189188
}
190189
}
191190
break;
@@ -201,19 +200,17 @@ export class IotTemplateCollection extends EntityCollection<IotTemplateAttribute
201200
if(template.IsValid)
202201
{
203202
result=template.Move(path.join(destPath, template.Attributes.Id));
204-
if(result.Status==StatusResult.Error)
205-
{
206-
this.ContextUI.Output(`Error. The template ${template.DescriptionFilePath}. ${result.Message}. ${result.SystemMessage}`);
203+
if(result.Status==StatusResult.Error) {
204+
this.ContextUI.Output(result);
207205
break;
208-
}
206+
}
209207
this.Update(template.Attributes.Id,template);
210-
this.ContextUI.Output(`Template updated: [${template.Attributes.Id}] ${template.DescriptionFilePath}`);
208+
this.ContextUI.Output(`Template added/updated: [${template.Attributes.Id}] ${template.ParentDir}`);
211209
} else {
212-
this.ContextUI.Output(`Error. The template ${template.DescriptionFilePath} has not been validated`);
213-
this.LogValidationErrors(template.ValidationErrors);
210+
this.ContextUI.Output(`[ERROR] The template ${template.DescriptionFilePath} has not been validated`);
211+
this.ContextUI.Output(template.ValidationErrorsToString);
214212
}
215213
}
216-
break;
217214
}
218215
default: {
219216
//statements;

0 commit comments

Comments
 (0)