-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelixir-sdk.dang
More file actions
234 lines (205 loc) · 8.7 KB
/
Copy pathelixir-sdk.dang
File metadata and controls
234 lines (205 loc) · 8.7 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
234
"""
Manage Dagger modules that use the Elixir SDK.
"""
type ElixirSdk {
# Matches `"sdk": { "source": "elixir" }` in a legacy dagger.json.
let legacyConfigPattern = "\"sdk\"\\s*:\\s*\\{[^}]*\"source\"\\s*:\\s*\"elixir\""
# Matches a `[runtime]` table whose source names this SDK's runtime in a CLI
# 1.0 dagger-module.toml. Accepts the bare `elixir` name as well as the remote
# runtime ref, with or without a version suffix.
let tomlConfigPattern = "\\[runtime\\][^\\[]*source\\s*=\\s*\"(elixir|github\\.com/dagger/elixir-sdk/runtime[^\"]*)\""
"""
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"]
"""
Marker filename that skips generate when found at or above an Elixir SDK module root.
"""
pub skipGenerateFilename: String! = ".dagger-elixir-sdk-skip-generate"
"""
Runtime source to write into modules created by this SDK.
Elixir has no built-in engine runtime, so new modules point at this
repository's build-only runtime module.
"""
pub targetRuntime: String! { "github.com/dagger/elixir-sdk/runtime" }
"""
Return init templates tracked by this module.
Templates live under templates/<name> and are materialized into the new
module before generation. Passing an empty template to init uses the module
default.
"""
pub templates: [Template!]! {
if (currentModule.source.exists("templates")) {
let root = currentModule.source.directory("templates")
root.entries.map { name =>
Template(
name: name.trimSuffix("/"),
source: root.directory(name),
)
}
} else {
directory.entries.map { name =>
Template(
name: name,
source: directory,
)
}
}
}
"""
Return every managed Elixir SDK module visible from the client's cwd: the
nearest enclosing module plus modules at or below the cwd.
The engine owns both the list and the cwd policy — `currentModule.asSDK`
returns the modules registered to this SDK that sit at or below the client's
cwd, plus the nearest enclosing one when the cwd is not itself registered — so
running from a subdirectory acts on the project you are in, and the projects
beneath it. Paths come back workspace-root-relative, the currency
`Mod.rootPath` uses.
A module is registered by the directory holding its config, so one whose
source points elsewhere is listed at its config path and not from inside its
own source tree. Address those by path (see `mod`).
"""
pub modules(ws: Workspace!): [Mod!]! {
currentModule
.asSDK(workspace: ws)
.modules.{{path}}
.map { module =>
Mod(
rootPath: normalizePath(module.path),
ws: ws,
skipGenerateFilename: skipGenerateFilename,
)
}
}
"""
Return the Elixir SDK module at or above a workspace path.
When `findUp` is true, `path` may point inside the module.
"""
pub mod(ws: Workspace!, path: String! = ".", findUp: Boolean! = true): Mod! {
let modPath = if (findUp) {
let foundToml = ws.findUp("dagger-module.toml", path)
let foundJson = ws.findUp("dagger.json", path)
let tomlBase = if (foundToml == null) { null } else { configDir(foundToml, "dagger-module.toml") }
let jsonBase = if (foundJson == null) { null } else { configDir(foundJson, "dagger.json") }
if (tomlBase == null and jsonBase == null) {
raise "no Dagger module found containing path: " + path
} else if (tomlBase != null and pathDepth(tomlBase) > pathDepth(jsonBase)) {
validateConfig(ws, tomlBase, "dagger-module.toml", tomlConfigPattern, path)
} else if (jsonBase != null) {
validateConfig(ws, jsonBase, "dagger.json", legacyConfigPattern, path)
} else {
raise "no Dagger module found containing path: " + path
}
} else {
normalizePath(path)
}
Mod(
rootPath: modPath,
ws: ws,
skipGenerateFilename: skipGenerateFilename,
)
}
"""
Initialize Elixir-owned files for a new Dagger module.
Returns a Changeset with only the SDK-owned template files layered onto the
new module's `path`. Engine-owned files (dagger-module.toml, workspace config
updates) are produced by the engine and merged with this changeset.
Pass `template` to pick a starter under templates/<template>. It defaults to
`default` (a small working module). Use `empty` for a bare object module. An
explicitly empty template also selects `default` for compatibility.
The new module does not compile until `dagger generate` runs: its `mix.exs`
depends on `./dagger_sdk`, which generation writes.
"""
pub initModule(
ws: Workspace!,
name: String!,
path: String!,
"""
Template to use to init the module: default*, empty
"""
template: String! = "default",
): 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 selectedTemplate = if (template == "") { "default" } else { template }
if (currentModule.source.exists("templates/" + selectedTemplate) == false) {
raise "unknown init template: " + template
} else {
# Workspace.withNewDirectory replaces the overlay entry at its path, so
# anything already sitting at modPath has to be carried over explicitly —
# otherwise init reports those files removed, which the SDK contract
# forbids.
let scope = if (modPath == ".") { "**" } else { modPath + "/**" }
let root = ws.directory("/", include: [scope])
let rendered = renderedTemplate(name, selectedTemplate)
let source = if (root.exists(modPath)) {
root.directory(modPath).withDirectory(".", rendered)
} else {
rendered
}
# `modPath` is workspace-root-relative, so anchor it at "/": a relative
# workspace path resolves from ws.cwd and would be prefixed a second time
# when initModule is called directly from a subdirectory rather than
# driven by the engine.
ws.withNewDirectory("/" + modPath, source).changes(ws)
}
}
"""
Render an Elixir template with the requested module name.
"""
let renderedTemplate(name: String!, templateName: 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-template"))
.withDirectory("/template", currentModule.source.directory("templates/" + templateName))
.withWorkdir("/helper")
.withExec(["go", "build", "-o", "/usr/local/bin/render-template", "."])
.withExec(["render-template", name, "/template", "/rendered"])
.directory("/rendered")
}
"""
Generate every managed Elixir SDK module visible from the client's cwd.
Modules with the generate skip marker are skipped.
"""
pub generateAll(ws: Workspace!): Changeset! @generate {
# Each module generates onto the workspace the previous one produced, so the
# results accumulate by overlay and one diff at the end carries them all.
# Folding per-module changesets instead would mean measuring each one against
# `ws` — past the cwd cutover the engine measures those from the cwd, which
# is not the frame Workspace.withChanges applies them in.
modules(ws)
.reduce(ws) { stagedWs, mod => mod.generatedWorkspace(stagedWs) }
.changes(ws)
}
let normalizePath(path: String!): String! {
let normalized = path.trimPrefix("./").trimPrefix("/").trimSuffix("/")
if (normalized == "") { "." } else { normalized }
}
let pathDepth(path: String): Int! {
if (path == null) { -1 } else if (path == ".") { 0 } else { path.split("/").length }
}
let configDir(path: String!, filename: String!): String! {
normalizePath(path.trimPrefix("/").trimSuffix(filename))
}
let validateConfig(ws: Workspace!, base: String!, filename: String!, pattern: String!, requestedPath: String!): String! {
let configPath = if (base == ".") { filename } else { base + "/" + filename }
let matches = ws
.directory("/", include: [configPath])
.file(configPath)
.search(pattern: pattern, multiline: true, dotall: true, limit: 1)
.{{id}}
.length > 0
if (matches) { base } else { raise "Dagger module does not use the Elixir SDK: " + requestedPath }
}
}