aboutsummaryrefslogtreecommitdiffstats
path: root/buildSrc/src/main/groovy/refinery-frontend-worktree.gradle
blob: 1d239fd532abbbe05cc5a3e04bbe4af3cb0bc611 (plain) (blame)
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
plugins {
	id 'refinery-frontend-conventions'
}

frontend {
	yarnGlobalInstallScript = "install -g yarn@${project.ext['frontend.yarn1Version']}"
	yarnInstallScript = "set version ${frontend.yarnVersion.get()} --only-if-needed"
	if (project.hasProperty('ci')) {
		installScript = 'install --immutable --inline-builds'
	} else {
		installScript = 'install'
	}
}

ext.frontedPropertiesFile = "${frontend.nodeInstallDirectory.get()}/frontend.properties"

String getFrontendProperty(String propertyName) {
	FileInputStream inputStream = null
	Properties props = new Properties()
	try {
		inputStream = new FileInputStream(frontedPropertiesFile)
		props.load(inputStream)
	} catch (IOException ignored) {
		return null
	} finally {
		if (inputStream != null) {
			inputStream.close()
		}
	}
	return props.get(propertyName)
}

void putFrontedProperty(String propertyName, String propertyValue) {
	FileInputStream inputStream = null
	Properties props = new Properties()
	try {
		inputStream = new FileInputStream(frontedPropertiesFile)
		props.load(inputStream)
	} catch (FileNotFoundException ignored) {
		// Use an empty Properties object instead
	} finally {
		if (inputStream != null) {
			inputStream.close()
		}
	}
	props.put(propertyName, propertyValue)
	FileOutputStream outputStream = null
	try {
		outputStream = new FileOutputStream(frontedPropertiesFile)
		props.store(outputStream, null)
	} catch (IOException ignored) {
	} finally {
		if (outputStream != null) {
			outputStream.close()
		}
	}
}

tasks.named('installNode') {
	onlyIf {
		getFrontendProperty('installedNodeVersion') != frontend.nodeVersion.get()
	}
	doLast {
		putFrontedProperty('installedNodeVersion', frontend.nodeVersion.get())
	}
}

tasks.named('installYarnGlobally') {
	onlyIf {
		getFrontendProperty('installedYarn1Version') != project.ext['frontend.yarn1Version']
	}
	doLast {
		putFrontedProperty('installedYarn1Version', project.ext['frontend.yarn1Version'])
	}
	outputs.dir "${frontend.nodeInstallDirectory.get()}/lib/node_modules/yarn"
}

tasks.named('installYarn') {
	outputs.file ".yarn/releases/yarn-${frontend.yarnVersion.get()}.cjs"
}

tasks.named('installFrontend') {
	inputs.files('package.json', 'yarn.lock')
	outputs.files('.pnp.cjs', '.pnp.loader.mjs')
}

tasks.register('clobberFrontend', Delete) {
	delete frontend.nodeInstallDirectory.get()
	delete '.yarn/cache'
	delete '.yarn/install-state.gz'
	delete '.yarn/sdks'
	delete '.yarn/unplugged'
	delete '.pnp.cjs'
	delete '.pnp.loader.mjs'
}