Skip to content

Commit

Permalink
fix: ensure single trailing newline for raw generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
odsod committed Jan 29, 2022
1 parent 5dfd5af commit 90f6e89
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
9 changes: 5 additions & 4 deletions internal/codegen/file.go
Expand Up @@ -81,12 +81,13 @@ func (f *File) Write(p []byte) (int, error) {
return n, err // nolint: wrapcheck // false positive
}

func (f *File) Bytes() []byte {
return f.buf.Bytes()
// RawContent returns the raw content of the file.
func (f *File) RawContent() []byte {
return append(bytes.TrimSpace(f.buf.Bytes()), '\n')
}

// Content returns the formatted Go source of the file.
func (f *File) Content() (_ []byte, err error) {
// GoContent returns the formatted Go source of the file.
func (f *File) GoContent() (_ []byte, err error) {
if f.err != nil {
return nil, fmt.Errorf("content of %s: %w", f.cfg.Filename, f.err)
}
Expand Down
5 changes: 2 additions & 3 deletions sg/generate.go
Expand Up @@ -80,7 +80,7 @@ func genMakefiles(ctx context.Context, mks ...Makefile) {
if err := generateInitFile(initFile, pkg); err != nil {
panic(err)
}
initFileContent, err := initFile.Content()
initFileContent, err := initFile.GoContent()
if err != nil {
panic(err)
}
Expand All @@ -105,8 +105,7 @@ func genMakefiles(ctx context.Context, mks ...Makefile) {
if err := generateMakefile(ctx, mk, pkg, v, mks...); err != nil {
panic(err)
}
// Remove trailing whitespace with len
if err := os.WriteFile(v.Path, mk.Bytes()[:len(mk.Bytes())-1], 0o600); err != nil {
if err := os.WriteFile(v.Path, mk.RawContent(), 0o600); err != nil {
panic(err)
}
}
Expand Down
15 changes: 3 additions & 12 deletions sg/makefile.go
@@ -1,7 +1,6 @@
package sg

import (
"bytes"
"context"
"fmt"
"go/ast"
Expand All @@ -18,22 +17,16 @@ func generateMakefile(ctx context.Context, g *codegen.File, pkg *doc.Package, mk
if err != nil {
return err
}
cmd := Command(context.Background(), "go", "list", "-m")
var b bytes.Buffer
cmd.Stdout = &b
if err := cmd.Run(); err != nil {
return err
}
dependencies := fmt.Sprintf("%s/go.mod %s/*.go", includePath, includePath)
if strings.TrimSpace(b.String()) == "go.einride.tech/sage" {
if strings.TrimSpace(Output(Command(ctx, "go", "list", "-m"))) == "go.einride.tech/sage" {
dependencies = fmt.Sprintf("%s/go.mod $(shell find %s/.. -type f -name '*.go')", includePath, includePath)
}
g.P("# To learn more, see ", includePath, "/sagefile.go and https://github.com/einride/sage.")
g.P()
if len(mk.defaultTargetName()) != 0 {
g.P(".DEFAULT_GOAL := ", toMakeTarget(mk.defaultTargetName()))
g.P()
g.P(".DEFAULT_GOAL := ", toMakeTarget(mk.defaultTargetName()))
}
g.P()
g.P("sagefile := ", filepath.Join(includePath, BinDir, SageFileBinary))
g.P()
g.P("$(sagefile): ", dependencies)
Expand Down Expand Up @@ -76,10 +69,8 @@ func generateMakefile(ctx context.Context, g *codegen.File, pkg *doc.Package, mk
g.P(".PHONY: ", toMakeTarget(i.namespaceName()))
g.P(toMakeTarget(i.namespaceName()), ":")
g.P("\t$(MAKE) -C ", mkPath, " -f ", filepath.Base(i.Path))
g.P()
}
}
g.P()
return nil
}

Expand Down

0 comments on commit 90f6e89

Please sign in to comment.