-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dang
More file actions
150 lines (137 loc) · 5.64 KB
/
Copy pathmain.dang
File metadata and controls
150 lines (137 loc) · 5.64 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
"""
Manage Dagger modules that use the Java SDK (new self-contained code organisation).
Modules created by `init` use the self-contained layout: the Java SDK is vendored
as source and code is generated into the module (see `generate`), and the module
runtime is this repository's build/package-only runtime
(`github.com/dagger/java-sdk/runtime`). Because the generated files are committed,
the runtime skips codegen at module load and just builds and packages the
committed sources.
"""
type JavaSdk {
"""
Runtime source written into the dagger-module.toml of new Java modules.
"""
pub targetRuntime: String! { "github.com/dagger/java-sdk/runtime" }
"""
Marker filename that skips generate when found at or above a Java SDK module root.
"""
pub skipGenerateFilename: String! = ".dagger-java-sdk-skip-generate"
"""
Config filenames that mark a Dagger module root: the CLI 1.0
`dagger-module.toml` (workspace-managed modules) and the legacy `dagger.json`.
A managed module is discovered by whichever it uses.
"""
let moduleConfigFilenames: [String!]! = ["dagger-module.toml", "dagger.json"]
"""
Commit the compiled Dagger Java SDK as a jar into each generated module so its
runtime build compiles only the module's own code against the jar instead of
recompiling the vendored SDK sources. The sources stay checked in for IDE use.
Opt-in (set `settings.vendorSdkJar = true` under `[modules.java-sdk]` in the
workspace dagger.toml) because it adds a committed binary artifact.
"""
pub vendorSdkJar: Boolean! = false
"""
Initialize Java-owned files for a new Dagger module.
"""
pub initModule(
ws: Workspace!,
name: String!,
path: String!,
): Changeset! {
let rawPath = path.trimPrefix("./").trimPrefix("/")
let modPath = if (rawPath == "" or rawPath == ".") {
"."
} else if (rawPath == ".." or rawPath.trimPrefix("../") != rawPath) {
raise "path escapes workspace: " + rawPath
} else {
rawPath.trimSuffix("/")
}
let before = if (modPath == ".") {
ws.directory("/", include: ["**"])
} else {
ws.directory("/", include: [modPath + "/**"])
}
before.withDirectory(modPath, renderedTemplate(name, "default")).changes(before)
}
"""
Render a Java init template, substituting the requested module name.
"""
let renderedTemplate(name: String!, template: String!): Directory! {
container
.from("golang:1.25-alpine")
.withoutEntrypoint
.withMountedCache("/go/pkg/mod", cacheVolume("go-mod"))
.withMountedCache("/root/.cache/go-build", cacheVolume("go-build"))
.withDirectory(
"/helper",
currentModule.source.directory("helpers/render-java-template"),
)
.withDirectory("/template", currentModule.source.directory("templates/" + template))
.withWorkdir("/helper")
.withExec(["go", "build", "-o", "/usr/local/bin/render-java-template", "."])
.withExec(["render-java-template", name, "/template", "/rendered"])
.directory("/rendered")
}
"""
Return every Java SDK module this workspace manages that is visible from the
client's current location.
Discovery is anchored at the client's cwd (never the workspace root): the
nearest enclosing module plus every module at or below the cwd, intersected
with the SDK's engine-owned list of managed modules
(currentModule.asSDK.modules). So running from a subdirectory acts on the
project you're in — and the projects beneath it — not the whole workspace.
Discovery is the polyfill's cwd-aware findConfigDirs (dagger/dagger#13688);
this maps its cwd-relative results to workspace-root-relative paths and keeps
the ones this SDK manages, whether they use dagger-module.toml or dagger.json.
"""
pub modules(ws: Workspace!): [Mod!]! {
let managed = ws.sdk(name: currentModule.name).modules.{{source}}
let cwd = normalizePath(ws.cwd)
polyfill.workspace(ws)
.findConfigDirs(moduleConfigFilenames, exclude: ["**/target/**"])
.map { dir => moduleRelPath(cwd, dir) }
.uniq
.filter { path => managed.filter { m => normalizePath(m.source) == path }.length > 0 }
.map { path => Mod(rootPath: path, ws: ws, skipGenerateFilename: skipGenerateFilename, vendorSdkJar: vendorSdkJar) }
}
"""
Normalize a workspace path: strip a leading "./" or "/" and any trailing "/",
and map the empty/root path to ".".
"""
let normalizePath(path: String!): String! {
let normalized = path.trimPrefix("./").trimPrefix("/").trimSuffix("/")
if (normalized == "") { "." } else { normalized }
}
"""
Resolve a findConfigDirs result — a cwd-relative path, at or below the cwd
("." , "sub/dir") or a strict ancestor (".." , "../..") — against the cwd into a
workspace-root-relative path, the format both managed module sources and
Mod.rootPath use.
"""
let moduleRelPath(cwd: String!, dir: String!): String! {
let base = if (cwd == "" or cwd == ".") { [] } else { cwd.split("/") }
let segs = dir.split("/").reduce(base) { acc, seg =>
if (seg == "..") {
acc.dropLast(1)
} else if (seg == "." or seg == "") {
acc
} else {
acc + [seg]
}
}
if (segs.length == 0) { "." } else { segs.join("/") }
}
"""
Generate every managed Java SDK module visible from the client's current
location (runs at `dagger generate`). Discovery goes through modules(ws), so
running from a subdirectory generates only the project you're in and the
projects beneath it.
"""
pub generateAll(ws: Workspace!): Changeset! @generate {
changeset.withChangesets(
modules(ws)
.filter { mod => mod.skipGenerate(ws) == false }
.map { mod => mod.generate(ws) },
)
}
}