Skip to content

Commit

Permalink
chore: just run go run
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Voltaire authored and viktorvoltaire committed Jan 27, 2022
1 parent 0d1fdf7 commit 975dd7e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

.DEFAULT_GOAL := all

sagefile := .sage/tools/bin/sagefile
sagefile := .sage/bin/sagefile

$(sagefile): .sage/go.mod $(shell find .sage/.. -type f -name '*.go')
@cd .sage && go run ../main.go run
@cd .sage && go mod tidy && go run .

.PHONY: clean-sage
clean-sage:
Expand Down
19 changes: 1 addition & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ func main() {
usage := func() {
logger.Info(`Usage:
init
to initialize sage
run
to run sage`)
to initialize sage`)
os.Exit(0)
}
if len(os.Args) < 2 || len(os.Args) > 2 {
Expand All @@ -40,26 +38,11 @@ func main() {
switch os.Args[1] {
case "init":
initSage(ctx)
case "run":
runSage(ctx)
default:
usage()
}
}

func runSage(ctx context.Context) {
cmd := sg.Command(ctx, "go", "mod", "tidy")
cmd.Dir = sg.FromSageDir()
if err := cmd.Run(); err != nil {
panic(err)
}
cmd = sg.Command(ctx, "go", "run", ".")
cmd.Dir = sg.FromSageDir()
if err := cmd.Run(); err != nil {
panic(err)
}
}

func initSage(ctx context.Context) {
logger := logr.FromContextOrDiscard(ctx)
logger.Info("initializing sage...")
Expand Down
2 changes: 1 addition & 1 deletion sg/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func GenerateMakefiles(mks ...Makefile) {

// compile uses the go tool to compile the files into an executable at path.
func compile(ctx context.Context) error {
cmd := Command(ctx, "go", "build", "-o", FromToolsDir(SageFileBinary), ".")
cmd := Command(ctx, "go", "build", "-o", FromBinDir(SageFileBinary), ".")
cmd.Dir = FromSageDir()
if err := cmd.Run(); err != nil {
return fmt.Errorf("error compiling sagefiles: %w", err)
Expand Down
10 changes: 4 additions & 6 deletions sg/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func generateMakefile(g *codegen.File, pkg *doc.Package, mk Makefile, mks ...Makefile) error {
includePath, err := filepath.Rel(filepath.Dir(mk.Path), FromGitRoot(SageDir))
includePath, err := filepath.Rel(filepath.Dir(mk.Path), FromSageDir())
if err != nil {
return err
}
Expand All @@ -25,10 +25,8 @@ func generateMakefile(g *codegen.File, pkg *doc.Package, mk Makefile, mks ...Mak
return err
}
dependencies := fmt.Sprintf("%s/go.mod %s/*.go", includePath, includePath)
genCommand := fmt.Sprintf("cd %s && go run go.einride.tech/sage run", includePath)
if strings.TrimSpace(b.String()) == "go.einride.tech/sage" {
dependencies = fmt.Sprintf("%s/go.mod $(shell find %s/.. -type f -name '*.go')", includePath, includePath)
genCommand = fmt.Sprintf("cd %s && go run ../main.go run", includePath)
}

g.P("# To learn more, see ", includePath, "/sagefile.go and https://github.com/einride/sage.")
Expand All @@ -37,10 +35,10 @@ func generateMakefile(g *codegen.File, pkg *doc.Package, mk Makefile, mks ...Mak
g.P(".DEFAULT_GOAL := ", toMakeTarget(mk.defaultTargetName()))
g.P()
}
g.P("sagefile := ", filepath.Join(includePath, ToolsDir, SageFileBinary))
g.P("sagefile := ", filepath.Join(includePath, BinDir, SageFileBinary))
g.P()
g.P("$(sagefile): ", dependencies)
g.P("\t@", genCommand)
g.P("\t@cd ", includePath, " && go mod tidy && go run .")
g.P()
g.P(".PHONY: clean-sage")
g.P("clean-sage:")
Expand Down Expand Up @@ -78,7 +76,7 @@ func generateMakefile(g *codegen.File, pkg *doc.Package, mk Makefile, mks ...Mak
g.P()
g.P(".PHONY: ", toMakeTarget(i.namespaceName()))
g.P(toMakeTarget(i.namespaceName()), ":")
g.P("\tmake -C ", mkPath, " -f ", filepath.Base(i.Path))
g.P("\t$(MAKE) -C ", mkPath, " -f ", filepath.Base(i.Path))
g.P()
}
}
Expand Down
2 changes: 1 addition & 1 deletion sg/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
SageDir = ".sage"
ToolsDir = "tools"
BinDir = "bin"
SageFileBinary = "bin/sagefile"
SageFileBinary = "sagefile"
)

func FromWorkDir(pathElems ...string) string {
Expand Down

0 comments on commit 975dd7e

Please sign in to comment.