Skip to content

Commit

Permalink
vscode-extension: Run docker image with the same version as WAMR (#1815)
Browse files Browse the repository at this point in the history
  • Loading branch information
TianlongLiang authored Dec 16, 2022
1 parent 97d2b5a commit d0c4c70
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
docker run --rm -it --name=wasm-debug-server-ctr ^
-v "%cd%":/mnt ^
-p 1234:1234 ^
wasm-debug-server:1.0 ^
wasm-debug-server:%2 ^
/bin/bash -c "./debug.sh %1"
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ set -e
docker run --rm -it --name=wasm-debug-server-ctr \
-v "$(pwd)":/mnt \
-p 1234:1234 \
wasm-debug-server:1.0 \
wasm-debug-server:$2 \
/bin/bash -c "./debug.sh $1"
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
docker run --rm --name=wasm-toolchain-ctr ^
-it -v "%cd%":/mnt ^
--env=PROJ_PATH="%cd%" ^
wasm-toolchain:1.0 ^
wasm-toolchain:%2 ^
/bin/bash -c "./build_wasm.sh %1"
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ set -e
docker run --rm --name=wasm-toolchain-ctr \
-it -v "$(pwd)":/mnt \
--env=PROJ_PATH="$(pwd)" \
wasm-toolchain:1.0 \
wasm-toolchain:$2 \
/bin/bash -c "./build_wasm.sh $1"
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

docker run --rm -it --name=wasm-debug-server-ctr ^
-v "%cd%":/mnt ^
wasm-debug-server:1.0 ^
wasm-debug-server:%2 ^
/bin/bash -c "./run.sh %1"
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ set -e

docker run --rm -it --name=wasm-debug-server-ctr \
-v "$(pwd)":/mnt \
wasm-debug-server:1.0 \
wasm-debug-server:$2 \
/bin/bash -c "./run.sh $1"
13 changes: 10 additions & 3 deletions test-tools/wamr-ide/VSCode-Extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
} from './utilities/directoryUtilities';
import { decorationProvider } from './decorationProvider';
import { WasmDebugConfigurationProvider } from './debugConfigurationProvider';
import { isLLDBInstalled, promptInstallLLDB } from './utilities/lldbUtilities';
import {
isLLDBInstalled,
promptInstallLLDB,
getWAMRExtensionVersion,
} from './utilities/lldbUtilities';

let wasmTaskProvider: WasmTaskProvider;
let wasmDebugConfigProvider: WasmDebugConfigurationProvider;
Expand All @@ -43,6 +47,8 @@ export async function activate(context: vscode.ExtensionContext) {
excludeFileArr = new Array(),
scriptMap = new Map();

const wamrVersion = getWAMRExtensionVersion(context);

/**
* Get OS platform information for differ windows and linux execution script
*/
Expand Down Expand Up @@ -83,7 +89,7 @@ export async function activate(context: vscode.ExtensionContext) {
typeMap.set('Debug', 'Debug');
typeMap.set('Destroy', 'Destroy');

wasmTaskProvider = new WasmTaskProvider(typeMap, scriptMap);
wasmTaskProvider = new WasmTaskProvider(typeMap, scriptMap, wamrVersion);

vscode.tasks.registerTaskProvider('wasm', wasmTaskProvider);

Expand Down Expand Up @@ -670,7 +676,8 @@ export async function activate(context: vscode.ExtensionContext) {
let _path = curWorkspace.concat(
OS_PLATFORM === 'win32'
? '\\'
: OS_PLATFORM === 'linux' || OS_PLATFORM === 'darwin'
: OS_PLATFORM === 'linux' ||
OS_PLATFORM === 'darwin'
? '/'
: '',
option
Expand Down
15 changes: 10 additions & 5 deletions test-tools/wamr-ide/VSCode-Extension/src/taskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export interface OwnShellOption {
export class WasmTaskProvider implements vscode.TaskProvider {
constructor(
public _type: Map<string, string>,
public _script: Map<string, string>
public _script: Map<string, string>,
public _wamrVersion: string
) {}

buildShellOption: OwnShellOption | undefined;
Expand All @@ -31,7 +32,11 @@ export class WasmTaskProvider implements vscode.TaskProvider {
let targetName =
TargetConfigPanel.BUILD_ARGS.output_file_name.split('.')[0];

if (os.platform() === 'linux' || os.platform() === 'darwin' || os.platform() === 'win32') {
if (
os.platform() === 'linux' ||
os.platform() === 'darwin' ||
os.platform() === 'win32'
) {
/* build */
this.buildShellOption = {
cmd:
Expand All @@ -40,7 +45,7 @@ export class WasmTaskProvider implements vscode.TaskProvider {
: (this._script.get('buildScript') as string),
options: {
executable: this._script.get('buildScript'),
shellArgs: [targetName, os.platform()],
shellArgs: [targetName, this._wamrVersion],
},
};

Expand All @@ -52,7 +57,7 @@ export class WasmTaskProvider implements vscode.TaskProvider {
: (this._script.get('debugScript') as string),
options: {
executable: this._script.get('debugScript'),
shellArgs: [targetName],
shellArgs: [targetName, this._wamrVersion],
},
};

Expand All @@ -64,7 +69,7 @@ export class WasmTaskProvider implements vscode.TaskProvider {
: (this._script.get('runScript') as string),
options: {
executable: this._script.get('runScript'),
shellArgs: [targetName],
shellArgs: [targetName, this._wamrVersion],
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,53 +137,73 @@ export function checkFolderName(folderName: string) {
return valid;
}

export function downloadFile(url: string, destinationPath: string): Promise<void> {
export function downloadFile(
url: string,
destinationPath: string
): Promise<void> {
return new Promise((resolve, reject) => {
const file = fileSystem.createWriteStream(destinationPath);
const stream = request(url, undefined, (error, response, body) => {
if (response.statusCode !== 200) {
reject(new Error(`Download from ${url} failed with ${response.statusMessage}`));
reject(
new Error(
`Download from ${url} failed with ${response.statusMessage}`
)
);
}
}).pipe(file);
stream.on("close", resolve);
stream.on("error", reject);
stream.on('close', resolve);
stream.on('error', reject);
});
}

export function unzipFile(sourcePath: string, getDestinationFileName: (entryName: string) => string): Promise<string[]> {
export function unzipFile(
sourcePath: string,
getDestinationFileName: (entryName: string) => string
): Promise<string[]> {
return new Promise((resolve, reject) => {
const unzippedFilePaths: string[] = [];
yauzl.open(sourcePath, { lazyEntries: true }, function(error, zipfile) {
if (error) {
reject(error);
return;
}
zipfile.readEntry();
zipfile.on("entry", function(entry) {
// This entry is a directory so skip it
if (/\/$/.test(entry.fileName)) {
zipfile.readEntry();
yauzl.open(
sourcePath,
{ lazyEntries: true },
function (error, zipfile) {
if (error) {
reject(error);
return;
}

zipfile.openReadStream(entry, function(error, readStream) {
if (error) {
reject(error);
}
zipfile.readEntry();
zipfile.on('entry', function (entry) {
// This entry is a directory so skip it
if (/\/$/.test(entry.fileName)) {
zipfile.readEntry();
return;
}
readStream.on("end", () => zipfile.readEntry());
const destinationFileName = getDestinationFileName(entry.fileName);
fileSystem.mkdirSync(path.dirname(destinationFileName), { recursive: true });

const file = fileSystem.createWriteStream(destinationFileName);
readStream.pipe(file).on("error", reject);
unzippedFilePaths.push(destinationFileName);
zipfile.openReadStream(entry, function (error, readStream) {
if (error) {
reject(error);
return;
}
readStream.on('end', () => zipfile.readEntry());
const destinationFileName = getDestinationFileName(
entry.fileName
);
fileSystem.mkdirSync(
path.dirname(destinationFileName),
{ recursive: true }
);

const file =
fileSystem.createWriteStream(destinationFileName);
readStream.pipe(file).on('error', reject);
unzippedFilePaths.push(destinationFileName);
});
});
zipfile.on('end', function () {
zipfile.close();
resolve(unzippedFilePaths);
});
});
zipfile.on("end", function() {
zipfile.close();
resolve(unzippedFilePaths);
});
});
}
);
});
}
}
76 changes: 54 additions & 22 deletions test-tools/wamr-ide/VSCode-Extension/src/utilities/lldbUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,41 @@ 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';

const LLDB_RESOURCE_DIR = "resource/debug";
const LLDB_OS_DOWNLOAD_URL_SUFFIX_MAP: Partial<Record<NodeJS.Platform, string>> = {
"linux": "x86_64-ubuntu-22.04",
"darwin": "universal-macos-latest"
import {
checkIfFileExists,
downloadFile,
unzipFile,
} from './directoryUtilities';

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

const WAMR_LLDB_NOT_SUPPORTED_ERROR = new Error("WAMR LLDB is not supported on this platform");
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] === "inst") {
const dirs = filename.split('/');
if (dirs[0] === 'inst') {
dirs.shift();
}

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

export function getWAMRExtensionVersion(
context: vscode.ExtensionContext
): string {
return require(path.join(context.extensionPath, 'package.json')).version;
}

function getLLDBDownloadUrl(context: vscode.ExtensionContext): string {
const wamrVersion = require(path.join(context.extensionPath, "package.json")).version;
const wamrVersion = getWAMRExtensionVersion(context);
const lldbOsUrlSuffix = LLDB_OS_DOWNLOAD_URL_SUFFIX_MAP[os.platform()];

if (!lldbOsUrlSuffix) {
Expand All @@ -40,15 +54,25 @@ function getLLDBDownloadUrl(context: vscode.ExtensionContext): string {
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");
const lldbBinaryPath = path.join(
extensionPath,
LLDB_RESOURCE_DIR,
lldbOSDir,
'bin',
'lldb'
);
return checkIfFileExists(lldbBinaryPath);
}

export async function promptInstallLLDB(context: vscode.ExtensionContext) {
const extensionPath = context.extensionPath;
const setupPrompt = "setup";
const skipPrompt = "skip";
const response = await vscode.window.showWarningMessage('No LLDB instance found. Setup now?', setupPrompt, skipPrompt);
const setupPrompt = 'setup';
const skipPrompt = 'skip';
const response = await vscode.window.showWarningMessage(
'No LLDB instance found. Setup now?',
setupPrompt,
skipPrompt
);

if (response === skipPrompt) {
return;
Expand All @@ -61,23 +85,31 @@ export async function promptInstallLLDB(context: vscode.ExtensionContext) {
throw WAMR_LLDB_NOT_SUPPORTED_ERROR;
}

const lldbDestinationFolder = path.join(extensionPath, LLDB_RESOURCE_DIR, destinationDir);
const lldbZipPath = path.join(lldbDestinationFolder, "bundle.zip");
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...`);
vscode.window.showInformationMessage(
`LLDB downloaded to ${lldbZipPath}. Installing...`
);

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

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

// Remove the bundle.zip
fs.unlink(lldbZipPath, () => {});
}


0 comments on commit d0c4c70

Please sign in to comment.