yshrsmz / BuildKonfig

BuildConfig for Kotlin Multiplatform Project
Apache License 2.0
740 stars 33 forks source link

provide a way to read values from properties file #48

Open yshrsmz opened 3 years ago

yshrsmz commented 3 years ago

Not sure the DSL below is possible, but something like this

buildKonfig {
  defaultConfigs {
    // load all values as string
    fromProperties file('./secrets.properties') {
      // specify type
      propertyAs type: 'INT', key: 'KEY_OF_PROPERTY'

      // also specify different name
      propertyAs type: 'LONG', key: 'key.of.other', name: 'DIFFERENT_NAME'
    }

    // you can also add other stuff as usual
    buildKonfigField('STRING', 'FOO', 'bar')
  }
}
yshrsmz commented 3 years ago

https://github.com/google/secrets-gradle-plugin

PhilipDukhov commented 3 years ago

@yshrsmz do you have a plan to add multiplatform support for secrets-gradle-plugin?

yshrsmz commented 3 years ago

@PhilipDukhov I think I'm not going to "support" secrets-gradle-plugin, as it looks like an Android-specific plugin, but borrow some idea/implementation from it.

Do you have any idea or suggestion?

PhilipDukhov commented 3 years ago

Ah I see. I think it could be done using same techniques moko-resources uses under the hood.

yshrsmz commented 3 years ago

Thanks, will take a look!

chris-hatton commented 2 years ago

In case it helps anyone I'm using BuildKonfig happily in conjunction with the SnakeYAML library, within Gradle (Kotlin DSL), like so:

import com.codingfeline.buildkonfig.compiler.FieldSpec.Type
import org.yaml.snakeyaml.Yaml

...

buildkonfig {
    packageName = "mypackage"
    objectName = "MyConfig"

    val configFilename: String = projectDir.toString() + File.separator + "MyConfig.yml"
    val appConfigYaml: String = File(configFilename).readText()
    val appConfigMap: Map<String, Any?> = Yaml().load(appConfigYaml)

    defaultConfigs {
        appConfigMap.forEach { (key, value) ->
            val (fieldType: Type, fieldContent: String) = when (value) {
                is Boolean -> Type.BOOLEAN to value.toString()
                is String -> Type.STRING to value
                is List<*> -> Type.STRING to value.joinToString(",") { it.toString() }
                else -> Type.STRING to (value?.toString() ?: "")
            }
            buildConfigField(
                type = fieldType,
                name = key,
                value = fieldContent
            )
        }
    }
}
yshrsmz commented 2 years ago

It seems like properties file is not suitable for BuildKonfig, as it's just a key-value map

Something like yaml or toml should be fine

https://github.com/Peanuuutz/tomlkt