@@ -32,72 +32,67 @@ import (
3232// ProfileCreate creates a new project file if it does not exist. If a profile name with the associated FQBN is specified,
3333// it is added to the project.
3434func (s * arduinoCoreServerImpl ) ProfileCreate (ctx context.Context , req * rpc.ProfileCreateRequest ) (* rpc.ProfileCreateResponse , error ) {
35+ if req .GetProfileName () == "" {
36+ return nil , & cmderrors.MissingProfileError {}
37+ }
38+ if req .GetFqbn () == "" {
39+ return nil , & cmderrors.MissingFQBNError {}
40+ }
41+
3542 // Returns an error if the main file is missing from the sketch so there is no need to check if the path exists
3643 sk , err := sketch .New (paths .New (req .GetSketchPath ()))
3744 if err != nil {
3845 return nil , err
3946 }
40- projectFilePath := sk .GetProjectPath ()
4147
42- if ! projectFilePath .Exist () {
43- err := projectFilePath .WriteFile ([]byte ("profiles: {}\n " ))
44- if err != nil {
45- return nil , err
46- }
48+ fqbn , err := fqbn .Parse (req .GetFqbn ())
49+ if err != nil {
50+ return nil , & cmderrors.InvalidFQBNError {Cause : err }
4751 }
4852
49- if req .GetProfileName () != "" {
50- if req .GetFqbn () == "" {
51- return nil , & cmderrors.MissingFQBNError {}
52- }
53- fqbn , err := fqbn .Parse (req .GetFqbn ())
54- if err != nil {
55- return nil , & cmderrors.InvalidFQBNError {Cause : err }
56- }
57-
58- // Check that the profile name is unique
59- if profile , _ := sk .GetProfile (req .ProfileName ); profile != nil {
60- return nil , & cmderrors.ProfileAlreadyExitsError {Profile : req .ProfileName }
61- }
53+ // Check that the profile name is unique
54+ if profile , _ := sk .GetProfile (req .ProfileName ); profile != nil {
55+ return nil , & cmderrors.ProfileAlreadyExitsError {Profile : req .ProfileName }
56+ }
6257
63- pme , release , err := instances .GetPackageManagerExplorer (req .GetInstance ())
64- if err != nil {
65- return nil , err
66- }
67- defer release ()
68- if pme .Dirty () {
69- return nil , & cmderrors.InstanceNeedsReinitialization {}
70- }
58+ pme , release , err := instances .GetPackageManagerExplorer (req .GetInstance ())
59+ if err != nil {
60+ return nil , err
61+ }
62+ defer release ()
63+ if pme .Dirty () {
64+ return nil , & cmderrors.InstanceNeedsReinitialization {}
65+ }
7166
72- // Automatically detect the target platform if it is installed on the user's machine
73- _ , targetPlatform , _ , _ , _ , err := pme .ResolveFQBN (fqbn )
74- if err != nil {
75- if targetPlatform == nil {
76- return nil , & cmderrors.PlatformNotFoundError {
77- Platform : fmt .Sprintf ("%s:%s" , fqbn .Vendor , fqbn .Architecture ),
78- Cause : errors .New (i18n .Tr ("platform not installed" )),
79- }
67+ // Automatically detect the target platform if it is installed on the user's machine
68+ _ , targetPlatform , _ , _ , _ , err := pme .ResolveFQBN (fqbn )
69+ if err != nil {
70+ if targetPlatform == nil {
71+ return nil , & cmderrors.PlatformNotFoundError {
72+ Platform : fmt .Sprintf ("%s:%s" , fqbn .Vendor , fqbn .Architecture ),
73+ Cause : errors .New (i18n .Tr ("platform not installed" )),
8074 }
81- return nil , & cmderrors.InvalidFQBNError {Cause : err }
8275 }
76+ return nil , & cmderrors.InvalidFQBNError {Cause : err }
77+ }
8378
84- newProfile := & sketch.Profile {Name : req .GetProfileName (), FQBN : req .GetFqbn ()}
85- // TODO: what to do with the PlatformIndexURL?
86- newProfile .Platforms = append (newProfile .Platforms , & sketch.ProfilePlatformReference {
87- Packager : targetPlatform .Platform .Package .Name ,
88- Architecture : targetPlatform .Platform .Architecture ,
89- Version : targetPlatform .Version ,
90- })
79+ newProfile := & sketch.Profile {Name : req .GetProfileName (), FQBN : req .GetFqbn ()}
80+ // TODO: what to do with the PlatformIndexURL?
81+ newProfile .Platforms = append (newProfile .Platforms , & sketch.ProfilePlatformReference {
82+ Packager : targetPlatform .Platform .Package .Name ,
83+ Architecture : targetPlatform .Platform .Architecture ,
84+ Version : targetPlatform .Version ,
85+ })
9186
92- sk .Project .Profiles = append (sk .Project .Profiles , newProfile )
93- if req .DefaultProfile {
94- sk .Project .DefaultProfile = newProfile .Name
95- }
87+ sk .Project .Profiles = append (sk .Project .Profiles , newProfile )
88+ if req .DefaultProfile {
89+ sk .Project .DefaultProfile = newProfile .Name
90+ }
9691
97- err = projectFilePath . WriteFile ([] byte ( sk .Project . AsYaml ()) )
98- if err != nil {
99- return nil , err
100- }
92+ projectFilePath := sk .GetProjectPath ( )
93+ err = projectFilePath . WriteFile ([] byte ( sk . Project . AsYaml ()))
94+ if err != nil {
95+ return nil , err
10196 }
10297
10398 return & rpc.ProfileCreateResponse {}, nil
0 commit comments