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 if ("projects" in cfg) {
48 this.curProject = cfg.projects[0];
49 this.confValid = (cfg.projects.length && this.curProject.id != null);
51 this.curProject = null;
52 this.confValid = false;
56 // Command output data tunneling
57 this.sdkSvr.CmdOutput$.subscribe(data => {
58 this.cmdOutput += data.stdout + "\n";
62 this.sdkSvr.CmdExit$.subscribe(exit => {
63 if (this.startTime.has(exit.cmdID)) {
64 this.cmdInfo = 'Last command duration: ' + this._computeTime(this.startTime.get(exit.cmdID));
65 this.startTime.delete(exit.cmdID);
68 if (exit && exit.code !== 0) {
69 this.cmdOutput += "--- Command exited with code " + exit.code + " ---\n\n";
73 this._scrollToBottom();
76 ngAfterViewChecked() {
77 this._scrollToBottom();
85 if (!this.curProject) {
86 this.alertSvr.warning('No active project', true);
89 let prjID = this.curProject.id;
91 this.cmdOutput += this._outputHeader();
93 let t0 = performance.now();
94 this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;
96 this.sdkSvr.make(prjID, this.buildForm.value.subpath, args)
98 this.startTime.set(String(res.cmdID), t0);
101 this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
102 this.alertSvr.error('ERROR: ' + err);
106 private _scrollToBottom(): void {
108 this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
112 private _computeTime(t0: number, t1?: number): string {
113 let enlap = Math.round((t1 || performance.now()) - t0);
114 if (enlap < 1000.0) {
115 return enlap.toFixed(2) + ' ms';
117 return (enlap / 1000.0).toFixed(3) + ' seconds';
121 private _outputHeader(): string {
122 return "--- " + new Date().toString() + " ---\n";
125 private _outputFooter(): string {