// Copies main project Gradle properties to sample project. Can't use buildSrc because settings.gradle
// must access these properties.

List<String> propertiesFiles = [
    "../gradle.properties",
    "../local.properties",
]

propertiesFiles.each { fileName ->
    File file = file(fileName)
    if (file.exists()) {
        Properties properties = new Properties()
        file.withInputStream { properties.load(it) }

        int moduleNameStartIndex = fileName.indexOf('/') + 1
        int moduleNameEndIndex = fileName.lastIndexOf('/')
        String namespace
        if (moduleNameStartIndex < moduleNameEndIndex) {
            namespace = fileName.substring(moduleNameStartIndex, moduleNameEndIndex)
                .replace('/', '.')
        } else {
            namespace = null
        }

        properties.each { key, value ->
            String namespacedKey
            if (namespace == null) {
                namespacedKey = key
            } else {
                namespacedKey = "$namespace.$key"
            }
            try {
                properties.set(namespacedKey, value)
            } catch (MissingMethodException ignored) {
                // We are in a pluginManagement block that can't set properties, so set an extra instead:
                ext.set(namespacedKey, value)
            }
        }
    }

}
