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