add diffing
This commit is contained in:
parent
a0932dafcb
commit
3558c7f30c
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,5 +3,5 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
vendor
|
vendor
|
||||||
.idea
|
.idea
|
||||||
internal/example/built-example
|
internal/*/built-example
|
||||||
coverage.txt
|
coverage.txt
|
||||||
|
@ -189,34 +189,39 @@ func TocActionFunc(c *cli.Context) error {
|
|||||||
// of https://github.com/urfave/cli/issues/1057
|
// of https://github.com/urfave/cli/issues/1057
|
||||||
func checkBinarySizeActionFunc(c *cli.Context) (err error) {
|
func checkBinarySizeActionFunc(c *cli.Context) (err error) {
|
||||||
const (
|
const (
|
||||||
sourceFilePath = "./internal/example/example.go"
|
cliSourceFilePath = "./internal/example-cli/example-cli.go"
|
||||||
builtFilePath = "./internal/example/built-example"
|
cliBuiltFilePath = "./internal/example-cli/built-example"
|
||||||
desiredMinBinarySize = 3.3
|
helloSourceFilePath = "./internal/example-hello-world/example-hello-world.go"
|
||||||
desiredMaxBinarySize = 3.8
|
helloBuiltFilePath = "./internal/example-hello-world/built-example"
|
||||||
|
desiredMinBinarySize = 2.1
|
||||||
|
desiredMaxBinarySize = 2.6
|
||||||
badNewsEmoji = "🚨"
|
badNewsEmoji = "🚨"
|
||||||
goodNewsEmoji = "✨"
|
goodNewsEmoji = "✨"
|
||||||
checksPassedEmoji = "✅"
|
checksPassedEmoji = "✅"
|
||||||
mbStringFormatter = "%.1fMB"
|
mbStringFormatter = "%.1fMB"
|
||||||
)
|
)
|
||||||
|
|
||||||
// build example binary
|
// get cli example size
|
||||||
err = runCmd("go", "build", "-o", builtFilePath, "-ldflags", "-s -w", sourceFilePath)
|
cliSize, err := getSize(cliSourceFilePath, cliBuiltFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// get file info
|
// get hello world size
|
||||||
fileInfo, err := os.Stat(builtFilePath)
|
helloSize, err := getSize(helloSourceFilePath, helloBuiltFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The CLI size diff is the number we are interested in.
|
||||||
|
// This tells us how much our CLI package contributes to the binary size.
|
||||||
|
cliSizeDiff := cliSize - helloSize
|
||||||
|
|
||||||
// get human readable size, in MB with one decimal place.
|
// get human readable size, in MB with one decimal place.
|
||||||
// example output is: 35.2MB. (note: this simply an example)
|
// example output is: 35.2MB. (note: this simply an example)
|
||||||
// that output is much easier to reason about than the `35223432`
|
// that output is much easier to reason about than the `35223432`
|
||||||
// that you would see output without the rounding
|
// that you would see output without the rounding
|
||||||
fileSize := fileInfo.Size()
|
fileSizeInMB := float64(cliSizeDiff) / float64(1000000)
|
||||||
fileSizeInMB := float64(fileSize) / float64(1000000)
|
|
||||||
roundedFileSize := math.Round(fileSizeInMB*10) / 10
|
roundedFileSize := math.Round(fileSizeInMB*10) / 10
|
||||||
roundedFileSizeString := fmt.Sprintf(mbStringFormatter, roundedFileSize)
|
roundedFileSizeString := fmt.Sprintf(mbStringFormatter, roundedFileSize)
|
||||||
|
|
||||||
@ -264,3 +269,24 @@ func checkBinarySizeActionFunc(c *cli.Context) (err error) {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSize(sourcePath string, builtPath string) (size int64, err error) {
|
||||||
|
// build example binary
|
||||||
|
err = runCmd("go", "build", "-o", builtPath, "-ldflags", "-s -w", sourcePath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("issue getting size for example binary")
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// get file info
|
||||||
|
fileInfo, err := os.Stat(builtPath)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("issue getting size for example binary")
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// size!
|
||||||
|
size = fileInfo.Size()
|
||||||
|
|
||||||
|
return size, nil
|
||||||
|
}
|
||||||
|
7
internal/example-hello-world/example-hello-world.go
Normal file
7
internal/example-hello-world/example-hello-world.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("hello world")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user