From fa22ceecea8cab32871242b5c84fc7ec378b896f Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Tue, 28 Nov 2023 19:27:38 -0700 Subject: [PATCH 1/2] Add MergedProperties --- .../paperweight/util/MergedProperties.kt | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/MergedProperties.kt diff --git a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/MergedProperties.kt b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/MergedProperties.kt new file mode 100644 index 00000000..6f927065 --- /dev/null +++ b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/MergedProperties.kt @@ -0,0 +1,57 @@ +/* + * paperweight is a Gradle plugin for the PaperMC project. + * + * Copyright (c) 2023 Kyle Wood (DenWav) + * Contributors + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 only, no later versions. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + */ + +package io.papermc.paperweight.util + +import java.util.Properties +import kotlin.io.path.* +import kotlin.reflect.KProperty +import org.gradle.api.Project + +class MergedProperties(private val properties: List) { + operator fun getValue(thisRef: Any?, property: KProperty<*>): String { + for (props in properties) { + if (props.containsKey(property.name)) { + return props.getProperty(property.name) + } + } + error("No value for property '${property.name}'") + } + + companion object { + fun fromAllProjects(project: Project, relativePath: String): MergedProperties { + // give our project priority + val projects = listOf(project) + project.rootProject.allprojects + val properties = projects.asSequence() + .distinct() + .map { it.layout.projectDirectory.file(relativePath).path } + .filter { it.exists() } + .map { + Properties().apply { + load(it.bufferedReader()) + } + } + .toList() + return MergedProperties(properties) + } + } +} From f5f4c97b961439764b98e97c14c61f5133491c78 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Tue, 28 Nov 2023 20:09:03 -0700 Subject: [PATCH 2/2] Add operator fun get to MergedProperties --- .../io/papermc/paperweight/util/MergedProperties.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/MergedProperties.kt b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/MergedProperties.kt index 6f927065..1a0df05a 100644 --- a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/MergedProperties.kt +++ b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/MergedProperties.kt @@ -28,13 +28,19 @@ import kotlin.reflect.KProperty import org.gradle.api.Project class MergedProperties(private val properties: List) { + // val someProperty by mergedProperties operator fun getValue(thisRef: Any?, property: KProperty<*>): String { + return get(property.name) + } + + // mergedProperties["someProperty"] | mergedProperties.get("someProperty") + operator fun get(propertyName: String): String { for (props in properties) { - if (props.containsKey(property.name)) { - return props.getProperty(property.name) + if (props.containsKey(propertyName)) { + return props.getProperty(propertyName) } } - error("No value for property '${property.name}'") + error("No value for property '$propertyName'") } companion object {