Skip to content

Commit d9f6b66

Browse files
committed
Added support for advanced-sizers
1 parent 64792ce commit d9f6b66

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

legacy/builder/phases/sizer.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package phases
1717

1818
import (
19+
"encoding/json"
1920
"fmt"
2021
"regexp"
2122
"strconv"
@@ -41,11 +42,53 @@ func (s *Sizer) Run(ctx *types.Context) error {
4142

4243
buildProperties := ctx.BuildProperties
4344

44-
err := checkSize(ctx, buildProperties)
45+
if buildProperties.ContainsKey("recipe.advanced_size.pattern") {
46+
return checkSizeAdvanced(ctx, buildProperties)
47+
}
48+
49+
return checkSize(ctx, buildProperties)
50+
}
51+
52+
func checkSizeAdvanced(ctx *types.Context, properties *properties.Map) error {
53+
command, err := builder_utils.PrepareCommandForRecipe(properties, "recipe.advanced_size.pattern", false, ctx.PackageManager.GetEnvVarsForSpawnedProcess())
4554
if err != nil {
46-
return errors.WithStack(err)
55+
return errors.New(tr("Error while determining sketch size: %s", err))
4756
}
4857

58+
out, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.Show /* stderr */)
59+
if err != nil {
60+
return errors.New(tr("Error while determining sketch size: %s", err))
61+
}
62+
63+
type AdvancedSizerResponse struct {
64+
// Output are the messages displayed in console to the user
65+
Output string `json:"output"`
66+
// Severity may be one of "info", "warning" or "error". Warnings and errors will
67+
// likely be printed in red. Errors will stop build/upload.
68+
Severity string `json:"severity"`
69+
// Sections are the sections sizes for machine readable use
70+
Sections []types.ExecutableSectionSize `json:"sections"`
71+
// ErrorMessage is a one line error message like:
72+
// "text section exceeds available space in board"
73+
// it must be set when Severity is "error"
74+
ErrorMessage string `json:"error"`
75+
}
76+
77+
var resp AdvancedSizerResponse
78+
if err := json.Unmarshal(out, &resp); err != nil {
79+
return errors.New(tr("Error while determining sketch size: %s", err))
80+
}
81+
82+
ctx.ExecutableSectionsSize = resp.Sections
83+
switch resp.Severity {
84+
case "error":
85+
ctx.Warn(resp.Output)
86+
return errors.New(resp.ErrorMessage)
87+
case "warning":
88+
ctx.Warn(resp.Output)
89+
default: // or "info"
90+
ctx.Info(resp.Output)
91+
}
4992
return nil
5093
}
5194

0 commit comments

Comments
 (0)