16
16
package phases
17
17
18
18
import (
19
+ "encoding/json"
19
20
"regexp"
20
21
"strconv"
21
22
@@ -40,11 +41,56 @@ func (s *Sizer) Run(ctx *types.Context) error {
40
41
41
42
buildProperties := ctx .BuildProperties
42
43
44
+ if buildProperties .ContainsKey ("recipe.advanced_size.pattern" ) {
45
+ err := checkSizeAdvanced (ctx , buildProperties )
46
+ return errors .WithStack (err )
47
+ }
48
+
43
49
err := checkSize (ctx , buildProperties )
50
+ return errors .WithStack (err )
51
+ }
52
+
53
+ func checkSizeAdvanced (ctx * types.Context , properties * properties.Map ) error {
54
+ command , err := builder_utils .PrepareCommandForRecipe (properties , "recipe.advanced_size.pattern" , false )
44
55
if err != nil {
45
- return errors .WithStack (err )
56
+ return errors .New ("Error while determining sketch size: " + err .Error ())
57
+ }
58
+
59
+ out , _ , err := utils .ExecCommand (ctx , command , utils .Capture /* stdout */ , utils .Show /* stderr */ )
60
+ if err != nil {
61
+ return errors .New ("Error while determining sketch size: " + err .Error ())
62
+ }
63
+
64
+ type AdvancedSizerResponse struct {
65
+ // Output are the messages displayed in console to the user
66
+ Output string `json:"output"`
67
+ // Severity may be one of "info", "warning" or "error". Warnings and errors will
68
+ // likely be printed in red. Errors will stop build/upload.
69
+ Severity string `json:"severity"`
70
+ // Sections are the sections sizes for machine readable use
71
+ Sections []types.ExecutableSectionSize `json:"sections"`
72
+ // ErrorMessage is a one line error message like:
73
+ // "text section exceeds available space in board"
74
+ // it must be set when Severity is "error"
75
+ ErrorMessage string `json:"error"`
46
76
}
47
77
78
+ var resp AdvancedSizerResponse
79
+ if err := json .Unmarshal (out , & resp ); err != nil {
80
+ return errors .New ("Error while determining sketch size: " + err .Error ())
81
+ }
82
+
83
+ ctx .ExecutableSectionsSize = resp .Sections
84
+ logger := ctx .GetLogger ()
85
+ switch resp .Severity {
86
+ case "error" :
87
+ logger .Println ("error" , "{0}" , resp .Output )
88
+ return errors .New (resp .ErrorMessage )
89
+ case "warning" :
90
+ logger .Println ("warn" , "{0}" , resp .Output )
91
+ default : // "info"
92
+ logger .Println ("info" , "{0}" , resp .Output )
93
+ }
48
94
return nil
49
95
}
50
96
0 commit comments