Skip to content

Commit

Permalink
feat: remove go dependency for every build step
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkema committed Feb 22, 2022
1 parent 0fb1dbd commit 8bcb027
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ $(sagefile):
@cd .sage && go mod tidy && go run .

.PHONY: sage
sage: $(shell git clean -fxq $(sagefile))
sage: $(sagefile)

.PHONY: update-sage
Expand Down
3 changes: 2 additions & 1 deletion sg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// GenerateMakefiles defines which Makefiles should be generated.
func GenerateMakefiles(mks ...Makefile) {
ctx := WithLogger(context.Background(), NewLogger("sage"))
Logger(ctx).Println("building binary and generating Makefiles...")
if len(mks) == 0 {
panic("no makefiles to generate, see https://github.com/einride/sage#readme for more info")
}
Expand Down Expand Up @@ -67,7 +68,7 @@ func GenerateMakefiles(mks ...Makefile) {
mk := codegen.NewMakefile(codegen.FileConfig{
GeneratedBy: "go.einride.tech/sage",
})
if err := generateMakefile(mk, pkg, v, mks...); err != nil {
if err := generateMakefile(ctx, mk, pkg, v, mks...); err != nil {
panic(err)
}
if err := os.WriteFile(v.Path, mk.RawContent(), 0o600); err != nil {
Expand Down
13 changes: 10 additions & 3 deletions sg/makefile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sg

import (
"context"
"fmt"
"go/ast"
"go/doc"
Expand Down Expand Up @@ -43,7 +44,7 @@ func (m Makefile) defaultTargetName() string {
return result
}

func generateMakefile(g *codegen.File, pkg *doc.Package, mk Makefile, mks ...Makefile) error {
func generateMakefile(ctx context.Context, g *codegen.File, pkg *doc.Package, mk Makefile, mks ...Makefile) error {
includePath, err := filepath.Rel(filepath.Dir(mk.Path), FromSageDir())
if err != nil {
return err
Expand All @@ -56,11 +57,17 @@ func generateMakefile(g *codegen.File, pkg *doc.Package, mk Makefile, mks ...Mak
g.P()
g.P("sagefile := ", filepath.Join(includePath, binDir, sageFileBinary))
g.P()
g.P(".PHONY: $(sagefile)")
g.P("$(sagefile):")

dependencies := fmt.Sprintf(" %s/go.mod %s/*.go", includePath, includePath)
if strings.TrimSpace(Output(Command(ctx, "go", "list", "-m"))) == "go.einride.tech/sage" {
g.P(".PHONY: $(sagefile)")
dependencies = ""
}
g.P("$(sagefile):", dependencies)
g.P("\t@cd ", includePath, " && go mod tidy && go run .")
g.P()
g.P(".PHONY: sage")
g.P("sage: $(shell git clean -fxq $(sagefile))")
g.P("sage: $(sagefile)")
g.P()
g.P(".PHONY: update-sage")
Expand Down

0 comments on commit 8bcb027

Please sign in to comment.