Skip to content

Commit b497e28

Browse files
committed
scripts: show binary size with urfave_cli_no_template tag
In check-binary-size, also show how much the package contributes to the binary size when built with urfave_cli_no_template tag. This is for informational purposes only; the limits are checked for the default build. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent 2884995 commit b497e28

1 file changed

Lines changed: 49 additions & 19 deletions

File tree

‎scripts/build.go‎

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -438,40 +438,31 @@ func GfmrunActionFunc(ctx context.Context, cmd *cli.Command) error {
438438
// of https://github.com/urfave/cli/issues/1057
439439
func checkBinarySizeActionFunc(ctx context.Context, cmd *cli.Command) (err error) {
440440
const (
441-
cliSourceFilePath = "./examples/example-cli/example-cli.go"
442-
cliBuiltFilePath = "./examples/example-cli/built-example"
443-
helloSourceFilePath = "./examples/example-hello-world/example-hello-world.go"
444-
helloBuiltFilePath = "./examples/example-hello-world/built-example"
445441
desiredMaxBinarySize = 2.2
446442
desiredMinBinarySize = 1.49
447443
mbStringFormatter = "%.1fMB"
448444
)
449445

450446
tags := cmd.String("tags")
451447

452-
// get cli example size
453-
cliSize, err := getSize(ctx, cliSourceFilePath, cliBuiltFilePath, tags)
448+
cliSizeDiff, err := getCLISize(ctx, tags)
454449
if err != nil {
455450
return err
456451
}
457452

458-
// get hello world size
459-
helloSize, err := getSize(ctx, helloSourceFilePath, helloBuiltFilePath, tags)
453+
// Also get the size with urfave_cli_no_template build tag,
454+
// for informational purposes.
455+
noTmplTags := "urfave_cli_no_template"
456+
if tags != "" {
457+
noTmplTags = tags + "," + noTmplTags
458+
}
459+
noTmplSizeDiff, err := getCLISize(ctx, noTmplTags)
460460
if err != nil {
461461
return err
462462
}
463463

464-
// The CLI size diff is the number we are interested in.
465-
// This tells us how much our CLI package contributes to the binary size.
466-
cliSizeDiff := cliSize - helloSize
467-
468-
// get human readable size, in MB with one decimal place.
469-
// example output is: 35.2MB. (note: this simply an example)
470-
// that output is much easier to reason about than the `35223432`
471-
// that you would see output without the rounding
472-
fileSizeInMB := float64(cliSizeDiff) / float64(1000000)
473-
roundedFileSize := math.Round(fileSizeInMB*10) / 10
474-
roundedFileSizeString := fmt.Sprintf(mbStringFormatter, roundedFileSize)
464+
roundedFileSize := roundToMB(cliSizeDiff)
465+
roundedFileSizeString := roundToMBString(cliSizeDiff)
475466

476467
// check against bounds
477468
isLessThanDesiredMin := roundedFileSize < desiredMinBinarySize
@@ -481,6 +472,7 @@ func checkBinarySizeActionFunc(ctx context.Context, cmd *cli.Command) (err error
481472

482473
// show guidance
483474
fmt.Printf("\n%s is the current binary size\n", roundedFileSizeString)
475+
fmt.Printf("%s is the current binary size with urfave_cli_no_template build tag\n", roundToMBString(noTmplSizeDiff))
484476
// show guidance for min size
485477
if isLessThanDesiredMin {
486478
fmt.Printf(" %s %s is the target min size\n", goodNewsEmoji, desiredMinSizeString)
@@ -680,6 +672,44 @@ func V3Diff(ctx context.Context, cmd *cli.Command) error {
680672
return err
681673
}
682674

675+
// getCLISize returns how much the cli package contributes to the binary size,
676+
// by comparing the sizes of the cli example and the hello world example.
677+
func getCLISize(ctx context.Context, tags string) (int64, error) {
678+
const (
679+
cliSourceFilePath = "./examples/example-cli/example-cli.go"
680+
cliBuiltFilePath = "./examples/example-cli/built-example"
681+
helloSourceFilePath = "./examples/example-hello-world/example-hello-world.go"
682+
helloBuiltFilePath = "./examples/example-hello-world/built-example"
683+
)
684+
685+
// get cli example size
686+
cliSize, err := getSize(ctx, cliSourceFilePath, cliBuiltFilePath, tags)
687+
if err != nil {
688+
return 0, err
689+
}
690+
691+
// get hello world size
692+
helloSize, err := getSize(ctx, helloSourceFilePath, helloBuiltFilePath, tags)
693+
if err != nil {
694+
return 0, err
695+
}
696+
697+
// The CLI size diff is the number we are interested in.
698+
// This tells us how much our CLI package contributes to the binary size.
699+
return cliSize - helloSize, nil
700+
}
701+
702+
// roundToMB returns size in MB, rounded to one decimal place.
703+
func roundToMB(size int64) float64 {
704+
return math.Round(float64(size)/1e6*10) / 10
705+
}
706+
707+
// roundToMBString returns size in MB, in a human readable form,
708+
// such as 35.2MB. This is much easier to reason about than 35223432.
709+
func roundToMBString(size int64) string {
710+
return fmt.Sprintf("%.1fMB", roundToMB(size))
711+
}
712+
683713
func getSize(ctx context.Context, sourcePath, builtPath, tags string) (int64, error) {
684714
args := []string{"build"}
685715

0 commit comments

Comments
 (0)