urfave-cli/internal/build/build.go

293 lines
7.2 KiB
Go
Raw Normal View History

2020-02-29 07:20:46 +00:00
// local build script file, similar to a makefile or collection of bash scripts in other projects
2019-08-04 14:36:44 +00:00
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
2020-02-29 07:51:42 +00:00
"math"
2019-08-04 14:36:44 +00:00
"os"
"os/exec"
"strings"
2019-08-23 04:42:07 +00:00
2019-09-15 09:22:46 +00:00
"github.com/urfave/cli/v2"
2019-08-04 14:36:44 +00:00
)
2019-08-06 07:03:33 +00:00
var packages = []string{"cli", "altsrc"}
2019-08-04 14:36:44 +00:00
func main() {
app := cli.NewApp()
app.Name = "builder"
app.Usage = "Generates a new urfave/cli build!"
app.Commands = cli.Commands{
2019-09-15 09:22:46 +00:00
{
2019-08-04 14:36:44 +00:00
Name: "vet",
Action: VetActionFunc,
},
2019-09-15 09:22:46 +00:00
{
2019-08-04 14:36:44 +00:00
Name: "test",
Action: TestActionFunc,
},
2019-09-15 09:22:46 +00:00
{
2019-08-04 14:36:44 +00:00
Name: "gfmrun",
Action: GfmrunActionFunc,
},
2019-09-15 09:22:46 +00:00
{
2019-08-04 14:36:44 +00:00
Name: "toc",
Action: TocActionFunc,
},
2020-02-29 07:19:43 +00:00
{
Name: "check-binary-size",
Action: checkBinarySizeActionFunc,
},
2019-08-04 14:36:44 +00:00
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
2019-08-24 10:34:03 +00:00
func runCmd(arg string, args ...string) error {
cmd := exec.Command(arg, args...)
2019-08-23 04:42:07 +00:00
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
2019-08-04 14:36:44 +00:00
}
2019-08-24 03:09:08 +00:00
func VetActionFunc(_ *cli.Context) error {
return runCmd("go", "vet")
}
2019-08-04 14:36:44 +00:00
func TestActionFunc(c *cli.Context) error {
for _, pkg := range packages {
var packageName string
if pkg == "cli" {
2019-09-15 09:22:46 +00:00
packageName = "github.com/urfave/cli/v2"
2019-08-04 14:36:44 +00:00
} else {
2019-09-15 09:22:46 +00:00
packageName = fmt.Sprintf("github.com/urfave/cli/v2/%s", pkg)
2019-08-04 14:36:44 +00:00
}
coverProfile := fmt.Sprintf("--coverprofile=%s.coverprofile", pkg)
2019-08-24 03:09:08 +00:00
err := runCmd("go", "test", "-v", coverProfile, packageName)
2019-08-04 14:36:44 +00:00
if err != nil {
return err
}
}
return testCleanup()
}
func testCleanup() error {
var out bytes.Buffer
for _, pkg := range packages {
file, err := os.Open(fmt.Sprintf("%s.coverprofile", pkg))
if err != nil {
return err
}
b, err := ioutil.ReadAll(file)
if err != nil {
return err
}
out.Write(b)
err = file.Close()
if err != nil {
return err
}
err = os.Remove(fmt.Sprintf("%s.coverprofile", pkg))
if err != nil {
return err
}
}
outFile, err := os.Create("coverage.txt")
if err != nil {
return err
}
_, err = out.WriteTo(outFile)
if err != nil {
return err
}
err = outFile.Close()
if err != nil {
return err
}
return nil
}
2019-09-16 04:21:52 +00:00
func GfmrunActionFunc(c *cli.Context) error {
filename := c.Args().Get(0)
if filename == "" {
filename = "README.md"
}
file, err := os.Open(filename)
2019-08-04 14:36:44 +00:00
if err != nil {
return err
}
2019-11-18 22:48:45 +00:00
defer file.Close()
2019-08-04 14:36:44 +00:00
var counter int
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "package main") {
counter++
}
}
2019-11-08 00:56:05 +00:00
err = file.Close()
if err != nil {
return err
}
2019-08-04 14:36:44 +00:00
err = scanner.Err()
if err != nil {
return err
}
2019-09-16 04:21:52 +00:00
return runCmd("gfmrun", "-c", fmt.Sprint(counter), "-s", filename)
2019-08-04 14:36:44 +00:00
}
2019-09-16 04:21:52 +00:00
func TocActionFunc(c *cli.Context) error {
filename := c.Args().Get(0)
if filename == "" {
filename = "README.md"
}
2020-01-13 05:29:32 +00:00
err := runCmd("markdown-toc", "-i", filename)
2019-08-04 14:36:44 +00:00
if err != nil {
return err
}
2019-08-24 03:09:08 +00:00
err = runCmd("git", "diff", "--exit-code")
2019-08-04 14:36:44 +00:00
if err != nil {
return err
}
return nil
}
2020-02-29 07:19:43 +00:00
2020-02-29 07:58:29 +00:00
// 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
2020-02-29 07:19:43 +00:00
func checkBinarySizeActionFunc(c *cli.Context) (err error) {
2020-02-29 07:29:27 +00:00
const (
2020-02-29 22:00:26 +00:00
cliSourceFilePath = "./internal/example-cli/example-cli.go"
cliBuiltFilePath = "./internal/example-cli/built-example"
helloSourceFilePath = "./internal/example-hello-world/example-hello-world.go"
helloBuiltFilePath = "./internal/example-hello-world/built-example"
2020-02-29 22:02:03 +00:00
desiredMinBinarySize = 2.0
2020-02-29 22:04:21 +00:00
desiredMaxBinarySize = 2.1
2020-02-29 08:25:00 +00:00
badNewsEmoji = "🚨"
goodNewsEmoji = "✨"
checksPassedEmoji = "✅"
mbStringFormatter = "%.1fMB"
2020-02-29 07:29:27 +00:00
)
2020-02-29 22:00:26 +00:00
// get cli example size
cliSize, err := getSize(cliSourceFilePath, cliBuiltFilePath)
2020-02-29 07:19:43 +00:00
if err != nil {
return err
}
2020-02-29 22:00:26 +00:00
// get hello world size
helloSize, err := getSize(helloSourceFilePath, helloBuiltFilePath)
2020-02-29 07:29:27 +00:00
if err != nil {
return err
}
2020-02-29 07:56:57 +00:00
2020-02-29 22:00:26 +00:00
// 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
2020-02-29 07:56:57 +00:00
// get human readable size, in MB with one decimal place.
2020-02-29 07:59:11 +00:00
// example output is: 35.2MB. (note: this simply an example)
2020-02-29 07:56:57 +00:00
// that output is much easier to reason about than the `35223432`
// that you would see output without the rounding
2020-02-29 22:00:26 +00:00
fileSizeInMB := float64(cliSizeDiff) / float64(1000000)
2020-02-29 08:39:47 +00:00
roundedFileSize := math.Round(fileSizeInMB*10) / 10
2020-02-29 08:25:00 +00:00
roundedFileSizeString := fmt.Sprintf(mbStringFormatter, roundedFileSize)
// check against bounds
isLessThanDesiredMin := roundedFileSize < desiredMinBinarySize
isMoreThanDesiredMax := roundedFileSize > desiredMaxBinarySize
desiredMinSizeString := fmt.Sprintf(mbStringFormatter, desiredMinBinarySize)
desiredMaxSizeString := fmt.Sprintf(mbStringFormatter, desiredMaxBinarySize)
// show guidance
fmt.Println(fmt.Sprintf("\n%s is the current binary size", roundedFileSizeString))
2020-02-29 08:33:51 +00:00
// show guidance for min size
2020-02-29 08:25:00 +00:00
if isLessThanDesiredMin {
2020-02-29 08:33:51 +00:00
fmt.Println(fmt.Sprintf(" %s %s is the target min size", goodNewsEmoji, desiredMinSizeString))
fmt.Println("") // visual spacing
fmt.Println(" The binary is smaller than the target min size, which is great news!")
2020-02-29 08:47:10 +00:00
fmt.Println(" That means that your changes are shrinking the binary size.")
2020-02-29 08:33:51 +00:00
fmt.Println(" You'll want to go into ./internal/build/build.go and decrease")
fmt.Println(" the desiredMinBinarySize, and also probably decrease the ")
fmt.Println(" desiredMaxBinarySize by the same amount. That will ensure that")
fmt.Println(" future PRs will enforce the newly shrunk binary sizes.")
fmt.Println("") // visual spacing
2020-02-29 08:25:00 +00:00
os.Exit(1)
} else {
2020-02-29 08:33:51 +00:00
fmt.Println(fmt.Sprintf(" %s %s is the target min size", checksPassedEmoji, desiredMinSizeString))
2020-02-29 08:25:00 +00:00
}
2020-02-29 08:33:51 +00:00
// show guidance for max size
2020-02-29 08:25:00 +00:00
if isMoreThanDesiredMax {
2020-02-29 08:33:51 +00:00
fmt.Println(fmt.Sprintf(" %s %s is the target max size", badNewsEmoji, desiredMaxSizeString))
fmt.Println("") // visual spacing
2020-02-29 08:47:10 +00:00
fmt.Println(" The binary is larger than the target max size.")
fmt.Println(" That means that your changes are increasing the binary size.")
fmt.Println(" The first thing you'll want to do is ask your yourself")
fmt.Println(" Is this change worth increasing the binary size?")
fmt.Println(" Larger binary sizes for this package can dissuade its use.")
fmt.Println(" If this change is worth the increase, then we can up the")
fmt.Println(" desired max binary size. To do that you'll want to go into")
fmt.Println(" ./internal/build/build.go and increase the desiredMaxBinarySize,")
fmt.Println(" and increase the desiredMinBinarySize by the same amount.")
fmt.Println("") // visual spacing
2020-02-29 08:25:00 +00:00
os.Exit(1)
} else {
2020-02-29 08:33:51 +00:00
fmt.Println(fmt.Sprintf(" %s %s is the target max size", checksPassedEmoji, desiredMaxSizeString))
2020-02-29 08:25:00 +00:00
}
2020-02-29 07:29:27 +00:00
2020-02-29 07:19:43 +00:00
return nil
}
2020-02-29 22:00:26 +00:00
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
}