Skip to content

Commit ddaefa0

Browse files
Luca BianconiBikappa
Luca Bianconi
authored andcommitted
feat: allow to show compilation properties without providing a sketch
1 parent c660b33 commit ddaefa0

File tree

7 files changed

+79
-41
lines changed

7 files changed

+79
-41
lines changed

Diff for: commands/compile/compile.go

+48-31
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
7373
return nil, &arduino.MissingSketchPathError{}
7474
}
7575
sketchPath := paths.New(req.GetSketchPath())
76-
sk, err := sketch.New(sketchPath)
77-
if err != nil {
78-
return nil, &arduino.CantOpenSketchError{Cause: err}
76+
builderCtx := &types.Context{}
77+
builderCtx.PackageManager = pme
78+
if pme.GetProfile() != nil {
79+
builderCtx.LibrariesManager = lm
7980
}
8081

82+
sk, newSketchErr := sketch.New(sketchPath)
83+
8184
fqbnIn := req.GetFqbn()
8285
if fqbnIn == "" && sk != nil {
8386
fqbnIn = sk.GetDefaultFQBN()
@@ -111,13 +114,23 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
111114
securityKeysOverride = append(securityKeysOverride, "build.keys.keychain="+req.KeysKeychain, "build.keys.sign_key="+req.GetSignKey(), "build.keys.encrypt_key="+req.EncryptKey)
112115
}
113116

114-
builderCtx := &types.Context{}
115-
builderCtx.PackageManager = pme
116-
if pme.GetProfile() != nil {
117-
builderCtx.LibrariesManager = lm
118-
}
119117
builderCtx.UseCachedLibrariesResolution = req.GetSkipLibrariesDiscovery()
120118
builderCtx.FQBN = fqbn
119+
defer func() {
120+
appendBuildProperties(r, builderCtx)
121+
}()
122+
r = &rpc.CompileResponse{}
123+
if newSketchErr != nil {
124+
if req.GetShowProperties() {
125+
// Just get build properties and exit
126+
compileErr := builder.RunParseHardware(builderCtx)
127+
if compileErr != nil {
128+
compileErr = &arduino.CompileFailedError{Message: compileErr.Error()}
129+
}
130+
return r, compileErr
131+
}
132+
return nil, &arduino.CantOpenSketchError{Cause: err}
133+
}
121134
builderCtx.Sketch = sk
122135
builderCtx.ProgressCB = progressCB
123136

@@ -183,7 +196,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
183196
builderCtx.OnlyUpdateCompilationDatabase = req.GetCreateCompilationDatabaseOnly()
184197
builderCtx.SourceOverride = req.GetSourceOverride()
185198

186-
r = &rpc.CompileResponse{}
187199
defer func() {
188200
if p := builderCtx.BuildPath; p != nil {
189201
r.BuildPath = p.String()
@@ -196,18 +208,6 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
196208
}
197209
}()
198210

199-
defer func() {
200-
buildProperties := builderCtx.BuildProperties
201-
if buildProperties == nil {
202-
return
203-
}
204-
keys := buildProperties.Keys()
205-
sort.Strings(keys)
206-
for _, key := range keys {
207-
r.BuildProperties = append(r.BuildProperties, key+"="+buildProperties.Get(key))
208-
}
209-
}()
210-
211211
if req.GetShowProperties() {
212212
// Just get build properties and exit
213213
compileErr := builder.RunParseHardware(builderCtx)
@@ -227,16 +227,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
227227
}
228228

229229
defer func() {
230-
importedLibs := []*rpc.Library{}
231-
for _, lib := range builderCtx.ImportedLibraries {
232-
rpcLib, err := lib.ToRPCLibrary()
233-
if err != nil {
234-
msg := tr("Error getting information for library %s", lib.Name) + ": " + err.Error() + "\n"
235-
errStream.Write([]byte(msg))
236-
}
237-
importedLibs = append(importedLibs, rpcLib)
238-
}
239-
r.UsedLibraries = importedLibs
230+
appendUserLibraries(r, builderCtx, errStream)
240231
}()
241232

242233
// if it's a regular build, go on...
@@ -295,6 +286,32 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
295286
return r, nil
296287
}
297288

289+
func appendUserLibraries(r *rpc.CompileResponse, builderCtx *types.Context, errStream io.Writer) {
290+
importedLibs := []*rpc.Library{}
291+
for _, lib := range builderCtx.ImportedLibraries {
292+
rpcLib, err := lib.ToRPCLibrary()
293+
if err != nil {
294+
msg := tr("Error getting information for library %s", lib.Name) + ": " + err.Error() + "\n"
295+
errStream.Write([]byte(msg))
296+
}
297+
importedLibs = append(importedLibs, rpcLib)
298+
}
299+
r.UsedLibraries = importedLibs
300+
}
301+
302+
func appendBuildProperties(r *rpc.CompileResponse, builderCtx *types.Context) bool {
303+
buildProperties := builderCtx.BuildProperties
304+
if buildProperties == nil {
305+
return true
306+
}
307+
keys := buildProperties.Keys()
308+
sort.Strings(keys)
309+
for _, key := range keys {
310+
r.BuildProperties = append(r.BuildProperties, key+"="+buildProperties.Get(key))
311+
}
312+
return false
313+
}
314+
298315
// maybePurgeBuildCache runs the build files cache purge if the policy conditions are met.
299316
func maybePurgeBuildCache() {
300317

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ func InitSketchPath(path string) (sketchPath *paths.Path) {
4343
}
4444

4545
// NewSketch is a helper function useful to create a sketch instance
46-
func NewSketch(sketchPath *paths.Path) *sketch.Sketch {
46+
func NewSketch(sketchPath *paths.Path) (*sketch.Sketch, error) {
47+
return sketch.New(sketchPath)
48+
}
49+
50+
// MustNewSketch is a helper function useful to create a sketch instance, exits if the
51+
// initialization fails
52+
func MustNewSketch(sketchPath *paths.Path) *sketch.Sketch {
4753
sketch, err := sketch.New(sketchPath)
4854
if err != nil {
4955
feedback.Fatal(tr("Error opening sketch: %v", err), feedback.ErrGeneric)

Diff for: internal/cli/board/attach.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func initAttachCommand() *cobra.Command {
5050

5151
func runAttachCommand(path string, port *arguments.Port, fqbn string) {
5252
sketchPath := arguments.InitSketchPath(path)
53-
sk := arguments.NewSketch(sketchPath)
53+
sk := arguments.MustNewSketch(sketchPath)
5454

5555
var currentPort *boardAttachPortResult
5656
if currentAddress, currentProtocol := sk.GetDefaultPortAddressAndProtocol(); currentAddress != "" {

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

+16-6
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,29 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
180180
feedback.Fatal(tr("You cannot use the %s flag while compiling with a profile.", "--library"), feedback.ErrBadArgument)
181181
}
182182
}
183+
showPropertiesM, err := parseShowPropertiesMode(showProperties)
184+
if err != nil {
185+
feedback.Fatal(tr("Error parsing --show-properties flag: %v", err), feedback.ErrGeneric)
186+
}
183187

184188
path := ""
185189
if len(args) > 0 {
186190
path = args[0]
187191
}
188192

189193
sketchPath := arguments.InitSketchPath(path)
190-
sk := arguments.NewSketch(sketchPath)
194+
sk, err := arguments.NewSketch(sketchPath)
195+
196+
if err != nil {
197+
showPropertiesWithEmptySketchPath := path == "" && showPropertiesM != showPropertiesModeDisabled
198+
if showPropertiesWithEmptySketchPath {
199+
// properties were requested and no sketch path was provided
200+
// let's use an empty sketch struct and hope for the best
201+
sk = nil
202+
} else {
203+
feedback.Fatal(tr("Error opening sketch: %v", err), feedback.ErrGeneric)
204+
}
205+
}
191206

192207
inst, profile := instance.CreateAndInitWithProfile(profileArg.Get(), sketchPath)
193208
if fqbnArg.String() == "" {
@@ -215,11 +230,6 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
215230
overrides = o.Overrides
216231
}
217232

218-
showPropertiesM, err := parseShowPropertiesMode(showProperties)
219-
if err != nil {
220-
feedback.Fatal(tr("Error parsing --show-properties flag: %v", err), feedback.ErrGeneric)
221-
}
222-
223233
var stdOut, stdErr io.Writer
224234
var stdIORes func() *feedback.OutputStreamsResult
225235
if showPropertiesM != showPropertiesModeDisabled {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func runDebugCommand(command *cobra.Command, args []string) {
7575
}
7676

7777
sketchPath := arguments.InitSketchPath(path)
78-
sk := arguments.NewSketch(sketchPath)
78+
sk := arguments.MustNewSketch(sketchPath)
7979
fqbn, port := arguments.CalculateFQBNAndPort(&portArgs, &fqbnArg, instance, sk)
8080
debugConfigRequested := &dbg.DebugConfigRequest{
8181
Instance: instance,

Diff for: legacy/builder/fail_if_buildpath_equals_sketchpath.go

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
type FailIfBuildPathEqualsSketchPath struct{}
2424

2525
func (s *FailIfBuildPathEqualsSketchPath) Run(ctx *types.Context) error {
26+
if ctx.BuildPath == nil || ctx.Sketch == nil {
27+
return nil
28+
}
2629
buildPath := ctx.BuildPath.Canonical()
2730
sketchPath := ctx.Sketch.FullPath.Canonical()
2831
if buildPath.EqualsTo(sketchPath) {

Diff for: legacy/builder/setup_build_properties.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ func (s *SetupBuildProperties) Run(ctx *types.Context) error {
109109
buildProperties.Set("software", DEFAULT_SOFTWARE)
110110
}
111111

112-
buildProperties.SetPath("build.source.path", ctx.Sketch.FullPath)
112+
if ctx.Sketch != nil {
113+
buildProperties.SetPath("build.source.path", ctx.Sketch.FullPath)
114+
}
113115

114116
now := time.Now()
115117
buildProperties.Set("extra.time.utc", strconv.FormatInt(now.Unix(), 10))

0 commit comments

Comments
 (0)