3ebdfe5c6b9f5ac4f6b8ce7e5f6eab0354afe9f7
[src/xds/xds-agent.git] / webapp / src / app / pages / build / build.component.ts
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';
5
6 import 'rxjs/add/operator/scan';
7 import 'rxjs/add/operator/startWith';
8
9 import { BuildSettingsModalComponent } from './build-settings-modal/build-settings-modal.component';
10
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';
15
16 @Component({
17   selector: 'xds-panel-build',
18   templateUrl: './build.component.html',
19   styleUrls: ['./build.component.scss'],
20   encapsulation: ViewEncapsulation.None,
21 })
22
23 export class BuildComponent implements OnInit, AfterViewChecked {
24   @ViewChild('scrollOutput') private scrollContainer: ElementRef;
25
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;
30
31   public buildIsCollapsed = false;
32   public cmdOutput: string;
33   public cmdInfo: string;
34   public curPrj: IProject;
35
36   private startTime: Map<string, number> = new Map<string, number>();
37
38   constructor(
39     private prjSvr: ProjectService,
40     private xdsSvr: XDSAgentService,
41     private alertSvr: AlertService,
42     private sdkSvr: SdkService,
43     private modalService: NgbModal,
44   ) {
45     this.cmdOutput = '';
46     this.cmdInfo = '';       // TODO: to be remove (only for debug)
47   }
48
49   ngOnInit() {
50     // Retreive current project
51     this.prjSvr.curProject$.subscribe(p => this.curPrj = p);
52
53     // Command output data tunneling
54     this.xdsSvr.CmdOutput$.subscribe(data => {
55       this.cmdOutput += data.stdout;
56       this.cmdOutput += data.stderr;
57     });
58
59     // Command exit
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);
64       }
65
66       if (exit && exit.code !== 0) {
67         this.cmdOutput += '--- Command exited with code ' + exit.code + ' ---\n\n';
68       }
69     });
70
71     this._scrollToBottom();
72   }
73
74   ngAfterViewChecked() {
75     this._scrollToBottom();
76   }
77
78   resetOutput() {
79     this.cmdOutput = '';
80   }
81
82   isSetupValid(): boolean {
83     return (typeof this.curPrj !== 'undefined');
84   }
85
86   settingsShow() {
87     if (!this.isSetupValid()) {
88       return this.alertSvr.warning('Please select first a valid project.', true);
89     }
90
91     const activeModal = this.modalService.open(BuildSettingsModalComponent, { size: 'lg', container: 'nb-layout' });
92     activeModal.componentInstance.modalHeader = 'Large Modal';
93   }
94
95   execCmd(cmdName: string) {
96     if (!this.isSetupValid()) {
97       return this.alertSvr.warning('Please select first a valid project.', true);
98     }
99
100     if (!this.curPrj.uiSettings) {
101       return this.alertSvr.warning('Invalid setting structure', true);
102     }
103
104     let cmd = '';
105     switch (cmdName) {
106       case 'clean':
107         cmd = this.curPrj.uiSettings.cmdClean;
108         break;
109       case 'prebuild':
110         cmd = this.curPrj.uiSettings.cmdPrebuild;
111         break;
112       case 'build':
113         cmd = this.curPrj.uiSettings.cmdBuild;
114         break;
115       case 'populate':
116         cmd = this.curPrj.uiSettings.cmdPopulate;
117         break;
118       case 'exec':
119         if (this.curPrj.uiSettings.cmdArgs instanceof Array)  {
120           cmd = this.curPrj.uiSettings.cmdArgs.join(' ');
121         } else {
122           cmd = this.curPrj.uiSettings.cmdArgs;
123         }
124         break;
125       default:
126         return this.alertSvr.warning('Unknown command name ' + cmdName);
127     }
128
129     const prjID = this.curPrj.id;
130     const dir = this.curPrj.uiSettings.subpath;
131     const args: string[] = [];
132     const sdkid = this.sdkSvr.getCurrentId();
133
134     let env = '';
135     if (this.curPrj.uiSettings.envVars instanceof Array) {
136       env = this.curPrj.uiSettings.envVars.join(' ');
137     } else {
138       env = this.curPrj.uiSettings.envVars;
139     }
140
141     this.cmdOutput += this._outputHeader();
142
143     // Detect key=value in env string to build array of string
144     const envArr = [];
145     env.split(';').forEach(v => envArr.push(v.trim()));
146
147     const t0 = performance.now();
148     this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;
149
150     this.xdsSvr.exec(prjID, dir, cmd, sdkid, args, envArr)
151       .subscribe(res => {
152         this.startTime.set(String(res.cmdID), t0);
153       },
154       err => {
155         this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
156         this.alertSvr.error('ERROR: ' + err);
157       });
158   }
159
160   private _scrollToBottom(): void {
161     try {
162       this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
163     } catch (err) { }
164   }
165
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';
170     } else {
171       return (enlap / 1000.0).toFixed(3) + ' seconds';
172     }
173   }
174
175   private _outputHeader(): string {
176     return '--- ' + new Date().toString() + ' ---\n';
177   }
178
179   private _outputFooter(): string {
180     return '\n';
181   }
182 }