Skip to content

Commit a32d158

Browse files
committed
Make progress smoother
By adding more steps to the progress report we can have a smoother progress indication. However, the result will be slightly more than 100% due to counting the same operation multi-step operation twice. Overall, the result looks better anyway
1 parent 2fc66f3 commit a32d158

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

Diff for: builder.go

+6-19
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"strconv"
3636
"time"
3737

38+
"github.com/arduino/arduino-builder/builder_utils"
3839
"github.com/arduino/arduino-builder/constants"
3940
"github.com/arduino/arduino-builder/i18n"
4041
"github.com/arduino/arduino-builder/phases"
@@ -183,36 +184,22 @@ func (s *ParseHardwareAndDumpBuildProperties) Run(ctx *types.Context) error {
183184
}
184185

185186
func runCommands(ctx *types.Context, commands []types.Command, progressEnabled bool) error {
186-
commandsLength := len(commands)
187-
progressForEachCommand := float32(100) / float32(commandsLength)
188187

189-
progress := float32(0)
188+
ctx.Progress.PrintEnabled = progressEnabled
189+
ctx.Progress.Progress = 0
190+
190191
for _, command := range commands {
191192
PrintRingNameIfDebug(ctx, command)
192-
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, progress)
193+
ctx.Progress.Steps = 100.0 / float64(len(commands))
194+
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
193195
err := command.Run(ctx)
194196
if err != nil {
195197
return i18n.WrapError(err)
196198
}
197-
progress += progressForEachCommand
198199
}
199-
200-
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, 100)
201-
202200
return nil
203201
}
204202

205-
func printProgressIfProgressEnabledAndMachineLogger(progressEnabled bool, ctx *types.Context, progress float32) {
206-
if !progressEnabled {
207-
return
208-
}
209-
210-
log := ctx.GetLogger()
211-
if log.Name() == "machine" {
212-
log.Println(constants.LOG_LEVEL_INFO, constants.MSG_PROGRESS, strconv.FormatFloat(float64(progress), 'f', 2, 32))
213-
}
214-
}
215-
216203
func PrintRingNameIfDebug(ctx *types.Context, command types.Command) {
217204
if ctx.DebugLevel >= 10 {
218205
ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_RUNNING_COMMAND, strconv.FormatInt(time.Now().Unix(), 10), reflect.Indirect(reflect.ValueOf(command)).Type().Name())

Diff for: builder_utils/utils.go

+16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"os"
3535
"os/exec"
3636
"path/filepath"
37+
"strconv"
3738
"strings"
3839
"sync"
3940

@@ -44,6 +45,19 @@ import (
4445
"github.com/arduino/go-properties-map"
4546
)
4647

48+
func PrintProgressIfProgressEnabledAndMachineLogger(ctx *types.Context) {
49+
50+
if !ctx.Progress.PrintEnabled {
51+
return
52+
}
53+
54+
log := ctx.GetLogger()
55+
if log.Name() == "machine" {
56+
log.Println(constants.LOG_LEVEL_INFO, constants.MSG_PROGRESS, strconv.FormatFloat(ctx.Progress.Progress, 'f', 2, 32))
57+
ctx.Progress.Progress += ctx.Progress.Steps
58+
}
59+
}
60+
4761
func CompileFilesRecursive(ctx *types.Context, objectFiles []string, sourcePath string, buildPath string, buildProperties properties.Map, includes []string) ([]string, error) {
4862
objectFiles, err := CompileFiles(ctx, objectFiles, sourcePath, false, buildPath, buildProperties, includes)
4963
if err != nil {
@@ -153,12 +167,14 @@ func compileFilesWithRecipe(ctx *types.Context, objectFiles []string, sourcePath
153167
errorsChan := make(chan error)
154168
doneChan := make(chan struct{})
155169

170+
ctx.Progress.Steps = ctx.Progress.Steps / float64(len(sources))
156171
var wg sync.WaitGroup
157172
wg.Add(len(sources))
158173

159174
for _, source := range sources {
160175
go func(source string) {
161176
defer wg.Done()
177+
PrintProgressIfProgressEnabledAndMachineLogger(ctx)
162178
objectFile, err := compileFileWithRecipe(ctx, sourcePath, source, buildPath, buildProperties, includes, recipe)
163179
if err != nil {
164180
errorsChan <- err

Diff for: container_setup.go

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
package builder
3131

3232
import (
33+
"github.com/arduino/arduino-builder/builder_utils"
3334
"github.com/arduino/arduino-builder/i18n"
3435
"github.com/arduino/arduino-builder/types"
3536
)
@@ -54,7 +55,10 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
5455
&AddMissingBuildPropertiesFromParentPlatformTxtFiles{},
5556
}
5657

58+
ctx.Progress.Steps = ctx.Progress.Steps / float64(len(commands))
59+
5760
for _, command := range commands {
61+
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
5862
PrintRingNameIfDebug(ctx, command)
5963
err := command.Run(ctx)
6064
if err != nil {

Diff for: types/context.go

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
"github.com/arduino/go-properties-map"
88
)
99

10+
type ProgressStruct struct {
11+
PrintEnabled bool
12+
Steps float64
13+
Progress float64
14+
}
15+
1016
// Context structure
1117
type Context struct {
1218
// Build options
@@ -81,6 +87,9 @@ type Context struct {
8187
Verbose bool
8288
DebugPreprocessor bool
8389

90+
// Dry run, only create progress map
91+
Progress ProgressStruct
92+
8493
// Contents of a custom build properties file (line by line)
8594
CustomBuildProperties []string
8695

0 commit comments

Comments
 (0)