Skip to content

Commit c5d1075

Browse files
committed
Fix progressbar
Port of arduino/arduino-builder#289
1 parent ce6c187 commit c5d1075

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

legacy/builder/builder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (s *PreprocessSketch) Run(ctx *types.Context) error {
143143
} else {
144144
commands = append(commands, &ContainerAddPrototypes{})
145145
}
146-
return runCommands(ctx, commands, true)
146+
return runCommands(ctx, commands, false)
147147
}
148148

149149
type Preprocess struct{}
@@ -201,12 +201,12 @@ func (s *ParseHardwareAndDumpBuildProperties) Run(ctx *types.Context) error {
201201
func runCommands(ctx *types.Context, commands []types.Command, progressEnabled bool) error {
202202

203203
ctx.Progress.PrintEnabled = progressEnabled
204-
ctx.Progress.Progress = 0
205204

206205
for _, command := range commands {
207206
PrintRingNameIfDebug(ctx, command)
208-
ctx.Progress.Steps = 100.0 / float64(len(commands))
209-
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
207+
stepSize := float64(100-(6*len(ctx.ImportedLibraries))) / float64(len(commands))
208+
ctx.Progress.Steps = stepSize
209+
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx, progressEnabled, stepSize)
210210
err := command.Run(ctx)
211211
if err != nil {
212212
return i18n.WrapError(err)

legacy/builder/builder_utils/utils.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,20 @@ import (
4646
"github.com/arduino/go-properties-orderedmap"
4747
)
4848

49-
func PrintProgressIfProgressEnabledAndMachineLogger(ctx *types.Context) {
49+
func PrintProgressIfProgressEnabledAndMachineLogger(ctx *types.Context, progressEnabled bool, stepSize float64) {
5050

51-
if !ctx.Progress.PrintEnabled {
51+
var mut sync.Mutex
52+
if !progressEnabled {
5253
return
5354
}
5455

5556
log := ctx.GetLogger()
5657
if log.Name() == "machine" {
58+
mut.Lock()
5759
log.Println(constants.LOG_LEVEL_INFO, constants.MSG_PROGRESS, strconv.FormatFloat(ctx.Progress.Progress, 'f', 2, 32))
58-
ctx.Progress.Progress += ctx.Progress.Steps
60+
ctx.Progress.Progress += stepSize
61+
log.Flush()
62+
mut.Unlock()
5963
}
6064
}
6165

@@ -174,11 +178,11 @@ func compileFilesWithRecipe(ctx *types.Context, sourcePath *paths.Path, sources
174178
var errors []error
175179
var errorsMux sync.Mutex
176180

177-
ctx.Progress.Steps = ctx.Progress.Steps / float64(len(sources))
181+
stepSize := ctx.Progress.Steps / float64(len(sources))
178182

179183
queue := make(chan *paths.Path)
180184
job := func(source *paths.Path) {
181-
PrintProgressIfProgressEnabledAndMachineLogger(ctx)
185+
go PrintProgressIfProgressEnabledAndMachineLogger(ctx, true, stepSize)
182186
objectFile, err := compileFileWithRecipe(ctx, sourcePath, source, buildPath, buildProperties, includes, recipe)
183187
if err != nil {
184188
errorsMux.Lock()

legacy/builder/container_find_includes.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,11 @@ func (s *ContainerFindIncludes) Run(ctx *types.Context) error {
148148
queueSourceFilesFromFolder(ctx, sourceFilePaths, sketch, srcSubfolderPath, true /* recurse */)
149149
}
150150

151+
stepSize := ctx.Progress.Steps / 20.0
152+
stepLimit := ctx.Progress.Progress + ctx.Progress.Steps
153+
151154
for !sourceFilePaths.Empty() {
152-
err := findIncludesUntilDone(ctx, cache, sourceFilePaths.Pop())
155+
err := findIncludesUntilDone(ctx, cache, sourceFilePaths.Pop(), stepSize, stepLimit)
153156
if err != nil {
154157
cachePath.Remove()
155158
return i18n.WrapError(err)
@@ -290,7 +293,7 @@ func writeCache(cache *includeCache, path *paths.Path) error {
290293
return nil
291294
}
292295

293-
func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile types.SourceFile) error {
296+
func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile types.SourceFile, stepSize, stepLimit float64) error {
294297
sourcePath := sourceFile.SourcePath(ctx)
295298
targetFilePath := paths.NullPath()
296299
depPath := sourceFile.DepfilePath(ctx)
@@ -318,6 +321,8 @@ func findIncludesUntilDone(ctx *types.Context, cache *includeCache, sourceFile t
318321
var include string
319322
cache.ExpectFile(sourcePath)
320323

324+
go builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx, ctx.Progress.Progress < stepLimit, stepSize)
325+
321326
includes := ctx.IncludeFolders
322327
if library, ok := sourceFile.Origin.(*libraries.Library); ok && library.UtilityDir != nil {
323328
includes = append(includes, library.UtilityDir)

legacy/builder/container_setup.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
5252
&LibrariesLoader{},
5353
}
5454

55-
ctx.Progress.Steps = ctx.Progress.Steps / float64(len(commands))
55+
stepSize := ctx.Progress.Steps / float64(len(commands))
56+
ctx.Progress.Steps = stepSize
5657

5758
for _, command := range commands {
58-
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
59+
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx, ctx.Progress.PrintEnabled, stepSize)
5960
PrintRingNameIfDebug(ctx, command)
6061
err := command.Run(ctx)
6162
if err != nil {
@@ -84,10 +85,11 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
8485
&AddMissingBuildPropertiesFromParentPlatformTxtFiles{},
8586
}
8687

87-
ctx.Progress.Steps = ctx.Progress.Steps / float64(len(commands))
88+
stepSize = ctx.Progress.Steps / float64(len(commands))
89+
ctx.Progress.Steps = stepSize
8890

8991
for _, command := range commands {
90-
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
92+
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx, ctx.Progress.PrintEnabled, stepSize)
9193
PrintRingNameIfDebug(ctx, command)
9294
err := command.Run(ctx)
9395
if err != nil {

0 commit comments

Comments
 (0)