-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod.dang
More file actions
233 lines (206 loc) · 9.85 KB
/
Copy pathmod.dang
File metadata and controls
233 lines (206 loc) · 9.85 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
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
"""
A Dagger module that uses the Java SDK.
"""
type Mod {
"""
Workspace-root-relative path of this module root — the module's stable
identity, independent of where the client is invoked from.
"""
rootPath: String!
"""
Workspace containing this module.
"""
let ws: Workspace!
"""
Whether to commit the compiled SDK jar into the module (see JavaSdk.vendorSdkJar).
"""
let vendorSdkJar: Boolean!
"""
The shared local Maven repository.
Mounted LOCKED, not shared: seeding it from the committed prebuilt repository
is a plain recursive copy, and two generations running at once race on it —
`cp: cannot create directory ...: File exists`. Generating two Java modules
concurrently is ordinary, so the mount serializes rather than the callers.
"""
let mavenRepo: CacheVolume! { cacheVolume("sdk-java-maven-m2") }
"""
Maven container used for codegen (pinned digest, matches the builtin runtime).
"""
let mvn: Container! {
container.from("maven:3.9.9-eclipse-temurin-21-alpine@sha256:4cbb8bf76c46b97e028998f2486ed014759a8e932480431039bdb93dffe6813e")
}
"""
The vendored Java SDK Maven reactor shipped with this module.
"""
let sdkSourceDir: Directory! { currentModule.source.directory("sdk") }
"""
Vendor the Java SDK as buildable source for a module:
src/main/java the hand-written SDK library
src/processor/java the annotation processor that generates the entrypoint
src/generated/java the client bindings generated from the engine schema
src/processor/resources/META-INF/services/...Processor
The returned directory is dropped at <module>/sdk.
"""
let prebuiltCodegenRepo: String! { "prebuilt/m2" }
let prebuiltCodegenPlugin: String! { "prebuilt/dagger-codegen-maven-plugin.jar" }
"""
A maven container with the codegen plugin available in the local repository.
Fast path: when the packager module has committed the plugin's local Maven
repository under prebuilt/m2, drop it into ~/.m2/repository with a plain copy —
no maven invocation. Otherwise fall back to installing a committed plugin jar,
and finally to compiling the plugin from the vendored sources.
"""
let codegenBase(introspectionJSON: File!): Container! {
let base = mvn
.withoutEntrypoint
.withMountedCache("/root/.m2", mavenRepo, sharing: CacheSharingMode.LOCKED)
.withMountedFile("/schema.json", introspectionJSON)
.withDirectory("/dagger-io", sdkSourceDir)
.withWorkdir("/dagger-io")
if (currentModule.source.exists(prebuiltCodegenRepo + "/io/dagger")) {
base
.withDirectory("/prebuilt-m2", currentModule.source.directory(prebuiltCodegenRepo))
.withExec(["sh", "-c", "mkdir -p /root/.m2/repository && cp -r /prebuilt-m2/. /root/.m2/repository/"])
} else if (currentModule.source.exists(prebuiltCodegenPlugin)) {
base
.withMountedFile("/codegen-plugin.jar", currentModule.source.file(prebuiltCodegenPlugin))
.withExec(["mvn", "install:install-file", "-Dfile=/dagger-io/pom.xml", "-Dpackaging=pom", "-DpomFile=/dagger-io/pom.xml", "--no-transfer-progress"])
.withExec(["mvn", "install:install-file", "-Dfile=/codegen-plugin.jar", "-DpomFile=/dagger-io/dagger-codegen-maven-plugin/pom.xml", "--no-transfer-progress"])
} else {
base.withExec(["mvn", "--projects", "dagger-codegen-maven-plugin", "--also-make", "install", "-T1C", "-Dmaven.test.skip=true", "-Dfmt.skip=true", "--no-transfer-progress"])
}
}
"""
Build and install the SDK + annotation processor jars, returning the maven
container.
Uses install (rather than generate-sources) so the compiled jars land in the
local Maven repository — the dagger-prebuilt-sdk pom profile compiles the
entrypoint against them instead of recompiling the vendored SDK sources. Jars
are installed under a per-module version so modules that resolve to different
schemas (different dependencies, or a different engine) never share a Maven
coordinate; the codegen plugin keeps its own fixed version and resolves from the
prebuilt repo. Cached across a module's own edits.
"""
let sdkBuilt(introspectionJSON: File!, name: String!): Container! {
codegenBase(introspectionJSON)
.withExec(["mvn", "versions:set", "-DnewVersion=" + name, "-DgenerateBackupPoms=false", "--no-transfer-progress"])
.withExec(["mvn", "--projects", "dagger-java-sdk,dagger-java-annotation-processor", "--also-make", "install", "-Ddaggerengine.schema=/schema.json", "-Ddaggerengine.version=" + engineVersion, "-Dmaven.test.skip=true", "-Dfmt.skip=true", "--no-transfer-progress"])
}
"""
The live engine version, without build metadata.
Codegen only reads the engine version off the CLI when it has to query the
schema itself. Here the schema is handed to it, so without this the version
stays whatever the pom happens to say, and generation cannot tell which shapes
the engine on the other end actually supports.
The `+<commit>` suffix is dropped: it changes on every engine build and would
make every module's SDK rebuild for no reason.
"""
let engineVersion: String! { version.split("+")[0] ?? version }
let vendoredSdk(introspectionJSON: File!, name: String!): Directory! {
let built = sdkBuilt(introspectionJSON, name)
directory
.withDirectory("src/main/java", built.directory("/dagger-io/dagger-java-sdk/src/main/java"))
.withDirectory(
"src/processor/java",
built.directory("/dagger-io/dagger-java-annotation-processor/src/main/java"),
)
.withDirectory(
"src/generated/java",
built.directory("/dagger-io/dagger-java-sdk/target/generated-sources/dagger"),
)
.withNewFile(
"src/processor/resources/META-INF/services/javax.annotation.processing.Processor",
"io.dagger.annotation.processor.DaggerModuleAnnotationProcessor\n",
)
}
"""
Capture the compiled SDK jar as a small local Maven repository to commit under
<module>/sdk/repo. Stored at a fixed version so the module pom's
dagger-vendored-sdk-jar profile resolves it; the runtime build then compiles
only the module's own code against the jar rather than the vendored sources.
"""
let vendoredSdkJar(introspectionJSON: File!, name: String!): Directory! {
let jar = sdkBuilt(introspectionJSON, name)
.withExec(["sh", "-c", "mkdir -p /out && cp /root/.m2/repository/io/dagger/dagger-java-sdk/" + name + "/dagger-java-sdk-" + name + ".jar /out/dagger-java-sdk-0.21.4.jar"])
.file("/out/dagger-java-sdk-0.21.4.jar")
directory
.withFile("io/dagger/dagger-java-sdk/0.21.4/dagger-java-sdk-0.21.4.jar", jar)
.withNewFile(
"io/dagger/dagger-java-sdk/0.21.4/dagger-java-sdk-0.21.4.pom",
"<project><modelVersion>4.0.0</modelVersion><groupId>io.dagger</groupId><artifactId>dagger-java-sdk</artifactId><version>0.21.4</version><packaging>jar</packaging></project>\n",
)
}
"""
Compile the module with the annotation processor enabled (proc=full) so it
emits io.dagger.gen.entrypoint.Entrypoint, and return the generated-sources
directory (the entrypoint) for vendoring under <module>/src/generated/java.
"""
let generatedEntrypoint(moduleDir: Directory!, name: String!): Directory! {
mvn
.withoutEntrypoint
.withMountedCache("/root/.m2", mavenRepo, sharing: CacheSharingMode.LOCKED)
.withDirectory("/module", moduleDir)
.withWorkdir("/module")
.withEnvVariable("_DAGGER_JAVA_SDK_MODULE_NAME", name)
.withExec(["mvn", "compile", "-Ddagger.proc=full", "-Ddagger.sdk=prebuilt", "-Ddagger.sdk.version=" + name, "--no-transfer-progress"])
.directory("/module/target/generated-sources/annotations")
}
"""
The workspace with this module's vendored SDK and generated entrypoint merged
in.
"""
generated: Workspace! {
let modSource = ws.moduleSource(workspaceRef(rootPath))
let name = modSource.moduleName
let introspectionJSON = modSource.introspectionSchemaJSON
let vendored = vendoredSdk(introspectionJSON, name)
# the module as committed, with the whole committed sdk/ dropped (source and
# any previously vendored jar) and the freshly vendored SDK sources overlaid,
# plus any stale generated entrypoint dropped so the processor regenerates it
let baseDir = moduleDir(ws, rootPath)
.withoutDirectory("sdk")
.withDirectory("sdk", vendored)
.withoutDirectory("src/generated")
let entrypoint = generatedEntrypoint(baseDir, name)
let staged = ws
.withNewDirectory(workspaceRef(joinPath(rootPath, "sdk")), vendored)
.withNewDirectory(workspaceRef(joinPath(rootPath, "src/generated/java")), entrypoint)
if (vendorSdkJar) {
staged.withNewDirectory(
workspaceRef(joinPath(rootPath, "sdk/repo")),
vendoredSdkJar(introspectionJSON, name),
)
} else {
staged
}
}
"""
The module's committed source directory, workspace-rooted.
Maven build output is left out: a class file an IDE dropped under target/ is
newer than its source, so maven-compiler-plugin skips recompiling it and the
stale class wins over the entrypoint this generation is producing.
"""
let moduleDir(ws: Workspace!, modPathArg: String!): Directory! {
let buildOutputExclude = ["**/target/**"]
if (modPathArg == ".") {
ws.directory("/", include: ["**"], exclude: buildOutputExclude)
} else {
ws
.directory("/", include: [modPathArg + "/**"], exclude: buildOutputExclude)
.directory(modPathArg)
}
}
"""
A module path as a workspace-root-absolute path, the form Workspace resolves
from the workspace root rather than from the client's cwd.
"""
let workspaceRef(modPathArg: String!): String! {
if (modPathArg == ".") { "/" } else { "/" + modPathArg }
}
"""
Join a module path with a sub-path, handling the root (".") module.
"""
let joinPath(modPathArg: String!, sub: String!): String! {
if (modPathArg == ".") { sub } else { modPathArg + "/" + sub }
}
}