Repository navigation
Expand file tree
/
Copy pathplugin.lua
More file actions
304 lines (280 loc) · 13.2 KB
/
Copy pathplugin.lua
File metadata and controls
304 lines (280 loc) · 13.2 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
daukle.plugin{ api = 1, uses = { "provision", "artifact", "exec", "write", "read", "parse", "env" },
exports = { "lib/jdks", "lib/launcher", "lib/entries" } }
local jdks = daukle.require("lib/jdks")
--[[ Aliased because "entries" is already a local inside java:run and java:jar,
where it names the classpath being assembled. ]]
local entry_points = daukle.require("lib/entries")
local settings = daukle.require("lib/settings")
local jdk_tools = daukle.require("lib/jdk_tools")
local classpaths = daukle.require("lib/classpaths")
daukle.toolchain{
name = "java",
generate = function(context)
local config = settings.config_of(context)
settings.source_paths(config, context.root)
settings.test_source_root_of(config)
settings.resource_root_of(config)
settings.test_resource_root_of(config)
settings.release_of(config)
settings.checked_args(config.compileArgs, "compileArgs")
settings.checked_args(config.runArgs, "runArgs")
settings.checked_args(config.jarArgs, "jarArgs")
settings.checked_args(config.testArgs, "testArgs")
classpaths.checked_classpath(config.classpath, "classpath")
classpaths.checked_classpath(config.testClasspath, "testClasspath")
jdks.for_host{ os = context.host.os, arch = context.host.arch,
version = settings.version_of(context) }
return {}
end,
}
local SOURCE_ARCHIVE = ".daukle-sources.jar"
local TEST_SOURCE_ARCHIVE = ".daukle-test-sources.jar"
local SOURCE_ARGFILE = ".daukle-sources.args"
local TEST_SOURCE_ARGFILE = ".daukle-test-sources.args"
local CLASSES = "classes"
local TEST_CLASSES = "test-classes"
--- Passed first to every javac, so a compileArgs "-proc" still wins.
---
--- @implNote the default changed under the same manifest: JDK 21 runs a
--- processor found on the class path and JDK 25 runs none unless processing is
--- configured. Every pinned JDK accepts this flag, 17.0.13 included although
--- JDK 17's own page does not list it, and a compile with no processor is
--- byte-identical with or without it.
local ANNOTATION_PROCESSING = "-proc:full"
local function artifact_name(project, suffix)
local last = string.match(project, "([^/]+)$")
if last == nil then
error('the project id "' .. project .. '" has no final segment to name a jar from', 0)
end
return last .. (suffix or "") .. ".jar"
end
--[[ One runner for java:run and for every java:run-<entry>, so a classpath or
resource change cannot reach one and miss the others.
@implNote a project-wide "runArgs" applies to EVERY entry and the entry's
own are appended after it, which is the same rule daukle/gradle settled on
for project-wide keys: they reach every module rather than only the
unnamed one. An entry that must not see them is a second toolchain. ]]
local function run_class(context, main_class, run_args)
local root, pick = jdk_tools.provision_jdk(context)
local config = settings.config_of(context)
local resources = jdk_tools.resource_directories(context, root, pick, config,
{ settings.resource_root_of })
local classpath = classpaths.appended({ CLASSES }, resources)
classpaths.appended(classpath, classpaths.classpath_of(context, config, "classpath"))
local argv = settings.append_args({ "-cp", classpaths.classpath_argument(context, classpath) },
config.runArgs, "runArgs")
if run_args ~= nil then
for index = 1, #run_args do argv[#argv + 1] = run_args[index] end
end
argv[#argv + 1] = main_class
jdk_tools.run_tool(root:tool(jdk_tools.executable(context.host.os, pick, "java")), argv)
end
daukle.task{
name = "java:compile",
run = function(context)
local config = settings.config_of(context)
local paths, source_root = settings.source_paths(config, context.root)
local root, pick = jdk_tools.provision_jdk(context)
if paths == nil then
paths = jdk_tools.enumerated_sources(context, root, pick, source_root, SOURCE_ARCHIVE)
if paths == nil then
error('no directory "' .. source_root .. '" to compile from: set "sourceRoot" if the'
.. ' sources live elsewhere', 0)
end
if #paths == 0 then
error('no ".java" source was found under "' .. source_root .. '": set "sourceRoot" if the'
.. ' sources live elsewhere, or name "roots" or "main"', 0)
end
end
local argv = { ANNOTATION_PROCESSING, "-d", CLASSES,
"-sourcepath", context.root .. "/" .. source_root }
local release = settings.release_of(config)
if release ~= nil then
argv[#argv + 1] = "--release"
argv[#argv + 1] = release
end
local classpath = classpaths.classpath_argument(context,
classpaths.classpath_of(context, config,
"classpath"))
if classpath ~= nil then
argv[#argv + 1] = "-cp"
argv[#argv + 1] = classpath
end
settings.append_args(argv, config.compileArgs, "compileArgs")
argv[#argv + 1] = jdk_tools.source_argfile(paths, SOURCE_ARGFILE)
jdk_tools.run_tool(root:tool(jdk_tools.executable(context.host.os, pick, "javac")), argv)
end,
}
daukle.task{
name = "java:run",
dependsOn = { "java:compile" },
run = function(context)
local config = settings.config_of(context)
run_class(context, settings.main_of(config), nil)
end,
}
daukle.task{
name = "java:jar",
dependsOn = { "java:compile" },
run = function(context)
local root, pick = jdk_tools.provision_jdk(context)
local config = settings.config_of(context)
local argv = { "--create", "--file", artifact_name(context.project) }
--[[ A library has no entry point, which is D-74's finding one task over:
Gradle's jar needs no main class and writes Main-Class only when given
one. java:run still requires it, because a run target genuinely needs
one. D-81. ]]
if config.main ~= nil then
argv[#argv + 1] = "--main-class"
argv[#argv + 1] = settings.main_of(config)
end
settings.append_args(argv, config.jarArgs, "jarArgs")
argv[#argv + 1] = "-C"
argv[#argv + 1] = CLASSES
argv[#argv + 1] = "."
local resources = jdk_tools.resource_directories(context, root, pick, config,
{ settings.resource_root_of })
for index = 1, #resources do
argv[#argv + 1] = "-C"
argv[#argv + 1] = resources[index]
argv[#argv + 1] = "."
end
jdk_tools.run_tool(root:tool(jdk_tools.executable(context.host.os, pick, "jar")), argv)
end,
}
daukle.task{
name = "java:test-compile",
dependsOn = { "java:compile" },
run = function(context)
local config = settings.config_of(context)
local source_root = settings.test_source_root_of(config)
local root, pick = jdk_tools.provision_jdk(context)
local paths = jdk_tools.enumerated_sources(context, root, pick, source_root,
TEST_SOURCE_ARCHIVE)
if paths == nil then
error('no directory "' .. source_root .. '" to compile tests from: create it, or set'
.. ' "testSourceRoot" if the tests live elsewhere', 0)
end
if #paths == 0 then
error('no ".java" test was found under "' .. source_root .. '": set "testSourceRoot" if'
.. ' the tests live elsewhere', 0)
end
local argv = { ANNOTATION_PROCESSING, "-d", TEST_CLASSES,
"-sourcepath", context.root .. "/" .. source_root }
local release = settings.release_of(config)
if release ~= nil then
argv[#argv + 1] = "--release"
argv[#argv + 1] = release
end
--[[ The launcher is last so a project that pins its own junit in
testClasspath compiles against that one, and a project that pins
nothing still compiles. ]]
local resources = jdk_tools.resource_directories(context, root, pick, config,
{ settings.resource_root_of,
settings.test_resource_root_of })
local entries = classpaths.appended({ CLASSES }, resources)
classpaths.appended(entries, classpaths.test_classpath_entries(context, config))
entries[#entries + 1] = classpaths.launcher_jar()
argv[#argv + 1] = "-cp"
argv[#argv + 1] = classpaths.classpath_argument(context, entries)
settings.append_args(argv, config.compileArgs, "compileArgs")
argv[#argv + 1] = jdk_tools.source_argfile(paths, TEST_SOURCE_ARGFILE)
jdk_tools.run_tool(root:tool(jdk_tools.executable(context.host.os, pick, "javac")), argv)
end,
}
--[[ Without --fail-if-no-tests the launcher exits 0 on a tree it discovered
nothing in, so a java:test that trusted the default would be green on a
project whose tests reach nothing. D-51 is the same shape. D-79. ]]
daukle.task{
name = "java:test",
dependsOn = { "java:test-compile" },
run = function(context)
local config = settings.config_of(context)
local source_root = settings.test_source_root_of(config)
local root, pick = jdk_tools.provision_jdk(context)
local resources = jdk_tools.resource_directories(context, root, pick, config,
{ settings.resource_root_of,
settings.test_resource_root_of })
local entries = classpaths.appended({ TEST_CLASSES, CLASSES }, resources)
classpaths.appended(entries, classpaths.test_classpath_entries(context, config))
--[[ java -jar ignores -cp, so the project classpath goes to the launcher's
own option rather than to the JVM. ]]
local argv = { "-jar", classpaths.launcher_jar(), "execute",
"--class-path", classpaths.classpath_argument(context, entries),
"--scan-class-path", TEST_CLASSES,
"--details=summary", "--fail-if-no-tests" }
settings.append_args(argv, config.testArgs, "testArgs")
local java = root:tool(jdk_tools.executable(context.host.os, pick, "java"))
local result = jdk_tools.run_tool(java, argv, { check = false })
if result.code == 1 then
error("a test failed; the summary above names which", 0)
end
if result.code == 2 then
error('no test was found under "' .. source_root .. '": the sources compiled, but none of'
.. ' them holds a test the launcher recognises', 0)
end
if result.code ~= 0 then
error("the test launcher exited with code " .. tostring(result.code), 0)
end
end,
}
--[[ A sources jar packages sources AND resources, which is what Gradle's
sourcesJar does (sourceSets.main.allSource) rather than merely resembles.
It needs no compilation, so it depends on nothing. D-81. ]]
daukle.task{
name = "java:sources-jar",
run = function(context)
local config = settings.config_of(context)
local _, source_root = settings.source_paths(config, context.root)
local root, pick = jdk_tools.provision_jdk(context)
local sources = jdk_tools.readable_directory(context, root, pick, source_root)
if sources == nil then
error('no directory "' .. source_root .. '" to package sources from: set "sourceRoot" if'
.. ' the sources live elsewhere', 0)
end
local argv = settings.append_args({ "--create", "--file",
artifact_name(context.project, "-sources") },
config.jarArgs, "jarArgs")
argv[#argv + 1] = "-C"
argv[#argv + 1] = sources
argv[#argv + 1] = "."
local resources = jdk_tools.resource_directories(context, root, pick, config,
{ settings.resource_root_of })
for index = 1, #resources do
argv[#argv + 1] = "-C"
argv[#argv + 1] = resources[index]
argv[#argv + 1] = "."
end
jdk_tools.run_tool(root:tool(jdk_tools.executable(context.host.os, pick, "jar")), argv)
end,
}
--[[ A task per declared entry point, registered from the CHUNK because a daukle
task takes no arguments: N runnable classes need N registered tasks and a
fixed vocabulary cannot substitute. D-110 granted the chunk-time manifest
read this needs.
@implNote the ".toml" gate is not optional. Parsing daukle.manifest when it
is Lua is FATAL on every command rather than degraded, which is the crash
daukle/cmake@1.4.0 shipped. A project with a Lua manifest keeps java:run
and gains no entry tasks, because nothing a chunk can reach sees a
Lua-declared config. ]]
local function declarative_manifest()
if string.match(daukle.manifest, "%.toml$") == nil then return nil end
return daukle.parse(daukle.read(daukle.manifest), daukle.manifest)
end
local function declared_entries()
local document = declarative_manifest()
if document == nil then return {} end
local toolchains = document.toolchains
local config = type(toolchains) == "table" and toolchains.java or nil
if type(config) ~= "table" then return {} end
return entry_points.declared(config, settings.class_to_path)
end
for _, entry in ipairs(declared_entries()) do
daukle.task{
name = "java:run-" .. entry.name,
dependsOn = { "java:compile" },
run = function(context)
run_class(context, entry.main, entry.runArgs)
end,
}
end