@@ -12,11 +12,14 @@ import { ConnectedBoards } from './components/connected-boards';
12
12
import { CoreService } from '../common/protocol/core-service' ;
13
13
import { WorkspaceServiceExt } from './workspace-service-ext' ;
14
14
import { ToolOutputServiceClient } from '../common/protocol/tool-output-service' ;
15
- import { ConfirmDialog } from '@theia/core/lib/browser' ;
15
+ import { ConfirmDialog , OpenerService } from '@theia/core/lib/browser' ;
16
16
import { QuickPickService } from '@theia/core/lib/common/quick-pick-service' ;
17
17
import { BoardsListWidgetFrontendContribution } from './boards/boards-widget-frontend-contribution' ;
18
18
import { BoardsNotificationService } from './boards-notification-service' ;
19
-
19
+ import { FileSystem , FileStat } from '@theia/filesystem/lib/common' ;
20
+ import { WorkspaceRootUriAwareCommandHandler } from '@theia/workspace/lib/browser/workspace-commands' ;
21
+ import { SelectionService } from '@theia/core' ;
22
+ import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service' ;
20
23
21
24
@injectable ( )
22
25
export class ArduinoFrontendContribution extends DefaultFrontendApplicationContribution implements TabBarToolbarContribution , CommandContribution {
@@ -45,6 +48,19 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
45
48
@inject ( BoardsNotificationService )
46
49
protected readonly boardsNotificationService : BoardsNotificationService ;
47
50
51
+ @inject ( FileSystem )
52
+ protected readonly fileSystem : FileSystem ;
53
+
54
+ @inject ( OpenerService )
55
+ protected readonly openerService : OpenerService ;
56
+
57
+ @inject ( WorkspaceService )
58
+ protected readonly workspaceService : WorkspaceService ;
59
+
60
+ @inject ( SelectionService )
61
+ protected readonly selectionService : SelectionService ;
62
+
63
+
48
64
@postConstruct ( )
49
65
protected async init ( ) : Promise < void > {
50
66
// This is a hack. Otherwise, the backend services won't bind.
@@ -114,6 +130,64 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
114
130
}
115
131
}
116
132
} ) ;
133
+ registry . registerCommand ( ArduinoCommands . NEW_SKETCH , new WorkspaceRootUriAwareCommandHandler ( this . workspaceService , this . selectionService , {
134
+ execute : async uri => {
135
+ const parent = await this . getDirectory ( uri )
136
+ if ( ! parent ) {
137
+ return ;
138
+ }
139
+
140
+ const parentUri = new URI ( parent . uri ) ;
141
+ const monthNames = [ "january" , "february" , "march" , "april" , "may" , "june" ,
142
+ "july" , "august" , "september" , "october" , "november" , "december"
143
+ ] ;
144
+ const today = new Date ( ) ;
145
+
146
+ const sketchBaseName = `sketch_${ monthNames [ today . getMonth ( ) ] } ${ today . getDay ( ) } ` ;
147
+ let sketchName : string | undefined ;
148
+ for ( let i = 97 ; i < 97 + 26 ; i ++ ) {
149
+ let sketchNameCandidate = `${ sketchBaseName } ${ String . fromCharCode ( i ) } ` ;
150
+ if ( await this . fileSystem . exists ( parentUri . resolve ( sketchNameCandidate ) . toString ( ) ) ) {
151
+ continue ;
152
+ }
153
+
154
+ sketchName = sketchNameCandidate ;
155
+ break ;
156
+ }
157
+
158
+ if ( ! sketchName ) {
159
+ new ConfirmDialog ( { title : "New sketch" , msg : "Cannot create a unique sketch name" , ok : "Ok" } ) . open ( ) ;
160
+ return ;
161
+ }
162
+
163
+ try {
164
+ const sketchDir = parentUri . resolve ( sketchName ) ;
165
+ const sketchFile = sketchDir . resolve ( `${ sketchName } .ino` ) ;
166
+ this . fileSystem . createFolder ( sketchDir . toString ( ) ) ;
167
+ this . fileSystem . createFile ( sketchFile . toString ( ) , { content : `
168
+ void setup() {
169
+
170
+ }
171
+
172
+ void loop() {
173
+
174
+ }
175
+ ` } ) ;
176
+ const opener = await this . openerService . getOpener ( sketchFile )
177
+ opener . open ( sketchFile , { reveal : true } ) ;
178
+ } catch ( e ) {
179
+ new ConfirmDialog ( { title : "New sketch" , msg : "Cannot create new sketch: " + e , ok : "Ok" } ) . open ( ) ;
180
+ }
181
+ }
182
+ } ) )
183
+ }
184
+
185
+ protected async getDirectory ( candidate : URI ) : Promise < FileStat | undefined > {
186
+ const stat = await this . fileSystem . getFileStat ( candidate . toString ( ) ) ;
187
+ if ( stat && stat . isDirectory ) {
188
+ return stat ;
189
+ }
190
+ return this . fileSystem . getFileStat ( candidate . parent . toString ( ) ) ;
117
191
}
118
192
119
193
private async onNoBoardsInstalled ( ) {
0 commit comments