|
| 1 | +# gobake Examples |
| 2 | + |
| 3 | +## 1. Cross-Compilation for Multiple Platforms |
| 4 | + |
| 5 | +This recipe builds your application for Windows, Linux, and macOS. |
| 6 | + |
| 7 | +```go |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "fmt" |
| 12 | + "github.com/fezcode/gobake" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + bake := gobake.NewEngine() |
| 17 | + bake.LoadRecipeInfo("recipe.piml") |
| 18 | + |
| 19 | + bake.Task("release", "Build for all platforms", func(ctx *gobake.Context) error { |
| 20 | + platforms := []struct { |
| 21 | + OS string |
| 22 | + Arch string |
| 23 | + Ext string |
| 24 | + }{ |
| 25 | + {"linux", "amd64", ""}, |
| 26 | + {"windows", "amd64", ".exe"}, |
| 27 | + {"darwin", "arm64", ""}, |
| 28 | + } |
| 29 | + |
| 30 | + for _, p := range platforms { |
| 31 | + output := fmt.Sprintf("dist/%s-%s-%s%s", |
| 32 | + bake.Info.Name, p.OS, p.Arch, p.Ext) |
| 33 | + |
| 34 | + err := ctx.BakeBinary(p.OS, p.Arch, output, "-ldflags", "-s -w") |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + } |
| 39 | + return nil |
| 40 | + }) |
| 41 | + |
| 42 | + bake.Execute() |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## 2. Using External Tools |
| 47 | + |
| 48 | +This recipe installs `stringer` and uses it to generate code before building. |
| 49 | + |
| 50 | +**recipe.piml:** |
| 51 | +```piml |
| 52 | +(name) my-app |
| 53 | +(tools) |
| 54 | + > golang.org/x/tools/cmd/stringer@latest |
| 55 | +``` |
| 56 | + |
| 57 | +**Recipe.go:** |
| 58 | +```go |
| 59 | +package main |
| 60 | + |
| 61 | +import "github.com/fezcode/gobake" |
| 62 | + |
| 63 | +func main() { |
| 64 | + bake := gobake.NewEngine() |
| 65 | + bake.LoadRecipeInfo("recipe.piml") |
| 66 | + |
| 67 | + bake.Task("generate", "Generates code", func(ctx *gobake.Context) error { |
| 68 | + // Ensure tools are installed first |
| 69 | + if err := ctx.InstallTools(); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + ctx.Log("Running stringer...") |
| 73 | + return ctx.Run("go", "generate", "./...") |
| 74 | + }) |
| 75 | + |
| 76 | + bake.Task("build", "Builds app", func(ctx *gobake.Context) error { |
| 77 | + if err := ctx.Run("gobake", "generate"); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + return ctx.Run("go", "build", ".") |
| 81 | + }) |
| 82 | + |
| 83 | + bake.Execute() |
| 84 | +} |
| 85 | +``` |
0 commit comments