From 431fa59a71075f1bec2581e0c4a75ab9c287e8f3 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Tue, 25 Mar 2025 10:57:10 -0400 Subject: [PATCH] update mocha-vscode to better work with different SUV backends introduced in mocha-suv@9407922 --- package.json | 42 +++++++++++- src/extension.ts | 42 +++++++++--- src/suvManager/SuvManagerTreeDataItem.ts | 30 +++++++++ .../SuvManagerTreeDataProvider.ts | 13 ++-- .../SuvManagerTreeDecorationProvider.ts | 66 +++++++++++++++++++ 5 files changed, 176 insertions(+), 17 deletions(-) create mode 100644 src/suvManager/SuvManagerTreeDataItem.ts rename src/{ => suvManager}/SuvManagerTreeDataProvider.ts (82%) create mode 100644 src/suvManager/SuvManagerTreeDecorationProvider.ts diff --git a/package.json b/package.json index c973913..fcc464b 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,11 @@ "main": "./out/extension.js", "contributes": { "commands": [ + { + "command": "mocha.suvmanager.set_selected_item", + "title": "", + "category": "Mocha" + }, { "command": "mocha.suvmanager.suv_manager", "title": "Manage SUVs", @@ -24,9 +29,19 @@ "shortTitle": "Show in Web", "category": "Mocha" }, + { + "command": "mocha.suvmanager.suv_copy_id", + "title": "Copy SUV ID", + "category": "Mocha" + }, { "command": "mocha.suvmanager.suv_up", - "title": "Launch SUV", + "title": "Start SUV", + "category": "Mocha" + }, + { + "command": "mocha.suvmanager.suv_down", + "title": "Stop SUV", "category": "Mocha" }, { @@ -38,6 +53,12 @@ "dark": "icons/commands/add-connection-dark.svg" } }, + { + "command": "mocha.suvmanager.suv_refresh", + "title": "Refresh SUV List", + "category": "Mocha", + "icon": "$(refresh)" + }, { "command": "mocha.add_documentation_comment", "title": "Add Documentation Comment", @@ -150,17 +171,32 @@ "command": "mocha.suvmanager.suv_new", "when": "view == mocha.suvManager", "group": "navigation@1" + }, + { + "command": "mocha.suvmanager.suv_refresh", + "when": "view == mocha.suvManager", + "group": "navigation@1" } ], "view/item/context": [ { "command": "mocha.suvmanager.suv_show", - "group": "YourGroup@1", + "group": "0_view@1", "when": "view == mocha.suvManager" }, { "command": "mocha.suvmanager.suv_up", - "group": "YourGroup@2", + "group": "1_control@1", + "when": "view == mocha.suvManager" + }, + { + "command": "mocha.suvmanager.suv_down", + "group": "1_control@1", + "when": "view == mocha.suvManager" + }, + { + "command": "mocha.suvmanager.suv_copy_id", + "group": "2_copy", "when": "view == mocha.suvManager" } ] diff --git a/src/extension.ts b/src/extension.ts index b7cd777..c888f52 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,13 +2,15 @@ // Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; -import { SuvManagerTreeDataProvider } from './SuvManagerTreeDataProvider'; import { GenericTreeDataProvider } from './tdp/GenericTreeDataProvider'; import { GenericTreeDataItem } from './tdp/GenericTreeDataItem'; import { ModuleExplorerTreeDataProvider } from './ModuleExplorerTreeDataProvider'; import { userInfo } from 'os'; import { Machine } from './Machine'; +import { SuvManagerTreeDecorationProvider } from './suvManager/SuvManagerTreeDecorationProvider'; +import { SuvManagerTreeDataProvider } from './suvManager/SuvManagerTreeDataProvider'; + import * as path from 'path'; import https from 'node:https'; @@ -16,7 +18,10 @@ import https from 'node:https'; const cp = require('child_process'); let dpModuleExplorer = new ModuleExplorerTreeDataProvider(); -let treeSuvManager : vscode.TreeView | undefined = undefined; +let treeSuvManager: vscode.TreeView | undefined = undefined; + +// register the decoration provider +vscode.window.registerFileDecorationProvider(new SuvManagerTreeDecorationProvider()); export function mkotsuri(suvId : string, tenantName : string, command : string, serviceName : string = "zq", version : string = "v1") : vscode.Uri { @@ -129,6 +134,7 @@ export function activate(context: vscode.ExtensionContext) { treeSuvManager = vscode.window.createTreeView("mocha.suvManager", { "canSelectMany": true, "showCollapseAll": true, "treeDataProvider": treeDataProvider }); treeDataProvider.treeview = treeSuvManager; treeSuvManager.badge = { "value": 1, "tooltip": "1 SUV(s) running" }; + let treeModuleExplorer = vscode.window.createTreeView("mocha.moduleExplorer", { "canSelectMany": true, "showCollapseAll": true, "treeDataProvider": dpModuleExplorer }); @@ -141,8 +147,16 @@ export function activate(context: vscode.ExtensionContext) { // This line of code will only be executed once when your extension is activated console.log('Congratulations, your extension "suvmanager" is now active!'); + vscode.commands.registerCommand("mocha.suvmanager.set_selected_item", (item:vscode.TreeItem) => { + cp.exec('mocha suv status -f csv ' + item.label, (err: string, stdout: string, stderr: string) => { + let listSuvs = new Array(); + let list = stdout.trim().split('\n'); + console.log(list[0]); + }); + }); + let cmd_zq_import_function_signature = vscode.commands.registerCommand("mocha.zq_import_function_signature", () => { -/* +/*s const rootCas = require('ssl-root-cas').create(); rootCas.addFile(path.resolve(__dirname, 'localhost.crt')); https.globalAgent.options.ca = rootCas; @@ -234,18 +248,26 @@ export function activate(context: vscode.ExtensionContext) { vscode.window.showInformationMessage('Add code here to display SUV Manager panel!'); }); context.subscriptions.push(cmd_suv_manager); + + let cmd_suv_refresh = vscode.commands.registerCommand('mocha.suvmanager.suv_refresh', () => { + vscode.window.showInformationMessage('Add code here to display SUV Manager panel!'); + }); + context.subscriptions.push(cmd_suv_refresh); let cmd_suv_show = vscode.commands.registerCommand('mocha.suvmanager.suv_show', (...args) => { if (args.length === 0) { - cp.exec('mocha suv list', (err: string, stdout: string, stderr: string) => { + cp.exec('mocha suv status -f csv', (err: string, stdout: string, stderr: string) => { vscode.window.showInformationMessage(stdout); - let list = stdout.split(' '); + let list = stdout.split('\n'); let list2 = new Array(); list.forEach((element) => { - list2.push(element.trim()); + let list3 = element.trim().split(','); + if (list3[2] === 'running') { + list2.push(list3[0].trim()); + } }); - var w = vscode.window.showQuickPick(list2, { "title": "Open SUV in Web Browser", "placeHolder": "Choose an SUV to open" }).then((value) => { + var w = vscode.window.showQuickPick(list2, { "title": "Launch SUV", "placeHolder": "Pick an SUV to launch (e.g. i-012345678)" }).then((value) => { if (value !== undefined) { openWebBrowser(vscode.Uri.parse("https://" + value + ".privatesuv.com")); } @@ -262,14 +284,16 @@ export function activate(context: vscode.ExtensionContext) { let cmd_suv_up = vscode.commands.registerCommand('mocha.suvmanager.suv_up', (...args) => { if (args.length === 0) { cp.exec('mocha suv list', (err: string, stdout: string, stderr: string) => { + + vscode.window.showInformationMessage(stdout); - let list = stdout.split(' '); + let list = stdout.split('\n'); let list2 = new Array(); list.forEach((element) => { list2.push(element.trim()); }); - var w = vscode.window.showQuickPick(list2, { "title": "Launch SUV", "placeHolder": "Pick an SUV to launch (e.g. i-012345678)" }).then((value) => { + var w = vscode.window.showQuickPick(list2, { "title": "Open SUV in Web Browser", "placeHolder": "Choose an SUV to open" }).then((value) => { if (value !== undefined) { launchSuv(value); } diff --git a/src/suvManager/SuvManagerTreeDataItem.ts b/src/suvManager/SuvManagerTreeDataItem.ts new file mode 100644 index 0000000..cc8ea46 --- /dev/null +++ b/src/suvManager/SuvManagerTreeDataItem.ts @@ -0,0 +1,30 @@ +// Copyright (C) 2025 Michael Becker +// +// This file is part of mocha-vscode. +// +// mocha-vscode is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// mocha-vscode is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with mocha-vscode. If not, see . + +import * as vscode from 'vscode'; + +export class SuvManagerTreeDataItem extends vscode.TreeItem { + constructor(label : string) { + super(label); + + // this is important, otherwise provideFileDecoration will not be called + this.resourceUri = vscode.Uri.parse('x-suv-manager://machines/' + label); + + this.iconPath = "$(database)"; + this.command = { "command": "mocha.suvmanager.set_selected_item", "title": "Set Selected Item", "arguments": [this] }; + } +} \ No newline at end of file diff --git a/src/SuvManagerTreeDataProvider.ts b/src/suvManager/SuvManagerTreeDataProvider.ts similarity index 82% rename from src/SuvManagerTreeDataProvider.ts rename to src/suvManager/SuvManagerTreeDataProvider.ts index db873ca..416af18 100644 --- a/src/SuvManagerTreeDataProvider.ts +++ b/src/suvManager/SuvManagerTreeDataProvider.ts @@ -2,7 +2,8 @@ import * as vscode from 'vscode'; import * as fs from 'fs'; import * as path from 'path'; -import { Machine } from './Machine'; +import { Machine } from '../Machine'; +import { SuvManagerTreeDataItem } from './SuvManagerTreeDataItem'; const cp = require('child_process'); @@ -30,7 +31,7 @@ export class SuvManagerTreeDataProvider implements vscode.TreeDataProvider { @@ -42,7 +43,7 @@ export class SuvManagerTreeDataProvider implements vscode.TreeDataProvider { - cp.exec('mocha suv list', (err: string, stdout: string, stderr: string) => + cp.exec('mocha suv status -f csv', (err: string, stdout: string, stderr: string) => { let listSuvs = new Array(); let list = stdout.split('\n'); @@ -60,13 +61,15 @@ export class SuvManagerTreeDataProvider implements vscode.TreeDataProvider { - if (element == "") + if (element === "") { return; } + let fields = element.trim().split(','); + let m1 = new Machine(); - m1.name = element.trim(); + m1.name = fields[0].trim(); ct ++; listSuvs.push(m1); diff --git a/src/suvManager/SuvManagerTreeDecorationProvider.ts b/src/suvManager/SuvManagerTreeDecorationProvider.ts new file mode 100644 index 0000000..b1e19d2 --- /dev/null +++ b/src/suvManager/SuvManagerTreeDecorationProvider.ts @@ -0,0 +1,66 @@ +// Copyright (C) 2025 Michael Becker +// +// This file is part of mocha-vscode. +// +// mocha-vscode is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// mocha-vscode is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with mocha-vscode. If not, see . + +// define the decoration provider +import * as vscode from 'vscode'; + +const cp = require('child_process'); + +export class SuvManagerTreeDecorationProvider implements vscode.FileDecorationProvider { + provideFileDecoration(uri: vscode.Uri, token: vscode.CancellationToken): vscode.ProviderResult { + + let activeSuv = ''; + + // https://code.visualstudio.com/api/references/theme-color#lists-and-trees + if (uri.scheme === 'x-suv-manager') { + let p = uri.path.substring(1); + + return new Promise((resolve, reject) => { + cp.exec('mocha suv status -f csv ' + p, (err: string, stdout: string, stderr: string) => { + let list = stdout; + let details = list.split(','); + + let suvId: string = details[0].trim(); + let status : string = details[2].trim(); + + let cc = new vscode.ThemeColor('disabledForeground'); + if (status === "stopped") { + + } + else if (status === "running") { + if (activeSuv === suvId) { + cc = new vscode.ThemeColor('gitDecoration.untrackedResourceForeground'); + } + else { + cc = new vscode.ThemeColor('foreground'); + } + } + + resolve({ + // color: new vscode.ThemeColor('list.warningForeground'), + color: cc, + // badge: "1" + tooltip: details[1] + ", " + details[2], + badge: details[1][0].toUpperCase() + }); + }); + }); + } + + return undefined; + } +}