@@ -9,6 +9,7 @@ import { ViewProviderId } from './ViewProviderId';
99import { ViewProviderCommand } from './ViewProviderCommand' ;
1010import { ViewProviderEvent } from './ViewProviderEvent' ;
1111import { WorkspaceStateKey } from './WorkspaceStateKey' ;
12+ import * as fsExtra from 'fs-extra' ;
1213
1314export 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}
0 commit comments