diff options
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.kts | 87 |
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 @@ | |||
1 | package tools.refinery.gradle | ||
2 | |||
3 | import java.io.FileInputStream | ||
4 | import java.io.FileNotFoundException | ||
5 | import java.io.FileOutputStream | ||
6 | import java.util.Properties | ||
7 | |||
8 | plugins { | ||
9 | id("tools.refinery.gradle.internal.frontend-conventions") | ||
10 | } | ||
11 | |||
12 | val yarn1Version = providers.gradleProperty("frontend.yarn1Version") | ||
13 | |||
14 | frontend { | ||
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 | |||
22 | val frontendPropertiesFile = frontend.nodeInstallDirectory.map { dir -> "$dir/frontend.properties" } | ||
23 | |||
24 | fun 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 | |||
36 | fun getFrontendProperty(propertyName: String): String? { | ||
37 | val props = readFrontendProperties() | ||
38 | return props[propertyName]?.toString() | ||
39 | } | ||
40 | |||
41 | fun 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 | |||
49 | tasks { | ||
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 | } | ||