This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
151 lines (123 loc) · 3.53 KB
/
action.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
package run
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/charmbracelet/log"
"github.com/coffeebeats/gdbuild/internal/action"
"github.com/coffeebeats/gdbuild/internal/osutil"
)
/* -------------------------------------------------------------------------- */
/* Function: NewVerifyArtifactsAction */
/* -------------------------------------------------------------------------- */
// NewVerifyArtifactsAction creates an 'action.Action' which verifies that all
// required artifacts have been generated.
func NewVerifyArtifactsAction(
_ *Context,
root osutil.Path,
artifacts []string,
) action.WithDescription[action.Function] {
fn := func(_ context.Context) error {
if err := root.CheckIsDir(); err != nil {
return err
}
found := make(map[string]struct{})
pathRoot := filepath.ToSlash(root.String())
if err := fs.WalkDir(os.DirFS(pathRoot), ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
found[path+"/"] = struct{}{}
}
found[path] = struct{}{}
return nil
}); err != nil {
return err
}
for _, a := range artifacts {
if _, ok := found[a]; !ok {
return fmt.Errorf(
"%w: required file not generated: %s",
ErrMissingInput,
a,
)
}
log.Debugf(
"found required artifact: %s",
filepath.Join(pathRoot, a),
)
}
return nil
}
return action.WithDescription[action.Function]{
Action: fn,
Description: "validate generated artifacts: " + strings.Join(artifacts, ", "),
}
}
/* -------------------------------------------------------------------------- */
/* Function: NewCopyArtifactsAction */
/* -------------------------------------------------------------------------- */
// NewCopyArtifactsAction creates an 'action.Action' which moves the generated
// Godot artifacts to the output directory.
func NewCopyArtifactsAction( //nolint:ireturn
rc *Context,
root osutil.Path,
artifacts []string,
) action.Action {
if rc.PathOut == "" {
return action.NoOp{}
}
fn := func(ctx context.Context) error {
if rc.PathOut == "" {
return nil
}
pathOut := rc.PathOut.String()
if err := osutil.EnsureDir(pathOut, osutil.ModeUserRWXGroupRX); err != nil {
return err
}
if err := root.CheckIsDir(); err != nil {
return err
}
for _, a := range artifacts {
pathArtifact := filepath.Join(root.String(), a)
log.Debugf("copying artifact %s to directory: %s", a, pathOut)
if err := osutil.CopyFile(
ctx,
pathArtifact,
filepath.Join(pathOut, a),
); err != nil {
return err
}
}
return nil
}
return action.WithDescription[action.Function]{
Action: fn,
Description: "move generated artifacts to output directory: " + rc.PathOut.String(),
}
}
/* -------------------------------------------------------------------------- */
/* Function: NewWaitAction */
/* -------------------------------------------------------------------------- */
// NewWaitAction creates an 'action.Action' which blocks the thread for the
// specified duration.
func NewWaitAction(d time.Duration) action.WithDescription[action.Function] {
fn := func(ctx context.Context) error {
timer := time.NewTimer(d)
select {
case <-timer.C:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
return action.WithDescription[action.Function]{
Action: fn,
Description: "wait for the specified duration: " + d.String(),
}
}