aboutsummaryrefslogtreecommitdiffstats
path: root/buildSrc/src/main/kotlin/tools/refinery/gradle/frontend-worktree.gradle.kts
diff options
context:
space:
mode:
Diffstat (limited to 'buildSrc/src/main/kotlin/tools/refinery/gradle/frontend-worktree.gradle.kts')
-rw-r--r--buildSrc/src/main/kotlin/tools/refinery/gradle/frontend-worktree.gradle.kts87
1 files changed, 87 insertions, 0 deletions
diff --git a/buildSrc/src/main/kotlin/tools/refinery/gradle/frontend-worktree.gradle.kts b/buildSrc/src/main/kotlin/tools/refinery/gradle/frontend-worktree.gradle.kts
new file mode 100644
index 00000000..3225a1b1
--- /dev/null
+++ b/buildSrc/src/main/kotlin/tools/refinery/gradle/frontend-worktree.gradle.kts
@@ -0,0 +1,87 @@
1package tools.refinery.gradle
2
3import java.io.FileInputStream
4import java.io.FileNotFoundException
5import java.io.FileOutputStream
6import java.util.Properties
7
8plugins {
9 id("tools.refinery.gradle.internal.frontend-conventions")
10}
11
12val yarn1Version = providers.gradleProperty("frontend.yarn1Version")
13
14frontend {
15 yarnGlobalInstallScript.set(yarn1Version.map { version -> "install -g yarn@$version" })
16 yarnInstallScript.set(frontend.yarnVersion.map { version -> "set version $version --only-if-needed" })
17 installScript.set(provider {
18 if (project.hasProperty("ci")) "install --immutable --inline-builds" else "install"
19 })
20}
21
22val frontendPropertiesFile = frontend.nodeInstallDirectory.map { dir -> "$dir/frontend.properties" }
23
24fun readFrontendProperties(): Properties {
25 val props = Properties()
26 try {
27 FileInputStream(frontendPropertiesFile.get()).use { inputStream ->
28 props.load(inputStream)
29 }
30 } catch (ignored: FileNotFoundException) {
31 // Ignore missing file.
32 }
33 return props
34}
35
36fun getFrontendProperty(propertyName: String): String? {
37 val props = readFrontendProperties()
38 return props[propertyName]?.toString()
39}
40
41fun putFrontedProperty(propertyName: String, propertyValue: String) {
42 val props = readFrontendProperties()
43 props[propertyName] = propertyValue
44 FileOutputStream(frontendPropertiesFile.get()).use { outputStream ->
45 props.store(outputStream, "generated by refinery-frontend-worktree")
46 }
47}
48
49tasks {
50 installNode {
51 onlyIf {
52 getFrontendProperty("installedNodeVersion") != frontend.nodeVersion.get()
53 }
54 doLast {
55 putFrontedProperty("installedNodeVersion", frontend.nodeVersion.get())
56 }
57 }
58
59 installYarnGlobally {
60 onlyIf {
61 getFrontendProperty("installedYarn1Version") != yarn1Version.get()
62 }
63 doLast {
64 putFrontedProperty("installedYarn1Version", yarn1Version.get())
65 }
66 outputs.dir(frontend.nodeInstallDirectory.map { dir -> "$dir/lib/node_modules/yarn" })
67 }
68
69 installYarn {
70 outputs.file(frontend.yarnVersion.map { version -> ".yarn/releases/yarn-$version.cjs" })
71 }
72
73 installFrontend {
74 inputs.files("package.json", "yarn.lock")
75 outputs.files(".pnp.cjs", ".pnp.loader.mjs")
76 }
77
78 register("clobberFrontend", Delete::class) {
79 delete(frontend.nodeInstallDirectory)
80 delete(".yarn/cache")
81 delete(".yarn/install-state.gz")
82 delete(".yarn/sdks")
83 delete(".yarn/unplugged")
84 delete(".pnp.cjs")
85 delete(".pnp.loader.mjs")
86 }
87}