aboutsummaryrefslogtreecommitdiffstats
path: root/webapp/src/app/devel/build/build.component.ts
blob: 87df4e194d673ca0dc792ef5fb4c219c632173d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { Component, AfterViewChecked, ElementRef, ViewChild, OnInit, Input } from '@angular/core';
import { Observable } from 'rxjs';
import { FormControl, FormGroup, Validators, FormBuilder } from '@angular/forms';
import { CookieService } from 'ngx-cookie';

import 'rxjs/add/operator/scan';
import 'rxjs/add/operator/startWith';

import { XDSAgentService, ICmdOutput } from "../../services/xdsagent.service";
import { ProjectService, IProject } from "../../services/project.service";
import { AlertService, IAlert } from "../../services/alert.service";
import { SdkService } from "../../services/sdk.service";

@Component({
    selector: 'panel-build',
    moduleId: module.id,
    templateUrl: './build.component.html',
    styleUrls: ['./build.component.css']
})

export class BuildComponent implements OnInit, AfterViewChecked {
    @ViewChild('scrollOutput') private scrollContainer: ElementRef;

    @Input() curProject: IProject;

    public buildForm: FormGroup;
    public subpathCtrl = new FormControl("", Validators.required);
    public debugEnable: boolean = false;
    public buildIsCollapsed: boolean = false;
    public cmdOutput: string;
    public cmdInfo: string;

    private startTime: Map<string, number> = new Map<string, number>();

    constructor(
        private xdsSvr: XDSAgentService,
        private fb: FormBuilder,
        private alertSvr: AlertService,
        private sdkSvr: SdkService,
        private cookie: CookieService,
    ) {
        this.cmdOutput = "";
        this.cmdInfo = "";      // TODO: to be remove (only for debug)
        this.buildForm = fb.group({
            subpath: this.subpathCtrl,
            cmdClean: ["", Validators.nullValidator],
            cmdPrebuild: ["", Validators.nullValidator],
            cmdBuild: ["", Validators.nullValidator],
            cmdPopulate: ["", Validators.nullValidator],
            cmdArgs: ["", Validators.nullValidator],
            envVars: ["", Validators.nullValidator],
        });
    }

    ngOnInit() {
        // Set default settings
        // TODO save & restore values from cookies
        this.buildForm.patchValue({
            subpath: "",
            cmdClean: "rm -rf build",
            cmdPrebuild: "mkdir -p build && cd build && cmake ..",
            cmdBuild: "cd build && make",
            cmdPopulate: "cd build && make remote-target-populate",
            cmdArgs: "",
            envVars: "",
        });

        // Command output data tunneling
        this.xdsSvr.CmdOutput$.subscribe(data => {
            this.cmdOutput += data.stdout;
            this.cmdOutput += data.stderr;
        });

        // Command exit
        this.xdsSvr.CmdExit$.subscribe(exit => {
            if (this.startTime.has(exit.cmdID)) {
                this.cmdInfo = 'Last command duration: ' + this._computeTime(this.startTime.get(exit.cmdID));
                this.startTime.delete(exit.cmdID);
            }

            if (exit && exit.code !== 0) {
                this.cmdOutput += "--- Command exited with code " + exit.code + " ---\n\n";
            }
        });

        this._scrollToBottom();

        // only use for debug
        this.debugEnable = (this.cookie.get("debug_build") === "1");
    }

    ngAfterViewChecked() {
        this._scrollToBottom();
    }

    reset() {
        this.cmdOutput = '';
    }

    clean() {
        this._exec(
            this.buildForm.value.cmdClean,
            this.buildForm.value.subpath,
            [],
            this.buildForm.value.envVars);
    }

    preBuild() {
        this._exec(
            this.buildForm.value.cmdPrebuild,
            this.buildForm.value.subpath,
            [],
            this.buildForm.value.envVars);
    }

    build() {
        this._exec(
            this.buildForm.value.cmdBuild,
            this.buildForm.value.subpath,
            [],
            this.buildForm.value.envVars
        );
    }

    populate() {
        this._exec(
            this.buildForm.value.cmdPopulate,
            this.buildForm.value.subpath,
            [], // args
            this.buildForm.value.envVars
        );
    }

    execCmd() {
        this._exec(
            this.buildForm.value.cmdArgs,
            this.buildForm.value.subpath,
            [],
            this.buildForm.value.envVars
        );
    }

    private _exec(cmd: string, dir: string, args: string[], env: string) {
        if (!this.curProject) {
            this.alertSvr.warning('No active project', true);
        }

        let prjID = this.curProject.id;

        this.cmdOutput += this._outputHeader();

        let sdkid = this.sdkSvr.getCurrentId();

        // Detect key=value in env string to build array of string
        let envArr = [];
        env.split(';').forEach(v => envArr.push(v.trim()));

        let t0 = performance.now();
        this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;

        this.xdsSvr.exec(prjID, dir, cmd, sdkid, args, envArr)
            .subscribe(res => {
                this.startTime.set(String(res.cmdID), t0);
            },
            err => {
                this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
                this.alertSvr.error('ERROR: ' + err);
            });
    }

    make(args: string) {
        if (!this.curProject) {
            this.alertSvr.warning('No active project', true);
        }

        let prjID = this.curProject.id;

        this.cmdOutput += this._outputHeader();

        let sdkid = this.sdkSvr.getCurrentId();

        let argsArr = args ? args.split(' ') : this.buildForm.value.cmdArgs.split(' ');

        // Detect key=value in env string to build array of string
        let envArr = [];
        this.buildForm.value.envVars.split(';').forEach(v => envArr.push(v.trim()));

        let t0 = performance.now();
        this.cmdInfo = 'Start build of ' + prjID + ' at ' + t0;

        this.xdsSvr.make(prjID, this.buildForm.value.subpath, sdkid, argsArr, envArr)
            .subscribe(res => {
                this.startTime.set(String(res.cmdID), t0);
            },
            err => {
                this.cmdInfo = 'Last command duration: ' + this._computeTime(t0);
                this.alertSvr.error('ERROR: ' + err);
            });
    }

    private _scrollToBottom(): void {
        try {
            this.scrollContainer.nativeElement.scrollTop = this.scrollContainer.nativeElement.scrollHeight;
        } catch (err) { }
    }

    private _computeTime(t0: number, t1?: number): string {
        let enlap = Math.round((t1 || performance.now()) - t0);
        if (enlap < 1000.0) {
            return enlap.toFixed(2) + ' ms';
        } else {
            return (enlap / 1000.0).toFixed(3) + ' seconds';
        }
    }

    private _outputHeader(): string {
        return "--- " + new Date().toString() + " ---\n";
    }

    private _outputFooter(): string {
        return "\n";
    }
}