Skip to content

Commit b748aec

Browse files
Add support for default build profile (arduino#2203)
* Add support for default profile to compile command * Add support for default profiles to upload command * Add TestCompileWithDefaultProfile to integration tests * Get the profile's FQBN if it's not already specified in the request * Update documentation regarding sketch projects * Added integration tests for all default_profile cases * Reverted old sketch_with_profile test --------- Co-authored-by: Cristian Maglie <[email protected]>
1 parent 048415c commit b748aec

File tree

10 files changed

+161
-13
lines changed

10 files changed

+161
-13
lines changed

Diff for: commands/compile/compile.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
8181

8282
fqbnIn := req.GetFqbn()
8383
if fqbnIn == "" && sk != nil {
84-
fqbnIn = sk.GetDefaultFQBN()
84+
if pme.GetProfile() != nil {
85+
fqbnIn = pme.GetProfile().FQBN
86+
} else {
87+
fqbnIn = sk.GetDefaultFQBN()
88+
}
8589
}
8690
if fqbnIn == "" {
8791
return nil, &arduino.MissingFQBNError{}

Diff for: commands/upload/upload.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,17 @@ func Upload(ctx context.Context, req *rpc.UploadRequest, outStream io.Writer, er
143143
}
144144
defer pmeRelease()
145145

146+
fqbn := req.GetFqbn()
147+
if fqbn == "" && pme.GetProfile() != nil {
148+
fqbn = pme.GetProfile().FQBN
149+
}
150+
146151
updatedPort, err := runProgramAction(
147152
pme,
148153
sk,
149154
req.GetImportFile(),
150155
req.GetImportDir(),
151-
req.GetFqbn(),
156+
fqbn,
152157
req.GetPort(),
153158
req.GetProgrammer(),
154159
req.GetVerbose(),

Diff for: docs/sketch-project-file.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ profiles:
9595
- ArduinoIoTCloud (1.0.2)
9696
- Arduino_ConnectionHandler (0.6.4)
9797
- TinyDHT sensor library (1.1.0)
98+
99+
default_profile: nanorp
98100
```
99101

100102
### Building a sketch
@@ -116,6 +118,16 @@ not be used in any way. In other words, the build is isolated from the system an
116118
specified in the profile: this will ensure that the build is portable and reproducible independently from the platforms
117119
and libraries installed in the system.
118120

121+
### Using a default profile
122+
123+
If a `default_profile` is specified in the `sketch.yaml` then the “classic” compile command:
124+
125+
```
126+
arduino-cli compile [sketch]
127+
```
128+
129+
will, instead, trigger a profile-based build using the default profile indicated in the `sketch.yaml`.
130+
119131
## Default flags for Arduino CLI usage
120132

121133
The sketch project file may be used to set the default value for some command line flags of the Arduino CLI, in
@@ -124,15 +136,17 @@ particular:
124136
- The `default_fqbn` key sets the default value for the `--fqbn` flag
125137
- The `default_port` key sets the default value for the `--port` flag
126138
- The `default_protocol` key sets the default value for the `--protocol` flag
139+
- The `default_profile` key sets the default value for the `--profile` flag
127140

128141
For example:
129142

130143
```
131144
default_fqbn: arduino:avr:uno
132145
default_port: /dev/ttyACM0
133146
default_protocol: serial
147+
default_profile: myprofile
134148
```
135149

136-
With this configuration set, it is not necessary to specify the `--fqbn`, `--port`, or `--protocol` flags to the
137-
[`arduino-cli compile`](commands/arduino-cli_compile.md) or [`arduino-cli upload`](commands/arduino-cli_upload.md)
150+
With this configuration set, it is not necessary to specify the `--fqbn`, `--port`, `--protocol` or `--profile` flags to
151+
the [`arduino-cli compile`](commands/arduino-cli_compile.md) or [`arduino-cli upload`](commands/arduino-cli_upload.md)
138152
commands when compiling or uploading the sketch.

Diff for: internal/cli/compile/compile.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,25 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
159159
}
160160

161161
sketchPath := arguments.InitSketchPath(path)
162-
inst, profile := instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
163-
if fqbnArg.String() == "" {
164-
fqbnArg.Set(profile.GetFqbn())
165-
}
166162

167163
sk, err := sketch.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
168164
if err != nil {
169165
feedback.FatalError(err, feedback.ErrGeneric)
170166
}
167+
168+
var inst *rpc.Instance
169+
var profile *rpc.Profile
170+
171+
if profileArg.Get() == "" {
172+
inst, profile = instance.CreateAndInitWithProfile(sk.GetDefaultProfile().GetName(), sketchPath)
173+
} else {
174+
inst, profile = instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
175+
}
176+
177+
if fqbnArg.String() == "" {
178+
fqbnArg.Set(profile.GetFqbn())
179+
}
180+
171181
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, sk.GetDefaultFqbn(), sk.GetDefaultPort(), sk.GetDefaultProtocol())
172182

173183
if keysKeychain != "" || signKey != "" || encryptKey != "" {

Diff for: internal/cli/upload/upload.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,25 @@ func runUploadCommand(command *cobra.Command, args []string) {
9595
feedback.Fatal(tr("Error during Upload: %v", err), feedback.ErrGeneric)
9696
}
9797

98-
instance, profile := instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
98+
var inst *rpc.Instance
99+
var profile *rpc.Profile
100+
101+
if profileArg.Get() == "" {
102+
inst, profile = instance.CreateAndInitWithProfile(sk.Project.DefaultProfile, sketchPath)
103+
} else {
104+
inst, profile = instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
105+
}
106+
99107
if fqbnArg.String() == "" {
100108
fqbnArg.Set(profile.GetFqbn())
101109
}
102110

103111
defaultFQBN := sk.GetDefaultFQBN()
104112
defaultAddress, defaultProtocol := sk.GetDefaultPortAddressAndProtocol()
105-
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, instance, defaultFQBN, defaultAddress, defaultProtocol)
113+
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, defaultFQBN, defaultAddress, defaultProtocol)
106114

107115
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
108-
Instance: instance,
116+
Instance: inst,
109117
Fqbn: fqbn,
110118
Protocol: port.Protocol,
111119
})
@@ -122,7 +130,7 @@ func runUploadCommand(command *cobra.Command, args []string) {
122130
}
123131

124132
// FIXME: Here we must not access package manager...
125-
pme, release := commands.GetPackageManagerExplorer(&rpc.UploadRequest{Instance: instance})
133+
pme, release := commands.GetPackageManagerExplorer(&rpc.UploadRequest{Instance: inst})
126134
platform := pme.FindPlatform(&packagemanager.PlatformReference{
127135
Package: split[0],
128136
PlatformArchitecture: split[1],
@@ -156,7 +164,7 @@ func runUploadCommand(command *cobra.Command, args []string) {
156164

157165
stdOut, stdErr, stdIOResult := feedback.OutputStreams()
158166
req := &rpc.UploadRequest{
159-
Instance: instance,
167+
Instance: inst,
160168
Fqbn: fqbn,
161169
SketchPath: path,
162170
Port: port,

Diff for: internal/integrationtest/profiles/profiles_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/arduino/arduino-cli/internal/integrationtest"
2222
"github.com/stretchr/testify/require"
23+
"go.bug.st/testifyjson/requirejson"
2324
)
2425

2526
func TestCompileWithProfiles(t *testing.T) {
@@ -69,3 +70,83 @@ func TestBuilderDidNotCatchLibsFromUnusedPlatforms(t *testing.T) {
6970
require.NotContains(t, string(stdout), "samd")
7071
require.NotContains(t, string(stderr), "samd")
7172
}
73+
74+
func TestCompileWithDefaultProfile(t *testing.T) {
75+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
76+
defer env.CleanUp()
77+
78+
// Init the environment explicitly
79+
_, _, err := cli.Run("core", "update-index")
80+
require.NoError(t, err)
81+
82+
// Installa core/libs globally
83+
_, _, err = cli.Run("core", "install", "arduino:[email protected]")
84+
require.NoError(t, err)
85+
86+
// copy sketch_with_profile into the working directory
87+
sketchWithoutDefProfilePath := cli.CopySketch("sketch_without_default_profile")
88+
sketchWithDefProfilePath := cli.CopySketch("sketch_with_default_profile")
89+
90+
{
91+
// no default profile -> error missing FQBN
92+
_, _, err := cli.Run("compile", sketchWithoutDefProfilePath.String(), "--format", "json")
93+
require.Error(t, err)
94+
}
95+
{
96+
// specified fbqn -> compile with specified FQBN and use global installation
97+
stdout, _, err := cli.Run("compile", "-b", "arduino:avr:nano", sketchWithoutDefProfilePath.String(), "--format", "json")
98+
require.NoError(t, err)
99+
jsonOut := requirejson.Parse(t, stdout)
100+
jsonOut.Query(".builder_result.build_platform").MustContain(`{"id":"arduino:avr", "version":"1.8.3"}`)
101+
jsonOut.Query(".builder_result.build_properties").MustContain(`[ "build.fqbn=arduino:avr:nano" ]`)
102+
}
103+
{
104+
// specified profile -> use the specified profile
105+
stdout, _, err := cli.Run("compile", "--profile", "avr1", sketchWithoutDefProfilePath.String(), "--format", "json")
106+
require.NoError(t, err)
107+
jsonOut := requirejson.Parse(t, stdout)
108+
jsonOut.Query(".builder_result.build_platform").MustContain(`{"id":"arduino:avr", "version":"1.8.4"}`)
109+
jsonOut.Query(".builder_result.build_properties").MustContain(`[ "build.fqbn=arduino:avr:uno" ]`)
110+
}
111+
{
112+
// specified profile and fqbn -> use the specified profile and override fqbn
113+
stdout, _, err := cli.Run("compile", "--profile", "avr1", "-b", "arduino:avr:nano", sketchWithoutDefProfilePath.String(), "--format", "json")
114+
require.NoError(t, err)
115+
jsonOut := requirejson.Parse(t, stdout)
116+
jsonOut.Query(".builder_result.build_platform").MustContain(`{"id":"arduino:avr", "version":"1.8.4"}`)
117+
jsonOut.Query(".builder_result.build_properties").MustContain(`[ "build.fqbn=arduino:avr:nano" ]`)
118+
}
119+
120+
{
121+
// default profile -> use default profile
122+
stdout, _, err := cli.Run("compile", sketchWithDefProfilePath.String(), "--format", "json")
123+
require.NoError(t, err)
124+
jsonOut := requirejson.Parse(t, stdout)
125+
jsonOut.Query(".builder_result.build_platform").MustContain(`{"id":"arduino:avr", "version":"1.8.5"}`)
126+
jsonOut.Query(".builder_result.build_properties").MustContain(`[ "build.fqbn=arduino:avr:leonardo" ]`)
127+
}
128+
{
129+
// default profile, specified fbqn -> use default profile, override fqbn
130+
stdout, _, err := cli.Run("compile", "-b", "arduino:avr:nano", sketchWithDefProfilePath.String(), "--format", "json")
131+
require.NoError(t, err)
132+
jsonOut := requirejson.Parse(t, stdout)
133+
jsonOut.Query(".builder_result.build_platform").MustContain(`{"id":"arduino:avr", "version":"1.8.5"}`)
134+
jsonOut.Query(".builder_result.build_properties").MustContain(`[ "build.fqbn=arduino:avr:nano" ]`)
135+
}
136+
{
137+
// default profile, specified different profile -> use the specified profile
138+
stdout, _, err := cli.Run("compile", "--profile", "avr1", sketchWithDefProfilePath.String(), "--format", "json")
139+
require.NoError(t, err)
140+
jsonOut := requirejson.Parse(t, stdout)
141+
jsonOut.Query(".builder_result.build_platform").MustContain(`{"id":"arduino:avr", "version":"1.8.4"}`)
142+
jsonOut.Query(".builder_result.build_properties").MustContain(`[ "build.fqbn=arduino:avr:uno" ]`)
143+
}
144+
{
145+
// default profile, specified different profile and fqbn -> use the specified profile and override fqbn
146+
stdout, _, err := cli.Run("compile", "--profile", "avr1", "-b", "arduino:avr:nano", sketchWithDefProfilePath.String(), "--format", "json")
147+
require.NoError(t, err)
148+
jsonOut := requirejson.Parse(t, stdout)
149+
jsonOut.Query(".builder_result.build_platform").MustContain(`{"id":"arduino:avr", "version":"1.8.4"}`)
150+
jsonOut.Query(".builder_result.build_properties").MustContain(`[ "build.fqbn=arduino:avr:nano" ]`)
151+
}
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
profiles:
2+
avr1:
3+
fqbn: arduino:avr:uno
4+
platforms:
5+
- platform: arduino:avr (1.8.4)
6+
7+
avr2:
8+
fqbn: arduino:avr:leonardo
9+
platforms:
10+
- platform: arduino:avr (1.8.5)
11+
12+
default_profile: avr2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
profiles:
2+
avr1:
3+
fqbn: arduino:avr:uno
4+
platforms:
5+
- platform: arduino:avr (1.8.4)
6+
7+
avr2:
8+
fqbn: arduino:avr:leonardo
9+
platforms:
10+
- platform: arduino:avr (1.8.5)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}

0 commit comments

Comments
 (0)