Skip to content

Commit 8533c69

Browse files
committed
fix reading symbolic links
1 parent ddd267d commit 8533c69

File tree

7 files changed

+117
-68
lines changed

7 files changed

+117
-68
lines changed

.gitignore

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
1-
*.iml
2-
.secret
3-
.secrets
4-
.gradle
5-
/build-logic/local.properties
6-
/.idea/caches
7-
/.idea/libraries
8-
/.idea/modules.xml
9-
/.idea/workspace.xml
10-
/.idea/navEditor.xml
11-
/.idea/assetWizardSettings.xml
12-
local.properties
13-
.DS_Store
14-
/build-logic/build
15-
/build-logic/plugins/build
16-
/build-logic/plugins/convention/build
17-
/build-logic/plugins/android/build
18-
/build-logic/plugins/minecraft/build
19-
/build
20-
/buildSrc/build
21-
/captures
22-
.externalNativeBuild
23-
.cxx
24-
build-logic/local.properties
25-
androidApp/build
26-
androidApp/libs
271
.idea
28-
androidApp/private_key.pepk
29-
androidApp/google-services.json
30-
androidApp/keystore.jks
31-
desktop/build
32-
keys.propertiesd
33-
# Samples
34-
samples/android-app/build
35-
samples/android-library/build
36-
samples/kmm-compose/build
37-
samples/kmm-lib/build
2+
.kotlin
3+
.gradle
4+
**/build
5+
**/.kotlin

build-logic/build.gradle.kts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension
2+
import java.nio.file.Files
3+
import java.util.Properties
4+
import kotlin.io.path.reader
25

36
plugins {
47
`kotlin-dsl`
58
alias(libs.plugins.vaniktech) apply false
6-
id("ru.astrainteractive.gradleplugin.detekt") version "1.11.0" apply true
9+
// id("ru.astrainteractive.gradleplugin.detekt") version "1.11.4" apply true
710
}
811

912
val klibs = libs
1013

14+
private fun requireFileProperty(key: String): Result<String> {
15+
return runCatching {
16+
val reader = project.file("gradle.properties").let { file ->
17+
val path = file.toPath()
18+
if (Files.isSymbolicLink(path)) {
19+
Files.readSymbolicLink(path).reader()
20+
} else {
21+
file.reader()
22+
}
23+
}
24+
val properties = Properties().apply { load(reader) }
25+
properties[key]?.toString() ?: error("Could not find property $key")
26+
}
27+
}
28+
29+
private fun requireGradleProperty(key: String): Result<String> {
30+
return runCatching {
31+
rootProject.property(key)
32+
?.toString()
33+
?: error("Could not find property $key")
34+
}
35+
}
36+
1137
private fun requireProperty(key: String): String {
12-
return rootProject.property(key)
13-
?.toString()
14-
?: error("Could not find property $key")
38+
return requireGradleProperty(key)
39+
.getOrNull()
40+
?: requireFileProperty(key).getOrThrow()
1541
}
1642

1743
data class ProjectConfiguration(

build-logic/gradle.properties

Lines changed: 0 additions & 11 deletions
This file was deleted.

build-logic/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../gradle.properties
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ru.astrainteractive.gradleplugin.property
2+
3+
import org.gradle.api.GradleException
4+
import org.gradle.api.Project
5+
import java.io.File
6+
import java.nio.file.Files
7+
import java.util.Properties
8+
import kotlin.io.path.reader
9+
10+
private fun File.takeIfIsLinkOrExist(): File? {
11+
if (exists() || Files.isSymbolicLink(toPath())) return this
12+
return null
13+
}
14+
15+
internal fun Project.fileOrParentFile(name: String): File? {
16+
val parentOrNull = parent
17+
val projectFileOrNull = file(name).takeIfIsLinkOrExist()
18+
return when {
19+
projectFileOrNull != null -> projectFileOrNull
20+
parentOrNull != null -> parentOrNull.fileOrParentFile(name)
21+
else -> rootProject.file(name).takeIfIsLinkOrExist()
22+
}
23+
}
24+
25+
internal fun Project.requireFileOrParentFile(name: String): File {
26+
return fileOrParentFile(name)
27+
?: throw GradleException("No local.properties file found")
28+
}
29+
30+
internal fun Properties.fromFile(file: File): Properties {
31+
val reader = file.let { file ->
32+
val path = file.toPath()
33+
if (Files.isSymbolicLink(path)) {
34+
Files.readSymbolicLink(path).reader()
35+
} else {
36+
file.reader()
37+
}
38+
}
39+
load(reader)
40+
return this
41+
}

build-logic/plugins/convention/src/main/kotlin/ru/astrainteractive/gradleplugin/property/internal/ProjectPropertyValue.kt

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package ru.astrainteractive.gradleplugin.property.internal
33
import org.gradle.api.GradleException
44
import org.gradle.api.Project
55
import ru.astrainteractive.gradleplugin.property.PropertyValue
6+
import ru.astrainteractive.gradleplugin.property.fileOrParentFile
7+
import ru.astrainteractive.gradleplugin.property.fromFile
8+
import java.util.Properties
69

710
/**
811
* This class will load property from gradle.properties project file
@@ -11,10 +14,33 @@ class ProjectPropertyValue(
1114
private val project: Project,
1215
override val key: String
1316
) : PropertyValue {
17+
private fun getGradleProperty(): Result<String> {
18+
project.logger.warn("#getGradleProperty $key")
19+
return runCatching {
20+
project.property(key)
21+
?.toString()
22+
?: throw GradleException("Required property $key not defined!")
23+
}
24+
}
25+
26+
private fun getFileProperty(): Result<String> {
27+
project.logger.warn("#getFileProperty $key")
28+
return runCatching {
29+
val file = project
30+
.fileOrParentFile("gradle.properties")
31+
?: throw GradleException("No gradle.properties found in project")
32+
Properties()
33+
.fromFile(file)
34+
.getProperty(key)
35+
?: throw GradleException("Required property $key not found in gradle.properties!")
36+
}
37+
}
38+
1439
override fun getValue() = runCatching {
15-
project.property(key)
16-
?.toString()
17-
?: throw GradleException("Required property $key not defined!")
40+
getGradleProperty()
41+
.onFailure { project.logger.warn("Couldn't find $key with gradle's project.property()") }
42+
.getOrNull()
43+
?: getFileProperty().getOrThrow()
1844
}
1945

2046
override fun isExist() = project.hasProperty(key)

build-logic/plugins/convention/src/main/kotlin/ru/astrainteractive/gradleplugin/property/internal/SecretPropertyValue.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package ru.astrainteractive.gradleplugin.property.internal
33
import org.gradle.api.GradleException
44
import org.gradle.api.Project
55
import ru.astrainteractive.gradleplugin.property.PropertyValue
6+
import ru.astrainteractive.gradleplugin.property.fromFile
7+
import ru.astrainteractive.gradleplugin.property.requireFileOrParentFile
68
import java.io.File
79
import java.util.Properties
810

@@ -14,16 +16,8 @@ class SecretPropertyValue(
1416
override val key: String
1517
) : PropertyValue {
1618

17-
private fun Project.fileOrParentFile(name: String): File? {
18-
val projectFile = file(name).takeIf(File::exists)
19-
if (projectFile != null) return projectFile
20-
return parent?.fileOrParentFile(name)
21-
}
22-
2319
private val localPropertiesFile: File
24-
get() = project.fileOrParentFile("local.properties")
25-
?: project.rootProject.file("local.properties").takeIf(File::exists)
26-
?: throw GradleException("No local.properties file found")
20+
get() = project.requireFileOrParentFile("local.properties")
2721

2822
/**
2923
* System.getenv doesn't allow dots
@@ -36,11 +30,9 @@ class SecretPropertyValue(
3630
if (systemEnvProperty != null) return@runCatching systemEnvProperty
3731
project.logger.warn("System.enviroment $envKey is missing. Getting it from local.properties")
3832
// if not ci getting from local.properties
39-
val properties = Properties().apply {
40-
val secretPropsFile = localPropertiesFile
41-
load(secretPropsFile.reader())
42-
}
43-
return@runCatching properties.getProperty(key)
33+
return@runCatching Properties()
34+
.fromFile(localPropertiesFile)
35+
.getProperty(key)
4436
?: throw GradleException("Required property $key not defined!")
4537
}
4638

gradle.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ kotlin.code.style=official
33
makeevrserg.java.source=8
44
makeevrserg.java.target=11
55
makeevrserg.java.ktarget=11
6+
# Config
7+
project.name=GradlePlugin
8+
project.description=GradlePlugin for my kotlin projects
9+
project.group=ru.astrainteractive.gradleplugin
10+
project.web=https://github.com/makeevrserg/gradle-plugin
11+
project.version.string=1.11.4

0 commit comments

Comments
 (0)