urfave-cli/build.go

181 lines
2.7 KiB
Go
Raw Normal View History

2019-08-06 20:20:50 +00:00
//+build ignore
2019-08-04 14:36:44 +00:00
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"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,
},
}
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"
}
err := runCmd("node_modules/.bin/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
}