-
Notifications
You must be signed in to change notification settings - Fork 559
/
sdk_rust.go
181 lines (150 loc) · 4.38 KB
/
sdk_rust.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
package main
import (
"context"
"fmt"
"regexp"
"strings"
"golang.org/x/sync/errgroup"
"github.com/dagger/dagger/ci/util"
)
const (
rustGeneratedAPIPath = "sdk/rust/crates/dagger-sdk/src/gen.rs"
rustVersionFilePath = "sdk/rust/crates/dagger-sdk/src/core/mod.rs"
// https://hub.docker.com/_/rust
rustDockerStable = "rust:1.77-bookworm"
cargoChefVersion = "0.1.62"
)
type RustSDK struct {
Dagger *Dagger // +private
}
// Lint the Rust SDK
func (r RustSDK) Lint(ctx context.Context) error {
base := r.rustBase(rustDockerStable)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
_, err := base.
WithExec([]string{"cargo", "check", "--all", "--release"}).
Sync(ctx)
return err
})
eg.Go(func() error {
_, err := base.
WithExec([]string{"cargo", "fmt", "--check"}).
Sync(ctx)
return err
})
eg.Go(func() error {
return util.DiffDirectoryF(ctx, r.Dagger.Source, r.Generate, "sdk/rust")
})
return eg.Wait()
}
// Test the Rust SDK
func (r RustSDK) Test(ctx context.Context) error {
installer, err := r.Dagger.installer(ctx, "sdk-rust-test")
if err != nil {
return err
}
_, err = r.rustBase(rustDockerStable).
With(installer).
WithExec([]string{"rustc", "--version"}).
WithExec([]string{"cargo", "test", "--release", "--all"}).
Sync(ctx)
return err
}
// Regenerate the Rust SDK API
func (r RustSDK) Generate(ctx context.Context) (*Directory, error) {
installer, err := r.Dagger.installer(ctx, "sdk-rust-generate")
if err != nil {
return nil, err
}
generated := r.rustBase(rustDockerStable).
With(installer).
WithExec([]string{"cargo", "run", "-p", "dagger-bootstrap", "generate", "--output", fmt.Sprintf("/%s", rustGeneratedAPIPath)}).
WithExec([]string{"cargo", "fix", "--all", "--allow-no-vcs"}).
WithExec([]string{"cargo", "fmt"}).
File(strings.TrimPrefix(rustGeneratedAPIPath, "sdk/rust/"))
return dag.Directory().
WithDirectory("sdk/rust", r.Dagger.Source.Directory("sdk/rust")).
WithFile(rustGeneratedAPIPath, generated), nil
}
// Publish the Rust SDK
func (r RustSDK) Publish(
ctx context.Context,
tag string,
// +optional
dryRun bool,
// +optional
cargoRegistryToken *Secret,
) error {
var (
version = strings.TrimPrefix(tag, "sdk/rust/v")
crate = "dagger-sdk"
)
base := r.
rustBase(rustDockerStable).
WithExec([]string{
"cargo", "install", "cargo-edit", "--locked",
}).
WithExec([]string{
"cargo", "set-version", "-p", crate, version,
})
args := []string{
"cargo", "publish", "-p", crate, "-v", "--all-features",
}
if dryRun {
args = append(args, "--dry-run")
base = base.WithExec(args)
} else {
base = base.
WithSecretVariable("CARGO_REGISTRY_TOKEN", cargoRegistryToken).
WithExec(args)
}
_, err := base.Sync(ctx)
return err
}
// Bump the Rust SDK's Engine dependency
func (r RustSDK) Bump(ctx context.Context, version string) (*Directory, error) {
versionStr := `pub const DAGGER_ENGINE_VERSION: &'static str = "([0-9\.-a-zA-Z]+)";`
versionStrf := `pub const DAGGER_ENGINE_VERSION: &'static str = "%s";`
version = strings.TrimPrefix(version, "v")
versionContents, err := r.Dagger.Source.File(rustVersionFilePath).Contents(ctx)
if err != nil {
return nil, err
}
versionRe, err := regexp.Compile(versionStr)
if err != nil {
return nil, err
}
versionBumpedContents := versionRe.ReplaceAllString(
versionContents,
fmt.Sprintf(versionStrf, version),
)
return dag.Directory().WithNewFile(rustVersionFilePath, versionBumpedContents), nil
}
func (r RustSDK) rustBase(image string) *Container {
const appDir = "sdk/rust"
src := dag.Directory().WithDirectory("/", r.Dagger.Source.Directory(appDir))
mountPath := fmt.Sprintf("/%s", appDir)
base := dag.Container().
From(image).
WithDirectory(mountPath, src, ContainerWithDirectoryOpts{
Include: []string{
"**/Cargo.toml",
"**/Cargo.lock",
"**/main.rs",
"**/lib.rs",
},
}).
WithWorkdir(mountPath).
WithEnvVariable("CARGO_HOME", "/root/.cargo").
WithMountedCache("/root/.cargo", dag.CacheVolume("rust-cargo-"+image)).
// combine into one layer so there's no assumptions on state of cache volume across steps
With(util.ShellCmds(
"rustup component add rustfmt",
"cargo install --locked cargo-chef@"+cargoChefVersion,
"cargo chef prepare --recipe-path /tmp/recipe.json",
"cargo chef cook --release --workspace --recipe-path /tmp/recipe.json",
)).
WithMountedDirectory(mountPath, src)
return base
}