Initial main commit.
[src/xds/xds-server.git] / webapp / src / app / build / build.component.ts
1 import { Component, AfterViewChecked, ElementRef, ViewChild, OnInit } from '@angular/core';
2 import { Observable } from 'rxjs';
3 import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms';
4
5 import 'rxjs/add/operator/scan';
6 import 'rxjs/add/operator/startWith';
7
8 import { XDSServerService, ICmdOutput } from "../common/xdsserver.service";
9 import { ConfigService, IConfig, IProject } from "../common/config.service";
10 import { AlertService, IAlert } from "../common/alert.service";
11
12 @Component({
13     selector: 'build',
14     moduleId: module.id,
15     templateUrl: './build.component.html',
16     styleUrls: ['./build.component.css']
17 })
18
19 export class BuildComponent implements OnInit, AfterViewChecked {
20     @ViewChild('scrollOutput') private scrollContainer: ElementRef;
21
22     config$: Observable<IConfig>;
23
24     buildForm: FormGroup;
25     subpathCtrl = new FormControl("", Validators.required);
26
27     public cmdOutput: string;
28     public confValid: boolean;
29     public curProject: IProject;
30     public cmdInfo: string;
31
32     private startTime: Map<string, number> = new Map<string, number>();
33
34     // I initialize the app component.
35     constructor(private configSvr: ConfigService, private sdkSvr: XDSServerService,
36         private fb: FormBuilder, private alertSvr: AlertService
37     ) {
38         this.cmdOutput = "";
39         this.confValid = false;
40         this.cmdInfo = "";      // TODO: to be remove (only for debug)
41         this.buildForm = fb.group({ subpath: this.subpathCtrl });
42     }
43
44     ngOnInit() {
45         this.config$ = this.configSvr.conf;
46         this.config$.subscribe((cfg) => {
47             this.curProject = cfg.projects[0];
48
49             this.confValid = (cfg.projects.length && this.curProject.id != null);
50         });
51
52         // Command output data tunneling
53         this.sdkSvr.CmdOutput$.subscribe(data => {
54             this.cmdOutput += data.stdout + "\n";
55         });
56
57         // Command exit
58         this.sdkSvr.CmdExit$.subscribe(exit => {
59             if (this.startTime.has(exit.cmdID)) {
60                 this.cmdInfo = 'Last command duration: ' + this._computeTime(this.startTime.get(exit.cmdID));
61                 this.startTime.delete(exit.cmdID);
62             }
63
64             if (exit && exit.code !== 0) {
65                 this.cmdOutput += "--- Command exited with code " + exit.code + " ---\n\n";
66             }
67         });
68
69         this._scrollToBottom();
70     }
71
72     ngAfterViewChecked() {
73         this._scrollToBottom();
74     }
75
76     reset() {
77         this.cmdOutput = '';
78     }
79
80     make(args: string) {
81         let prjID = this.curProject.id;
82
83         this.cmdOutput += this._outputHeader();
84
85         let t0 = performance.now();
86         this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;
87
88         this.sdkSvr.make(prjID, this.buildForm.value.subpath, args)
89             .subscribe(res => {
90                 this.startTime.set(String(res.cmdID), t0);
91             },
92             err => {
93                 this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
94                 this.alertSvr.add({ type: "danger", msg: 'ERROR: ' + err });
95             });
96     }
97
98     private _scrollToBottom(): void {
99         try {
100             this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
101         } catch (err) { }
102     }
103
104     private _computeTime(t0: number, t1?: number): string {
105         let enlap = Math.round((t1 || performance.now()) - t0);
106         if (enlap < 1000.0) {
107             return enlap.toFixed(2) + ' ms';
108         } else {
109             return (enlap / 1000.0).toFixed(3) + ' seconds';
110         }
111     }
112
113     private _outputHeader(): string {
114         return "--- " + new Date().toString() + " ---\n";
115     }
116
117     private _outputFooter(): string {
118         return "\n";
119     }
120 }