1 import { Component, AfterViewChecked, ElementRef, ViewChild, OnInit, Input } from '@angular/core';
2 import { Observable } from 'rxjs';
3 import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms';
4 import { CookieService } from 'ngx-cookie';
6 import 'rxjs/add/operator/scan';
7 import 'rxjs/add/operator/startWith';
9 import { XDSServerService, ICmdOutput } from "../../services/xdsserver.service";
10 import { ConfigService, IConfig, IProject } from "../../services/config.service";
11 import { AlertService, IAlert } from "../../services/alert.service";
12 import { SdkService } from "../../services/sdk.service";
15 selector: 'panel-build',
17 templateUrl: './build.component.html',
18 styleUrls: ['./build.component.css']
21 export class BuildComponent implements OnInit, AfterViewChecked {
22 @ViewChild('scrollOutput') private scrollContainer: ElementRef;
24 @Input() curProject: IProject;
27 subpathCtrl = new FormControl("", Validators.required);
28 debugEnable: boolean = false;
30 public cmdOutput: string;
31 public cmdInfo: string;
33 private startTime: Map<string, number> = new Map<string, number>();
35 constructor(private configSvr: ConfigService,
36 private xdsSvr: XDSServerService,
37 private fb: FormBuilder,
38 private alertSvr: AlertService,
39 private sdkSvr: SdkService,
40 private cookie: CookieService,
43 this.cmdInfo = ""; // TODO: to be remove (only for debug)
44 this.buildForm = fb.group({
45 subpath: this.subpathCtrl,
46 cmdArgs: ["", Validators.nullValidator],
47 envVars: ["", Validators.nullValidator],
52 // Command output data tunneling
53 this.xdsSvr.CmdOutput$.subscribe(data => {
54 this.cmdOutput += data.stdout + "\n";
58 this.xdsSvr.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 this.debugEnable = (this.cookie.get("debug_build") !== "");
75 ngAfterViewChecked() {
76 this._scrollToBottom();
85 "mkdir -p build && cd build && cmake ..",
86 this.buildForm.value.subpath,
88 this.buildForm.value.envVars);
94 this.buildForm.value.subpath,
95 this.buildForm.value.cmdArgs,
96 this.buildForm.value.envVars
102 "SEB_TODO_script_populate",
103 this.buildForm.value.subpath,
105 this.buildForm.value.envVars
111 this.buildForm.value.cmdArgs,
112 this.buildForm.value.subpath,
114 this.buildForm.value.envVars
118 private _exec(cmd: string, dir: string, args: string[], env: string) {
119 if (!this.curProject) {
120 this.alertSvr.warning('No active project', true);
123 let prjID = this.curProject.id;
125 this.cmdOutput += this._outputHeader();
127 let sdkid = this.sdkSvr.getCurrentId();
129 // Detect key=value in env string to build array of string
131 env.split(';').forEach(v => envArr.push(v.trim()));
133 let t0 = performance.now();
134 this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;
136 this.xdsSvr.exec(prjID, dir, cmd, sdkid, args, envArr)
138 this.startTime.set(String(res.cmdID), t0);
141 this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
142 this.alertSvr.error('ERROR: ' + err);
147 if (!this.curProject) {
148 this.alertSvr.warning('No active project', true);
151 let prjID = this.curProject.id;
153 this.cmdOutput += this._outputHeader();
155 let sdkid = this.sdkSvr.getCurrentId();
157 let argsArr = args ? args.split(' ') : this.buildForm.value.cmdArgs.split(' ');
159 // Detect key=value in env string to build array of string
161 this.buildForm.value.envVars.split(';').forEach(v => envArr.push(v.trim()));
163 let t0 = performance.now();
164 this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;
166 this.xdsSvr.make(prjID, this.buildForm.value.subpath, sdkid, argsArr, envArr)
168 this.startTime.set(String(res.cmdID), t0);
171 this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
172 this.alertSvr.error('ERROR: ' + err);
176 private _scrollToBottom(): void {
178 this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
182 private _computeTime(t0: number, t1?: number): string {
183 let enlap = Math.round((t1 || performance.now()) - t0);
184 if (enlap < 1000.0) {
185 return enlap.toFixed(2) + ' ms';
187 return (enlap / 1000.0).toFixed(3) + ' seconds';
191 private _outputHeader(): string {
192 return "--- " + new Date().toString() + " ---\n";
195 private _outputFooter(): string {