Skip to content

Commit da5a22a

Browse files
committed
Added support for profiles in compile command
1 parent d821e32 commit da5a22a

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

arduino/errors.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,43 @@ func (e *UnknownFQBNError) ToRPCStatus() *status.Status {
193193
return status.New(codes.NotFound, e.Error())
194194
}
195195

196+
// UnknownProfileError is returned when the profile is not found
197+
type UnknownProfileError struct {
198+
Profile string
199+
Cause error
200+
}
201+
202+
func (e *UnknownProfileError) Error() string {
203+
return composeErrorMsg(tr("Profile '%s' not found", e.Profile), e.Cause)
204+
}
205+
206+
func (e *UnknownProfileError) Unwrap() error {
207+
return e.Cause
208+
}
209+
210+
// ToRPCStatus converts the error into a *status.Status
211+
func (e *UnknownProfileError) ToRPCStatus() *status.Status {
212+
return status.New(codes.NotFound, e.Error())
213+
}
214+
215+
// InvlaidProfileError is returned when the profile has errors
216+
type InvlaidProfileError struct {
217+
Cause error
218+
}
219+
220+
func (e *InvlaidProfileError) Error() string {
221+
return composeErrorMsg(tr("Invalid profile"), e.Cause)
222+
}
223+
224+
func (e *InvlaidProfileError) Unwrap() error {
225+
return e.Cause
226+
}
227+
228+
// ToRPCStatus converts the error into a *status.Status
229+
func (e *InvlaidProfileError) ToRPCStatus() *status.Status {
230+
return status.New(codes.FailedPrecondition, e.Error())
231+
}
232+
196233
// MissingPortAddressError is returned when the port protocol is mandatory and not specified
197234
type MissingPortAddressError struct{}
198235

cli/arguments/profiles.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020-2022 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 arguments
17+
18+
import "github.com/spf13/cobra"
19+
20+
// Profile contains the profile flag data.
21+
// This is useful so all flags used by commands that need
22+
// this information are consistent with each other.
23+
type Profile struct {
24+
profile string
25+
}
26+
27+
// AddToCommand adds the flags used to set fqbn to the specified Command
28+
func (f *Profile) AddToCommand(cmd *cobra.Command) {
29+
cmd.Flags().StringVarP(&f.profile, "profile", "m", "", tr("Sketch profile to use"))
30+
// TODO: register autocompletion
31+
}
32+
33+
// Get returns the profile
34+
func (f *Profile) Get() string {
35+
return f.profile
36+
}
37+
38+
// String returns the profile
39+
func (f *Profile) String() string {
40+
return f.profile
41+
}
42+
43+
// Set sets the profile
44+
func (f *Profile) Set(profile string) {
45+
f.profile = profile
46+
}

cli/compile/compile.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646

4747
var (
4848
fqbnArg arguments.Fqbn // Fully Qualified Board Name, e.g.: arduino:avr:uno.
49+
profile arguments.Profile // Profile to use
4950
showProperties bool // Show all build preferences used instead of compiling.
5051
preprocess bool // Print preprocessed code to stdout.
5152
buildCachePath string // Builds of 'core.a' are saved into this path to be cached and reused.
@@ -91,6 +92,7 @@ func NewCommand() *cobra.Command {
9192
}
9293

9394
fqbnArg.AddToCommand(compileCommand)
95+
profile.AddToCommand(compileCommand)
9496
compileCommand.Flags().BoolVar(&showProperties, "show-properties", false, tr("Show all build properties used instead of compiling."))
9597
compileCommand.Flags().BoolVar(&preprocess, "preprocess", false, tr("Print preprocessed code to stdout instead of compiling."))
9698
compileCommand.Flags().StringVar(&buildCachePath, "build-cache-path", "", tr("Builds of 'core.a' are saved into this path to be cached and reused."))
@@ -138,8 +140,6 @@ func NewCommand() *cobra.Command {
138140
}
139141

140142
func runCompileCommand(cmd *cobra.Command, args []string) {
141-
inst := instance.CreateAndInit()
142-
143143
logrus.Info("Executing `arduino-cli compile`")
144144

145145
path := ""
@@ -149,6 +149,12 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
149149

150150
sketchPath := arguments.InitSketchPath(path)
151151
sk := arguments.NewSketch(sketchPath)
152+
153+
inst, profileFqbn := instance.CreateAndInitWithProfile(profile.Get(), sketchPath)
154+
if fqbnArg.String() == "" {
155+
fqbnArg.Set(profileFqbn)
156+
}
157+
152158
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, inst, sk)
153159

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

commands/compile/compile.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
9191
if pm == nil {
9292
return nil, &arduino.InvalidInstanceError{}
9393
}
94+
lm := commands.GetLibraryManager(req.GetInstance().GetId())
95+
if lm == nil {
96+
return nil, &arduino.InvalidInstanceError{}
97+
}
9498

9599
logrus.Tracef("Compile %s for %s started", req.GetSketchPath(), req.GetFqbn())
96100
if req.GetSketchPath() == "" {
@@ -109,6 +113,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
109113
if fqbnIn == "" {
110114
return nil, &arduino.MissingFQBNError{}
111115
}
116+
112117
fqbn, err := cores.ParseFQBN(fqbnIn)
113118
if err != nil {
114119
return nil, &arduino.InvalidFQBNError{Cause: err}
@@ -136,6 +141,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
136141

137142
builderCtx := &types.Context{}
138143
builderCtx.PackageManager = pm
144+
builderCtx.LibrariesManager = lm
139145
builderCtx.FQBN = fqbn
140146
builderCtx.SketchLocation = sk.FullPath
141147
builderCtx.ProgressCB = progressCB
@@ -188,6 +194,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
188194
builderCtx.ArduinoAPIVersion = "10607"
189195

190196
// Check if Arduino IDE is installed and get it's libraries location.
197+
// TODO: Remove?
191198
dataDir := paths.New(configuration.Settings.GetString("directories.Data"))
192199
preferencesTxt := dataDir.Join("preferences.txt")
193200
ideProperties, err := properties.LoadFromPath(preferencesTxt)
@@ -210,7 +217,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
210217
builderCtx.Stderr = errStream
211218
builderCtx.Clean = req.GetClean()
212219
builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly()
213-
214220
builderCtx.SourceOverride = req.GetSourceOverride()
215221

216222
r = &rpc.CompileResponse{}

0 commit comments

Comments
 (0)