Skip to content

Commit

Permalink
migrate native runner.server extension to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Oct 29, 2024
1 parent 76240e1 commit a9df3df
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 38 deletions.
4 changes: 3 additions & 1 deletion src/runner-server-vscode/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.vsix
node_modules
native
native
*.js
*.js.map
4 changes: 2 additions & 2 deletions src/runner-server-vscode/.vscodeignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
!index.js
!node_modules
*.js
*.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
const vscode = require('vscode');
const { LanguageClient, TransportKind } = require('vscode-languageclient/node');
const path = require('path');
const cp = require('child_process');
import { commands, window, ExtensionContext, Uri, ViewColumn, env } from 'vscode';
import { LanguageClient, TransportKind } from 'vscode-languageclient/node';
import { join } from 'path';
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';

var startRunner = null;
var finishPromise = null;
var startRunner : ChildProcessWithoutNullStreams | null = null;
var finishPromise : Promise<void> | null = null;

/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
function activate(context : ExtensionContext) {

(async() => {
console.log("Aquire Dotnet!")

// App requires .NET 8.0
const commandRes = await vscode.commands.executeCommand('dotnet.acquire', { version: '8.0', requestingExtensionId: `${context.extension.packageJSON.publisher}.${context.extension.packageJSON.name}`, mode: "aspnetcore" });
const commandRes = await commands.executeCommand('dotnet.acquire', { version: '8.0', requestingExtensionId: `${context.extension.packageJSON.publisher}.${context.extension.packageJSON.name}`, mode: "aspnetcore" }) as any;
const dotnetPath = commandRes.dotnetPath;
console.log("Dotnet " + dotnetPath)

Expand All @@ -31,12 +28,12 @@ function activate(context) {
run: {
transport: TransportKind.stdio,
command: dotnetPath,
args: [ path.join(context.extensionPath, 'native', 'Runner.Language.Server.dll') ]
args: [ join(context.extensionPath, 'native', 'Runner.Language.Server.dll') ]
},
debug: {
transport: TransportKind.stdio,
command: dotnetPath,
args: [ path.join(context.extensionPath, 'native', 'Runner.Language.Server.dll') ]
args: [ join(context.extensionPath, 'native', 'Runner.Language.Server.dll') ]
}
},
{
Expand All @@ -46,12 +43,12 @@ function activate(context) {

var stopServer = new AbortController();

var serverproc = cp.spawn(dotnetPath, [ path.join(context.extensionPath, 'native', 'Runner.Server.dll'), '--urls', 'http://*:0' ], { encoding: 'utf-8', killSignal: 'SIGINT', signal: stopServer.signal, windowsHide: true, stdio: 'pipe', shell: false });
var serverproc = spawn(dotnetPath, [ join(context.extensionPath, 'native', 'Runner.Server.dll'), '--urls', 'http://*:0' ], { killSignal: 'SIGINT', signal: stopServer.signal, windowsHide: true, stdio: 'pipe', shell: false });
context.subscriptions.push({ dispose: () => stopServer.abort() })
serverproc.addListener('exit', code => {
console.log(code);
});
var address = null;
var address : string | null = null;
serverproc.stdout.on('data', async (data) => {
var sdata = data.asciiSlice();
console.log(sdata)
Expand All @@ -60,28 +57,28 @@ function activate(context) {
var end = sdata.indexOf('\n', i + 1);
address = sdata.substring(i, end).replace("[::]", "localhost").replace("0.0.0.0", "localhost").trim();

var panel = vscode.window.createWebviewPanel(
var panel = window.createWebviewPanel(
"runner.server",
"Runner Server",
vscode.ViewColumn.Two,
ViewColumn.Two,
{
enableScripts: true,
// Without this we loose your webview position when the webview is in background
retainContextWhenHidden: true
}
);

const fullWebServerUri = await vscode.env.asExternalUri(
vscode.Uri.parse(address)
const fullWebServerUri = address && await env.asExternalUri(
Uri.parse(address)
);

var args = [ path.join(context.extensionPath, 'native', 'Runner.Client.dll'), 'startrunner', '--parallel', '4' ];
if(address) {
var args = [ join(context.extensionPath, 'native', 'Runner.Client.dll'), 'startrunner', '--parallel', '4' ];
args.push('--server', address)
}

finishPromise = new Promise(onexit => {
startRunner = cp.spawn(dotnetPath, args, { encoding: 'utf-8', windowsHide: true, stdio: 'pipe', shell: false, env: { ...process.env, RUNNER_CLIENT_EXIT_AFTER_ENTER: "1" } });
finishPromise = new Promise<void>(onexit => {
startRunner = spawn(dotnetPath, args, { windowsHide: true, stdio: 'pipe', shell: false, env: { ...process.env, RUNNER_CLIENT_EXIT_AFTER_ENTER: "1" } });
startRunner.stdout.on('data', async (data) => {
var sdata = data.asciiSlice();
console.log(sdata)
Expand All @@ -94,7 +91,7 @@ function activate(context) {
const cspSource = panel.webview.cspSource;
// Get the content Uri
const style = panel.webview.asWebviewUri(
vscode.Uri.joinPath(context.extensionUri, 'style.css')
Uri.joinPath(context.extensionUri, 'style.css')
);
panel.webview.html = `<!DOCTYPE html>
<html>
Expand All @@ -114,29 +111,29 @@ function activate(context) {
}
});

vscode.commands.registerCommand("runner.server.start-client", () => {
var args = [ path.join(context.extensionPath, 'native', 'Runner.Client.dll'), '--interactive' ];
commands.registerCommand("runner.server.start-client", () => {
var args = [ join(context.extensionPath, 'native', 'Runner.Client.dll'), '--interactive' ];
if(address) {
args.push('--server', address)
}
context.subscriptions.push(vscode.window.createTerminal("runner.client", dotnetPath, args))
context.subscriptions.push(window.createTerminal("runner.client", dotnetPath, args))
});

vscode.commands.registerCommand("runner.server.runworkflow", (workflow) => {
commands.registerCommand("runner.server.runworkflow", (workflow) => {
console.log(`runner.server.runjob {workflow}`)
var args = [ path.join(context.extensionPath, 'native', 'Runner.Client.dll'), '-W', vscode.Uri.parse(workflow).fsPath ];
var args = [ join(context.extensionPath, 'native', 'Runner.Client.dll'), '-W', Uri.parse(workflow).fsPath ];
if(address) {
args.push('--server', address)
}
context.subscriptions.push(vscode.window.createTerminal("runner.client", dotnetPath, args))
context.subscriptions.push(window.createTerminal("runner.client", dotnetPath, args))
});
vscode.commands.registerCommand("runner.server.runjob", (workflow, job) => {
commands.registerCommand("runner.server.runjob", (workflow, job) => {
console.log(`runner.server.runjob {workflow}.{job}`)
var args = [ path.join(context.extensionPath, 'native', 'Runner.Client.dll'), '-W', vscode.Uri.parse(workflow).fsPath, '-j', job ];
var args = [ join(context.extensionPath, 'native', 'Runner.Client.dll'), '-W', Uri.parse(workflow).fsPath, '-j', job ];
if(address) {
args.push('--server', address)
}
context.subscriptions.push(vscode.window.createTerminal("runner.client", dotnetPath, args))
context.subscriptions.push(window.createTerminal("runner.client", dotnetPath, args))
});

context.subscriptions.push(client);
Expand All @@ -146,7 +143,7 @@ function activate(context) {

// this method is called when your extension is deactivated
async function deactivate() {
if(finishPromise !== null) {
if(finishPromise !== null && startRunner !== null) {
startRunner.stdin.write("\n");
await finishPromise;
}
Expand Down
4 changes: 2 additions & 2 deletions src/runner-server-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"activationEvents": [
"onLanguage:yaml"
],
"main": "./index.js",
"main": "./dist/main-node.js",
"sponsor": {
"url": "https://paypal.me/christopherhx"
},
Expand All @@ -43,7 +43,7 @@
]
},
"scripts": {
"build": "dotnet publish ../Runner.Language.Server --no-self-contained -p:UseAppHost=false -p:BUILD_OS=Any -p:RuntimeFrameworkVersion=6.0.0 --output native"
"build": "npx webpack && dotnet publish ../Runner.Language.Server --no-self-contained -p:UseAppHost=false -p:BUILD_OS=Any -p:RuntimeFrameworkVersion=6.0.0 --output native"
},
"devDependencies": {
"@types/vscode": "^1.73.0",
Expand Down
15 changes: 15 additions & 0 deletions src/runner-server-vscode/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"outDir": "out",
"lib": [
"WebWorker",
"ES2020"
],
"sourceMap": true,
},
"exclude": [
"node_modules"
]
}

0 comments on commit a9df3df

Please sign in to comment.