@@ -23,8 +23,11 @@ import (
2323 "sort"
2424 "strings"
2525
26+ "github.com/arduino/arduino-cli/commands/cmderrors"
27+ f "github.com/arduino/arduino-cli/internal/algorithms"
2628 "github.com/arduino/arduino-cli/internal/arduino/globals"
2729 "github.com/arduino/arduino-cli/internal/i18n"
30+ rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2831 "github.com/arduino/go-paths-helper"
2932)
3033
@@ -180,15 +183,14 @@ func (s *Sketch) supportedFiles() (*paths.PathList, error) {
180183 return & files , nil
181184}
182185
183- // GetProfile returns the requested profile or nil if the profile
184- // is not found.
185- func (s * Sketch ) GetProfile (profileName string ) * Profile {
186+ // GetProfile returns the requested profile or an error if not found
187+ func (s * Sketch ) GetProfile (profileName string ) (* Profile , error ) {
186188 for _ , p := range s .Project .Profiles {
187189 if p .Name == profileName {
188- return p
190+ return p , nil
189191 }
190192 }
191- return nil
193+ return nil , & cmderrors. UnknownProfileError { Profile : profileName }
192194}
193195
194196// checkSketchCasing returns an error if the casing of the sketch folder and the main file are different.
@@ -278,23 +280,6 @@ func (e *InvalidSketchFolderNameError) Error() string {
278280 return tr ("no valid sketch found in %[1]s: missing %[2]s" , e .SketchFolder , e .SketchFile )
279281}
280282
281- // CheckForPdeFiles returns all files ending with .pde extension
282- // in sketch, this is mainly used to warn the user that these files
283- // must be changed to .ino extension.
284- // When .pde files won't be supported anymore this function must be removed.
285- func CheckForPdeFiles (sketch * paths.Path ) []* paths.Path {
286- if sketch .IsNotDir () {
287- sketch = sketch .Parent ()
288- }
289-
290- files , err := sketch .ReadDirRecursive ()
291- if err != nil {
292- return []* paths.Path {}
293- }
294- files .FilterSuffix (".pde" )
295- return files
296- }
297-
298283// DefaultBuildPath generates the default build directory for a given sketch.
299284// The build path is in a temporary directory and is unique for each sketch.
300285func (s * Sketch ) DefaultBuildPath () * paths.Path {
@@ -307,3 +292,23 @@ func (s *Sketch) Hash() string {
307292 md5SumBytes := md5 .Sum ([]byte (path ))
308293 return strings .ToUpper (hex .EncodeToString (md5SumBytes [:]))
309294}
295+
296+ // ToRpc converts this Sketch into a rpc.LoadSketchResponse
297+ func (s * Sketch ) ToRpc () * rpc.Sketch {
298+ defaultPort , defaultProtocol := s .GetDefaultPortAddressAndProtocol ()
299+ res := & rpc.Sketch {
300+ MainFile : s .MainFile .String (),
301+ LocationPath : s .FullPath .String (),
302+ OtherSketchFiles : s .OtherSketchFiles .AsStrings (),
303+ AdditionalFiles : s .AdditionalFiles .AsStrings (),
304+ RootFolderFiles : s .RootFolderFiles .AsStrings (),
305+ DefaultFqbn : s .GetDefaultFQBN (),
306+ DefaultPort : defaultPort ,
307+ DefaultProtocol : defaultProtocol ,
308+ Profiles : f .Map (s .Project .Profiles , (* Profile ).ToRpc ),
309+ }
310+ if defaultProfile , err := s .GetProfile (s .Project .DefaultProfile ); err == nil {
311+ res .DefaultProfile = defaultProfile .ToRpc ()
312+ }
313+ return res
314+ }
0 commit comments