-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
354 lines (255 loc) · 9.62 KB
/
build.gradle
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import java.nio.file.Files
import java.nio.file.Paths
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
plugins {
id 'java'
id 'application'
id 'maven-publish'
id 'de.undercouch.download' version '5.4.0'
id 'com.vaadin' version "${vaadinVersion}"
id 'io.spring.dependency-management' version '1.1.0'
id 'org.springframework.boot' version "${springBootVersion}"
}
group 'co.zimmermann'
version '0.1.0-SNAPSHOT'
sourceCompatibility = 17
targetCompatibility = 17
final isWindows = DefaultNativePlatform.currentOperatingSystem.windows
final onWindows = {
if (isWindows) {
it.call()
}
}
// ext.pythonZip = "python-3.9.13-embed-amd64.zip"
// ext.pythonDownloadUrl = "https://www.python.org/ftp/python/3.9.13/${pythonZip}"
ext.minicondaInstaller = 'Miniconda3-py39_4.12.0-Windows-x86_64.exe'
ext.minicondaDownloadUrl = "https://repo.anaconda.com/miniconda/${minicondaInstaller}"
([compileJava] + (tasks.withType JavaExec)).each { it.doFirst {
[
[project.ext.pythonDir, "Python prefix dir"],
[project.ext.pythonPath, "Python module search path"],
[project.ext.jepDir, "Python/JEP package dir"],
[project.ext.jepVersion, "Python/JEP package version"],
].each { final value, final valueName -> if (value instanceof PythonFail) {
throw new RuntimeException("${valueName} could not be determined. Python FAILED with exit code ${value.code}\n" +
"Command: ${value.command}\n\n${value.error}\n")
} }
} }
class PythonFail {
final List command
final int code
final String error
PythonFail(final List command, final int code, final String error) {
this.command = command
this.code = code
this.error = error
}
boolean asBoolean() {
return false
}
}
final evaluatePythonCode = {
final command = [project.ext.pythonExe, '-c', "print(${it})"]
logger.info "Executing ${command}"
final process = command.execute()
final output = new StringWriter()
final error = new StringWriter()
process.waitForProcessOutput output, error
if (process.exitValue() == 0) {
return output.toString().trim()
}
final fail = new PythonFail(command, process.exitValue(), error.toString())
logger.warn "Python FAILED with exit code ${fail.code}\n\n${fail.error}"
return fail
}
ext {
pythonProjectDir = file "${project.buildDir}/python-project"
pythonExe = (project.pythonExe.trim() ? (Paths.get project.pythonExe)
: (project.buildDir.toPath() resolve "python/${isWindows ? 'python.exe' : 'bin/python'}")) as String
pythonDir = evaluatePythonCode "__import__('sys').prefix"
pythonPath = evaluatePythonCode "__import__('os').pathsep.join(__import__('sys').path).strip(__import__('os').pathsep)"
jepDir = evaluatePythonCode "__import__('os').path.dirname(__import__('importlib.util').util.find_spec('jep').origin)"
jepVersion = evaluatePythonCode "__import__('pkg_resources').require('jep')[0].version"
}
repositories {
mavenLocal()
mavenCentral()
maven {
url = 'https://vaadin.com/nexus/content/repositories/vaadin-addons'
}
if (project.ext.jepDir) {
flatDir {
dirs project.ext.jepDir
}
}
}
dependencyManagement {
imports {
mavenBom "com.vaadin:vaadin-bom:${project.vaadinVersion}"
}
}
dependencies {
implementation 'com.vaadin:vaadin-spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-devtools' // for Java live reload / Vaadin development mode
implementation 'com.storedobject.chart:so-charts:3.1.3'
implementation 'com.vladsch.flexmark:flexmark:0.64.0'
implementation 'de.f0rce:ace:3.4.2'
// implementation 'org.vaadin.artur:a-vaadin-helper:1.9.0'
// implementation 'javax.servlet:javax.servlet-api:4.0.1'
implementation 'jakarta.servlet:jakarta.servlet-api:6.0.0'
implementation 'com.pivovarit:throwing-function:1.5.1'
implementation 'one.util:streamex:0.8.1'
if (project.ext.jepDir && project.ext.jepVersion) {
implementation (files "${project.ext.jepDir}/jep-${project.ext.jepVersion}.jar")
}
annotationProcessor 'org.projectlombok:lombok:1.18.26'
compileOnly 'org.projectlombok:lombok:1.18.26'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.26'
testCompileOnly 'org.projectlombok:lombok:1.18.26'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
vaadin {
nodeVersion = 'v18.16.0'
// pnpmEnable = true
requireHomeNodeExec = true
// useGlobalPnpm = false
}
tasks.register 'processPythonCompanyonsPackage', {
dependsOn processResources
group 'companyons'
it.doFirst {
final pythonProjectInputDir = file "${project.rootDir}/src/main/python"
final pythonProjectOutputDir = file "${project.ext.pythonProjectDir}"
if (pythonProjectOutputDir.exists()) {
pythonProjectOutputDir.deleteDir()
}
pythonProjectOutputDir.mkdir()
pythonProjectInputDir.traverse {
final outputPath = file "${pythonProjectOutputDir}/${pythonProjectInputDir.relativePath it}" toPath()
if (it.isDirectory()) {
Files.createDirectory outputPath
} else {
Files.copy it.toPath(), outputPath
}
}
}
}
tasks.register 'uninstallPythonCompanyonsPackage', Exec, {
group 'companyons'
workingDir = project.ext.pythonProjectDir
commandLine = [project.ext.pythonExe, '-m', 'pip', 'uninstall', '--yes', 'companyons']
}
tasks.register 'installPythonCompanyonsPackage', Exec, {
dependsOn processPythonCompanyonsPackage
dependsOn uninstallPythonCompanyonsPackage
group 'companyons'
workingDir = project.ext.pythonProjectDir
commandLine = [project.ext.pythonExe, '-m', 'pip', 'install', '.']
}
bootRun.dependsOn installPythonCompanyonsPackage
tasks.register 'installPythonJediPackage', Exec, {
group 'companyons'
commandLine = [project.ext.pythonExe, '-m', 'pip', 'install', 'jedi']
}
bootRun.dependsOn installPythonJediPackage
tasks.register 'downloadPython', Download, {
group 'companyons'
/* Adapted from https://github.com/michel-kraemer/gradle-download-task#readme
*/
src isWindows ? minicondaDownloadUrl : null
dest buildDir
overwrite false
}
/*
task downloadPip(type: Download) {
}
*/
tasks.register 'installPython', Exec, {
dependsOn downloadPython
// dependsOn downloadPip
group 'companyons'
workingDir = project.buildDir
final basePath = project.buildDir.toPath()
commandLine = [(basePath.resolve minicondaInstaller), '/S', '/NoRegistry=1', "/D=${basePath.resolve 'python'}"]
/* Adapted from https://discuss.gradle.org/t/extracting-single-file-from-zip-using-gradle-task/30486/2
*/
/*
final downloadedPythonZip = buildDir.toPath() resolve pythonZip
from zipTree(downloadedPythonZip)
final pythonDir = ext.pythonDir = buildDir.toPath() resolve 'python'
into pythonDir
*/
}
tasks.register 'installCondaPackages', Exec, {
group 'companyons'
final packages = project.condaPackages.split(/\s+/) findAll { it?.trim() } toList()
commandLine = [project.ext.pythonExe, '-m', 'conda', 'install', '--yes'] + packages
}
tasks.register 'installPipPackages', Exec, {
group 'companyons'
final packages = project.pipPackages.split(/\s+/) findAll { it?.trim() } toList()
commandLine = [project.ext.pythonExe, '-m', 'pip', 'install'] + packages
}
tasks.register 'installPythonPackages', {
group 'companyons'
if (project.condaPackages?.trim()) {
dependsOn installCondaPackages
}
if (project.pipPackages?.trim()) {
dependsOn installPipPackages
}
}
bootRun.dependsOn installPythonPackages
tasks.register 'uninstallJep', Exec, {
group 'companyons'
commandLine = [project.ext.pythonExe, '-m', 'pip', 'uninstall', '--yes', 'jep']
}
tasks.register 'installJep', Exec, {
dependsOn uninstallJep
group 'companyons'
workingDir = rootDir.toPath() resolve 'jep'
commandLine = [project.ext.pythonExe, '-m', 'pip', 'install', '.']
}
tasks.withType Exec configureEach {
environment 'JAVA_HOME', (System.getProperty 'java.home')
onWindows {
if (project.ext.pythonDir) {
environment 'PATH', (String.join File.pathSeparator, [project.ext.pythonDir] + [
// where dependencies of native jep.dll are located
'bin',
'DLLs',
'Library/bin',
'Library/usr/bin',
'Scripts',
].collect { Paths.get project.ext.pythonDir, it toString() } + [(System.getenv 'PATH')])
}
}
}
tasks.withType JavaExec configureEach {
systemProperty 'java.library.path', project.ext.jepDir // where native jep library is located
onWindows {
if (project.ext.pythonDir) {
environment 'PATH', (String.join File.pathSeparator, [project.ext.pythonDir] + [
// where dependencies of native jep.dll are located
'bin',
'DLLs',
'Library/bin',
'Library/usr/bin',
'Scripts',
].collect { Paths.get project.ext.pythonDir, it toString() } + [(System.getenv 'PATH')])
}
}
environment 'PYTHONHOME', project.ext.pythonDir
environment 'PYTHONPATH', project.ext.pythonPath
}
test {
useJUnitPlatform()
}
publishing {
publications {
maven MavenPublication, {
from components.java
}
}
}