-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.gradle
More file actions
1072 lines (882 loc) · 40.3 KB
/
build.gradle
File metadata and controls
1072 lines (882 loc) · 40.3 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Author: Stuart Beesley - February 2026
This is Groovy DSL file
build.gradle: gradle (wrapper) buildfile for Python Moneydance extensions
- can build python extensions (including bundled java/kotlin)
################################################################
WARNING: This uses gradle 9 and IntelliJ IDEA CE 2021.1
As such, gradle features will not run within the IJ app
Use gradle from terminal/command line
IJ2021.1 is the last version to support Jython2.7
################################################################
** REVIEW PROPERTIES IN [ROOT]/gradle.properties file
** SET LOCAL/USER OVERRIDING PROPERTIES in /userconfig/user.gradle.properties
Execute ./gradlew to show usage
./gradlew extensionname to build extension
./gradlew cleanextensionname to cleanup a single extension's build files
./gradlew genKeys to generate private/public signing keyfile(s) - set keypass= first
Notes:
- TO START create/edit "user.gradle.properties" and set "keypass=xxx" and then run "genKeys"
- Python packaging (with precompile) requires python2.7 installed
- By default all java/kotlin debug symbols option are turned on
- java: includes source file name, line numbers, and local variable tables (equivalent to javac -g) - has full debug symbols.
- kotlin: line numbers are always emitted, source mapping is present for stack traces. Parameter/call assertions are on by default.
*/
import org.jetbrains.kotlin.config.KotlinCompilerVersion
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.zip.ZipFile
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.9.21' // plugin version to match Moneydance's build system and also bundled runtime kotlin-stdlib-x.x.x.jar
// use '2.3.10' to upgrade
}
repositories {
mavenCentral()
}
// detect 1.9.x vs 2.x plugin...
def kotlinPluginVersion = plugins.getPlugin("org.jetbrains.kotlin.jvm").class.package.implementationVersion
def isKotlin2 = kotlinPluginVersion?.startsWith("2.")
logger.lifecycle("Kotlin plugin version (${kotlinPluginVersion}) - compiler: ${KotlinCompilerVersion.VERSION}")
// Detect Kotlin 1.9 and run specific commands
if (!isKotlin2) {
// Kotlin 1.9 only: disable classpath snapshotting (removed / not applicable in Kotlin 2.x)
// Moneydance build jars are mutable and order-sensitive; classpath snapshotting
// can cause incorrect incremental rebuilds. Incremental compilation remains enabled.
project.ext["kotlin.incremental.useClasspathSnapshot"] = "false"
}
// Using gradle (wrapper) 9.x, with kotlin plugin 1.9.21 (note final release before kotlin 2.0 is 1.9.25 which would have maximum compatibility with gradle 9)
// - ideally match versions to the Moneydance build system, and bundled runtime jar, but..:
// - latest gradle is fine as this is a separate build system, and we lock the kotlin language / api version to 1.9
// - java / kotlin target versions are set to allow extensions to have maximum possible of backwards compatibility.
// - we set these value here - rather than using "gradle.properties" - as these are not really very user configurable.
ext {
compilerJDK = 21 // JDK version to execute java/kotlin compile, sign, and packaging tasks
targetRelease = 17 // equivalent java --release (source/target/bytecode/API) version
kotlinLangVersion = KotlinVersion.KOTLIN_1_9 // kotlin language version (lock to plugin version) - match to Moneydance bundled kotlin-stdlib-x.x.x.jar
kotlinApiVersion = KotlinVersion.KOTLIN_1_9 // kotlin API version (lock to plugin version) - match to Moneydance bundled kotlin-stdlib-x.x.x.jar
// use KotlinVersion.KOTLIN_2_1 (for example) to upgrade
}
// set flag to let subordinate gradle scripts know that the master script is executing
ext.executingMainBuild = true
apply from: rootProject.file("gradle/usage.gradle.kts")
defaultTasks "verifyConfig"
// Guard against configuration cache being enabled via CLI or user settings.
// This build is not 'configuration-cache' safe (dynamic feature registration, mutable classpaths, JavaExec signing tasks).
// Fail fast here to avoid hard build failures.
// Note: this check triggers a harmless deprecation warning. There seems to be no way to avoid this (perhaps wait for v10)
// if you remove this check, then you will get a hard fail later, if 'configuration-cache=true' as the script executes unsupported statements
// to eliminate the deprecation warning, simply remove this check. //TODO - re-evaluate when using gradle10
if (gradle.startParameter.configurationCacheRequested) {
throw new GradleException("Configuration cache must be disabled in gradle.properties for this build - use: org.gradle.configuration-cache=false")
}
// load user specific properties..
def userConfigDir = "${project.rootDir}/userconfig"
def localPropsFile = file("${userConfigDir}/user.gradle.properties")
if (localPropsFile.exists()) {
def p = new Properties()
localPropsFile.withInputStream { p.load(it) }
p.each { k, v -> project.ext.set(k.toString(), v)}
}
// verify all required properties are set
["lib", "dist", "pythonSrc", "javaForPythonSrc", "privkeyid", "extprivkeyfile", "extpubkeyfile"]
.each {
if (!project.hasProperty(it)) throw new GradleException("Missing gradle.property: $it")
}
/*
* Bridge property names into Gradle variables
*/
def debug = project.findProperty("debug") == "true"
def libPath = project.property("lib")
def distPath = project.property("dist")
def pythonSrcPath = project.property("pythonSrc")
def javaForPythonSrcPath = project.property("javaForPythonSrc")
def mdbuildlibs = project.findProperty("md_ext_lib_dir") // note: this is an optional path that will override where the std set of build libs are found
def extPrivKeyFile = project.property("extprivkeyfile")
def extPubKeyFile = project.property("extpubkeyfile")
def privKeyID = project.property("privkeyid")
def keyPassValue = project.findProperty("keypass") // don't force keypass= to exist
def allowPushIKOpen = project.findProperty("allowPushIKOpen") == "true" // when true allows push to IK's Open project dirs
def IKOpenPushDir = project.findProperty("IKOpenPushDir") // when using allowPushIKOpen, specify the full push path
// export for imported gradle files
ext.debug = debug
ext.libPath = libPath
ext.distPath = distPath
ext.pythonSrcPath = pythonSrcPath
ext.javaForPythonSrcPath = javaForPythonSrcPath
ext.extPrivKeyFile = extPrivKeyFile
ext.extPubKeyFile = extPubKeyFile
ext.privKeyID = privKeyID
ext.keyPassValue = keyPassValue
ext.allowPushIKOpen = allowPushIKOpen
ext.IKOpenPushDir = IKOpenPushDir
ext.moneydanceChecked = false // we use this to check the classpath for moneydance jar class only once
// common keypass, priv/pub file(s) missing validation code block
ext.requireSigningInputs = {
if (!keyPassValue?.toString()?.trim()) throw new GradleException("\n*****\nERROR - keypass must be set in: ${userConfigDir}/user.gradle.properties\n*****\n")
if (!file(extPrivKeyFile).exists()) throw new GradleException("\n*****\nERROR - Missing private key file: ${extPrivKeyFile} (run genKeys)\n*****\n")
// if (!file(extPubKeyFile).exists()) throw new GradleException("\n*****\nERROR - Missing public key file: ${extPubKeyFile} (run genKeys)\n*****\n")
}
// common list zip contents block
ext.listZipContents = { File zipFile, String label ->
if (!debug) return
if (!zipFile.exists() || zipFile.length() == 0) return
logger.lifecycle("\n\n---- ZIP CONTENTS: ${label} ----")
def zf = new ZipFile(zipFile)
zf.entries().toList()
.sort { it.name }
.each { logger.lifecycle(it.name) }
zf.close()
logger.lifecycle("---------------------------------------")
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Build a list of Moneydance jars for the classpath – shared by compile + sign (order matters)
// Note: we prefer the jars in md_ext_lib_dir when specified, otherwise the 'normal' jars in the lib folder
////////////////////////////////////////////////////////////////////////////////////////////////////////////
def mdJars = []
if (mdbuildlibs) {
mdJars += [
files("${mdbuildlibs}/moneydance.jar"), // attempt to load user specified moneydance jar first
fileTree("${mdbuildlibs}") { // then load any more user jar files, but not extadmin
include("*.jar")
exclude("moneydance.jar")
exclude("extadmin.jar")
},
files("${mdbuildlibs}/extadmin.jar") // since extadmin has a StringUtils, it needs to come after moneydance.jar
]
}
mdJars += [
files( // now load all jars needed by extensions
"${libPath}/kotlin-stdlib-1.9.21.jar",
"${libPath}/moneydance-dev.jar",
"${libPath}/moneydance-private.jar", // if you absolutely MUST use something that isn't in the exposed MD API then put a moneydance.jar file from Moneydance 2015 or higher here. But, please don't
"${libPath}/extadmin.jar", // since extadmin has a StringUtils, it needs to come after moneydance.jar
"${libPath}/mdpython.jar" // used for Python pre-compile
)
]
ext.mdJars = mdJars // export for imported gradle files
configurations {
mdClasspath {
canBeConsumed = false
canBeResolved = true
}
compileOnly.extendsFrom(mdClasspath)
}
dependencies {
mdJars.each { add("mdClasspath", it) }
}
// define common checker to detect moneydance.jar file(s) on classpath
// specifically look for a known model class, and detect not found, or where multiple found
def requireMoneydanceClass = { Map args = [:] ->
def classPath = args.classPath ?: "com/infinitekind/moneydance/model/AccountBook.class"
def failWhenNotFound = args.failWhenNotFound != false
def showResults = args.showResults == true
def foundIn = []
def ordered = configurations.mdClasspath.incoming.files.toList()
ordered.each { jar ->
if (jar.exists()) {
def zip = new ZipFile(jar)
if (zip.getEntry(classPath as String) != null) {
foundIn << jar
}
zip.close()
}
}
if (failWhenNotFound && foundIn.isEmpty()) { throw new GradleException("Required Moneydance class ${classPath} not found on mdClasspath") }
if (showResults) {
logger.lifecycle("------------------")
logger.lifecycle("Classpath resolution order:")
ordered.eachWithIndex { jar, i ->
logger.lifecycle(" ${i + 1}. ${jar.absolutePath}${if (jar.exists()) "" else " (MISSING)"}")
}
logger.lifecycle("")
logger.lifecycle("Found ${classPath} in ${foundIn.size()} jar(s):")
foundIn.each { jar ->
logger.lifecycle(" -> ${jar.absolutePath}")
}
if (foundIn.isEmpty()) logger.error("Required Moneydance class: ${classPath} NOT found on classpath")
if (foundIn.size() > 1) logger.error("Multiple versions of required Moneydance class: ${classPath} found on classpath")
logger.lifecycle("------------------")
}
return foundIn.size()
}
ext.requireMoneydanceClass = requireMoneydanceClass
////////////////////// end building of mdjars list /////////////////////////////////////////////////////////////////////
// --------------------
// Java compiler config
// --------------------
java {
toolchain {
languageVersion = JavaLanguageVersion.of(compilerJDK) // JDK version used to execute java compile, package, sign tasks
}
}
tasks.withType(JavaCompile).configureEach {
options.release = targetRelease // specify the java --release (source/target/bytecode/API) version
options.encoding = "UTF-8" // utf8 encoding for all platforms
options.incremental = true // performance - incremental build - default in modern Gradle
options.compilerArgs += ["-Xlint:all"] // enable all compiler warnings (no impact on build)
if (!debug) {
options.compilerArgs += ["-Xlint:-options"] // disable compiler option warnings (no impact on build)
}
// some potentially useful compiler options
//options.debug = true // debugging info - default on - equivalent to javac -g
//options.debugOptions.debugLevel = "source,lines,vars" // debugging info - default on - equivalent to javac -g
//options.compilerArgs += ["-Werror"] // treat warnings as errors
doFirst {
if (!moneydanceChecked) {
requireMoneydanceClass()
moneydanceChecked = true
}
}
}
////////////////////// end java compiler config ////////////////////////////////////////////////////////////////////////
// ----------------------
// Kotlin compiler config
// ----------------------
kotlin {
jvmToolchain(compilerJDK) // JDK version used to execute java compile, package, sign tasks
}
tasks.withType(KotlinCompile).configureEach {
compilerOptions {
languageVersion.set(kotlinLangVersion) // kotlin language version
apiVersion.set(kotlinApiVersion) // kotlin API version
jvmTarget.set(JvmTarget.valueOf("JVM_${targetRelease}")) // specify the java --release (source/target/bytecode/API) version
if (isKotlin2) {
freeCompilerArgs.add("-jvm-default=no-compatibility") // interface default methods (plug in 2.x onwards)
} else {
freeCompilerArgs.add("-Xjvm-default=all") // interface default methods (plug in 1.9.x)
}
freeCompilerArgs.add("-Xuse-fast-jar-file-system") // faster incremental builds
// some potentially useful compiler options
//freeCompilerArgs.add("-Xdebug") // compiler diagnostics only (NOT runtime debugging)
//freeCompilerArgs.add("-Xno-param-assertions") // disable parameter null checks
//freeCompilerArgs.add("-Xno-call-assertions") // disable call-site null checks
//jvmTarget.set(JvmTarget.JVM_nn) // jvmTarget defaults to the configured Kotlin JVM toolchain
}
doFirst {
if (!moneydanceChecked) {
requireMoneydanceClass()
moneydanceChecked = true
}
}
}
////////////////////// end kotlin compiler config //////////////////////////////////////////////////////////////////////
// setup JavaExec tasks
tasks.withType(JavaExec).configureEach {
javaLauncher.set(
javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(compilerJDK) // JDK version used to execute java compile, package, sign tasks
}
)
systemProperty "file.encoding", "UTF-8"
systemProperty "moneydance_key_pass", keyPassValue
}
////////////////////// end JavaExec config /////////////////////////////////////////////////////////////////////////////
// turn off tasks we are not using
tasks.named("jar").configure { enabled = false }
tasks.named("test").configure { enabled = false }
tasks.named("build").configure { enabled = false }
tasks.named("assemble").configure { enabled = false }
tasks.named("classes").configure { enabled = false }
////////////////////// end turn-off unused tasks ///////////////////////////////////////////////////////////////////////
// add to gradle's inbuilt clean task to also clean up the dist folder and any bit of the python compile left behind.
// no need to deal with build as gradle handles this...
tasks.named("clean", Delete).configure {
delete(distPath)
// delete any legacy ANT build folders
delete(file("${pythonSrcPath}/build"))
doFirst {
// python generated artefacts
// due to file scan, put inside doFirst...
file(pythonSrcPath).eachDir { pySrcDir ->
delete(fileTree(pySrcDir) {
include("**/*.pyc")
include("**/*.class")
})
}
}
}
// create task to verify python installation
def pythonExec = project.findProperty("python-executable") ?: "python2.7"
tasks.register("verifyPython27") {
group = "TOOLS"
description = "Verify python installation / version"
doLast {
def proc = ["${pythonExec}", "--version"].execute()
proc.waitFor()
if (proc.exitValue() != 0 || !proc.err.text.contains("Python 2.7")) {
throw new GradleException("Python 2.7 is required. '${pythonExec}' not found, or not Python 2.7")
}
logger.lifecycle("validated python2.7 installed")
}
}
////////////////////// end verifyPython27 //////// /////////////////////////////////////////////////////////////////////
tasks.register("ensureUserConfig") {
group = "TOOLS"
description = "Ensure ${userConfigDir} folder exists (create if missing, does nothing if already exists)"
outputs.dir("${userConfigDir}")
doLast {
def dir = file("${userConfigDir}")
if (!dir.exists()) {
dir.mkdirs()
logger.lifecycle("[INFO] ${userConfigDir} folder created...")
}
}
}
tasks.register("ensureUserProperties") {
group = "TOOLS"
description = "Create new user.gradle.properties file (does nothing if already exists)"
outputs.file(file("${userConfigDir}/user.gradle.properties"))
dependsOn("ensureUserConfig")
doLast {
def dir = file("${userConfigDir}")
def propsFile = new File(dir, "user.gradle.properties")
if (!propsFile.exists()) {
propsFile.text =
"# gradle.user.properties file for building Moneydance extensions\n" +
"#keypass=your_genkeys_passphrase(set here then run genKeys)\n" +
"#md_ext_lib_dir=path_to_your_own_set_of_moneydance_jars(optional)"
logger.lifecycle("[INFO] user.gradle.properties file created...")
}
}
}
// task to verify settings / validate key paths...
tasks.register("verifyConfig") {
group = "INFO"
description = "Show usage, display settings, validate paths"
dependsOn("ensureUserConfig", "printUsage")
doLast {
logger.lifecycle("")
logger.lifecycle("Verify Config: configuration + path checks")
logger.lifecycle("------------------------------------------")
logger.lifecycle("")
logger.lifecycle("Gradle version = ${gradle.gradleVersion}")
logger.lifecycle("Kotlin plugin = ${KotlinCompilerVersion.VERSION}")
logger.lifecycle("CompilerJDK = ${compilerJDK}")
logger.lifecycle("target release = ${targetRelease}")
logger.lifecycle("kotlin language = ${kotlinLangVersion}")
logger.lifecycle("kotlin API version = ${kotlinApiVersion}")
logger.lifecycle("")
logger.lifecycle("debug = ${debug}")
logger.lifecycle("pythonExec = ${pythonExec}")
if (mdbuildlibs) {
logger.lifecycle("md_ext_lib_dir = ${mdbuildlibs}")
} else {
logger.lifecycle("optional md_ext_lib_dir (library override) <not specified>")
}
logger.lifecycle("lib = ${libPath}")
logger.lifecycle("dist = ${distPath}")
logger.lifecycle("pythonSrcPath = ${pythonSrcPath}")
logger.lifecycle("extprivkeyfile = ${extPrivKeyFile}")
logger.lifecycle("extpubkeyfile = ${extPubKeyFile}")
logger.lifecycle("javaForPythonSrcPath = ${javaForPythonSrcPath}")
logger.lifecycle("allowPushIKOpen = ${allowPushIKOpen}")
logger.lifecycle("IKOpenPushDir = ${IKOpenPushDir}")
def isPassBlank = !keyPassValue?.toString()?.trim()
if (!isPassBlank) {
logger.lifecycle("keypass = <set>")
} else {
logger.warn("keypass = <not set>")
}
def didntExist = [:]
def mustExist = [
"lib" : libPath,
"python src" : pythonSrcPath,
"javaForPythonSrc" : javaForPythonSrcPath,
"extprivkeyfile" : extPrivKeyFile,
"extpubkeyfile" : extPubKeyFile
]
mustExist.each { name, path ->
def f = (path) ? file(path) : null
if (!f || !f.exists()) {
didntExist[name] = f
logger.error("... MISSING: ${name}: ${f}")
} else {
logger.lifecycle("... EXISTS: ${name}: ${f}")
}
}
def optExist = [
"dist" : distPath,
"md_ext_lib_dir" : mdbuildlibs,
"IKOpenPushDir" : IKOpenPushDir
]
optExist.each { name, path ->
def f = (path) ? file(path) : null
if (f && f.exists()) {
logger.lifecycle("... OPTIONAL EXISTS: ${name}: ${f}")
} else {
logger.lifecycle("... OPTIONAL MISSING: ${name}: ${f}")
}
}
logger.lifecycle("------------------")
logger.lifecycle("configuration-cache property = ${providers.gradleProperty("org.gradle.configuration-cache").orNull ?: "<not set>"}")
logger.lifecycle("parallelProjectExecutionEnabled = ${gradle.startParameter.parallelProjectExecutionEnabled}")
logger.lifecycle("Kotlin incremental compilation enabled = ${providers.gradleProperty("kotlin.incremental").orNull != "false"}")
logger.lifecycle("Kotlin classpath snapshotting enabled = ${providers.gradleProperty("kotlin.incremental.useClasspathSnapshot").orNull == "true"}")
requireMoneydanceClass(failWhenNotFound: false, showResults: true)
if (isPassBlank) {
logger.error("\n")
logger.error("#################################")
logger.error("## Please set keypass property ##")
logger.error("#################################")
logger.error("\n")
}
if (!didntExist.isEmpty()) { throw new GradleException("Missing required paths: ${didntExist}") }
}
}
////////////////////// end verifyConfig ////////////////////////////////////////////////////////////////////////////////
// task that can clean extension's build files
def registerCleanTask = { String feature ->
tasks.register("clean${feature}", Delete) {
group = "CLEAN EXTENSION"
description = "Clean built artifacts for extension: ${feature}"
// Java/Kotlin artifact (i.e. specifically .mxt and anything else with this extension's name)
delete(fileTree(dir: distPath, includes: ["${feature}.*", "s-${feature}.*"]))
// compiled java/kotlin class outputs
delete(
layout.buildDirectory.dir("classes/java/pythonBundled"),
layout.buildDirectory.dir("classes/kotlin/pythonBundled"),
layout.buildDirectory.dir("resources/pythonBundled")
)
// Python generated artefacts
delete(fileTree("${pythonSrcPath}/${feature}") {
include("**/*.pyc")
include("**/*.class")
})
// remove python staging
delete(layout.buildDirectory.dir("tmp/staging/${feature}"))
doFirst { if (debug) logger.lifecycle("${path} - cleaning ${feature}") }
}
}
////////////////////// end clean extension /////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// task that can (re)generate keys for signing mxt files
// NOTE: please only run this from a terminal session
// if you need to type the key using keyboard
////////////////////////////////////////////////////////
tasks.register("genKeys", JavaExec) {
group = "GENKEYS"
description = "Generate Moneydance extension signing keys"
outputs.upToDateWhen { false }
outputs.files(
extPrivKeyFile,
extPubKeyFile
)
classpath = configurations.mdClasspath
mainClass.set("com.moneydance.admin.KeyAdmin")
args(
"genkey",
extPrivKeyFile,
extPubKeyFile
)
standardInput = System.in // <-- REQUIRED for keyboard input
doFirst {
def isPassBlank = !keyPassValue?.toString()?.trim()
logger.lifecycle("")
logger.lifecycle("########################################################################")
logger.lifecycle("# genKeys running....")
logger.lifecycle("# - you may be prompted to overwrite existing key file(s)")
if (!isPassBlank) {
logger.lifecycle("# - your passphrase as defined in ${localPropsFile} will be used")
logger.lifecycle("# - existing passphrase: ******")
} else {
logger.lifecycle("# - you will need to enter a passphrase to create new keyfiles")
logger.lifecycle("# - please save this passphrase in file ${localPropsFile}")
logger.lifecycle("# using the format keypass=yourpassphrase")
}
logger.lifecycle("########################################################################")
def priv = file(extPrivKeyFile)
def pub = file(extPubKeyFile)
if (priv.exists() || pub.exists()) {
logger.lifecycle("")
logger.lifecycle("-------------------------------")
print "\u0007"; System.out.flush() // beep-beep
print "Keys exist. Overwrite? [y/N]: "; System.out.flush() // ensure the message shows...
def answer = new BufferedReader(new InputStreamReader(System.in)).readLine()
if (!answer?.equalsIgnoreCase("y")) {
throw new GradleException("Key files already exist; please remove before regeneration; aborting")
}
}
}
doLast {
if (!file(extPrivKeyFile).exists()) throw new GradleException("Private key not created")
if (!file(extPubKeyFile).exists()) throw new GradleException("Public key not created")
}
}
////////////////////// end genKeys /////////////////////////////////////////////////////////////////////////////////////
// task to ensure dist folder exists (or create it)
tasks.register("ensureDist") {
group = "TOOLS"
description = "Makes the dist folder if it doesn't exist"
outputs.dir(distPath)
doLast {
file(distPath).mkdirs()
if (debug) logger.lifecycle("Ensured 'dist' folder exists (created if missing)")
}
}
// setup separate task to build the bundled java/kotlin code
tasks.register("javaForPython") {
group = "BUILD EXTENSION"
description = "Compile bundled java/kotlin for python extensions"
dependsOn(sourceSets.pythonBundled.classesTaskName)
mustRunAfter("clean", "cleanJavaForPython")
}
tasks.register("cleanJavaForPython", Delete) {
group = "CLEAN EXTENSION"
description = "Clean compiled bundled java/kotlin for python extensions"
delete(
layout.buildDirectory.dir("classes/java/pythonBundled"),
layout.buildDirectory.dir("classes/kotlin/pythonBundled"),
layout.buildDirectory.dir("resources/pythonBundled")
)
}
////////////////////// end genKeys /////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// python specific tasks:
// - cleanup
// - verify python environment
// - CPython: python -m py_compile <feature>.py
// - Jython: java org.python.util.jython compileall
// - define python feature modules
////////////////////////////////////////////////////////
def registerPythonPrecompile = { String feature, String pythonBase ->
//////////////////////////////////////////////////////////////////////////////////////////////
// PRE-COMPILING CPython to create CPython bytecode .pyc
// note: required when you getting 'Module or method too large' issues with large python files
//////////////////////////////////////////////////////////////////////////////////////////////
tasks.register("py_compile_${feature}", Exec) {
group = "other"
description = "Pre-compile CPython bytecode (.pyc) for ${feature}"
ignoreExitValue = false // trap error to cause fail on error
workingDir file(pythonBase)
inputs.file(file("${pythonBase}/${feature}.py"))
outputs.file(file("${pythonBase}/${feature}.pyc"))
outputs.upToDateWhen { false } // always repackage the whole extension - too difficult to use gradle's incremtal system
doFirst {
if (debug) logger.lifecycle("${path} START")
delete(
file("${pythonBase}/${feature}.pyc"),
file("${pythonBase}/${feature}\$py.class")
)
}
// commandLine "sh", "-c", "${pythonExec} -m py_compile ${feature}.py"
environment "PYTHONIOENCODING", "UTF-8"
commandLine pythonExec, "-m", "compileall", "-f", "${feature}.py" // compileall is more verbose, and -f forces recompile (or just py_compile file.py)
doLast { if (debug) logger.lifecycle("${path} END") }
}
tasks.named("py_compile_${feature}") { dependsOn("verifyPython27") } // verify python2.7 installed
////////////////////////////////////////////////////////
// PRE-COMPILING Jython to create $py.class
////////////////////////////////////////////////////////
//def mdPythonJar = file("${project.rootDir}/JythonLIB/jython-standalone-2.7.4.jar")
def mdPythonJar = file("${libPath}/mdpython.jar")
tasks.register("jython_compile_${feature}", JavaExec) {
group = "other"
description = "Pre-compile Jython bytecode (\$py.class) for ${feature}"
inputs.file("${pythonBase}/${feature}.py")
inputs.file("${pythonBase}/${feature}.pyc")
outputs.file(file("${pythonBase}/${feature}\$py.class"))
outputs.upToDateWhen { false } // always repackage the whole extension - too difficult to use gradle's incremtal system
classpath = files(mdPythonJar)
mainClass.set("org.python.util.jython")
args(
"-c",
"import compileall; compileall.compile_file('${pythonBase}/${feature}.py', force=True)"
)
doFirst { if (debug) logger.lifecycle("${path} START") }
doLast { if (debug) logger.lifecycle("${path} END") }
}
// enforce the correct sequence...
tasks.named("jython_compile_${feature}") {
mustRunAfter("py_compile_${feature}")
}
////////////////////////////////////////////
// join python compile processes into one...
////////////////////////////////////////////
tasks.register("precompile_${feature}") {
group = "other"
description = "Pre-compile Python (CPython + Jython) for ${feature}"
dependsOn(
"py_compile_${feature}",
"jython_compile_${feature}"
)
}
}
/////////////////////////////////////////////////
// execute Moneydance's python packaging routines
/////////////////////////////////////////////////
def registerPackagePythonExtension = { String feature, String pythonBase, boolean precompile ->
def stagingDir = layout.buildDirectory.dir("tmp/staging/${feature}").get().asFile
tasks.register("package_python_${feature}", JavaExec) {
group = "other"
description = "package python scripts into a signed extension mxt file for ${feature}"
outputs.file("${distPath}/${feature}.mxt")
outputs.upToDateWhen { false }
doFirst {
if (debug) logger.lifecycle("${path} START")
requireSigningInputs()
if (!moneydanceChecked) {
requireMoneydanceClass()
moneydanceChecked = true
}
// Validate mandatory extension files
def requiredFiles = [
file("${pythonBase}/meta_info.dict"),
file("${pythonBase}/script_info.dict"),
file("${pythonBase}/${feature}.py")
]
requiredFiles.each { f ->
if (!f.exists() || f.length() == 0) { throw new GradleException("Missing required extension file: ${f}") }
}
// reset staging dir
delete(stagingDir)
stagingDir.mkdirs()
// stage python files
copy {
from(pythonBase)
exclude("*.pyi")
exclude("*.mxt")
exclude("**/*.kt")
exclude("**/*.java")
exclude("${feature}_version_requirements.dict")
into(stagingDir)
}
if (feature == "toolbox") {
copy {
from("${pythonSrcPath}/useful_scripts")
include("ofx_*.py")
into(stagingDir)
}
}
// copy install-readme.txt
copy {
from(pythonSrcPath)
include("install-readme.txt")
into(stagingDir)
}
// stage bundled java/kotlin class files (if any)
if (bundledFeatureSources.containsKey(feature)) {
def classPatterns = bundledFeatureSources[feature].collect { name -> "**/${name}*.class" }
copy {
from(sourceSets.pythonBundled.output.classesDirs)
include(classPatterns)
exclude("META-INF/**")
into(stagingDir)
}
}
}
if (precompile) { dependsOn("precompile_${feature}") }
classpath = configurations.mdClasspath
mainClass.set("com.moneydance.admin.PythonExtensionPackager")
// package from staging dir instead of source
args(
extPrivKeyFile,
privKeyID,
feature,
stagingDir.absolutePath,
distPath
)
doLast {
// delete staging dir after packaging
delete(stagingDir)
def out = file("${distPath}/${feature}.mxt")
if (!out.exists() || out.length() == 0) { throw new GradleException("Python extension not created: ${out}") }
listZipContents(out, "${feature}.mxt")
if (debug) logger.lifecycle("${path} END")
}
}
}
ext.bundledFeatureSources = [:] // track which extensions are bundling extra kt/java files
def registerPythonModule = { String feature, boolean precompile = false, List<String> bundledSourcePatterns = [], boolean createZip = true ->
def fullPath = "${pythonSrcPath}/$feature"
if (bundledSourcePatterns && !bundledSourcePatterns.isEmpty()) {
bundledFeatureSources[feature] = bundledSourcePatterns
}
if (precompile) {
registerPythonPrecompile(feature, fullPath)
}
registerPackagePythonExtension(feature, fullPath, precompile)
if (bundledSourcePatterns && !bundledSourcePatterns.isEmpty()) {
tasks.named("package_python_${feature}") {
dependsOn("javaForPython")
}
}
if (createZip) {
tasks.register("zip_${feature}", Zip) {
group = "other"
description = "Create zip bundle for ${feature}"
dependsOn("ensureDist", "package_python_${feature}")
archiveFileName.set("${feature}.zip")
destinationDirectory.set(file(distPath))
from(file("${distPath}/${feature}.mxt"))
from(fullPath) { include("*.txt", "*.pdf") }
from(pythonSrcPath) { include("install-readme.txt") }
doFirst { if (debug) logger.lifecycle("${path} START") }
doLast { if (debug) logger.lifecycle("${path} END") }
}
}
tasks.register("cleanup_python_${feature}", Delete) {
group = "other"
description = "Delete compiled python bytecode and classes"
delete(fileTree(fullPath) {
include("**/*.pyc")
include("**/*.class")
})
}
if (precompile) {
tasks.named("package_python_${feature}") { dependsOn("precompile_${feature}") }
}
tasks.register(feature) {
group = "BUILD EXTENSION"
description = "Build python extension: ${feature}"
dependsOn("ensureDist")
mustRunAfter("clean", "cleanJavaForPython", "clean${feature}")
if (precompile) dependsOn("precompile_${feature}")
dependsOn("package_python_${feature}")
if (createZip) dependsOn("zip_${feature}")
doLast { logger.lifecycle("EXTENSION BUILD (python) '${feature}' ${path} END") }
}
}
sourceSets {
pythonBundled {
java.srcDir("${javaForPythonSrcPath}")
kotlin.srcDir("${javaForPythonSrcPath}")
}
}
dependencies { mdJars.each { add("pythonBundledCompileOnly", it) } }
///////////////// end python setup /////////////////////////////////////////////////////////////////////////////////////
// other tasks
tasks.register("useful_scripts", Zip) {
group = "BUILD EXTENSION"
description = "Create standalone useful_scripts bundle (no mxt)"
dependsOn("ensureDist")
archiveFileName.set("useful_scripts.zip")
destinationDirectory.set(file(distPath))
from("${pythonSrcPath}/useful_scripts") {
include("*.py", "*.pyc", "*.txt", "*.pdf", "*.csv")
}
from(pythonSrcPath) { include("install-readme.txt") }
doFirst { if (debug) logger.lifecycle("${path} START") }
doLast { if (debug) logger.lifecycle("${path} END") }
}
// register tasks to allow push to IK's Open project folders - don't do this unless you really know what you are doing!
if (allowPushIKOpen) {
if (!IKOpenPushDir) { throw new GradleException("ERROR: allowPushIKOpen=true but IKOpenPushDir not set") }
def pushBase = file(IKOpenPushDir)
if (!pushBase.exists() || !pushBase.isDirectory()) { throw new GradleException("ERROR: IKOpenPushDir is not a valid directory: ${pushBase}") }
// Safety guard — optional but recommended
if (!pushBase.absolutePath.endsWith("python_scripts")) { throw new GradleException("IKOpenPushDir must point to a python_scripts folder") }
def pushTasks = []
def registerPushTask = { String feature, String bundleJava = null ->
def taskName = "push${feature}"
pushTasks += taskName
tasks.register(taskName) {
group = "PUSH EXTENSION"
description = "Push '${feature}' source to IK's Open project"
dependsOn(feature)
doFirst { logger.lifecycle("PUSH START: ${feature}") }
doLast {
def featureDir = new File(pushBase, feature)
delete(featureDir)
featureDir.mkdirs()
copy {
from("${pythonSrcPath}/${feature}")
include("*.py", "*.txt", "*.dict", "*.pdf", "*.csv", "*.png")
exclude("${feature}_version_requirements.dict")
into(featureDir)
}
if (bundledFeatureSources.containsKey(feature)) {
delete(fileTree(dir: featureDir, includes: ["**/*.java", "**/*.kt"]))
copy {
from(javaForPythonSrcPath)
include(bundledFeatureSources[feature].collect { "**/${it}.kt" })
include(bundledFeatureSources[feature].collect { "**/${it}.java" })
into(featureDir)
}
}
if (feature != "extension_tester") {
copy {
from(pythonSrcPath)
include("install-readme.txt")
into(featureDir)
}
}
if (feature == "toolbox") {
copy {
from("${pythonSrcPath}/useful_scripts")
include("ofx_*.py")
into(featureDir)
}
}
logger.lifecycle("PUSH END: ${feature}")
}
}
}
registerPushTask("toolbox")
registerPushTask("extract_data")
registerPushTask("list_future_reminders")
registerPushTask("net_account_balances")
registerPushTask("security_performance_graph")
registerPushTask("accounts_categories_mega_search_window")
registerPushTask("extension_tester")
registerPushTask("useful_scripts")