From b1e93d6af3cf7484aeed4bff8a3dd97ae05de09d Mon Sep 17 00:00:00 2001 From: David Gamba Date: Sat, 15 Jun 2024 20:02:28 -0600 Subject: [PATCH] bake: add init fn --- bake/build.go | 6 ++++- bake/init.go | 41 +++++++++++++++++++++++++++++++++-- bake/main.go | 1 - bake/templates/main.go.gotmpl | 3 +++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/bake/build.go b/bake/build.go index a6e22ea..dd60697 100644 --- a/bake/build.go +++ b/bake/build.go @@ -21,7 +21,11 @@ func buildBinary(dir string) error { } if modified { Logger.Printf("Found modifications on %v, rebuilding binary...\n", files) - return run.CMD("go", "build").Dir(dir).Log().Run() + err = run.CMD("go", "build").Dir(dir).Log().Run() + if err != nil { + os.Remove(filepath.Join(dir, generatedMainFilename)) + return fmt.Errorf("failed to build binary: %w", err) + } } return nil } diff --git a/bake/init.go b/bake/init.go index 08e4088..ff9aa83 100644 --- a/bake/init.go +++ b/bake/init.go @@ -3,13 +3,16 @@ package main import ( "context" "fmt" + "os" + "path/filepath" + "github.com/DavidGamba/dgtools/run" "github.com/DavidGamba/go-getoptions" ) func initRun(dir string) getoptions.CommandFn { return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { - err := initFn(dir) + err := initFn(opt, dir) if err != nil { return fmt.Errorf("failed to inspect package: %w", err) } @@ -17,6 +20,40 @@ func initRun(dir string) getoptions.CommandFn { } } -func initFn(dir string) error { +func initFn(opt *getoptions.GetOpt, dir string) error { + base := filepath.Base(dir) + if base != "bake" { + dir = filepath.Join(dir, "bake") + } + os.MkdirAll(dir, 0755) + + _ = run.CMD("go", "mod", "init", "bake").Dir(dir).Log().Run() + // if err != nil { + // return fmt.Errorf("failed to initialize go mod: %w", err) + // } + _ = run.CMD("go", "work", "init").Dir(dir).Log().Env("GOWORK=off").Run() + // if err != nil { + // return fmt.Errorf("failed to initialize go work: %w", err) + // } + _ = run.CMD("go", "work", "use", ".").Dir(dir).Log().Run() + // if err != nil { + // return fmt.Errorf("failed to configure go work: %w", err) + // } + // github.com/DavidGamba/dgtools/buildutils + // github.com/DavidGamba/dgtools/fsmodtime + // github.com/DavidGamba/dgtools/run + // github.com/DavidGamba/go-getoptions + + _ = run.CMD("go", "get", "-u", "github.com/DavidGamba/dgtools/buildutils").Dir(dir).Log().Run() + _ = run.CMD("go", "get", "-u", "github.com/DavidGamba/dgtools/fsmodtime").Dir(dir).Log().Run() + _ = run.CMD("go", "get", "-u", "github.com/DavidGamba/dgtools/run").Dir(dir).Log().Run() + _ = run.CMD("go", "get", "-u", "github.com/DavidGamba/go-getoptions").Dir(dir).Log().Run() + + ot := NewOptTree(opt) + err := GenerateMainFile(ot, dir) + if err != nil { + return fmt.Errorf("failed to generate file: %w", err) + } + return nil } diff --git a/bake/main.go b/bake/main.go index e74bb1f..56491de 100644 --- a/bake/main.go +++ b/bake/main.go @@ -44,7 +44,6 @@ func program(args []string) int { InputArgs = args[1:] if err == nil { - Logger.Printf("Found bake directory at: %s\n", dir) // TODO: Ensure we populate TM ot, err := LoadAst(ctx, opt, dir) if err != nil { diff --git a/bake/templates/main.go.gotmpl b/bake/templates/main.go.gotmpl index 5ecb697..427689b 100644 --- a/bake/templates/main.go.gotmpl +++ b/bake/templates/main.go.gotmpl @@ -4,11 +4,14 @@ import ( "errors" "fmt" "io" + "log" "os" "github.com/DavidGamba/go-getoptions" ) +var Logger = log.New(os.Stderr, "", log.LstdFlags) + func main() { os.Exit(program(os.Args)) }