description = "Apereo CAS Bill of Material"

ext {
    skipPublication = true
}

def getProjectNamePattern() {
    return "^cas-mgmt-.*"
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            
            pom.withXml {
                println "Creating BOM with all project dependencies for ${project.name}"

                def iter = asNode().iterator()
                while (iter.hasNext()) {
                    def node = iter.next()
                    if (node.name().getLocalPart().equals("dependencies")) {
                        println "Removing dependencies block from generated POM"
                        iter.remove()
                    }
                    if (node.name().getLocalPart().equals("properties")) {
                        println "Removing properties block from generated POM"
                        iter.remove()
                    }
                }

                def dependencyManagement = asNode().appendNode("dependencyManagement")
                def dependencies = dependencyManagement.appendNode("dependencies")

                def dependencyVersions = [:]
                def allLibraries = []

                rootProject.subprojects
                        .findAll { p -> p != project && p.name.matches(getProjectNamePattern()) }
                        .each { p ->
                            createDependency(dependencies, groupId, p.name,
                                    p.version, dependencyVersions, allLibraries)
                        }

                libraries.each {
                    def artifact = it.value
                
                    if (artifact instanceof List) {
                        artifact.each { art ->
                            if (art.class.isArray()) {
                                art.each { coords ->
                                    def elem = coords.toString().split(":")
                                    createDependency(dependencies, elem[0], elem[1], elem[2],
                                            dependencyVersions, allLibraries)
                                }
                            } else {
                                createDependency(dependencies, art.group, art.name,
                                        art.version, dependencyVersions, allLibraries)
                            }
                        }
                    } else if (artifact instanceof Map) {
                        artifact.each {
                            def entry = it.value
                            entry.each { art ->
                                createDependency(dependencies, art.group, art.name,
                                        art.version, dependencyVersions, allLibraries)
                            }
                        }
                    } else {
                        createDependency(dependencies, artifact.group, artifact.name,
                                artifact.version, dependencyVersions, allLibraries)
                    }
                }

                def properties = asNode().appendNode("properties")
                dependencyVersions.each { ver ->
                    properties.appendNode(ver.key, ver.value)
                }
            }

            pom {
                url = "$projectUrl"
                inceptionYear = project.projectInceptionYear
                name = project.name
                description = project.name
                packaging = "pom"
                issueManagement {
                    system = project.projectIssueSystem
                    url = project.projectIssueUrl
                }
                licenses {
                    license {
                        name = project.projectLicenseName
                        url = project.projectLicenseUrl
                    }
                }
                scm {
                    connection = project.projectScmUrl
                    developerConnection = project.projectScmUrl
                    url = project.projectScmUrl
                }
                developers {
                    developer {
                        id = "mmoayyed"
                        name = "Misagh Moayyed"
                    }
                    developer {
                        id = "tsschmidt"
                        name = "Travis Schmidt"
                    }
                }
            }
        }
    }
}

def createDependency(dependencies, group, name, version, dependencyVersions, allLibraries) {
    def key = "${group}@${name}"
    
    if (!allLibraries.contains(key)) {
        logger.info "Dependency: ${group}:${name}:${version}"
        
        def dependency = dependencies.appendNode("dependency")
        dependency.appendNode("groupId", group)
        dependency.appendNode("artifactId", name)

        if (name.matches(getProjectNamePattern())) {
            dependency.appendNode("version", "\${cas-mgmt.version}")
            dependencyVersions.put("cas-mgmt.version", version)
        } else {
            def versionProp = "${group}.${name}.version"
            
            dependency.appendNode("version", String.format("\${%s}", versionProp))
            dependencyVersions.put(versionProp, version)
        }
        allLibraries.add(key)
    }
}