BuildKonfig icon indicating copy to clipboard operation
BuildKonfig copied to clipboard

provide a way to read values from properties file

Open yshrsmz opened this issue 4 years ago • 8 comments

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 avatar Feb 19 '21 02:02 yshrsmz

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

yshrsmz avatar Apr 08 '21 16:04 yshrsmz

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

PhilipDukhov avatar Apr 10 '21 08:04 PhilipDukhov

@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?

yshrsmz avatar Apr 10 '21 12:04 yshrsmz

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

PhilipDukhov avatar Apr 10 '21 12:04 PhilipDukhov

Thanks, will take a look!

yshrsmz avatar Apr 10 '21 15:04 yshrsmz

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

chris-hatton avatar Sep 30 '21 15:09 chris-hatton

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

yshrsmz avatar Aug 07 '22 10:08 yshrsmz