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