import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def processLibraryInfoJSON = {
    def currentDirectory = buildFile.parentFile.toPath()
    def packageJson = new File(currentDirectory.resolve('../package.json').toString())
    def name = "airbridge-react-native-sdk"
    def version = ""
    
    if (packageJson.exists()) {
        def jsonObject = new JsonSlurper().parseText(packageJson.text)
        name = jsonObject.name ?: name
        version = jsonObject.version ?: version
    }

    def filteredJson = [
            name   : name,
            version: version
        ]

    def file = rootProject.file('app/src/main/assets/libraryInfo.json')
    if (!file.exists()) {
        file.getParentFile().mkdirs()
        file.createNewFile()
    }

    file.write(new String(JsonOutput.prettyPrint(JsonOutput.toJson(filteredJson))))
}

preBuild.dependsOn processLibraryInfoJSON
