Build with Magefile #104341
97
magefiles/generate.go
Normal file
97
magefiles/generate.go
Normal file
@ -0,0 +1,97 @@
|
||||
//go:build mage
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/magefile/mage/mg"
|
||||
"github.com/magefile/mage/sh"
|
||||
)
|
||||
|
||||
func Generate() {
|
||||
mg.Deps(GenerateGo, GeneratePy, GenerateJS)
|
||||
}
|
||||
|
||||
func GenerateGo(ctx context.Context) error {
|
||||
r := NewRunner(ctx)
|
||||
r.Run(mg.GoCmd(), "generate", "./pkg/api/...")
|
||||
r.Run(mg.GoCmd(), "generate", "./internal/...")
|
||||
if err := r.Wait(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// The generators always produce UNIX line-ends. This creates false file
|
||||
// modifications with Git. Convert them to DOS line-ends to avoid this.
|
||||
if runtime.GOOS == "windows" {
|
||||
unix2dosModifiedFiles(".gen.go$")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GeneratePy() {
|
||||
panic("GeneratePy not implemented")
|
||||
}
|
||||
|
||||
func GenerateJS() {
|
||||
panic("GenerateJS not implemented")
|
||||
}
|
||||
|
||||
// unix2dosModifiedFiles changes line ends in files Git considers modified that match the given pattern.
|
||||
func unix2dosModifiedFiles(pattern string) {
|
||||
// Get modified files from Git. Expected lines like:
|
||||
//
|
||||
// M pkg/api/openapi_client.gen.go
|
||||
// M pkg/api/openapi_server.gen.go
|
||||
// M pkg/api/openapi_spec.gen.go
|
||||
// M pkg/api/openapi_types.gen.go
|
||||
// ?? magefiles/generate.go
|
||||
|
||||
gitStatus, err := sh.Output("git", "status", "--porcelain")
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error running 'git status': %s", err))
|
||||
}
|
||||
|
||||
// Construct a list of modified files that match the pattern.
|
||||
patternRe := regexp.MustCompile(pattern)
|
||||
modified := []string{}
|
||||
for _, line := range strings.Split(gitStatus, "\n") {
|
||||
if line[0:3] != " M " {
|
||||
continue
|
||||
}
|
||||
if !patternRe.MatchString(line[3:]) {
|
||||
continue
|
||||
}
|
||||
modified = append(modified, line[3:])
|
||||
}
|
||||
|
||||
// Run unix2dos on all found files.
|
||||
for _, path := range modified {
|
||||
unix2dos(path)
|
||||
}
|
||||
}
|
||||
|
||||
func unix2dos(filename string) {
|
||||
if mg.Verbose() {
|
||||
fmt.Printf("unix2dos %s\n", filename)
|
||||
}
|
||||
|
||||
// TODO: rewrite to stream the data instead of reading, copying, and writing
|
||||
// everything.
|
||||
contents, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error converting UNIX to DOS line ends: %v", err))
|
||||
}
|
||||
|
||||
lines := bytes.Split(contents, []byte("\n"))
|
||||
err = os.WriteFile(filename, bytes.Join(lines, []byte("\r\n")), os.ModePerm)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error writing DOS line ends: %v", err))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user