16
16
package phases
17
17
18
18
import (
19
+ "encoding/json"
19
20
"fmt"
20
21
"regexp"
21
22
"strconv"
@@ -41,11 +42,53 @@ func (s *Sizer) Run(ctx *types.Context) error {
41
42
42
43
buildProperties := ctx .BuildProperties
43
44
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 ())
45
54
if err != nil {
46
- return errors .WithStack ( err )
55
+ return errors .New ( tr ( "Error while determining sketch size: %s" , err ) )
47
56
}
48
57
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
+ }
49
92
return nil
50
93
}
51
94
0 commit comments