Skip to content

Commit ddc28de

Browse files
Added support for Exporting and Importing automation scripts. (#644)
2 parents e45821e + 6bd9a57 commit ddc28de

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,16 @@
374374
"when": "view == rokuAutomationView && brightscript.rokuAutomationView.autorunOnDeploy",
375375
"group": "navigation@2"
376376
},
377+
{
378+
"command": "extension.brightscript.rokuAutomationView.exportAllAutomations",
379+
"when": "view == rokuAutomationView",
380+
"group": "navigation@2"
381+
},
382+
{
383+
"command": "extension.brightscript.rokuAutomationView.importAllAutomations",
384+
"when": "view == rokuAutomationView",
385+
"group": "navigation@2"
386+
},
377387
{
378388
"command": "extension.brightscript.rokuFileSystemView.refresh",
379389
"when": "view == rokuFileSystemView && brightscript.isOnDeviceComponentAvailable",
@@ -3089,6 +3099,18 @@
30893099
"category": "BrighterScript",
30903100
"icon": "$(pass-filled)"
30913101
},
3102+
{
3103+
"command": "extension.brightscript.rokuAutomationView.importAllAutomations",
3104+
"title": "Import all automation scripts",
3105+
"category": "BrighterScript",
3106+
"icon": "$(file-directory)"
3107+
},
3108+
{
3109+
"command": "extension.brightscript.rokuAutomationView.exportAllAutomations",
3110+
"title": "Export all automation scripts",
3111+
"category": "BrighterScript",
3112+
"icon": "$(desktop-download)"
3113+
},
30923114
{
30933115
"command": "extension.brightscript.rokuFileSystemView.refresh",
30943116
"title": "Refresh",

src/commands/VscodeCommand.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export enum VscodeCommand {
1313
rokuAutomationViewDisableAutorunOnDeploy = 'extension.brightscript.rokuAutomationView.disableAutorunOnDeploy',
1414
rokuAutomationViewStartRecording = 'extension.brightscript.rokuAutomationView.startRecording',
1515
rokuAutomationViewStopRecording = 'extension.brightscript.rokuAutomationView.stopRecording',
16+
rokuAutomationViewImportAllAutomations = 'extension.brightscript.rokuAutomationView.importAllAutomations',
17+
rokuAutomationViewExportAllAutomations = 'extension.brightscript.rokuAutomationView.exportAllAutomations',
1618
enableRemoteControlMode = 'extension.brightscript.enableRemoteControlMode',
1719
disableRemoteControlMode = 'extension.brightscript.disableRemoteControlMode',
1820
rokuAppOverlaysViewAddNewOverlay = 'extension.brightscript.rokuAppOverlaysView.addNewOverlay',

src/viewProviders/RokuAutomationViewViewProvider.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ViewProviderId } from './ViewProviderId';
99
import { ViewProviderCommand } from './ViewProviderCommand';
1010
import { ViewProviderEvent } from './ViewProviderEvent';
1111
import { WorkspaceStateKey } from './WorkspaceStateKey';
12+
import * as fsExtra from 'fs-extra';
1213

1314
export class RokuAutomationViewViewProvider extends BaseRdbViewProvider {
1415
public readonly id = ViewProviderId.rokuAutomationView;
@@ -47,6 +48,107 @@ export class RokuAutomationViewViewProvider extends BaseRdbViewProvider {
4748
}
4849
});
4950

51+
this.registerCommand(VscodeCommand.rokuAutomationViewImportAllAutomations, async () => {
52+
if (this.isRecording) {
53+
// Only allow importing when we aren't currently recording
54+
void vscode.window.showInformationMessage('Cannot import automation scripts while recording. Please stop recording first.');
55+
return;
56+
}
57+
58+
// macOS does not have a title bar on the open dialog so we need to show a warning message
59+
if (process.platform === 'darwin') {
60+
const confirm = await vscode.window.showWarningMessage(
61+
'This will replace all automation scripts. Continue importing?',
62+
{ modal: true },
63+
'Yes'
64+
);
65+
if (confirm !== 'Yes') {
66+
return;
67+
}
68+
}
69+
70+
const filePath = await vscode.window.showOpenDialog({
71+
title: 'Import Automation Scripts (Warning: This will replace all currently loaded scripts)',
72+
filters: {
73+
'JSON': ['json'],
74+
'All Files': ['*']
75+
},
76+
defaultUri: vscode.Uri.file('automation.json'),
77+
canSelectMany: false
78+
});
79+
80+
if (!filePath) {
81+
return;
82+
}
83+
84+
try {
85+
const data = fsExtra.readFileSync(filePath[0].fsPath, 'utf8');
86+
const result = JSON.parse(data);
87+
this.selectedConfig = result.selectedConfig;
88+
this.rokuAutomationConfigs = result.configs;
89+
await this.extensionContext.workspaceState.update(WorkspaceStateKey.rokuAutomationConfigs, JSON.stringify(result));
90+
const message = this.createEventMessage(ViewProviderEvent.onRokuAutomationConfigsLoaded, {
91+
selectedConfig: this.selectedConfig,
92+
configs: this.rokuAutomationConfigs
93+
});
94+
95+
this.postOrQueueMessage(message);
96+
97+
this.updateCurrentRunningStep();
98+
this.onImportAllAutomations();
99+
void vscode.window.showInformationMessage('Automation scripts imported successfully from ' + filePath[0].fsPath);
100+
} catch (err) {
101+
void vscode.window.showErrorMessage('Failed to import automations: ' + (err as Error).message);
102+
}
103+
});
104+
105+
this.registerCommand(VscodeCommand.rokuAutomationViewExportAllAutomations, async () => {
106+
void vscode.window.showInformationMessage('Exporting automation data...');
107+
108+
if (this.isRecording) {
109+
// Only allow exporting when we aren't currently recording
110+
void vscode.window.showInformationMessage('Cannot export automation scripts while recording. Please stop recording first.');
111+
return;
112+
}
113+
114+
// Set the default save location to be the current workspace folder
115+
let defaultUri = vscode.Uri.file('automation.json');
116+
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
117+
// Use the first workspace folder
118+
defaultUri = vscode.Uri.joinPath(
119+
vscode.workspace.workspaceFolders[0].uri,
120+
'automation.json'
121+
);
122+
}
123+
124+
const filePath = await vscode.window.showSaveDialog({
125+
title: 'Export Automation scripts',
126+
filters: {
127+
'JSON': ['json'],
128+
'All Files': ['*']
129+
},
130+
defaultUri: defaultUri
131+
});
132+
133+
if (!filePath) {
134+
return;
135+
}
136+
137+
try {
138+
const obj =
139+
{
140+
selectedConfig: this.selectedConfig,
141+
configs: this.rokuAutomationConfigs
142+
};
143+
const json = JSON.stringify(obj, null, 4);
144+
145+
fsExtra.outputFileSync(filePath.fsPath, json);
146+
void vscode.window.showInformationMessage('Automation exported successfully to ' + filePath.fsPath);
147+
} catch (err) {
148+
void vscode.window.showErrorMessage('Failed to export automation scripts: ' + (err as Error).message);
149+
}
150+
});
151+
50152
this.registerCommand(VscodeCommand.rokuAutomationViewStartRecording, async () => {
51153
if (this.currentRunningStep === -1) {
52154
// Only allow recording when we aren't currently running
@@ -173,4 +275,22 @@ export class RokuAutomationViewViewProvider extends BaseRdbViewProvider {
173275

174276
this.updateCurrentRunningStep();
175277
}
278+
279+
protected onImportAllAutomations() {
280+
const message = this.createEventMessage(ViewProviderEvent.onRokuAutomationImportAllAutomations, {
281+
selectedConfig: this.selectedConfig,
282+
configs: this.rokuAutomationConfigs
283+
});
284+
285+
this.postOrQueueMessage(message);
286+
}
287+
288+
protected onExportAllAutomations() {
289+
const message = this.createEventMessage(ViewProviderEvent.onRokuAutomationExportAllAutomations, {
290+
selectedConfig: this.selectedConfig,
291+
configs: this.rokuAutomationConfigs
292+
});
293+
294+
this.postOrQueueMessage(message);
295+
}
176296
}

src/viewProviders/ViewProviderCommand.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export enum ViewProviderCommand {
1414
stopRokuAutomationConfig = 'stopRokuAutomationConfig',
1515
storeRokuAppOverlays = 'storeRokuAppOverlays',
1616
storeRokuAutomationConfigs = 'storeRokuAutomationConfigs',
17+
exportRokuAutomationConfigs = 'exportRokuAutomationConfigs',
18+
loadRokuAutomationConfigs = 'loadRokuAutomationConfigs',
1719
updateWorkspaceState = 'updateWorkspaceState',
1820
viewReady = 'viewReady'
1921
}

src/viewProviders/ViewProviderEvent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export enum ViewProviderEvent {
55
onRegistryUpdated = 'onRegistryUpdated',
66
onStoredNodeReferencesUpdated = 'onStoredNodeReferencesUpdated',
77
onRokuAutomationConfigsLoaded = 'onRokuAutomationConfigsLoaded',
8+
onRokuAutomationImportAllAutomations = 'onRokuAutomationImportAllAutomations',
9+
onRokuAutomationExportAllAutomations = 'onRokuAutomationExportAllAutomations',
810
onRokuAutomationConfigStepChange = 'onRokuAutomationConfigStepChange',
911
onRokuAutomationKeyPressed = 'onRokuAutomationKeyPressed',
1012
onRokuAppOverlayAdded = 'onRokuAppOverlayAdded',

0 commit comments

Comments
 (0)