1 import { Component, AfterViewChecked, ElementRef, ViewChild, OnInit } from '@angular/core';
2 import { Observable } from 'rxjs';
3 import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms';
5 import 'rxjs/add/operator/scan';
6 import 'rxjs/add/operator/startWith';
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";
15 templateUrl: './build.component.html',
16 styleUrls: ['./build.component.css']
19 export class BuildComponent implements OnInit, AfterViewChecked {
20 @ViewChild('scrollOutput') private scrollContainer: ElementRef;
22 config$: Observable<IConfig>;
25 subpathCtrl = new FormControl("", Validators.required);
27 public cmdOutput: string;
28 public confValid: boolean;
29 public curProject: IProject;
30 public cmdInfo: string;
32 private startTime: Map<string, number> = new Map<string, number>();
34 // I initialize the app component.
35 constructor(private configSvr: ConfigService, private sdkSvr: XDSServerService,
36 private fb: FormBuilder, private alertSvr: AlertService
39 this.confValid = false;
40 this.cmdInfo = ""; // TODO: to be remove (only for debug)
41 this.buildForm = fb.group({ subpath: this.subpathCtrl });
45 this.config$ = this.configSvr.conf;
46 this.config$.subscribe((cfg) => {
47 this.curProject = cfg.projects[0];
49 this.confValid = (cfg.projects.length && this.curProject.id != null);
52 // Command output data tunneling
53 this.sdkSvr.CmdOutput$.subscribe(data => {
54 this.cmdOutput += data.stdout + "\n";
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);
64 if (exit && exit.code !== 0) {
65 this.cmdOutput += "--- Command exited with code " + exit.code + " ---\n\n";
69 this._scrollToBottom();
72 ngAfterViewChecked() {
73 this._scrollToBottom();
81 let prjID = this.curProject.id;
83 this.cmdOutput += this._outputHeader();
85 let t0 = performance.now();
86 this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;
88 this.sdkSvr.make(prjID, this.buildForm.value.subpath, args)
90 this.startTime.set(String(res.cmdID), t0);
93 this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
94 this.alertSvr.add({ type: "danger", msg: 'ERROR: ' + err });
98 private _scrollToBottom(): void {
100 this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
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';
109 return (enlap / 1000.0).toFixed(3) + ' seconds';
113 private _outputHeader(): string {
114 return "--- " + new Date().toString() + " ---\n";
117 private _outputFooter(): string {