Skip to content

Commit

Permalink
Improve background script execution
Browse files Browse the repository at this point in the history
  • Loading branch information
arnoudkooi committed Mar 23, 2024
1 parent 84829e8 commit 3bd3418
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# CHANGELOG.md

## 3.3.0 (2024-03-23)
Features:
- Improved BG Script execution, you can now select to run it in current or global scope.

## 3.2.1 (2024-03-09)
Features:
- Execute Background Scripts in VS Code (SN Utils discussion #480, credit abhishekg999)


## 3.1.2 (2024-01-30)
Fixes / changes:
- Backgroundscript matching. (#issue 91)
Expand Down
23 changes: 17 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "sn-scriptsync",
"displayName": "sn-scriptsync",
"description": "SN Utils sn-riptSync for VS Code. (Personal work of Arnoud Kooi)",
"version": "3.2.1",
"description": "SN Utils sn-sriptSync for VS Code. (Personal work of Arnoud Kooi)",
"version": "3.3.0",
"publisher": "arnoudkooicom",
"icon": "img/icon256.png",
"repository": {
Expand Down Expand Up @@ -42,8 +42,13 @@
"title": "sn-scriptsync: Disable"
},
{
"command": "extension.bgScriptMirror",
"title": "sn-scriptsync: Selection to Background Script Mirror",
"command": "extension.bgScriptGlobal",
"title": "sn-scriptsync: Selection to Background Script (global)",
"when": "editorLangId == javascript"
},
{
"command": "extension.bgScriptScope",
"title": "sn-scriptsync: Selection to Background Script (scope)",
"when": "editorLangId == javascript"
},
{
Expand Down Expand Up @@ -107,9 +112,15 @@
"menus": {
"editor/context": [
{
"command": "extension.bgScriptMirror",
"command": "extension.bgScriptGlobal",
"group": "sn-scriptsync",
"title": "Selection to Background Script (global)",
"when": "editorLangId == javascript"
},
{
"command": "extension.bgScriptScope",
"group": "sn-scriptsync",
"title": "Selection to Background Script Mirror",
"title": "Selection to Background Script (scope)",
"when": "editorLangId == javascript"
},
{
Expand Down
6 changes: 4 additions & 2 deletions src/InfoTreeViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export class InfoTreeViewProvider implements vscode.TreeDataProvider<TreeItem> {
new TreeItem('Actions',{},[
new TreeItem('Load / Refresh Tree', { action : 'refreshTree'}),
new TreeItem('Load Scope', { action : 'loadScope'}),
new TreeItem('Open in Instance', { action : 'openInInstance'})
new TreeItem('Open in Instance', { action : 'openInInstance'}),
new TreeItem('New Background Script (global)', { action : 'selectionToBG', global : true}),
new TreeItem('New Background Script (scope)', { action : 'selectionToBG', global : false})
]),
new TreeItem('Links',{},[
new TreeItem('arnoudkooi.com', { action : 'openUrl', url : 'https://www.arnoudkooi.com' }),
Expand Down Expand Up @@ -43,7 +45,7 @@ class TreeItem extends vscode.TreeItem {
vscode.TreeItemCollapsibleState.Expanded);
this.children = children;
if (children === undefined)
this.command = {command: "infoTreeCommand" , title: 'Open page', arguments: [{ action: args.action, url : args.url }]};
this.command = {command: "infoTreeCommand" , title: 'Open page', arguments: [{ action: args.action, url : args.url, global : args.global }]};
}
}

51 changes: 26 additions & 25 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ export function activate(context: vscode.ExtensionContext) {
startServers();
});

vscode.commands.registerCommand('extension.bgScriptMirror', () => {
selectionToBG();
vscode.commands.registerCommand('extension.bgScriptGlobal', (context) => {
selectionToBG(true);
});
vscode.commands.registerCommand('extension.bgScriptScope', (context) => {
selectionToBG(false);
});

vscode.commands.registerCommand('extension.bgScriptExecute', () => {
bgScriptExecute();
});
Expand Down Expand Up @@ -256,20 +258,6 @@ export function activate(context: vscode.ExtensionContext) {
});
}
}
else if (listener.document.fileName.includes(path.sep + 'background' + path.sep)) {
if (!wss.clients.size) {
vscode.window.showErrorMessage("No WebSocket connection. Please open SN ScriptSync in a browser");
}

let scriptObj = eu.fileNameToObject(listener.document);
scriptObj.mirrorbgscript = true;
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(scriptObj));
}
});

}
});

}
Expand Down Expand Up @@ -320,7 +308,9 @@ function writeBGScriptStartToWebViewPanel(scriptObj: any) {

function writeResponseToWebViewPanel(jsn: any) {
initializeWebViewPanelIfNotExists();
const html = `<HEAD><BASE HREF="${jsn.instance?.url}"></HEAD>${jsn.data}`
if (jsn.data == "not authorized") jsn.data = `Not authorized<br />
please run /token in a <a href='/' target='_blank'>browser session</a> to refresh token`;
const html = `<HEAD><BASE HREF="${jsn.instance?.url}"></HEAD>${jsn.data}`;
webViewPanel.webview.html = html;
}

Expand Down Expand Up @@ -488,7 +478,7 @@ function startServers() {

//send immediatly a feedback to the incoming connection
ws.send('["Connected to VS Code ScriptScync WebSocket"]', function () { });
ws.send(JSON.stringify({ action : 'bannerMessage', message : 'You are using the 3.0 version of sn-scriptsync! Files are now stored in a new structure. <a href="https://youtu.be/cpyasfe93kQ" target="_blank">[Intro Video]</a>', class: 'alert alert-primary' }), function () { });
ws.send(JSON.stringify({ action : 'bannerMessage', message : 'Update: You can now create and run BG Scripts via the contextmenu in VS Code!', class: 'alert alert-primary' }), function () { });

});
updateScriptSyncStatusBarItem('Running');
Expand Down Expand Up @@ -1263,6 +1253,10 @@ vscode.commands.registerCommand('infoTreeCommand', (arg) => {
else if (arg.action == "openInInstance"){
openInInstance();
}
else if (arg.action == "selectionToBG"){
console.log("selectionToBG", arg);
selectionToBG(arg.global);
}
});


Expand Down Expand Up @@ -1327,7 +1321,7 @@ function updateScriptSyncStatusBarItem(message: string): void {
scriptSyncStatusBarItem.show();
}

async function selectionToBG() {
async function selectionToBG(global = true) {

if (!serverRunning) {
vscode.window.showInformationMessage("sn-scriptsync server must be running")
Expand All @@ -1339,16 +1333,24 @@ async function selectionToBG() {


let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage("Please open a script file first.")
return;
}
let scriptObj = eu.fileNameToObject(editor.document);

scriptObj.content = '// sn-scriptsync - Snippet received from: (delete file after usage.)\n// file://' + scriptObj.fileName + "\n\n"
+ String.raw`${editor.document.getText(editor.selection)}`;;
if (global){
scriptObj.scope = 'global';
scriptObj.scopeName = 'global';
}

scriptObj.content = `gs.info("// sn-scriptsync BG Script - scope: ${scriptObj?.scopeName}");\n\n`
+ String.raw`${editor.document.getText(editor.selection)}`;
scriptObj.field = 'bg';
scriptObj.table = 'background'
scriptObj.sys_id = my_id;
scriptObj.fieldType = 'script';
scriptObj.name = 'script';
scriptObj.mirrorbgscript = true;

saveFieldAsFile(scriptObj)

Expand Down Expand Up @@ -1377,9 +1379,8 @@ async function bgScriptExecute(showWarning = true) {
return;
}

scriptObj.instance.scope = scriptObj.scope; //expected like this in SN Utils scriptsync.js
editor.document.save();
// Uncomment mirrorbgscript since the condition on scriptsync.js:182 will prevent execution
// scriptObj.mirrorbgscript = true;
scriptObj.executeScript = true;

scriptObj.action = 'executeBackgroundScript';
Expand Down

0 comments on commit 3bd3418

Please sign in to comment.