-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmagefile.go
174 lines (150 loc) · 3.55 KB
/
magefile.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//go:build mage
// +build mage
package main
import (
"bufio"
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"sync"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
)
// Default target to run when none is specified
// If not set, running mage will list available targets
// var Default = Build
// A build step that requires additional params, or platform specific steps for example
func Build() error {
mg.Deps(InstallDeps)
fmt.Println("Building...")
cmd := exec.Command("go", "build", "-o", "zengo", ".")
return cmd.Run()
}
func asyncCommand(ctx context.Context, stepName string, name string, args ...string) error {
cmd := exec.CommandContext(ctx, name, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("stderr pipe: %w", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("stdout pipe: %w", err)
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("starting command: %w", err)
}
go func() {
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
line := scanner.Text()
fmt.Fprintf(os.Stderr, "%s: %s\n", stepName, line)
}
}()
go func() {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
line := scanner.Text()
fmt.Fprintf(os.Stdout, "%s: %s\n", stepName, line)
}
}()
return cmd.Wait()
}
func LiveServer(ctx context.Context) error {
fmt.Println("Starting air")
return asyncCommand(ctx, "server", "go", "run", "github.com/cosmtrek/[email protected]",
"--build.exclude_dir", "node_modules",
"--build.include_ext", "go",
"--build.stop_on_error", "false",
"--misc.clean_on_exit", "true")
}
func LiveTempl(ctx context.Context) error {
fmt.Println("Starting templ")
return asyncCommand(
ctx,
"templ",
"templ",
"generate",
"--watch",
"--proxy=http://localhost:3000",
"--open-browser=false",
"-v",
)
}
func LiveTailwind(ctx context.Context) error {
fmt.Println("Starting Tailwind")
return asyncCommand(ctx, "tailwindcss", "npx", "tailwindcss", "-i",
"./static/css/input.css",
"-o",
"./static/css/style.css",
"--watch=always",
)
}
func LiveSyncAssets(ctx context.Context) error {
return asyncCommand(ctx, "sync-assets", "go", "run", "github.com/cosmtrek/[email protected]",
"--build.cmd", "templ generate --notify-proxy",
"--build.bin", "true",
"--build.delay", "100",
"--build.exclude_dir", "",
"--build.include_dir", "static",
"--build.include_ext", "js,css",
)
}
func Dev() error {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
errCh := make(chan error)
wg := sync.WaitGroup{}
wg.Add(4)
go func() {
defer wg.Done()
if err := LiveTailwind(ctx); err != nil {
<-errCh
}
}()
go func() {
defer wg.Done()
if err := LiveTempl(ctx); err != nil {
<-errCh
}
}()
go func() {
defer wg.Done()
if err := LiveServer(ctx); err != nil {
<-errCh
}
}()
go func() {
defer wg.Done()
if err := LiveSyncAssets(ctx); err != nil {
<-errCh
}
}()
select {
case <-ctx.Done():
wg.Wait()
case err := <-errCh:
fmt.Println(err)
cancel()
wg.Wait()
return err
}
return nil
}
// A custom install step if you need your bin someplace other than go/bin
func Install() error {
mg.Deps(Build)
fmt.Println("Installing...")
return os.Rename("./MyApp", "/usr/bin/MyApp")
}
// Manage your deps, or running package managers.
func InstallDeps() error {
fmt.Println("Installing Deps...")
cmd := exec.Command("go", "get", "github.com/stretchr/piglatin")
return cmd.Run()
}
// Clean up after yourself
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll("MyApp")
}