Add first set of profile commands (#2917) · arduino/arduino-cli@6076d14 (original) (raw)

``

1

`+

// This file is part of arduino-cli.

`

``

2

`+

//

`

``

3

`+

// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)

`

``

4

`+

//

`

``

5

`+

// This software is released under the GNU General Public License version 3,

`

``

6

`+

// which covers the main part of arduino-cli.

`

``

7

`+

// The terms of this license can be found at:

`

``

8

`+

// https://www.gnu.org/licenses/gpl-3.0.en.html

`

``

9

`+

//

`

``

10

`+

// You can be released from the requirements of the above licenses by purchasing

`

``

11

`+

// a commercial license. Buying such a license is mandatory if you want to

`

``

12

`+

// modify or otherwise use the software for commercial activities involving the

`

``

13

`+

// Arduino software without disclosing the source code of your own applications.

`

``

14

`+

// To purchase a commercial license, send an email to license@arduino.cc.

`

``

15

+

``

16

`+

package commands

`

``

17

+

``

18

`+

import (

`

``

19

`+

"context"

`

``

20

`+

"errors"

`

``

21

`+

"fmt"

`

``

22

+

``

23

`+

"github.com/arduino/arduino-cli/commands/cmderrors"

`

``

24

`+

"github.com/arduino/arduino-cli/commands/internal/instances"

`

``

25

`+

"github.com/arduino/arduino-cli/internal/arduino/sketch"

`

``

26

`+

"github.com/arduino/arduino-cli/internal/i18n"

`

``

27

`+

"github.com/arduino/arduino-cli/pkg/fqbn"

`

``

28

`+

rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"

`

``

29

`+

"github.com/arduino/go-paths-helper"

`

``

30

`+

)

`

``

31

+

``

32

`+

// ProfileCreate creates a new project file if it does not exist. If a profile name with the associated FQBN is specified,

`

``

33

`+

// it is added to the project.

`

``

34

`+

func (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

+

``

42

`+

// Returns an error if the main file is missing from the sketch so there is no need to check if the path exists

`

``

43

`+

sk, err := sketch.New(paths.New(req.GetSketchPath()))

`

``

44

`+

if err != nil {

`

``

45

`+

return nil, err

`

``

46

`+

}

`

``

47

+

``

48

`+

fqbn, err := fqbn.Parse(req.GetFqbn())

`

``

49

`+

if err != nil {

`

``

50

`+

return nil, &cmderrors.InvalidFQBNError{Cause: err}

`

``

51

`+

}

`

``

52

+

``

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

`+

}

`

``

57

+

``

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

`+

}

`

``

66

+

``

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")),

`

``

74

`+

}

`

``

75

`+

}

`

``

76

`+

return nil, &cmderrors.InvalidFQBNError{Cause: err}

`

``

77

`+

}

`

``

78

+

``

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

`+

})

`

``

86

+

``

87

`+

sk.Project.Profiles = append(sk.Project.Profiles, newProfile)

`

``

88

`+

if req.DefaultProfile {

`

``

89

`+

sk.Project.DefaultProfile = newProfile.Name

`

``

90

`+

}

`

``

91

+

``

92

`+

projectFilePath := sk.GetProjectPath()

`

``

93

`+

err = projectFilePath.WriteFile([]byte(sk.Project.AsYaml()))

`

``

94

`+

if err != nil {

`

``

95

`+

return nil, err

`

``

96

`+

}

`

``

97

+

``

98

`+

return &rpc.ProfileCreateResponse{}, nil

`

``

99

`+

}

`