// Top-level build file where you can add configuration options common to all
// sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files.
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven { url "https://maven.google.com" } // Required for appcompat.
        // React Native (JS, Obj-C sources, Android binaries) is installed from
        // npm.
        maven { url "$rootDir/../node_modules/react-native/android" }
    }

    // Third-party react-native modules which Jitsi Meet SDK for Android depends
    // on and which are not available in third-party Maven repositories need to
    // be deployed in a Maven repository of ours.
    //

    if (project.name.startsWith('react-native-')) {
        apply plugin: 'maven-publish'
        publishing {
            publications {}
            repositories {
                maven { url "file:${rootProject.projectDir}/../../../jitsi/jitsi-maven-repository/releases" }
            }
        }
    }

    afterEvaluate { project ->
        if (project.name.startsWith('react-native-')) {
            def npmManifest = project.file('../package.json')
            def json = new groovy.json.JsonSlurper().parseText(npmManifest.text)

            // React Native modules have an npm peer dependency on react-native,
            // they do not have an npm dependency on it. Further below though we
            // choose a react-native version (range) when we represent them as
            // Maven artifacts. Effectively, we are forking the projects by not
            // complying with the full range of their npm peer dependency and,
            // consequently, we should qualify their version.
            def versionQualifier = '-jitsi-1'
            if ('react-native-webrtc'.equals(project.name))
                versionQualifier = '-jitsi-1'

            project.version = "${json.version}${versionQualifier}"

            project.android {
                compileSdkVersion rootProject.ext.compileSdkVersion
                if (rootProject.ext.has('buildToolsVersion')) {
                    buildToolsVersion rootProject.ext.buildToolsVersion
                }
                defaultConfig {
                    minSdkVersion rootProject.ext.minSdkVersion
                    targetSdkVersion rootProject.ext.targetSdkVersion
                }
            }

            task androidJavadocs(type: Javadoc) {
                source = android.sourceSets.main.java.source
                classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
                failOnError false
            }
            task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
                classifier = 'javadoc'
                from androidJavadocs.destinationDir
            }
            task androidSourcesJar(type: Jar) {
                classifier = 'sources'
                from android.sourceSets.main.java.source
            }

            publishing.publications {
                aarArchive(MavenPublication) {
                    groupId rootProject.ext.moduleGroupId
                    artifactId project.name
                    version project.version

                    artifact("${project.buildDir}/outputs/aar/${project.name}-release.aar") {
                        extension "aar"
                    }
                    artifact(androidSourcesJar)
                    artifact(androidJavadocsJar)
                    pom.withXml {
                        def pomXml = asNode()
                        pomXml.appendNode('name', project.name)
                        pomXml.appendNode('description', json.description)
                        pomXml.appendNode('url', json.homepage)
                        if (json.license) {
                            def license = pomXml.appendNode('licenses').appendNode('license')
                            license.appendNode('name', json.license)
                            license.appendNode('distribution', 'repo')
                        }

                        def dependencies = pomXml.appendNode('dependencies')
                        configurations.getByName('releaseCompileClasspath').getResolvedConfiguration().getFirstLevelModuleDependencies().each {
                            def artifactId = it.moduleName
                            def version = it.moduleVersion
                            // React Native signals breaking changes by
                            // increasing the minor version number. So the
                            // (third-party) React Native modules we utilize can
                            // depend not on a specific react-native release but
                            // a wider range.
                            if (artifactId.equals('react-native')) {
                                def versionNumber = VersionNumber.parse(version)
                                version = "${versionNumber.major}.${versionNumber.minor}"
                            }

                            def dependency = dependencies.appendNode('dependency')
                            dependency.appendNode('groupId', it.moduleGroup)
                            dependency.appendNode('artifactId', artifactId)
                            dependency.appendNode('version', version)
                        }
                    }
                }
            }
        }
    }
}

ext {
    buildToolsVersion = "26.0.2"
    compileSdkVersion = 26
    minSdkVersion    = 16
    targetSdkVersion = 25

    // The Maven artifact groupdId of the third-party react-native modules which
    // Jitsi Meet SDK for Android depends on and which are not available in
    // third-party Maven repositories so we have to deploy to a Maven repository
    // of ours.
    moduleGroupId = 'com.facebook.react'
}

// Force the version of the Android build tools we have chosen on all
// subprojects. The forcing was introduced for react-native and the third-party
// modules that we utilize such as react-native-background-timer.
subprojects { subproject ->
    afterEvaluate{
        if ((subproject.plugins.hasPlugin('android')
                    || subproject.plugins.hasPlugin('android-library'))
                && rootProject.ext.has('buildToolsVersion')) {
            android {
                buildToolsVersion rootProject.ext.buildToolsVersion
            }
        }
    }
}