@@ -12,11 +12,14 @@ import { ConnectedBoards } from './components/connected-boards';
1212import { CoreService } from '../common/protocol/core-service' ;
1313import { WorkspaceServiceExt } from './workspace-service-ext' ;
1414import { ToolOutputServiceClient } from '../common/protocol/tool-output-service' ;
15- import { ConfirmDialog } from '@theia/core/lib/browser' ;
15+ import { ConfirmDialog , OpenerService } from '@theia/core/lib/browser' ;
1616import { QuickPickService } from '@theia/core/lib/common/quick-pick-service' ;
1717import { BoardsListWidgetFrontendContribution } from './boards/boards-widget-frontend-contribution' ;
1818import { 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' ;
2023
2124@injectable ( )
2225export class ArduinoFrontendContribution extends DefaultFrontendApplicationContribution implements TabBarToolbarContribution , CommandContribution {
@@ -45,6 +48,19 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
4548 @inject ( BoardsNotificationService )
4649 protected readonly boardsNotificationService : BoardsNotificationService ;
4750
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+
4864 @postConstruct ( )
4965 protected async init ( ) : Promise < void > {
5066 // This is a hack. Otherwise, the backend services won't bind.
@@ -114,6 +130,64 @@ export class ArduinoFrontendContribution extends DefaultFrontendApplicationContr
114130 }
115131 }
116132 } ) ;
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 ( ) ) ;
117191 }
118192
119193 private async onNoBoardsInstalled ( ) {
0 commit comments