Skip to content

Commit

Permalink
Make progress smoother
Browse files Browse the repository at this point in the history
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
  • Loading branch information
facchinm committed Jan 23, 2018
1 parent 2fc66f3 commit a32d158
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
25 changes: 6 additions & 19 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"strconv"
"time"

"github.com/arduino/arduino-builder/builder_utils"
"github.com/arduino/arduino-builder/constants"
"github.com/arduino/arduino-builder/i18n"
"github.com/arduino/arduino-builder/phases"
Expand Down Expand Up @@ -183,36 +184,22 @@ func (s *ParseHardwareAndDumpBuildProperties) Run(ctx *types.Context) error {
}

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

progress := float32(0)
ctx.Progress.PrintEnabled = progressEnabled
ctx.Progress.Progress = 0

for _, command := range commands {
PrintRingNameIfDebug(ctx, command)
printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, progress)
ctx.Progress.Steps = 100.0 / float64(len(commands))
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
err := command.Run(ctx)
if err != nil {
return i18n.WrapError(err)
}
progress += progressForEachCommand
}

printProgressIfProgressEnabledAndMachineLogger(progressEnabled, ctx, 100)

return nil
}

func printProgressIfProgressEnabledAndMachineLogger(progressEnabled bool, ctx *types.Context, progress float32) {
if !progressEnabled {
return
}

log := ctx.GetLogger()
if log.Name() == "machine" {
log.Println(constants.LOG_LEVEL_INFO, constants.MSG_PROGRESS, strconv.FormatFloat(float64(progress), 'f', 2, 32))
}
}

func PrintRingNameIfDebug(ctx *types.Context, command types.Command) {
if ctx.DebugLevel >= 10 {
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())
Expand Down
16 changes: 16 additions & 0 deletions builder_utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"

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

func PrintProgressIfProgressEnabledAndMachineLogger(ctx *types.Context) {

if !ctx.Progress.PrintEnabled {
return
}

log := ctx.GetLogger()
if log.Name() == "machine" {
log.Println(constants.LOG_LEVEL_INFO, constants.MSG_PROGRESS, strconv.FormatFloat(ctx.Progress.Progress, 'f', 2, 32))
ctx.Progress.Progress += ctx.Progress.Steps
}
}

func CompileFilesRecursive(ctx *types.Context, objectFiles []string, sourcePath string, buildPath string, buildProperties properties.Map, includes []string) ([]string, error) {
objectFiles, err := CompileFiles(ctx, objectFiles, sourcePath, false, buildPath, buildProperties, includes)
if err != nil {
Expand Down Expand Up @@ -153,12 +167,14 @@ func compileFilesWithRecipe(ctx *types.Context, objectFiles []string, sourcePath
errorsChan := make(chan error)
doneChan := make(chan struct{})

ctx.Progress.Steps = ctx.Progress.Steps / float64(len(sources))
var wg sync.WaitGroup
wg.Add(len(sources))

for _, source := range sources {
go func(source string) {
defer wg.Done()
PrintProgressIfProgressEnabledAndMachineLogger(ctx)
objectFile, err := compileFileWithRecipe(ctx, sourcePath, source, buildPath, buildProperties, includes, recipe)
if err != nil {
errorsChan <- err
Expand Down
4 changes: 4 additions & 0 deletions container_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package builder

import (
"github.com/arduino/arduino-builder/builder_utils"
"github.com/arduino/arduino-builder/i18n"
"github.com/arduino/arduino-builder/types"
)
Expand All @@ -54,7 +55,10 @@ func (s *ContainerSetupHardwareToolsLibsSketchAndProps) Run(ctx *types.Context)
&AddMissingBuildPropertiesFromParentPlatformTxtFiles{},
}

ctx.Progress.Steps = ctx.Progress.Steps / float64(len(commands))

for _, command := range commands {
builder_utils.PrintProgressIfProgressEnabledAndMachineLogger(ctx)
PrintRingNameIfDebug(ctx, command)
err := command.Run(ctx)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"github.com/arduino/go-properties-map"
)

type ProgressStruct struct {
PrintEnabled bool
Steps float64
Progress float64
}

// Context structure
type Context struct {
// Build options
Expand Down Expand Up @@ -81,6 +87,9 @@ type Context struct {
Verbose bool
DebugPreprocessor bool

// Dry run, only create progress map
Progress ProgressStruct

// Contents of a custom build properties file (line by line)
CustomBuildProperties []string

Expand Down

0 comments on commit a32d158

Please sign in to comment.