aboutsummaryrefslogtreecommitdiff
path: root/editors/vscode/src/commands.ts
blob: ea0ab124a6904c69dfe3f539539c88340bfd9832 (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
import * as vscode from 'vscode';

import { Ctx, Cmd } from './ctx';
import { execFile } from 'child_process';
import path = require('path');
import { promises as fs } from "fs";
import { getDebugConfiguration } from './debug';

export function runDebugTest(ctx: Ctx): Cmd {
    return async(debugConfig: any) => {

        const fn = debugConfig.function;
        const cwd = debugConfig.cwd;
        const pkg = path.basename(cwd);

        var args = [];

        args.push("test");
        args.push(cwd);
        args.push(`-test-name:${fn}`);
        args.push("-debug");

        for(var i = 0; i < ctx.config.collections.length; i++) {
            const name = ctx.config.collections[i].name;
            const path = ctx.config.collections[i].path;
            if(name === "core" || name === "vendor") {
                continue;
            }
            args.push(`-collection:${name}=${path}`);
        }

        const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath;

        if(workspaceFolder === undefined) {
            return;
        }

        const odinExecution = execFile("odin", args, {cwd : workspaceFolder}, (err, stdout, stderr) => {
            if (err) {
                vscode.window.showErrorMessage(err.message);
            }
        });
 
        odinExecution.on("exit", (code) => {

            if(code !== 0) {
                throw Error("Odin test failed!");
            }

            const possibleExecutables = [
                path.join(workspaceFolder, pkg),
                path.join(workspaceFolder, pkg) + '.bin'
            ];

            let promises : Promise<string | null>[] = [];
            possibleExecutables.forEach((executable) => {
                promises.push(new Promise<string | null>((resolve) => {
                    fs.stat(executable).then((stats) => {
                        resolve(executable);
                    }).catch((error) => {
                        resolve(null);
                    });
                }));
            });

            Promise.all(promises).then(results => {
                let found = false;
                results.forEach((r) => {
                    if (r !== null && !found) {
                        found = true;
                        vscode.debug.startDebugging(undefined, getDebugConfiguration(ctx.config, r)).then(r => console.log("Result", r));
                    }
                });
                if (!found) {
                    throw Error("Not possible to find executable, candidates are: " + possibleExecutables);
                }
            });
        });
    };
}

export function runTest(ctx: Ctx): Cmd {

    return async(debugConfig: any) => {
        const fn = debugConfig.function;
        const cwd = debugConfig.cwd;
        const pkg = path.basename(cwd);

        var args = [];

        args.push("test");
        args.push(cwd);
        args.push(`-test-name:${fn}`);

        for(var i = 0; i < ctx.config.collections.length; i++) {
            const name = ctx.config.collections[i].name;
            const path = ctx.config.collections[i].path;
            if(name === "core") {
                continue;
            }
            args.push(`-collection:${name}=${path}`);
        }

        const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath;

        if(workspaceFolder === undefined) {
            return;
        }

        var definition = {
            type: "shell",
            command: "run", 
            args: args,
            cwd: workspaceFolder,
        };

        const shellExec = new vscode.ShellExecution("odin", args, { cwd: workspaceFolder});
 
        const target = vscode.workspace.workspaceFolders![0];
        var task = new vscode.Task(definition, target, "Run Test", "odin", shellExec);

        vscode.tasks.executeTask(task);
    };
}