Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MergedProperties #226

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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<Properties>) {
// 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(propertyName)) {
return props.getProperty(propertyName)
}
}
error("No value for property '$propertyName'")
}

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)
}
}
}