android 编译时候前面的百分比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

build/soong/ui/status/log.go

return fmt.Sprintf("[%3d%% %d/%d] ", 100*counts.FinishedActions/counts.TotalActions, counts.FinishedActions, counts.TotalActions)



func (s formatter) progress(counts status.Counts) string {
if s.format == "" {
return fmt.Sprintf("[%3d%% %d/%d] ",
100*counts.FinishedActions/counts.TotalActions, counts.FinishedActions,
counts.TotalActions)
}
buf := &strings.Builder{}
for i := 0; i < len(s.format); i++ {
c := s.format[i]
if c != '%' {
buf.WriteByte(c)
continue
}
i = i + 1
if i == len(s.format) {
buf.WriteByte(c)
break
}
c = s.format[i]
switch c {
case '%':
buf.WriteByte(c)
case 's':
fmt.Fprintf(buf, "%d", counts.StartedActions)
case 't':
fmt.Fprintf(buf, "%d", counts.TotalActions)
case 'r':
fmt.Fprintf(buf, "%d", counts.RunningActions)
case 'u':
fmt.Fprintf(buf, "%d", counts.TotalActions-counts.StartedActions)
case 'f':
fmt.Fprintf(buf, "%d", counts.FinishedActions)
case 'o':
fmt.Fprintf(buf, "%.1f",
float64(counts.FinishedActions)/time.Since(s.start).Seconds())
case 'c':
// TODO: implement?
buf.WriteRune('?')
case 'p':
fmt.Fprintf(buf, "%3d%%",
100*counts.FinishedActions/counts.TotalActions)
case 'e':
fmt.Fprintf(buf, "%.3f", time.Since(s.start).Seconds())
default:
buf.WriteString("unknown placeholder '")
buf.WriteByte(c)
buf.WriteString("'")}
}
return buf.String()
}

cmd/soong_ui/main.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33


// Main execution of soong_ui. The command format is as follows:
//
// soong_ui <command> [<arg 1> <arg 2> ... <arg n>]
//
// Command is the type of soong_ui execution. Only one type of
// execution is specified. The args are specific to the command.
func main() {
shared.ReexecWithDelveMaybe(os.Getenv("SOONG_UI_DELVE"), shared.ResolveDelveBinary())

buildStarted := time.Now()

c, args, err := getCommand(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing `soong` args: %s.\n", err)
os.Exit(1)
}

// Create a terminal output that mimics Ninja's.
ninja_status := os.Getenv("NINJA_STATUS")
if ninja_status == "" { // 如果从环境变量获取,是空,那么我们设置一个默认的,加上 环境变量中 product,variant的值
product := os.Getenv("TARGET_PRODUCT")
variant := os.Getenv("TARGET_BUILD_VARIANT")
ninja_status = fmt.Sprintf("[%s-%s][%%p %%f/%%t]", product, variant)
}
output := terminal.NewStatusOutput(c.stdio().Stdout(), ninja_status, c.simpleOutput,
build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD"),
build.OsEnvironment().IsEnvTrue("SOONG_UI_ANSI_OUTPUT"))

// Attach a new logger instance to the terminal output.
log := logger.New(output)
defer log.Cleanup()

ui/terminal/status.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// output.
//
// statusFormat takes nearly all the same options as NINJA_STATUS.
// %c is currently unsupported.
func NewStatusOutput(w io.Writer, statusFormat string, forceSimpleOutput, quietBuild, forceKeepANSI bool) status.StatusOutput {
formatter := newFormatter(statusFormat, quietBuild)

if forceSimpleOutput || quietBuild || !isSmartTerminal(w) {
return NewSimpleStatusOutput(w, formatter, forceKeepANSI, quietBuild)
} else {
return NewSimpleStatusOutput(w, formatter, forceKeepANSI, quietBuild)
}
}




ui/terminal/simple_status.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/ limitations under the License.

package terminal

import (
"fmt"
"io"
"strings"
"android/soong/ui/status"
)

type simpleStatusOutput struct {
writer io.Writer
formatter formatter
keepANSI bool
outputLevel status.MsgLevel
}

// NewSimpleStatusOutput returns a StatusOutput that represents the
// current build status similarly to Ninja's built-in terminal
// output.
func NewSimpleStatusOutput(w io.Writer, formatter formatter, keepANSI bool, quietBuild bool) status.StatusOutput {
level := status.StatusLvl
if quietBuild {
level = status.PrintLvl
}
return &simpleStatusOutput{
writer: w,
formatter: formatter,
keepANSI: keepANSI,
outputLevel: level,
}
}

func (s *simpleStatusOutput) Message(level status.MsgLevel, message string) {
if level >= s.outputLevel {
fmt.Fprintln(s.writer, s.formatter.message(level, message))
}
}

func (s *simpleStatusOutput) StartAction(_ *status.Action, _ status.Counts) {
}

func (s *simpleStatusOutput) FinishAction(result status.ActionResult, counts status.Counts) {
if s.outputLevel > status.StatusLvl {
return
}

prefix := s.formatter.progress(counts) // 前缀,百分比,进度
desc := result.Description // 描述
//cmd := result.Command // 执行的命令,这个会很长

progress := prefix + desc
fmt.Fprintln(s.writer, progress)
//progress := prefix + cmd
//fmt.Fprintln(s.writer, progress)

output := s.formatter.result(result)
if !s.keepANSI {
output = string(stripAnsiEscapes([]byte(output)))
}

if output != "" {
lines := strings.Split(output, "\n")
for _,v := range lines{
progress := prefix + v + "\n"
fmt.Fprint(s.writer, progress)
}
}
}

func (s *simpleStatusOutput) Flush() {}

func (s *simpleStatusOutput) Write(p []byte) (int, error) {
fmt.Fprint(s.writer, string(p))
return len(p), nil
}



ui/terminal/status_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2018 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package terminal
import (
"bytes"
"fmt"
"os"
"syscall"
"testing"
"android/soong/ui/status"
)
func TestStatusOutput(t *testing.T) {
tests := []struct {
name string
calls func(stat status.StatusOutput)
smart string
simple string
}{
{
name: "two actions",
calls: twoActions,
smart: "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action2\x1b[0m\x1b[K\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\n",
simple: "[ 50% 1/2] action1\n[100% 2/2] action2\n",
},
{
name: "two parallel actions",
calls: twoParallelActions,
smart: "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 0% 0/2] action2\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\n",
simple: "[ 50% 1/2] action1\n[100% 2/2] action2\n",
},
{
name: "action with output",
calls: actionsWithOutput,
smart: "\r\x1b[1m[ 0% 0/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action2\x1b[0m\x1b[K\r\x1b[1m[ 66% 2/3] action2\x1b[0m\x1b[K\noutput1\noutput2\n\r\x1b[1m[ 66% 2/3] action3\x1b[0m\x1b[K\r\x1b[1m[100% 3/3] action3\x1b[0m\x1b[K\n",
simple: "[ 33% 1/3] action1\n[ 66% 2/3] action2\noutput1\noutput2\n[100% 3/3] action3\n",
},
{
name: "action with output without newline",
calls: actionsWithOutputWithoutNewline,
smart: "\r\x1b[1m[ 0% 0/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action2\x1b[0m\x1b[K\r\x1b[1m[ 66% 2/3] action2\x1b[0m\x1b[K\noutput1\noutput2\n\r\x1b[1m[ 66% 2/3] action3\x1b[0m\x1b[K\r\x1b[1m[100% 3/3] action3\x1b[0m\x1b[K\n",
simple: "[ 33% 1/3] action1\n[ 66% 2/3] action2\noutput1\noutput2\n[100% 3/3] action3\n",
},
{
name: "action with error",
calls: actionsWithError,
smart: "\r\x1b[1m[ 0% 0/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action1\x1b[0m\x1b[K\r\x1b[1m[ 33% 1/3] action2\x1b[0m\x1b[K\r\x1b[1m[ 66% 2/3] action2\x1b[0m\x1b[K\nFAILED: f1 f2\ntouch f1 f2\nerror1\nerror2\n\r\x1b[1m[ 66% 2/3] action3\x1b[0m\x1b[K\r\x1b[1m[100% 3/3] action3\x1b[0m\x1b[K\n",
simple: "[ 33% 1/3] action1\n[ 66% 2/3] action2\nFAILED: f1 f2\ntouch f1 f2\nerror1\nerror2\n[100% 3/3] action3\n",
},
{
name: "action with empty description",
calls: actionWithEmptyDescription,
smart: "\r\x1b[1m[ 0% 0/1] command1\x1b[0m\x1b[K\r\x1b[1m[100% 1/1] command1\x1b[0m\x1b[K\n",
simple: "[100% 1/1] command1\n",
},
{
name: "messages",
calls: actionsWithMessages,
smart: "\r\x1b[1m[ 0% 0/2] action1\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action1\x1b[0m\x1b[K\r\x1b[1mstatus\x1b[0m\x1b[K\r\x1b[Kprint\nFAILED: error\n\r\x1b[1m[ 50% 1/2] action2\x1b[0m\x1b[K\r\x1b[1m[100% 2/2] action2\x1b[0m\x1b[K\n",
simple: "[ 50% 1/2] action1\nstatus\nprint\nFAILED: error\n[100% 2/2] action2\n",
},
{
name: "action with long description",
calls: actionWithLongDescription,
smart: "\r\x1b[1m[ 0% 0/2] action with very long descrip\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action with very long descrip\x1b[0m\x1b[K\n",
simple: "[ 50% 1/2] action with very long description to test eliding\n",
},
{
name: "action with output with ansi codes",
calls: actionWithOuptutWithAnsiCodes,
smart: "\r\x1b[1m[ 0% 0/1] action1\x1b[0m\x1b[K\r\x1b[1m[100% 1/1] action1\x1b[0m\x1b[K\n\x1b[31mcolor\x1b[0m\n",
simple: "[100% 1/1] action1\ncolor\n",
},
}
os.Setenv(tableHeightEnVar, "")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run("smart", func(t *testing.T) {
smart := &fakeSmartTerminal{termWidth: 40}
stat := NewSmartStatusOutput(smart, newFormatter("", false))
tt.calls(stat)
stat.Flush()
if g, w := smart.String(), tt.smart; g != w {
t.Logf("want:\n%q\ngot:\n%q", w, g)
}
})
t.Run("simple", func(t *testing.T) {
simple := &bytes.Buffer{}
stat := NewStatusOutput(simple, "", false, false, false)
tt.calls(stat)
stat.Flush()
if g, w := simple.String(), tt.simple; g != w {
t.Logf("want:\n%q\ngot:\n%q", w, g)
}
})
t.Run("force simple", func(t *testing.T) {
smart := &fakeSmartTerminal{termWidth: 40}
stat := NewStatusOutput(smart, "", true, false, false)
tt.calls(stat)
stat.Flush()
if g, w := smart.String(), tt.simple; g != w {
t.Logf("want:\n%q\ngot:\n%q", w, g)
}
})
})
}
}
type runner struct {
counts status.Counts
stat status.StatusOutput
}
func newRunner(stat status.StatusOutput, totalActions int) *runner {
return &runner{
counts: status.Counts{TotalActions: totalActions},
stat: stat,
}
}
func (r *runner) startAction(action *status.Action) {
r.counts.StartedActions++
r.counts.RunningActions++
r.stat.StartAction(action, r.counts)
}
func (r *runner) finishAction(result status.ActionResult) {
r.counts.FinishedActions++
r.counts.RunningActions--
r.stat.FinishAction(result, r.counts)
}
func (r *runner) finishAndStartAction(result status.ActionResult, action *status.Action) {
r.counts.FinishedActions++
r.stat.FinishAction(result, r.counts)
r.counts.StartedActions++
r.stat.StartAction(action, r.counts)
}
var (
action1 = &status.Action{Description: "action1"}
result1 = status.ActionResult{Action: action1}
action2 = &status.Action{Description: "action2"}
result2 = status.ActionResult{Action: action2}
action3 = &status.Action{Description: "action3"}
result3 = status.ActionResult{Action: action3}
)
func twoActions(stat status.StatusOutput) {
runner := newRunner(stat, 2)
runner.startAction(action1)
runner.finishAction(result1)
runner.startAction(action2)
runner.finishAction(result2)
}
func twoParallelActions(stat status.StatusOutput) {
runner := newRunner(stat, 2)
runner.startAction(action1)
runner.startAction(action2)
runner.finishAction(result1)
runner.finishAction(result2)
}
func actionsWithOutput(stat status.StatusOutput) {
result2WithOutput := status.ActionResult{Action: action2, Output: "output1\noutput2\n"}
runner := newRunner(stat, 3)
runner.startAction(action1)
runner.finishAction(result1)
runner.startAction(action2)
runner.finishAction(result2WithOutput)
runner.startAction(action3)
runner.finishAction(result3)
}
func actionsWithOutputWithoutNewline(stat status.StatusOutput) {
result2WithOutputWithoutNewline := status.ActionResult{Action: action2, Output: "output1\noutput2"}
runner := newRunner(stat, 3)
runner.startAction(action1)
runner.finishAction(result1)
runner.startAction(action2)
runner.finishAction(result2WithOutputWithoutNewline)
runner.startAction(action3)
runner.finishAction(result3)
}
func actionsWithError(stat status.StatusOutput) {
action2WithError := &status.Action{Description: "action2", Outputs: []string{"f1", "f2"}, Command: "touch f1 f2"}
result2WithError := status.ActionResult{Action: action2WithError, Output: "error1\nerror2\n", Error: fmt.Logf("error1")}
runner := newRunner(stat, 3)
runner.startAction(action1)
runner.finishAction(result1)
runner.startAction(action2WithError)
runner.finishAction(result2WithError)
runner.startAction(action3)
runner.finishAction(result3)
}
func actionWithEmptyDescription(stat status.StatusOutput) {
action1 := &status.Action{Command: "command1"}
result1 := status.ActionResult{Action: action1}
runner := newRunner(stat, 1)
runner.startAction(action1)
runner.finishAction(result1)
}
func actionsWithMessages(stat status.StatusOutput) {
runner := newRunner(stat, 2)
runner.startAction(action1)
runner.finishAction(result1)
stat.Message(status.VerboseLvl, "verbose")
stat.Message(status.StatusLvl, "status")
stat.Message(status.PrintLvl, "print")
stat.Message(status.ErrorLvl, "error")
runner.startAction(action2)
runner.finishAction(result2)
}
func actionWithLongDescription(stat status.StatusOutput) {
action1 := &status.Action{Description: "action with very long description to test eliding"}
result1 := status.ActionResult{Action: action1}
runner := newRunner(stat, 2)
runner.startAction(action1)
runner.finishAction(result1)
}
func actionWithOuptutWithAnsiCodes(stat status.StatusOutput) {
result1WithOutputWithAnsiCodes := status.ActionResult{Action: action1, Output: "\x1b[31mcolor\x1b[0m"}
runner := newRunner(stat, 1)
runner.startAction(action1)
runner.finishAction(result1WithOutputWithAnsiCodes)
}
func TestSmartStatusOutputWidthChange(t *testing.T) {
os.Setenv(tableHeightEnVar, "")
smart := &fakeSmartTerminal{termWidth: 40}
stat := NewSmartStatusOutput(smart, newFormatter("", false))
smartStat := stat.(*smartStatusOutput)
smartStat.sigwinchHandled = make(chan bool)
runner := newRunner(stat, 2)
action := &status.Action{Description: "action with very long description to test eliding"}
result := status.ActionResult{Action: action}
runner.startAction(action)
smart.termWidth = 30
// Fake a SIGWINCH
smartStat.sigwinch <- syscall.SIGWINCH
<-smartStat.sigwinchHandled
runner.finishAction(result)
stat.Flush()
w := "\r\x1b[1m[ 0% 0/2] action with very long descrip\x1b[0m\x1b[K\r\x1b[1m[ 50% 1/2] action with very lo\x1b[0m\x1b[K\n"
if g := smart.String(); g != w {
t.Logf("want:\n%q\ngot:\n%q", w, g)
}
}