-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.txt
More file actions
119 lines (93 loc) · 2.61 KB
/
examples.txt
File metadata and controls
119 lines (93 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# gobake Examples
## 1. Cross-Compilation for Multiple Platforms
This recipe builds your application for Windows, Linux, and macOS.
```go
//go:build gobake
package bake_recipe
import (
"fmt"
"github.com/fezcode/gobake"
)
func Run(bake *gobake.Engine) error {
if err := bake.LoadRecipeInfo("recipe.piml"); err != nil {
return err
}
bake.Task("release", "Build for all platforms", func(ctx *gobake.Context) error {
platforms := []struct {
OS string
Arch string
Ext string
}{
{"linux", "amd64", ""},
{"windows", "amd64", ".exe"},
{"darwin", "arm64", ""},
}
for _, p := range platforms {
output := fmt.Sprintf("dist/%s-%s-%s%s",
bake.Info.Name, p.OS, p.Arch, p.Ext)
err := ctx.BakeBinary(p.OS, p.Arch, output, "-ldflags", "-s -w")
if err != nil {
return err
}
}
return nil
})
return nil
}
```
## 2. Using External Tools
This recipe installs `stringer` and uses it to generate code before building.
**recipe.piml:**
```piml
(name) my-app
(tools)
> golang.org/x/tools/cmd/stringer@latest
```
**Recipe.go:**
```go
//go:build gobake
package bake_recipe
import (
"fmt"
"github.com/fezcode/gobake"
)
func Run(bake *gobake.Engine) error {
if err := bake.LoadRecipeInfo("recipe.piml"); err != nil {
return fmt.Errorf("error loading recipe.piml: %v", err)
}
bake.Task("generate", "Generates code", func(ctx *gobake.Context) error {
// Ensure tools are installed first
if err := ctx.InstallTools(); err != nil {
return err
}
ctx.Log("Running stringer...")
return ctx.Run("go", "generate", "./...")
})
bake.TaskWithDeps("build", "Builds app", []string{"generate"}, func(ctx *gobake.Context) error {
return ctx.Run("go", "build", ".")
})
return nil
}
```
## 3. Injecting Version with ldflags
Inject project version from `recipe.piml` into your binary.
**Recipe.go:**
```go
//go:build gobake
package bake_recipe
import (
"fmt"
"github.com/fezcode/gobake"
)
func Run(bake *gobake.Engine) error {
if err := bake.LoadRecipeInfo("recipe.piml"); err != nil {
return err
}
bake.Task("build", "Build with version", func(ctx *gobake.Context) error {
ldflags := fmt.Sprintf("-X main.Version=%s", bake.Info.Version)
ctx.Log("Building version %s...", bake.Info.Version)
return ctx.Run("go", "build", "-ldflags", ldflags, "-o", "bin/app")
})
return nil
}
```