summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/test-tools/wamr-ide/VSCode-Extension/src/utilities/lldbUtilities.ts
blob: 9170a75d380643d48d94c3cbffaaf91209a2429d (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
/*
 * Copyright (C) 2022 Amazon.com Inc. or its affiliates. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

import * as vscode from 'vscode';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';
import {
    checkIfFileExists,
    downloadFile,
    unzipFile,
} from './directoryUtilities';
import { SelectionOfPrompt } from '../constants';

const LLDB_RESOURCE_DIR = 'resource/debug';
const LLDB_OS_DOWNLOAD_URL_SUFFIX_MAP: Partial<
    Record<NodeJS.Platform, string>
> = {
    linux: 'x86_64-ubuntu-20.04',
    darwin: 'universal-macos-latest',
};

const WAMR_LLDB_NOT_SUPPORTED_ERROR = new Error(
    'WAMR LLDB is not supported on this platform'
);

function getLLDBUnzipFilePath(destinationFolder: string, filename: string) {
    const dirs = filename.split('/');
    if (dirs[0] === 'wamr-lldb') {
        dirs.shift();
    }

    return path.join(destinationFolder, ...dirs);
}

export function getWAMRExtensionVersion(
    context: vscode.ExtensionContext
): string {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    return require(path.join(context.extensionPath, 'package.json')).version;
}

function getLLDBDownloadUrl(context: vscode.ExtensionContext): string {
    const wamrVersion = getWAMRExtensionVersion(context);
    const lldbOsUrlSuffix = LLDB_OS_DOWNLOAD_URL_SUFFIX_MAP[os.platform()];

    if (!lldbOsUrlSuffix) {
        throw WAMR_LLDB_NOT_SUPPORTED_ERROR;
    }

    return `https://github.com/bytecodealliance/wasm-micro-runtime/releases/download/WAMR-${wamrVersion}/wamr-lldb-${wamrVersion}-${lldbOsUrlSuffix}.zip`;
}

export function isLLDBInstalled(context: vscode.ExtensionContext): boolean {
    const extensionPath = context.extensionPath;
    const lldbOSDir = os.platform();
    const lldbBinaryPath = path.join(
        extensionPath,
        LLDB_RESOURCE_DIR,
        lldbOSDir,
        'bin',
        'lldb'
    );
    return checkIfFileExists(lldbBinaryPath);
}

export async function promptInstallLLDB(
    context: vscode.ExtensionContext
): Promise<SelectionOfPrompt> {
    const extensionPath = context.extensionPath;

    const response = await vscode.window.showWarningMessage(
        'No LLDB instance found. Setup now?',
        SelectionOfPrompt.setUp,
        SelectionOfPrompt.skip
    );

    if (response === SelectionOfPrompt.skip) {
        return response;
    }

    const downloadUrl = getLLDBDownloadUrl(context);
    const destinationDir = os.platform();

    if (!downloadUrl) {
        throw WAMR_LLDB_NOT_SUPPORTED_ERROR;
    }

    const lldbDestinationFolder = path.join(
        extensionPath,
        LLDB_RESOURCE_DIR,
        destinationDir
    );
    const lldbZipPath = path.join(lldbDestinationFolder, 'bundle.zip');

    vscode.window.showInformationMessage(`Downloading LLDB...`);

    await downloadFile(downloadUrl, lldbZipPath);

    vscode.window.showInformationMessage(
        `LLDB downloaded to ${lldbZipPath}. Installing...`
    );

    const lldbFiles = await unzipFile(lldbZipPath, filename =>
        getLLDBUnzipFilePath(lldbDestinationFolder, filename)
    );
    // Allow execution of lldb
    lldbFiles.forEach(file => fs.chmodSync(file, '0775'));

    vscode.window.showInformationMessage(
        `LLDB installed at ${lldbDestinationFolder}`
    );

    // Remove the bundle.zip
    fs.unlinkSync(lldbZipPath);
    return SelectionOfPrompt.setUp;
}