/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

def supportRootFolder = ext.supportRootFolder
if (supportRootFolder == null) {
    throw new RuntimeException("Canonical root project directory is not set. You must specify " +
            "ext.supportRootFolder before including this script")
}
// Makes strong assumptions about the project structure.
def checkoutRoot = supportRootFolder.parentFile.parentFile

ext.repos = new Properties()
ext.repos.checkoutRoot = checkoutRoot.absolutePath
ext.repos.prebuiltsRoot = new File(checkoutRoot, "prebuilts").absolutePath

/**
 * Adds maven repositories to the given repository handler.
 */
def addMavenRepositories(RepositoryHandler handler) {
    def metalavaRepoOverride = System.getenv("METALAVA_REPO")
    if (metalavaRepoOverride != null) {
        for(extraRepo in metalavaRepoOverride.split(File.pathSeparator)) {
            handler.maven {
                url = extraRepo
            }
        }
    }
    handler.maven {
        url = "${repos.prebuiltsRoot}/androidx/internal"
        metadataSources {
            mavenPom()
            artifact()
        }
        content {
            includeGroupByRegex("android.*")
            includeGroupByRegex("com.android.support.*")
            excludeGroupByRegex("androidx.databinding.*")
        }
    }
    handler.maven {
        url = "${repos.prebuiltsRoot}/androidx/external"
        metadataSources {
            mavenPom()
            artifact()
        }
        if (metalavaRepoOverride != null) {
            // When using custom metalava repo, do not resolve metalava artifacts from this repo
            content {
                excludeGroup("com.android.tools.metalava")
            }
        }
    }
    if (System.getenv("ALLOW_PUBLIC_REPOS") != null || System.getProperty("ALLOW_PUBLIC_REPOS") != null) {
        handler.mavenCentral()
        handler.google()
        handler.gradlePluginPortal()
        handler.maven {
               url = "https://maven.pkg.jetbrains.space/public/p/compose/dev"
        }
        handler.mavenLocal()
    }
    // Ordering appears to be important: b/229733266
    def androidPluginRepoOverride = System.getenv("GRADLE_PLUGIN_REPO")
    if (androidPluginRepoOverride != null) {
        for(extraRepo in androidPluginRepoOverride.split(File.pathSeparator)) {
            handler.maven {
                url = extraRepo
            }
        }
    }
}

ext.repos.addMavenRepositories = this.&addMavenRepositories
