@@ -4,9 +4,12 @@ import { dirname } from 'path';
4
4
import { CoreService } from '../common/protocol/core-service' ;
5
5
import { CompileReq , CompileResp } from './cli-protocol/commands/compile_pb' ;
6
6
import { CoreClientProvider } from './core-client-provider' ;
7
- import { UploadReq , UploadResp , BurnBootloaderReq , BurnBootloaderResp } from './cli-protocol/commands/upload_pb' ;
7
+ import { UploadReq , UploadResp , BurnBootloaderReq , BurnBootloaderResp , UploadUsingProgrammerReq , UploadUsingProgrammerResp } from './cli-protocol/commands/upload_pb' ;
8
8
import { OutputService } from '../common/protocol/output-service' ;
9
9
import { NotificationServiceServer } from '../common/protocol' ;
10
+ import { ClientReadableStream } from '@grpc/grpc-js' ;
11
+ import { ArduinoCoreClient } from './cli-protocol/commands/commands_grpc_pb' ;
12
+ import { firstToUpperCase , firstToLowerCase } from '../common/utils' ;
10
13
11
14
@injectable ( )
12
15
export class CoreServiceImpl implements CoreService {
@@ -21,7 +24,7 @@ export class CoreServiceImpl implements CoreService {
21
24
protected readonly notificationService : NotificationServiceServer ;
22
25
23
26
async compile ( options : CoreService . Compile . Options ) : Promise < void > {
24
- this . outputService . append ( { name : 'compile' , chunk : 'Compiling ...\n' + JSON . stringify ( options , null , 2 ) + '\n--------------------------\n' } ) ;
27
+ this . outputService . append ( { name : 'compile' , chunk : 'Compile ...\n' + JSON . stringify ( options , null , 2 ) + '\n--------------------------\n' } ) ;
25
28
const { sketchUri, fqbn } = options ;
26
29
const sketchFilePath = FileUri . fsPath ( sketchUri ) ;
27
30
const sketchpath = dirname ( sketchFilePath ) ;
@@ -32,14 +35,12 @@ export class CoreServiceImpl implements CoreService {
32
35
}
33
36
const { client, instance } = coreClient ;
34
37
35
- if ( ! fqbn ) {
36
- throw new Error ( 'The selected board has no FQBN.' ) ;
37
- }
38
-
39
38
const compilerReq = new CompileReq ( ) ;
40
39
compilerReq . setInstance ( instance ) ;
41
40
compilerReq . setSketchpath ( sketchpath ) ;
42
- compilerReq . setFqbn ( fqbn ) ;
41
+ if ( fqbn ) {
42
+ compilerReq . setFqbn ( fqbn ) ;
43
+ }
43
44
compilerReq . setOptimizefordebug ( options . optimizeForDebug ) ;
44
45
compilerReq . setPreprocess ( false ) ;
45
46
compilerReq . setVerbose ( true ) ;
@@ -63,9 +64,23 @@ export class CoreServiceImpl implements CoreService {
63
64
}
64
65
65
66
async upload ( options : CoreService . Upload . Options ) : Promise < void > {
67
+ await this . doUpload ( options , ( ) => new UploadReq ( ) , ( client , req ) => client . upload ( req ) ) ;
68
+ }
69
+
70
+ async uploadUsingProgrammer ( options : CoreService . Upload . Options ) : Promise < void > {
71
+ await this . doUpload ( options , ( ) => new UploadUsingProgrammerReq ( ) , ( client , req ) => client . uploadUsingProgrammer ( req ) , 'upload using programmer' ) ;
72
+ }
73
+
74
+ protected async doUpload (
75
+ options : CoreService . Upload . Options ,
76
+ requestProvider : ( ) => UploadReq | UploadUsingProgrammerReq ,
77
+ responseHandler : ( client : ArduinoCoreClient , req : UploadReq | UploadUsingProgrammerReq ) => ClientReadableStream < UploadResp | UploadUsingProgrammerResp > ,
78
+ task : string = 'upload' ) : Promise < void > {
79
+
66
80
await this . compile ( options ) ;
67
- this . outputService . append ( { name : 'upload' , chunk : 'Uploading...\n' + JSON . stringify ( options , null , 2 ) + '\n--------------------------\n' } ) ;
68
- const { sketchUri, fqbn } = options ;
81
+ const chunk = firstToUpperCase ( task ) + '...\n' ;
82
+ this . outputService . append ( { name : 'upload' , chunk : chunk + JSON . stringify ( options , null , 2 ) + '\n--------------------------\n' } ) ;
83
+ const { sketchUri, fqbn, port, programmer } = options ;
69
84
const sketchFilePath = FileUri . fsPath ( sketchUri ) ;
70
85
const sketchpath = dirname ( sketchFilePath ) ;
71
86
@@ -75,34 +90,32 @@ export class CoreServiceImpl implements CoreService {
75
90
}
76
91
const { client, instance } = coreClient ;
77
92
78
- if ( ! fqbn ) {
79
- throw new Error ( 'The selected board has no FQBN.' ) ;
93
+ const req = requestProvider ( ) ;
94
+ req . setInstance ( instance ) ;
95
+ req . setSketchPath ( sketchpath ) ;
96
+ if ( fqbn ) {
97
+ req . setFqbn ( fqbn ) ;
80
98
}
81
-
82
- const uploadReq = new UploadReq ( ) ;
83
- uploadReq . setInstance ( instance ) ;
84
- uploadReq . setSketchPath ( sketchpath ) ;
85
- uploadReq . setFqbn ( fqbn ) ;
86
- if ( 'programmer' in options ) {
87
- uploadReq . setProgrammer ( options . programmer . id ) ;
99
+ if ( port ) {
100
+ req . setPort ( port ) ;
88
101
}
89
- if ( options . port ) {
90
- uploadReq . setPort ( options . port ) ;
102
+ if ( programmer ) {
103
+ req . setProgrammer ( programmer . id ) ;
91
104
}
92
- const result = client . upload ( uploadReq ) ;
105
+ const result = responseHandler ( client , req ) ;
93
106
94
107
try {
95
108
await new Promise < void > ( ( resolve , reject ) => {
96
109
result . on ( 'data' , ( resp : UploadResp ) => {
97
- this . outputService . append ( { name : 'upload' , chunk : Buffer . from ( resp . getOutStream_asU8 ( ) ) . toString ( ) } ) ;
98
- this . outputService . append ( { name : 'upload' , chunk : Buffer . from ( resp . getErrStream_asU8 ( ) ) . toString ( ) } ) ;
110
+ this . outputService . append ( { name : task , chunk : Buffer . from ( resp . getOutStream_asU8 ( ) ) . toString ( ) } ) ;
111
+ this . outputService . append ( { name : task , chunk : Buffer . from ( resp . getErrStream_asU8 ( ) ) . toString ( ) } ) ;
99
112
} ) ;
100
113
result . on ( 'error' , error => reject ( error ) ) ;
101
114
result . on ( 'end' , ( ) => resolve ( ) ) ;
102
115
} ) ;
103
- this . outputService . append ( { name : 'upload' , chunk : '\n--------------------------\nUpload complete.\n' } ) ;
116
+ this . outputService . append ( { name : 'upload' , chunk : '\n--------------------------\n' + firstToLowerCase ( task ) + ' complete.\n' } ) ;
104
117
} catch ( e ) {
105
- this . outputService . append ( { name : 'upload' , chunk : `Upload error: ${ e } \n` , severity : 'error' } ) ;
118
+ this . outputService . append ( { name : 'upload' , chunk : `${ firstToUpperCase ( task ) } error: ${ e } \n` , severity : 'error' } ) ;
106
119
throw e ;
107
120
}
108
121
}
@@ -113,19 +126,19 @@ export class CoreServiceImpl implements CoreService {
113
126
return ;
114
127
}
115
128
const { fqbn, port, programmer } = options ;
116
- if ( ! fqbn ) {
117
- throw new Error ( 'The selected board has no FQBN.' ) ;
129
+ const { client, instance } = coreClient ;
130
+ const burnReq = new BurnBootloaderReq ( ) ;
131
+ burnReq . setInstance ( instance ) ;
132
+ if ( fqbn ) {
133
+ burnReq . setFqbn ( fqbn ) ;
118
134
}
119
- if ( ! port ) {
120
- throw new Error ( 'Port must be specified.' ) ;
135
+ if ( port ) {
136
+ burnReq . setPort ( port ) ;
121
137
}
122
- const { client, instance } = coreClient ;
123
- const req = new BurnBootloaderReq ( ) ;
124
- req . setFqbn ( fqbn ) ;
125
- req . setPort ( port ) ;
126
- req . setProgrammer ( programmer . id ) ;
127
- req . setInstance ( instance ) ;
128
- const result = client . burnBootloader ( req ) ;
138
+ if ( programmer ) {
139
+ burnReq . setProgrammer ( programmer . id ) ;
140
+ }
141
+ const result = client . burnBootloader ( burnReq ) ;
129
142
try {
130
143
await new Promise < void > ( ( resolve , reject ) => {
131
144
result . on ( 'data' , ( resp : BurnBootloaderResp ) => {
0 commit comments