-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
796 lines (718 loc) · 21.3 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
package main
import (
"bytes"
"context"
"crypto/tls"
_ "embed"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/romshark/templier/internal/action"
"github.com/romshark/templier/internal/broadcaster"
"github.com/romshark/templier/internal/cmdrun"
"github.com/romshark/templier/internal/config"
"github.com/romshark/templier/internal/ctxrun"
"github.com/romshark/templier/internal/debounce"
"github.com/romshark/templier/internal/fswalk"
"github.com/romshark/templier/internal/log"
"github.com/romshark/templier/internal/server"
"github.com/romshark/templier/internal/statetrack"
"github.com/romshark/templier/internal/templgofilereg"
"github.com/romshark/templier/internal/watcher"
"golang.org/x/sync/errgroup"
"github.com/fsnotify/fsnotify"
"github.com/gobwas/glob"
)
const ServerHealthPreflightWaitInterval = 100 * time.Millisecond
var (
// rerunActive indicates whether the currently running app server
// is being restarted. The appLauncher
rerunActive atomic.Bool
chRerunServer = make(chan struct{}, 1)
chRunNewServer = make(chan string, 1)
)
type customWatcher struct {
name string
cmd config.CmdStr
include []glob.Glob
exclude []glob.Glob
debounced func(func())
failOnErr bool
requires action.Type
}
func (c customWatcher) isFilePathIncluded(s string) bool {
for _, glob := range c.include {
if glob.Match(s) {
for _, glob := range c.exclude {
if glob.Match(s) {
return false
}
}
return true
}
}
return false
}
func main() {
conf := config.MustParse()
log.SetLogLevel(log.LogLevel(conf.Log.Level))
if err := checkTemplVersion(context.Background()); err != nil {
log.Fatalf("checking templ version: %v", err)
}
// Make sure required cmds are available.
if _, err := exec.LookPath("templ"); err != nil {
log.FatalCmdNotAvailable(
"templ", "https://templ.guide/quick-start/installation",
)
}
if conf.Lint {
if _, err := exec.LookPath("golangci-lint"); err != nil {
log.FatalCmdNotAvailable(
"golangci-lint",
"https://github.com/golangci/golangci-lint"+
"?tab=readme-ov-file#install-golangci-lint",
)
}
}
for _, w := range conf.CustomWatchers {
if w.Cmd == "" {
continue
}
cmd := w.Cmd.Cmd()
if _, err := exec.LookPath(cmd); err != nil {
log.FatalCustomWatcherCmdNotAvailable(cmd, string(w.Name))
}
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
// Create a temporary directory
tempDirPath, err := os.MkdirTemp("", "templier-*")
if err != nil {
log.Fatalf("creating temporary directory: %v\n", err)
}
log.Debugf("set server binaries output path: %q", tempDirPath)
defer func() {
log.Debugf("removing server binaries output directory: %s", tempDirPath)
if err := os.RemoveAll(tempDirPath); err != nil {
log.Errorf("deleting temporary directory: %v\n", err)
}
}()
// At this point, use of fatal logging is no longer allowed.
// Use panic instead to ensure deferred functions are executed.
reload := broadcaster.NewSignalBroadcaster()
st := statetrack.NewTracker(len(conf.CustomWatchers))
errgrp, ctx := errgroup.WithContext(ctx)
var wg sync.WaitGroup
wg.Add(1)
errgrp.Go(func() error {
defer wg.Done()
// Run templ in watch mode to create debug components.
// Once ctx is canceled templ will write production output and exit.
err := cmdrun.RunTemplWatch(ctx, conf.App.DirSrcRootAbsolute(), st)
if err != nil && !errors.Is(err, context.Canceled) {
err = fmt.Errorf("running 'templ generate --watch': %w", err)
log.Error(err.Error())
}
log.Debugf("'templ generate --watch' stopped")
return err
})
// Initialize custom customWatchers.
customWatchers := make([]customWatcher, len(conf.CustomWatchers))
for i, w := range conf.CustomWatchers {
debouncer, debounced := debounce.NewSync(w.Debounce)
go debouncer(ctx)
// The following globs have already been validated during config parsing.
// It's safe to assume compilation succeeds.
include := make([]glob.Glob, len(w.Include))
for i, pattern := range w.Include {
include[i] = glob.MustCompile(pattern)
}
exclude := make([]glob.Glob, len(w.Exclude))
for i, pattern := range w.Exclude {
exclude[i] = glob.MustCompile(pattern)
}
customWatchers[i] = customWatcher{
name: string(w.Name),
debounced: debounced,
cmd: w.Cmd,
failOnErr: w.FailOnError,
include: include,
exclude: exclude,
requires: action.Type(w.Requires),
}
}
wg.Add(1)
errgrp.Go(func() error {
defer wg.Done()
err := runTemplierServer(ctx, st, reload, conf)
if err != nil {
err = fmt.Errorf("running templier server: %w", err)
log.Error(err.Error())
}
log.Debugf("templier server stopped")
return err
})
wg.Add(1)
errgrp.Go(func() error {
defer wg.Done()
runAppLauncher(ctx, st, reload, conf)
log.Debugf("app launcher stopped")
return nil
})
debouncer, debounced := debounce.NewSync(conf.Debounce)
go debouncer(ctx)
// Initial build, run all custom watcher cmd's and if they succeed then lint & build
for i, watcher := range conf.CustomWatchers {
o, err := cmdrun.Sh(ctx, conf.App.DirWork, string(watcher.Cmd))
output := string(o)
if errors.Is(err, cmdrun.ErrExitCode1) {
if !watcher.FailOnError {
log.Errorf(
"custom watcher %q exited with code 1: %s",
watcher.Cmd, output,
)
continue
}
st.Set(statetrack.IndexOffsetCustomWatcher+i, string(o))
continue
} else if err != nil {
log.Errorf("running custom watcher cmd %q: %v", watcher.Cmd, err)
continue
}
st.Set(statetrack.IndexOffsetCustomWatcher+i, "")
}
// Finalize initial build
if binaryFile := lintAndBuildServer(ctx, st, conf, tempDirPath); binaryFile != "" {
// Launch only when there's no errors on initial build.
chRunNewServer <- binaryFile
}
// Collect all _templ.go files and add them to the registry
templGoFileReg := templgofilereg.New()
if currentWorkingDir, err := os.Getwd(); err != nil {
log.Errorf("getting current working directory: %v", err)
} else {
if err := fswalk.Files(currentWorkingDir, func(name string) error {
if !strings.HasSuffix(name, "_templ.go") {
return nil
}
if _, err := templGoFileReg.Compare(name); err != nil {
log.Errorf("registering generated templ Go file: %q: %v", name, err)
}
log.Debugf("registered existing generated templ Go file: %q", name)
return nil
}); err != nil {
log.Errorf("collecting existing _templ.go files: %v", err)
}
}
onChangeHandler := FileChangeHandler{
binaryOutBasePath: tempDirPath,
customWatchers: customWatchers,
stateTracker: st,
reload: reload,
debounced: debounced,
conf: conf,
templGoFileRegistry: templGoFileReg,
buildRunner: ctxrun.New(),
}
onChangeHandler.watchBasePath, err = filepath.Abs(conf.App.DirWork)
if err != nil {
panic(fmt.Errorf("determining absolute base file path: %v", err))
}
log.Debugf("set absolute base file path: %q", onChangeHandler.watchBasePath)
watcher, err := watcher.New(conf.App.DirSrcRootAbsolute(), onChangeHandler.Handle)
if err != nil {
panic(fmt.Errorf("initializing file watcher: %w", err))
}
for _, expr := range conf.App.Exclude {
if err := watcher.Ignore(expr); err != nil {
panic(fmt.Errorf("adding ignore filter to watcher (%q): %w", expr, err))
}
}
// Ignore templ temp files generated by `templ fmt`.
if err := watcher.Ignore("*.templ[0-9]*"); err != nil {
panic(fmt.Errorf(
`adding ignore templ temp files filter to watcher ("*.templ*"): %w`,
err,
))
}
if err := watcher.Add(conf.App.DirSrcRootAbsolute()); err != nil {
panic(fmt.Errorf("setting up file watcher for app.dir-src-root(%q): %w",
conf.App.DirSrcRootAbsolute(), err))
}
wg.Add(1)
errgrp.Go(func() error {
defer wg.Done()
err := watcher.Run(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
err = fmt.Errorf("running file watcher: %w", err)
log.Error(err.Error())
}
log.Debugf("file watcher stopped")
return err
})
{
templierBaseURL := url.URL{
Scheme: "http",
Host: conf.TemplierHost,
}
if conf.TLS != nil {
templierBaseURL.Scheme = "https"
}
log.TemplierStarted(templierBaseURL.String())
}
if err := errgrp.Wait(); err != nil {
log.Debugf("sub-process failure: %v", err)
}
cancel() // Ask all sub-processes to exit gracefully.
log.Debugf("waiting for remaining sub-processes to shut down")
wg.Wait() // Wait for all sub-processes to exit.
}
func runTemplierServer(
ctx context.Context,
st *statetrack.Tracker,
reload *broadcaster.SignalBroadcaster,
conf *config.Config,
) error {
httpSrv := http.Server{
Addr: conf.TemplierHost,
Handler: server.New(
&http.Client{
Timeout: conf.ProxyTimeout,
},
st,
reload,
conf,
),
}
var errgrp errgroup.Group
errgrp.Go(func() error {
var err error
if conf.TLS != nil {
err = httpSrv.ListenAndServeTLS(conf.TLS.Cert, conf.TLS.Key)
} else {
err = httpSrv.ListenAndServe()
}
if errors.Is(err, http.ErrServerClosed) {
return nil
}
return err
})
errgrp.Go(func() error {
<-ctx.Done() // Wait for shutdown signal.
return httpSrv.Shutdown(ctx)
})
return errgrp.Wait()
}
func runAppLauncher(
ctx context.Context,
stateTracker *statetrack.Tracker,
reload *broadcaster.SignalBroadcaster,
conf *config.Config,
) {
var latestSrvCmd *exec.Cmd
var latestBinaryPath string
var waitExit sync.WaitGroup
stopServer := func() (stopped bool) {
if latestSrvCmd == nil || latestSrvCmd.Process == nil {
return false
}
log.Debugf("stopping app server with pid %d", latestSrvCmd.Process.Pid)
if err := latestSrvCmd.Process.Signal(os.Interrupt); err != nil {
log.Errorf("sending interrupt signal to app server: %v", err)
return false
}
return true
}
healthCheckClient := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
rerun := func(ctx context.Context) {
rerunActive.Store(true)
defer rerunActive.Store(false)
start := time.Now()
stopped := stopServer()
waitExit.Wait()
if stopped {
log.Durf("stopped server", time.Since(start))
}
if stateTracker.ErrIndex() != -1 {
// There's some error, we can't rerun now.
return
}
if ctx.Err() != nil {
return // Canceled.
}
c := exec.Command(latestBinaryPath)
if conf.App.Flags != nil {
c.Args = append(c.Args, conf.App.Flags...)
}
// Enable templ's development mode to read from .txt
// for faster reloads without recompilation.
c.Env = append(os.Environ(), "TEMPL_DEV_MODE=true")
var bufOutputCombined bytes.Buffer
c.Stdout = io.MultiWriter(os.Stdout, &bufOutputCombined)
c.Stderr = io.MultiWriter(os.Stderr, &bufOutputCombined)
latestSrvCmd = c
log.TemplierRestartingServer(conf.App.DirCmd)
log.Debugf("running app server command: %s", latestSrvCmd.String())
if err := c.Start(); err != nil {
log.Errorf("running %s: %v", conf.App.DirCmd, err)
}
if c.Process != nil {
log.Debugf("app server running (pid: %d)", c.Process.Pid)
}
var exitCode atomic.Int32
exitCode.Store(-1)
waitExit.Add(1)
go func() {
defer waitExit.Done()
err := c.Wait()
if err == nil {
return
}
if exitError, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
exitCode.Store(int32(exitError.ExitCode()))
return
}
// Some other error occurred
log.Errorf("health check: waiting for process: %v", err)
}()
const maxRetries = 100
for retry := 0; ; retry++ {
if ctx.Err() != nil {
// App launcher stopped or rerun canceled.
log.Debugf("rerun canceled")
return
}
if retry > maxRetries {
log.Errorf("waiting for server: %d retries failed", maxRetries)
return
}
// Wait for the server to be ready
log.Debugf("health check (%d/%d): %s %q",
retry, maxRetries, http.MethodOptions, conf.App.Host.URL.String())
r, err := http.NewRequest(
http.MethodOptions, conf.App.Host.URL.String(), http.NoBody,
)
r = r.WithContext(ctx)
if err != nil {
log.Errorf("initializing preflight request: %v", err)
continue
}
resp, err := healthCheckClient.Do(r)
if err == nil {
_ = resp.Body.Close()
log.Debugf("health check: OK, " +
"app server is ready to receive requests")
break // Server is ready to receive requests
}
if errors.Is(err, context.Canceled) {
return
}
log.Debugf("health check: err: %v", err)
if code := exitCode.Load(); code != -1 && code != 0 {
log.Errorf("health check: app server exited with exit code %d", code)
stateTracker.Set(statetrack.IndexExit, bufOutputCombined.String())
return
}
log.Debugf("health check: wait: %s", ServerHealthPreflightWaitInterval)
time.Sleep(ServerHealthPreflightWaitInterval)
}
if conf.Log.ClearOn == config.LogClearOnRestart {
log.ClearLogs()
}
if stopped {
log.Durf("restarted server", time.Since(start))
} else {
log.Durf("started server", time.Since(start))
}
// Notify all clients to reload the page
reload.BroadcastNonblock()
}
runner := ctxrun.New()
for {
select {
case <-chRerunServer:
runner.Go(ctx, func(ctx context.Context) {
rerun(ctx)
})
case newBinaryPath := <-chRunNewServer:
runner.Go(ctx, func(ctx context.Context) {
if latestBinaryPath != "" {
log.Debugf("remove executable: %s", latestBinaryPath)
if err := os.Remove(latestBinaryPath); err != nil {
log.Errorf("removing binary file %q: %v", latestBinaryPath, err)
}
}
latestBinaryPath = newBinaryPath
rerun(ctx)
})
case <-ctx.Done():
stopServer()
return
}
}
}
type FileChangeHandler struct {
binaryOutBasePath string
watchBasePath string
customWatchers []customWatcher
stateTracker *statetrack.Tracker
reload *broadcaster.SignalBroadcaster
debounced func(fn func())
conf *config.Config
templGoFileRegistry *templgofilereg.Comparer
buildRunner *ctxrun.Runner
}
func (h *FileChangeHandler) Handle(ctx context.Context, e fsnotify.Event) error {
switch e.Op {
case fsnotify.Chmod:
log.Debugf("ignoring file operation (%s): %q", e.Op.String(), e.Name)
return nil // Ignore chmod events.
case fsnotify.Remove:
// No need to check for _templ.go suffix, Remove is a no-op for other files.
h.templGoFileRegistry.Remove(e.Name)
}
if h.conf.Log.ClearOn == config.LogClearOnFileChange {
log.ClearLogs()
}
relativeFileName, err := filepath.Rel(h.watchBasePath, e.Name)
if err != nil {
panic(fmt.Errorf(
"determining relative path for %q with base path %q",
e.Name, h.conf.App.DirWork,
))
}
log.Debugf("handling file operation (%s): %q", e.Op.String(), relativeFileName)
if h.conf.Format {
if strings.HasSuffix(e.Name, ".templ") {
log.Debugf("format templ file %s", e.Name)
err := cmdrun.RunTemplFmt(context.Background(), h.conf.App.DirWork, e.Name)
if err != nil {
log.Errorf("templ formatting error: %v", err)
}
}
}
var wg sync.WaitGroup
var customWatcherTriggered atomic.Bool
var act action.SyncStatus
if len(h.customWatchers) > 0 {
// Each custom watcher will be executed in the goroutine of its debouncer.
wg.Add(len(h.customWatchers))
for i, w := range h.customWatchers {
if !w.isFilePathIncluded(relativeFileName) {
// File doesn't match any glob
wg.Done()
continue
}
customWatcherTriggered.Store(true)
index := i
w.debounced(func() { // This runs in a separate goroutine.
defer wg.Done()
start := time.Now()
defer func() { log.Durf(string(w.name), time.Since(start)) }()
if w.cmd != "" {
o, err := cmdrun.Sh(ctx, h.conf.App.DirWork, string(w.cmd))
output := string(o)
if errors.Is(err, cmdrun.ErrExitCode1) {
if w.failOnErr {
h.stateTracker.Set(
statetrack.IndexOffsetCustomWatcher+index, output,
)
h.reload.BroadcastNonblock()
} else {
// Log the error when fail-on-error is disabled.
log.Errorf(
"custom watcher %q exited with code 1: %s",
w.cmd, output,
)
}
return
} else if err != nil {
// The reason this cmd failed was not just exit code 1.
if w.failOnErr {
h.stateTracker.Set(
statetrack.IndexOffsetCustomWatcher+index, output,
)
}
log.Errorf(
"executing custom watcher %q: %s",
w.cmd, output,
)
}
}
h.stateTracker.Set(statetrack.IndexOffsetCustomWatcher+index, "")
act.Require(w.requires)
})
}
}
wg.Wait() // Wait for all custom watcher to finish before attempting reload.
if customWatcherTriggered.Load() {
// Custom watcher was triggered, apply custom action.
switch act.Load() {
case action.ActionNone:
// Custom watchers require no further action to be taken.
log.Debugf("custom watchers: no action")
return nil
case action.ActionReload:
// Custom watchers require just a reload of all browser tabs.
log.Debugf("custom watchers: notify reload")
h.reload.BroadcastNonblock()
return nil
case action.ActionRestart:
// Custom watchers require just a server restart.
log.Debugf("custom watchers: rerun app server")
chRerunServer <- struct{}{}
return nil
default:
log.Debugf("custom watchers: rebuild app server")
}
} else {
log.Debugf("custom watchers: no watcher triggered")
if strings.HasSuffix(e.Name, "_templ.txt") {
log.Debugf("ignore change in generated templ txt: %s", e.Name)
return nil
}
if strings.HasSuffix(e.Name, ".templ") {
log.Debugf("ignore templ file change: %s", e.Name)
return nil
}
// No custom watcher triggered, follow default pipeline.
if h.stateTracker.Get(statetrack.IndexTempl) != "" {
// A templ template is broken, don't continue.
return nil
}
if strings.HasSuffix(e.Name, "_templ.go") {
if recompile, err := h.templGoFileRegistry.Compare(e.Name); err != nil {
log.Errorf("checking generated templ go file: %v", err)
return nil
} else if !recompile {
log.Debugf("_templ.go change doesn't require recompilation")
// Don't reload browser tabs while the app server is restarting.
if !rerunActive.Load() {
// Reload browser tabs when a _templ.go file has changed without
// changing its code structure (load from _templ.txt is possible).
h.reload.BroadcastNonblock()
}
return nil
} else {
log.Debugf("change in _templ.go requires recompilation")
}
}
}
h.debounced(func() {
log.TemplierFileChange(e)
h.buildRunner.Go(context.Background(), func(ctx context.Context) {
newBinaryPath := lintAndBuildServer(
ctx, h.stateTracker, h.conf, h.binaryOutBasePath,
)
if h.stateTracker.ErrIndex() != -1 {
h.reload.BroadcastNonblock()
// Don't restart the server if there was any error.
return
}
chRunNewServer <- newBinaryPath
})
})
return nil
}
func runGolangCILint(ctx context.Context, st *statetrack.Tracker, conf *config.Config) {
startLinting := time.Now()
buf, err := cmdrun.Run(
ctx, conf.App.DirWork, nil,
"golangci-lint", "run", conf.App.DirSrcRoot+"/...",
)
if errors.Is(ctx.Err(), context.Canceled) {
log.Debugf("golangci-lint cmd aborted")
return // No need to check errors and continue.
}
if errors.Is(err, cmdrun.ErrExitCode1) {
bufStr := string(buf)
log.Error(bufStr)
st.Set(statetrack.IndexGolangciLint, bufStr)
return
} else if err != nil {
log.Errorf("failed running golangci-lint: %v", err)
return
}
st.Set(statetrack.IndexGolangciLint, "")
log.Durf("linted", time.Since(startLinting))
}
func buildServer(
ctx context.Context, st *statetrack.Tracker, conf *config.Config, outBasePath string,
) (newBinaryPath string) {
startBuilding := time.Now()
binaryPath := makeUniqueServerOutPath(outBasePath)
args := append([]string{"build"}, conf.CompilerFlags()...)
args = append(args, "-o", binaryPath, conf.App.DirCmd)
buf, err := cmdrun.Run(ctx, conf.App.DirWork, conf.CompilerEnv(), "go", args...)
if errors.Is(ctx.Err(), context.Canceled) {
log.Debugf("go build cmd aborted")
return // No need to check errors and continue.
}
if err != nil {
bufStr := string(buf)
log.Error(bufStr)
st.Set(statetrack.IndexGo, bufStr)
return
}
// Reset the process exit and go compiler errors
st.Set(statetrack.IndexGo, "")
st.Set(statetrack.IndexExit, "")
log.Durf("compiled cmd/server", time.Since(startBuilding))
return binaryPath
}
func makeUniqueServerOutPath(basePath string) string {
tm := time.Now()
return filepath.Join(basePath, "server_"+strconv.FormatInt(tm.UnixNano(), 16))
}
func lintAndBuildServer(
ctx context.Context, st *statetrack.Tracker, conf *config.Config, outBasePath string,
) (newBinaryPath string) {
if st.ErrIndex() == statetrack.IndexTempl {
return
}
var wg sync.WaitGroup
if conf.Lint {
wg.Add(1)
go func() {
defer wg.Done()
runGolangCILint(ctx, st, conf)
}()
}
wg.Add(1)
go func() {
defer wg.Done()
newBinaryPath = buildServer(ctx, st, conf, outBasePath)
if newBinaryPath != "" {
log.Debugf("new app server binary: %s", newBinaryPath)
}
}()
wg.Wait() // Wait for build and lint to finish.
return newBinaryPath
}
func checkTemplVersion(ctx context.Context) error {
out, err := cmdrun.Run(ctx, "", nil, "templ", "version")
if err != nil {
return err
}
outStr := strings.TrimSpace(string(out))
if !strings.HasPrefix(outStr, config.SupportedTemplVersion) {
log.WarnUnsupportedTemplVersion(
config.Version, config.SupportedTemplVersion, outStr,
)
}
return nil
}