MCPcopy
hub / github.com/urfave/cli / checkBinarySizeActionFunc

Function checkBinarySizeActionFunc

scripts/build.go:439–519  ·  view source on GitHub ↗

checkBinarySizeActionFunc checks the size of an example binary to ensure that we are keeping size down this was originally inspired by https://github.com/urfave/cli/issues/1055, and followed up on as a part of https://github.com/urfave/cli/issues/1057

(ctx context.Context, cmd *cli.Command)

Source from the content-addressed store, hash-verified

437// this was originally inspired by https://github.com/urfave/cli/issues/1055, and followed up on as a part
438// of https://github.com/urfave/cli/issues/1057
439func checkBinarySizeActionFunc(ctx context.Context, cmd *cli.Command) (err error) {
440 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"
445 desiredMaxBinarySize = 2.2
446 desiredMinBinarySize = 1.49
447 mbStringFormatter = "%.1fMB"
448 )
449
450 tags := cmd.String("tags")
451
452 // get cli example size
453 cliSize, err := getSize(ctx, cliSourceFilePath, cliBuiltFilePath, tags)
454 if err != nil {
455 return err
456 }
457
458 // get hello world size
459 helloSize, err := getSize(ctx, helloSourceFilePath, helloBuiltFilePath, tags)
460 if err != nil {
461 return err
462 }
463
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)
475
476 // check against bounds
477 isLessThanDesiredMin := roundedFileSize < desiredMinBinarySize
478 isMoreThanDesiredMax := roundedFileSize > desiredMaxBinarySize
479 desiredMinSizeString := fmt.Sprintf(mbStringFormatter, desiredMinBinarySize)
480 desiredMaxSizeString := fmt.Sprintf(mbStringFormatter, desiredMaxBinarySize)
481
482 // show guidance
483 fmt.Printf("\n%s is the current binary size\n", roundedFileSizeString)
484 // show guidance for min size
485 if isLessThanDesiredMin {
486 fmt.Printf(" %s %s is the target min size\n", goodNewsEmoji, desiredMinSizeString)
487 fmt.Println("") // visual spacing
488 fmt.Println(" The binary is smaller than the target min size, which is great news!")
489 fmt.Println(" That means that your changes are shrinking the binary size.")
490 fmt.Println(" You'll want to go into ./scripts/build.go and decrease")
491 fmt.Println(" the desiredMinBinarySize, and also probably decrease the ")
492 fmt.Println(" desiredMaxBinarySize by the same amount. That will ensure that")
493 fmt.Println(" future PRs will enforce the newly shrunk binary sizes.")
494 fmt.Println("") // visual spacing
495 os.Exit(1)
496 } else {

Callers

nothing calls this directly

Calls 2

getSizeFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected