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