diff --git a/bake/examples/website/bake/go.mod b/bake/examples/website/bake/go.mod index cfd2fc0..2e1ca1c 100644 --- a/bake/examples/website/bake/go.mod +++ b/bake/examples/website/bake/go.mod @@ -1,10 +1,10 @@ module bake -go 1.20 +go 1.22.3 require ( - github.com/DavidGamba/dgtools/buildutils v0.4.0 - github.com/DavidGamba/dgtools/fsmodtime v0.2.0 - github.com/DavidGamba/dgtools/run v0.9.0 - github.com/DavidGamba/go-getoptions v0.30.0 + github.com/DavidGamba/dgtools/buildutils v0.4.0 // indirect + github.com/DavidGamba/dgtools/fsmodtime v0.2.0 // indirect + github.com/DavidGamba/dgtools/run v0.9.0 // indirect + github.com/DavidGamba/go-getoptions v0.30.0 // indirect ) diff --git a/bake/examples/website/bake/main.go b/bake/examples/website/bake/main.go index 1a5ea91..b00d14e 100644 --- a/bake/examples/website/bake/main.go +++ b/bake/examples/website/bake/main.go @@ -27,70 +27,68 @@ func Build(opt *getoptions.GetOpt) getoptions.CommandFn { } } -// // build:go - Builds go project -// func Go(opt *getoptions.GetOpt) getoptions.CommandFn { -// return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { -// Logger.Println("Running build:go") -// root, err := buildutils.GitRepoRoot() -// if err != nil { -// return fmt.Errorf("failed to get repo root: %w", err) -// } -// err = os.Chdir(filepath.Join(root, "bake/examples/website")) -// if err != nil { -// return fmt.Errorf("failed to chdir: %w", err) -// } -// -// files, modified, err := fsmodtime.Target(os.DirFS("."), []string{"website"}, []string{"go.mod", "go.sum", "*.go"}) -// if err != nil { -// return fmt.Errorf("failed to detect changes: %w", err) -// } -// if !modified { -// return nil -// } -// Logger.Printf("Modified files: %v\n", files) -// -// err = run.CMD("go", "build").Log().Run() -// if err != nil { -// return fmt.Errorf("failed to build go project: %w", err) -// } -// -// return nil -// } -// } - -// // build:diagram - Builds diagram -// -// func Dot(opt *getoptions.GetOpt) getoptions.CommandFn { -// return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { -// Logger.Println("Running build:diagram") -// root, err := buildutils.GitRepoRoot() -// if err != nil { -// return fmt.Errorf("failed to get repo root: %w", err) -// } -// err = os.Chdir(filepath.Join(root, "bake/examples/website")) -// if err != nil { -// return fmt.Errorf("failed to chdir: %w", err) -// } -// -// files, modified, err := fsmodtime.Target(os.DirFS("."), []string{"public/diagram.png"}, []string{"diagram.dot"}) -// if err != nil { -// return fmt.Errorf("failed to detect changes: %w", err) -// } -// if !modified { -// return nil -// } -// Logger.Printf("Modified files: %v\n", files) -// -// _ = os.MkdirAll("public", 0755) -// err = run.CMD("dot", "-Tpng", "-opublic/diagram.png", "diagram.dot").Log().Run() -// if err != nil { -// return fmt.Errorf("failed to build diagram: %w", err) -// } -// -// return nil -// } -// } -// +// build:gobuild - Builds go project +func Go(opt *getoptions.GetOpt) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running build:go") + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + files, modified, err := fsmodtime.Target(os.DirFS("."), []string{"website"}, []string{"go.mod", "go.sum", "*.go"}) + if err != nil { + return fmt.Errorf("failed to detect changes: %w", err) + } + if !modified { + return nil + } + Logger.Printf("Modified files: %v\n", files) + + err = run.CMD("go", "build").Log().Run() + if err != nil { + return fmt.Errorf("failed to build go project: %w", err) + } + + return nil + } +} + +// build:diagram - Builds diagram +func Dot(opt *getoptions.GetOpt) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running build:diagram") + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + files, modified, err := fsmodtime.Target(os.DirFS("."), []string{"public/diagram.png"}, []string{"diagram.dot"}) + if err != nil { + return fmt.Errorf("failed to detect changes: %w", err) + } + if !modified { + return nil + } + Logger.Printf("Modified files: %v\n", files) + + _ = os.MkdirAll("public", 0755) + err = run.CMD("dot", "-Tpng", "-opublic/diagram.png", "diagram.dot").Log().Run() + if err != nil { + return fmt.Errorf("failed to build diagram: %w", err) + } + + return nil + } +} // build:index - Builds index page // NOTE: Run clean before changing the language since no changes will be detected. @@ -137,82 +135,81 @@ func Asciidoc(opt *getoptions.GetOpt) getoptions.CommandFn { } } -// -// // build:all - Builds website -// func All(opt *getoptions.GetOpt) getoptions.CommandFn { -// opt.String("lang", "en", opt.ValidValues("en", "es")) -// return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { -// Logger.Println("Running build:diagram") -// -// root, err := buildutils.GitRepoRoot() -// if err != nil { -// return fmt.Errorf("failed to get repo root: %w", err) -// } -// err = os.Chdir(filepath.Join(root, "bake/examples/website")) -// if err != nil { -// return fmt.Errorf("failed to chdir: %w", err) -// } -// -// g := dag.NewGraph("website") -// g.TaskDependensOn(TM.Get("build:index"), TM.Get("build:diagram")) -// g.AddTask(TM.Get("build:go")) -// -// err = g.Validate(TM) -// if err != nil { -// return fmt.Errorf("validation: %w", err) -// } -// err = g.Run(ctx, opt, args) -// if err != nil { -// return fmt.Errorf("dag err: %w", err) -// } -// -// return nil -// } -// } -// -// // serve - Serves website on :8080 -// func Serve(opt *getoptions.GetOpt) getoptions.CommandFn { -// return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { -// Logger.Println("Running serve") -// root, err := buildutils.GitRepoRoot() -// if err != nil { -// return fmt.Errorf("failed to get repo root: %w", err) -// } -// err = os.Chdir(filepath.Join(root, "bake/examples/website")) -// if err != nil { -// return fmt.Errorf("failed to chdir: %w", err) -// } -// -// err = run.CMD("./website").Ctx(ctx).Log().Run() -// if err != nil { -// return fmt.Errorf("failed to serve: %w", err) -// } -// -// return nil -// } -// } -// -// // clean - Clean build artifacts -// func Clean(opt *getoptions.GetOpt) getoptions.CommandFn { -// return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { -// Logger.Println("Running clean") -// root, err := buildutils.GitRepoRoot() -// if err != nil { -// return fmt.Errorf("failed to get repo root: %w", err) -// } -// err = os.Chdir(filepath.Join(root, "bake/examples/website")) -// if err != nil { -// return fmt.Errorf("failed to chdir: %w", err) -// } -// -// _ = os.RemoveAll("public") -// _ = os.Remove("website") -// -// return nil -// } -// } -// -// // notused - Not used because it doesn't return a getoptions.CommandFn -// func NotUsed() error { -// return nil -// } +// build:all - Builds website +func All(opt *getoptions.GetOpt) getoptions.CommandFn { + opt.String("lang", "en", opt.ValidValues("en", "es")) + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running build:all") + + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + g := dag.NewGraph("website") + g.TaskDependensOn(TM.Get("build:index"), TM.Get("build:diagram")) + g.AddTask(TM.Get("build:gobuild")) + + err = g.Validate(TM) + if err != nil { + return fmt.Errorf("validation: %w", err) + } + err = g.Run(ctx, opt, args) + if err != nil { + return fmt.Errorf("dag err: %w", err) + } + + return nil + } +} + +// serve - Serves website on :8080 +func Serve(opt *getoptions.GetOpt) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running serve") + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + err = run.CMD("./website").Ctx(ctx).Log().Run() + if err != nil { + return fmt.Errorf("failed to serve: %w", err) + } + + return nil + } +} + +// clean - Clean build artifacts +func Clean(opt *getoptions.GetOpt) getoptions.CommandFn { + return func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + Logger.Println("Running clean") + root, err := buildutils.GitRepoRoot() + if err != nil { + return fmt.Errorf("failed to get repo root: %w", err) + } + err = os.Chdir(filepath.Join(root, "bake/examples/website")) + if err != nil { + return fmt.Errorf("failed to chdir: %w", err) + } + + _ = os.RemoveAll("public") + _ = os.Remove("website") + + return nil + } +} + +// notused - Not used because it doesn't return a getoptions.CommandFn +func NotUsed() error { + return nil +}