|
| 1 | +package ru.endlesscode.bukkitgradle.server |
| 2 | + |
| 3 | +import org.gradle.api.provider.Provider |
| 4 | +import org.gradle.api.provider.ProviderFactory |
| 5 | +import org.slf4j.LoggerFactory |
| 6 | +import java.io.File |
| 7 | +import java.util.* |
| 8 | + |
| 9 | +internal class DeprecatedServerProperties(rootDir: File, private val providers: ProviderFactory) { |
| 10 | + |
| 11 | + val devServerDir: File? |
| 12 | + |
| 13 | + private val logger = LoggerFactory.getLogger("DeprecatedServerProperties") |
| 14 | + private val properties = Properties() |
| 15 | + |
| 16 | + private val propertiesFile = File(rootDir, NAME) |
| 17 | + |
| 18 | + init { |
| 19 | + if (propertiesFile.exists()) { |
| 20 | + properties.load(propertiesFile.reader()) |
| 21 | + } |
| 22 | + |
| 23 | + // Read properties eagerly to show deprecation warning |
| 24 | + devServerDir = getDir(DEV_SERVER_DIR) |
| 25 | + getDir(BUILD_TOOLS_DIR) |
| 26 | + } |
| 27 | + |
| 28 | + private fun getDir(property: Property): File? { |
| 29 | + val path = get(property) ?: return null |
| 30 | + |
| 31 | + showDeprecationWarning(property) |
| 32 | + return File(path) |
| 33 | + .absoluteFile |
| 34 | + .also { it.mkdirs() } |
| 35 | + } |
| 36 | + |
| 37 | + private fun get(property: Property): String? { |
| 38 | + val localProp = properties.getProperty(property.name) |
| 39 | + val globalEnv = getEnvProvider(property.envVariable) |
| 40 | + return localProp ?: globalEnv.orNull |
| 41 | + } |
| 42 | + |
| 43 | + private fun getEnvProvider(name: String): Provider<String> { |
| 44 | + return providers.environmentVariable(name) |
| 45 | + } |
| 46 | + |
| 47 | + private fun showDeprecationWarning(property: Property) { |
| 48 | + logger.warn( |
| 49 | + """ |
| 50 | + Property '${property.name}' is Deprecated. |
| 51 | + ${property.replacementNote} |
| 52 | + Please, remove the property from $NAME and unset '${property.envVariable}' environment variable. |
| 53 | + """.trimIndent() |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + private data class Property( |
| 58 | + val name: String, |
| 59 | + val envVariable: String, |
| 60 | + val replacementNote: String, |
| 61 | + ) |
| 62 | + |
| 63 | + private companion object { |
| 64 | + private const val NAME: String = "local.properties" |
| 65 | + |
| 66 | + private val BUILD_TOOLS_DIR: Property = Property( |
| 67 | + name = "buildtools.dir", |
| 68 | + envVariable = "BUKKIT_BUILDTOOLS_HOME", |
| 69 | + replacementNote = "Building and running Spigot server is not supported anymore.", |
| 70 | + ) |
| 71 | + private val DEV_SERVER_DIR: Property = Property( |
| 72 | + name = "server.dir", |
| 73 | + envVariable = "BUKKIT_DEV_SERVER_HOME", |
| 74 | + replacementNote = "Set Gradle property 'bukkitgradle.server.dir' in \$HOME/.gradle/gradle.properties instead.", |
| 75 | + ) |
| 76 | + } |
| 77 | +} |
0 commit comments