/*
 *  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
 *
 *      https://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 com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.apache.grails.forge.buildlogic.shadowjar.GrailsShadowLicenseTransform
import org.apache.grails.forge.buildlogic.shadowjar.GrailsShadowNoticeTransform

plugins {
    id 'groovy'
    id 'com.gradleup.shadow'
}

version = projectVersion
group = 'org.apache.grails'

dependencies {
    implementation platform("org.apache.grails:grails-bom:$projectVersion")

    // shell-cli dependencies
    implementation 'org.apache.grails:grails-shell-cli'
    implementation 'org.springframework.boot:spring-boot-cli'
    implementation 'org.apache.groovy:groovy'
    implementation 'org.apache.groovy:groovy-ant'
    implementation 'org.apache.groovy:groovy-templates'

    // The commands are pulled from the shellCliDependencies in the shell project, but they are not in the shell jar file,
    // so they must be added here. To keep the jar file smaller, only a subset is included here
    implementation 'org.apache.grails:grails-core' // for ApplicationContextCommandFactory to lookup commands
    implementation 'org.apache.grails:grails-url-mappings' // for UrlMappingsReportCommand
    // profiles will add additional libraries as needed to surface commands

    // forge dependencies
    implementation project(':grails-forge-cli'), {
        // exclude the slf4j-nop dependency because the simple slf4j implementation is already included
        // which will cause a duplicate warning message
        exclude group: 'org.slf4j', module: 'slf4j-nop'
    }
}

apply {
    from rootProject.layout.projectDirectory.file('gradle/java-config.gradle')
}

TaskProvider<Jar> shadowJarTask = tasks.named('shadowJar', ShadowJar)
shadowJarTask.configure { ShadowJar it ->
    it.archiveClassifier = 'all'

    // services & custom grails.factories have to be merged since commands can be created from both sources
    it.mergeServiceFiles()
    it.mergeServiceFiles('META-INF/grails.factories')

    // groovy extensions are shipped with grails-core
    it.mergeGroovyExtensionModules()

    // combines all NOTICE files
    it.transform(GrailsShadowNoticeTransform) {
        it.projectName = 'Apache Grails'
        it.inceptionYear = '2005'
    }

    // combines all LICENSE files
    it.transform(GrailsShadowLicenseTransform) {
        it.licenseText = rootProject.layout.projectDirectory.file('../licenses/LICENSE-Apache-2.0.txt').getAsFile().text
    }

    it.exclude(
            'META-INF/DEPENDENCIES', // until we publish our own SBOM, this won't be correct so exclude
            'about.html' // restatement of the Eclipse Distribution License - Version 1.0 for jakarta
    )
}
