Skip to content

Commit

Permalink
bake: add init fn
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 16, 2024
1 parent 492214b commit 9bca868
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
6 changes: 5 additions & 1 deletion bake/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
47 changes: 45 additions & 2 deletions bake/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,63 @@ 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)
}
return nil
}
}

func initFn(dir string) error {
func initFn(opt *getoptions.GetOpt, dir string) error {
Logger.Printf("Initializing bake project in %s\n", dir)
abs, err := filepath.Abs(dir)
if err != nil {
return fmt.Errorf("failed to get absolute path: %w", err)
}
base := filepath.Base(abs)
Logger.Printf("Base: %s\n", base)
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
}
1 change: 0 additions & 1 deletion bake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions bake/templates/main.go.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit 9bca868

Please sign in to comment.