Skip to content

Commit 963c1a7

Browse files
regression: Import fix for compile --input-dir . regression found in #2304 (#2305)
* Add support for default build profile (#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]> * [skip-changelog] Use `LoadSketch` in upload function and return `rpc.Port` in `GetPort` (#2297) * Change GetPort's returned type to rpc.Port * Use LoadSketch in runUploadCommand --------- Co-authored-by: MatteoPologruto <[email protected]>
1 parent 048415c commit 963c1a7

File tree

13 files changed

+172
-25
lines changed

13 files changed

+172
-25
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/arguments/fqbn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,5 @@ func CalculateFQBNAndPort(portArgs *Port, fqbnArg *Fqbn, instance *rpc.Instance,
8989
if err != nil {
9090
feedback.Fatal(tr("Error getting port metadata: %v", err), feedback.ErrGeneric)
9191
}
92-
return fqbn, port.ToRPC()
92+
return fqbn, port
9393
}

Diff for: internal/cli/arguments/port.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"time"
2121

2222
"github.com/arduino/arduino-cli/arduino"
23-
"github.com/arduino/arduino-cli/arduino/discovery"
2423
"github.com/arduino/arduino-cli/commands"
2524
"github.com/arduino/arduino-cli/commands/board"
2625
"github.com/arduino/arduino-cli/internal/cli/feedback"
@@ -70,8 +69,7 @@ func (p *Port) GetPortAddressAndProtocol(instance *rpc.Instance, defaultAddress,
7069

7170
// GetPort returns the Port obtained by parsing command line arguments.
7271
// The extra metadata for the ports is obtained using the pluggable discoveries.
73-
func (p *Port) GetPort(instance *rpc.Instance, defaultAddress, defaultProtocol string) (*discovery.Port, error) {
74-
// TODO: REMOVE discovery from here (use board.List instead)
72+
func (p *Port) GetPort(instance *rpc.Instance, defaultAddress, defaultProtocol string) (*rpc.Port, error) {
7573

7674
address := p.address
7775
protocol := p.protocol
@@ -84,7 +82,7 @@ func (p *Port) GetPort(instance *rpc.Instance, defaultAddress, defaultProtocol s
8482
// the attached board without specifying explictly a port.
8583
// Tools that work this way must be specified using the property
8684
// "BOARD_ID.upload.tool.default" in the platform's boards.txt.
87-
return &discovery.Port{
85+
return &rpc.Port{
8886
Protocol: "default",
8987
}, nil
9088
}
@@ -113,13 +111,13 @@ func (p *Port) GetPort(instance *rpc.Instance, defaultAddress, defaultProtocol s
113111
}
114112
port := portEvent.Port
115113
if (protocol == "" || protocol == port.Protocol) && address == port.Address {
116-
return port, nil
114+
return port.ToRPC(), nil
117115
}
118116

119117
case <-deadline:
120118
// No matching port found
121119
if protocol == "" {
122-
return &discovery.Port{
120+
return &rpc.Port{
123121
Address: address,
124122
Protocol: "serial",
125123
}, nil

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func runBootloaderCommand(command *cobra.Command, args []string) {
7676
if _, err := upload.BurnBootloader(context.Background(), &rpc.BurnBootloaderRequest{
7777
Instance: instance,
7878
Fqbn: fqbn.String(),
79-
Port: discoveryPort.ToRPC(),
79+
Port: discoveryPort,
8080
Verbose: verbose,
8181
Verify: verify,
8282
Programmer: programmer.String(),

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

+18-9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424

2525
"github.com/arduino/arduino-cli/arduino"
2626
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
27-
"github.com/arduino/arduino-cli/arduino/sketch"
2827
"github.com/arduino/arduino-cli/commands"
28+
sk "github.com/arduino/arduino-cli/commands/sketch"
2929
"github.com/arduino/arduino-cli/commands/upload"
3030
"github.com/arduino/arduino-cli/i18n"
3131
"github.com/arduino/arduino-cli/internal/cli/arguments"
@@ -90,22 +90,31 @@ func runUploadCommand(command *cobra.Command, args []string) {
9090
arguments.WarnDeprecatedFiles(sketchPath)
9191
}
9292

93-
sk, err := sketch.New(sketchPath)
93+
sketch, err := sk.LoadSketch(context.Background(), &rpc.LoadSketchRequest{SketchPath: sketchPath.String()})
9494
if err != nil && importDir == "" && importFile == "" {
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(sketch.GetDefaultProfile().GetName(), sketchPath)
103+
} else {
104+
inst, profile = instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
105+
}
106+
99107
if fqbnArg.String() == "" {
100108
fqbnArg.Set(profile.GetFqbn())
101109
}
102110

103-
defaultFQBN := sk.GetDefaultFQBN()
104-
defaultAddress, defaultProtocol := sk.GetDefaultPortAddressAndProtocol()
105-
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, instance, defaultFQBN, defaultAddress, defaultProtocol)
111+
defaultFQBN := sketch.GetDefaultFqbn()
112+
defaultAddress := sketch.GetDefaultPort()
113+
defaultProtocol := sketch.GetDefaultProtocol()
114+
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, defaultFQBN, defaultAddress, defaultProtocol)
106115

107116
userFieldRes, err := upload.SupportedUserFields(context.Background(), &rpc.SupportedUserFieldsRequest{
108-
Instance: instance,
117+
Instance: inst,
109118
Fqbn: fqbn,
110119
Protocol: port.Protocol,
111120
})
@@ -122,7 +131,7 @@ func runUploadCommand(command *cobra.Command, args []string) {
122131
}
123132

124133
// FIXME: Here we must not access package manager...
125-
pme, release := commands.GetPackageManagerExplorer(&rpc.UploadRequest{Instance: instance})
134+
pme, release := commands.GetPackageManagerExplorer(&rpc.UploadRequest{Instance: inst})
126135
platform := pme.FindPlatform(&packagemanager.PlatformReference{
127136
Package: split[0],
128137
PlatformArchitecture: split[1],
@@ -156,7 +165,7 @@ func runUploadCommand(command *cobra.Command, args []string) {
156165

157166
stdOut, stdErr, stdIOResult := feedback.OutputStreams()
158167
req := &rpc.UploadRequest{
159-
Instance: instance,
168+
Instance: inst,
160169
Fqbn: fqbn,
161170
SketchPath: path,
162171
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)