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;
34 public curPrj: IProject;
36 private startTime: Map<string, number> = new Map<string, number>();
39 private prjSvr: ProjectService,
40 private xdsSvr: XDSAgentService,
41 private alertSvr: AlertService,
42 private sdkSvr: SdkService,
43 private modalService: NgbModal,
46 this.cmdInfo = ''; // TODO: to be remove (only for debug)
50 // Retreive current project
51 this.prjSvr.curProject$.subscribe(p => this.curPrj = p);
53 // Command output data tunneling
54 this.xdsSvr.CmdOutput$.subscribe(data => {
55 this.cmdOutput += data.stdout;
56 this.cmdOutput += data.stderr;
60 this.xdsSvr.CmdExit$.subscribe(exit => {
61 if (this.startTime.has(exit.cmdID)) {
62 this.cmdInfo = 'Last command duration: ' + this._computeTime(this.startTime.get(exit.cmdID));
63 this.startTime.delete(exit.cmdID);
66 if (exit && exit.code !== 0) {
67 this.cmdOutput += '--- Command exited with code ' + exit.code + ' ---\n\n';
71 this._scrollToBottom();
74 ngAfterViewChecked() {
75 this._scrollToBottom();
82 isSetupValid(): boolean {
83 return (typeof this.curPrj !== 'undefined');
87 if (!this.isSetupValid()) {
88 return this.alertSvr.warning('Please select first a valid project.', true);
91 const activeModal = this.modalService.open(BuildSettingsModalComponent, { size: 'lg', container: 'nb-layout' });
92 activeModal.componentInstance.modalHeader = 'Large Modal';
95 execCmd(cmdName: string) {
96 if (!this.isSetupValid()) {
97 return this.alertSvr.warning('Please select first a valid project.', true);
100 if (!this.curPrj.uiSettings) {
101 return this.alertSvr.warning('Invalid setting structure', true);
107 cmd = this.curPrj.uiSettings.cmdClean;
110 cmd = this.curPrj.uiSettings.cmdPrebuild;
113 cmd = this.curPrj.uiSettings.cmdBuild;
116 cmd = this.curPrj.uiSettings.cmdPopulate;
119 if (this.curPrj.uiSettings.cmdArgs instanceof Array) {
120 cmd = this.curPrj.uiSettings.cmdArgs.join(' ');
122 cmd = this.curPrj.uiSettings.cmdArgs;
126 return this.alertSvr.warning('Unknown command name ' + cmdName);
129 const prjID = this.curPrj.id;
130 const dir = this.curPrj.uiSettings.subpath;
131 const args: string[] = [];
132 const sdkid = this.sdkSvr.getCurrentId();
135 if (this.curPrj.uiSettings.envVars instanceof Array) {
136 env = this.curPrj.uiSettings.envVars.join(' ');
138 env = this.curPrj.uiSettings.envVars;
141 this.cmdOutput += this._outputHeader();
143 // Detect key=value in env string to build array of string
145 env.split(';').forEach(v => envArr.push(v.trim()));
147 const t0 = performance.now();
148 this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;
150 this.xdsSvr.exec(prjID, dir, cmd, sdkid, args, envArr)
152 this.startTime.set(String(res.cmdID), t0);
155 this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
156 this.alertSvr.error('ERROR: ' + err);
160 private _scrollToBottom(): void {
162 this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
166 private _computeTime(t0: number, t1?: number): string {
167 const enlap = Math.round((t1 || performance.now()) - t0);
168 if (enlap < 1000.0) {
169 return enlap.toFixed(2) + ' ms';
171 return (enlap / 1000.0).toFixed(3) + ' seconds';
175 private _outputHeader(): string {
176 return '--- ' + new Date().toString() + ' ---\n';
179 private _outputFooter(): string {