1 import { Component, ViewEncapsulation, AfterViewChecked, ElementRef, ViewChild, OnInit, Input } from '@angular/core';
2 import { Observable } from 'rxjs/Observable';
3 import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms';
4 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
6 import 'rxjs/add/operator/scan';
7 import 'rxjs/add/operator/startWith';
9 import { BuildSettingsModalComponent } from './build-settings-modal/build-settings-modal.component';
11 import { XDSAgentService, ICmdOutput } from '../../@core-xds/services/xdsagent.service';
12 import { ProjectService, IProject } from '../../@core-xds/services/project.service';
13 import { AlertService, IAlert } from '../../@core-xds/services/alert.service';
14 import { SdkService } from '../../@core-xds/services/sdk.service';
17 selector: 'xds-panel-build',
18 templateUrl: './build.component.html',
19 styleUrls: ['./build.component.scss'],
20 encapsulation: ViewEncapsulation.None,
23 export class BuildComponent implements OnInit, AfterViewChecked {
24 @ViewChild('scrollOutput') private scrollContainer: ElementRef;
26 // FIXME workaround of https://github.com/angular/angular-cli/issues/2034
27 // should be removed with angular 5
28 // @Input() curProject: IProject;
29 @Input() curProject = <IProject>null;
31 public buildIsCollapsed = false;
32 public cmdOutput: string;
33 public cmdInfo: string;
35 private startTime: Map<string, number> = new Map<string, number>();
38 private prjSvr: ProjectService,
39 private xdsSvr: XDSAgentService,
40 private alertSvr: AlertService,
41 private sdkSvr: SdkService,
42 private modalService: NgbModal,
45 this.cmdInfo = ''; // TODO: to be remove (only for debug)
50 // Command output data tunneling
51 this.xdsSvr.CmdOutput$.subscribe(data => {
52 this.cmdOutput += data.stdout;
53 this.cmdOutput += data.stderr;
57 this.xdsSvr.CmdExit$.subscribe(exit => {
58 if (this.startTime.has(exit.cmdID)) {
59 this.cmdInfo = 'Last command duration: ' + this._computeTime(this.startTime.get(exit.cmdID));
60 this.startTime.delete(exit.cmdID);
63 if (exit && exit.code !== 0) {
64 this.cmdOutput += '--- Command exited with code ' + exit.code + ' ---\n\n';
68 this._scrollToBottom();
71 ngAfterViewChecked() {
72 this._scrollToBottom();
80 const activeModal = this.modalService.open(BuildSettingsModalComponent, { size: 'lg', container: 'nb-layout' });
81 activeModal.componentInstance.modalHeader = 'Large Modal';
85 const curPrj = this.prjSvr.getCurrent();
87 curPrj.uiSettings.cmdClean,
88 curPrj.uiSettings.subpath,
90 curPrj.uiSettings.envVars.join(' '));
94 const curPrj = this.prjSvr.getCurrent();
96 curPrj.uiSettings.cmdPrebuild,
97 curPrj.uiSettings.subpath,
99 curPrj.uiSettings.envVars.join(' '));
103 const curPrj = this.prjSvr.getCurrent();
105 curPrj.uiSettings.cmdBuild,
106 curPrj.uiSettings.subpath,
108 curPrj.uiSettings.envVars.join(' '),
113 const curPrj = this.prjSvr.getCurrent();
115 curPrj.uiSettings.cmdPopulate,
116 curPrj.uiSettings.subpath,
118 curPrj.uiSettings.envVars.join(' '),
123 const curPrj = this.prjSvr.getCurrent();
125 curPrj.uiSettings.cmdArgs.join(' '),
126 curPrj.uiSettings.subpath,
128 curPrj.uiSettings.envVars.join(' '),
132 private _exec(cmd: string, dir: string, args: string[], env: string) {
133 this.curProject = this.prjSvr.getCurrent();
134 const prjID = this.curProject.id;
136 if (!this.curProject) {
137 return this.alertSvr.warning('No active project', true);
140 this.cmdOutput += this._outputHeader();
142 const sdkid = this.sdkSvr.getCurrentId();
144 // Detect key=value in env string to build array of string
146 env.split(';').forEach(v => envArr.push(v.trim()));
148 const t0 = performance.now();
149 this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;
151 this.xdsSvr.exec(prjID, dir, cmd, sdkid, args, envArr)
153 this.startTime.set(String(res.cmdID), t0);
156 this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
157 this.alertSvr.error('ERROR: ' + err);
161 private _scrollToBottom(): void {
163 this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
167 private _computeTime(t0: number, t1?: number): string {
168 const enlap = Math.round((t1 || performance.now()) - t0);
169 if (enlap < 1000.0) {
170 return enlap.toFixed(2) + ' ms';
172 return (enlap / 1000.0).toFixed(3) + ' seconds';
176 private _outputHeader(): string {
177 return '--- ' + new Date().toString() + ' ---\n';
180 private _outputFooter(): string {