private async doBuild()

in Extension/src/LanguageServer/cppBuildTaskProvider.ts [372:447]


    private async doBuild(): Promise<any> {
        // Do build.
        let command: string = util.resolveVariables(this.command);
        let activeCommand: string = command;
        this.args.forEach((value, index) => {
            value = util.normalizeArg(util.resolveVariables(value));
            activeCommand = activeCommand + " " + value;
            this.args[index] = value;
        });
        if (this.options) {
            this.options.shell = true;
        } else {
            this.options = { "shell": true };
        }
        if (this.options.cwd) {
            this.options.cwd = util.resolveVariables(this.options.cwd);
        }

        const splitWriteEmitter = (lines: string | Buffer) => {
            const splitLines: string[] = lines.toString().split(/\r?\n/g);
            for (let i: number = 0; i < splitLines.length; i++) {
                let line: string = splitLines[i];

                // We may not get full lines.
                // Only output an endOfLine when a full line is detected.
                if (i !== splitLines.length - 1) {
                    line += this.endOfLine;
                }
                this.writeEmitter.fire(line);
            }
        };

        if (os.platform() === 'win32') {
            command = `cmd /c chcp 65001>nul && ${command}`;
        }

        this.writeEmitter.fire(activeCommand + this.endOfLine);
        let child: cp.ChildProcess | undefined;
        try {
            child = cp.spawn(command, this.args, this.options ? this.options : {});
            let error: string = "";
            let stdout: string = "";
            let stderr: string = "";
            const result: number = await new Promise<number>(resolve => {
                if (child) {
                    child.on('error', err => {
                        splitWriteEmitter(err.message);
                        error = err.message;
                        resolve(-1);
                    });
                    child.stdout?.on('data', data => {
                        const str: string = data.toString();
                        splitWriteEmitter(str);
                        stdout += str;
                    });
                    child.stderr?.on('data', data => {
                        const str: string = data.toString();
                        splitWriteEmitter(str);
                        stderr += str;
                    });
                    child.on('close', result => {
                        this.writeEmitter.fire(this.endOfLine);
                        if (result === null) {
                            this.writeEmitter.fire(localize("build.run.terminated", "Build run was terminated.") + this.endOfLine);
                            resolve(-1);
                        }
                        resolve(0);
                    });
                }
            });
            this.printBuildSummary(error, stdout, stderr);
            this.closeEmitter.fire(result);
        } catch {
            this.closeEmitter.fire(-1);
        }
    }