apply plugin: 'maven-publish'

def mavenRepositoryUrl = "http://nexus.yidianling.com/repository/AndroidReleases/"
def mavenRepositoryUrl2 = "http://nexus.yidianling.com/repository/AndroidSnapshots/"

def sourceJar(String name, String path, String ext) {
    return project.tasks.create("source${name.capitalize()}Jar", Jar) {
        group = "publishing"
        description = "package ${name} source to jar"
        from "$path/src/main/java"
        from "$path/src/main/kotlin"
        if (!ext.isEmpty()) {
            from "$path/src/${ext}/java"
        }
        destinationDir = file("$path/build/libs/")
        classifier = 'sources'
    }
}

def setScm(scm) {
//    scm.appendNode('connection', "https://github.com/${System.getenv("GITHUB_ACTOR")}/Shadow.git")
//
//    def commit
//    if ("${System.env.CI}".equalsIgnoreCase("true")) {
//        commit = System.getenv("GITHUB_SHA")
//    } else {
//        commit = gitShortRev()
//    }
//    scm.appendNode('url', "https://github.com/${System.getenv("GITHUB_ACTOR")}/Shadow/commit/$commit")
}

def checkPomVersion = {
    def root = asNode()
    def dependencyNodes = root.get('dependencies')
    for (def dependency : dependencyNodes) {
        def children = dependency.children()
        for (def child : children) {
            def versionNode = child.get("version").first()
            def version = versionNode.value().first()
            def groupId = child.get("groupId").first().value().first()
            def artifactId = child.get("artifactId").first().value().first()
            def scope = child.get("scope").first().value().first()
            println "$scope $groupId:$artifactId:$version"
            if (version == "unspecified") {
                throw new IllegalStateException("版本检查出错了!")
            }
        }
    }
}
def multiComponent = [':m-home', ':ydl-resource']
afterEvaluate {
    def pVersion = ''
    if (project.version == "unspecified") {
        throw new IllegalStateException("请指定${project.name}的Version")
    } else {
        pVersion = project.version
    }
    def isApiModule = project.path.startsWith(":api:")
    publishing {
        publications {
            if (multiComponent.contains(project.path)) {
                ydlComponent(MavenPublication) {
                    group 'com.ydl'
                    version "ydl-" + pVersion
                    from components.ydlRelease
                    artifact sourceJar(getName(), "${project.projectDir}", "ydl")
                    pom.withXml(checkPomVersion)
                }
                xlzxComponent(MavenPublication) {
                    group 'com.ydl'
                    version "xlzx-" + pVersion
                    from components.xlzxRelease
                    artifact sourceJar(getName(), "${project.projectDir}", "xlzx")
                    pom.withXml(checkPomVersion)
                }
            } else {
                component(MavenPublication) {
                    group isApiModule ? 'com.ydl.api' : 'com.ydl'
                    version pVersion
                    from components.release
                    artifact sourceJar(getName(), "${project.projectDir}", "")
                    pom.withXml(checkPomVersion)
                }
            }
        }
        repositories {
            def snapshot = pVersion.endsWith("-SNAPSHOT")
            maven {
                url snapshot ? mavenRepositoryUrl2 : mavenRepositoryUrl
                allowInsecureProtocol true
                credentials {
                    username "admin"
                    password "fjoi#1+#@"
                }
            }
        }
    }
}