/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.
 */
import org.apache.tools.ant.filters.ReplaceTokens

plugins {
    id 'org.apache.groovy-distribution'
    id 'org.apache.groovy-published-library'
    id 'org.apache.groovy-asciidoctor'
}

repositories {
    mavenLocal()
    mavenCentral()
}

docAggregation {
    excludeFromJavadoc '**/*.html',
            '**/*.groovy',
            '*.g4',
            '**/GroovyRecognizer.java', // generated file
            '**/*.tokens', // Antlr generated file
            '**/*.interp' // Antlr generated file
}

tasks.named('asciidoctor') {
    attributes reldir_root: '.',
            reldir_jmx: '.',
            reldir_swing: '.',
            reldir_console: '.',
            reldir_groovysh: '.'
}

distribution {
    docs ':', 'org.codehaus.groovy.runtime.ArrayGroovyMethods',
            'org.codehaus.groovy.runtime.DefaultGroovyMethods',
            'org.codehaus.groovy.runtime.DefaultGroovyStaticMethods',
            'org.codehaus.groovy.runtime.EncodingGroovyMethods',
            'org.codehaus.groovy.runtime.IOGroovyMethods',
            'org.codehaus.groovy.runtime.ProcessGroovyMethods',
            'org.codehaus.groovy.runtime.ResourceGroovyMethods',
            'org.codehaus.groovy.runtime.SocketGroovyMethods',
            'org.codehaus.groovy.runtime.StreamGroovyMethods',
            'org.codehaus.groovy.runtime.StringGroovyMethods',
            'org.codehaus.groovy.vmplugin.v8.PluginDefaultGroovyMethods',
            'org.codehaus.groovy.vmplugin.v9.PluginDefaultGroovyMethods',
            'org.codehaus.groovy.vmplugin.v10.PluginDefaultGroovyMethods'
    docs ':groovy-dateutil', 'org.apache.groovy.dateutil.extensions.DateUtilExtensions'
    docs ':groovy-datetime', 'org.apache.groovy.datetime.extensions.DateTimeExtensions',
            'org.apache.groovy.datetime.extensions.DateTimeStaticExtensions'
    docs ':groovy-sql', 'org.apache.groovy.sql.extensions.SqlExtensions'
    docs ':groovy-swing', 'org.apache.groovy.swing.extensions.SwingExtensions'
    docs ':groovy-xml', 'org.apache.groovy.xml.extensions.XmlExtensions'
    docs ':groovy-nio', 'org.apache.groovy.nio.extensions.NioExtensions'
    docs ':groovy-jsr223', 'org.codehaus.groovy.jsr223.ScriptExtensions',
            'org.codehaus.groovy.jsr223.ScriptStaticExtensions'

    distSpec = copySpec {
        from rootProject.file("licenses/LICENSE-BINZIP")
        from rootProject.file("notices/NOTICE-BINZIP")
        rename '^([A-Z]+)-([^.]*)', '$1'
        into('lib') {
            from(configurations.distributionsRuntimeClasspath) {
                exclude {
                    it.file.name.contains('activation') ||
                            it.file.name.contains('groovy-raw') ||
                            it.file.name.contains('livetribe-jsr223') ||
                            it.file.name.startsWith('openbeans-') ||
                            it.file.name.startsWith('asm-') ||
                            it.file.name.startsWith('antlr4-') ||
                            it.file.name.startsWith('picocli-')
                }
            }
            from rootProject.file('src/bin/groovy.icns')
        }
        rootProject.allprojects {
            pluginManager.withPlugin('org.apache.groovy-base') {
                if (groovyLibrary.hasGrooid()) {
                    into('grooid') {
                        from tasks.named('grooidJar')
                    }
                }
            }
        }
        into('conf') {
            from rootProject.file('src/conf')
        }
        into('bin') {
            from(rootProject.file('src/bin')) {
                filter(ReplaceTokens, tokens: [GROOVYJAR: "groovy-${versions.groovy}.jar".toString()])
                filePermissions { unix('rwxr-xr-x') }
                exclude 'groovy.icns'
            }
            from project.project(projects.groovyDocgenerator.path).file('src/main/resources/org/apache/groovy/docgenerator/groovy.ico')
        }
        into('licenses') {
            from rootProject.file('licenses')
            include 'antlr4-license.txt'
            include 'asm-license.txt'
            include 'hamcrest-license.txt'
            include 'jline2-license.txt'
            include 'jsr166y-license.txt'
            include 'jsr223-license.txt'
            include 'junit4-license.txt'
            include 'junit5-license.txt'
            include 'xstream-license.txt'
        }
    }

    docSpec = copySpec {
        into('html/api') {
            from tasks.named('javadocAll')
        }
        into('html/gapi') {
            from tasks.named('groovydocAll')
        }
        into('html/documentation') {
            from configurations.allAsciidoc
            from tasks.named('asciidoctor')
            from tasks.named('asciidoctorPdf')
            exclude '.asciidoctor'
        }
        into('html/groovy-jdk') {
            from tasks.named('docGDK')
        }
        into('licenses') {
            from rootProject.file('licenses')
            include 'asciidoc-style-license.txt'
            include 'jquery-js-license.txt'
            include 'normalize-stylesheet-license.txt'
        }
        from rootProject.file("licenses/LICENSE-DOC")
        from rootProject.file("notices/NOTICE-BASE")
        rename 'LICENSE-DOC', 'LICENSE'
        rename 'NOTICE-BASE', 'NOTICE'
    }

    srcSpec = copySpec {
        rootProject.subprojects {
            into("subprojects/${project.projectDir.name}") {
                from(project.projectDir) {
                    exclude 'build',
                            'target',
                            'out',             // used by Intellij IDEA
                            '**/*.iml',        // used by Intellij IDEA
                            '**/*.ipr',        // used by Intellij IDEA
                            '**/*.iws',        // used by Intellij IDEA
                            '.settings',       // used by Eclipse
                            '.classpath',      // used by Eclipse
                            '.gradle'          // used by Gradle
                }
            }
        }
        ['xdocs',
         'config',
         'build-logic',
         'licenses',
         'notices',
         'security',
         'gradle',
         'src',
         'bootstrap',
         'build.gradle',
         'settings.gradle',
         'versions.properties',
         'gradle.properties',
         'LICENSE',
         'NOTICE',
         'README.adoc'].each {
            def file = rootProject.file(it)
            from(file) {
                if (file.directory) {
                    into file.name
                    exclude 'build',
                            'target',
                            'wrapper',        // used by Gradle
                            '**/*.iml',       // used by Intellij IDEA
                            '**/*.ipr',       // used by Intellij IDEA
                            '**/*.iws',       // used by Intellij IDEA
                            '.settings',      // used by Eclipse
                            '.classpath',     // used by Eclipse
                            '.gradle',        // used by Gradle
                            '*.gpg'           // used by Gradle
                }
            }
        }
    }
}

tasks.withType(AbstractArchiveTask).configureEach {
    preserveFileTimestamps = false
    reproducibleFileOrder = true
}
