/*
 *  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.
 */

plugins {
    id 'java'
    id 'groovy'
    id 'distribution'
    id 'org.apache.grails.buildsrc.publish'
    id 'org.apache.grails.buildsrc.sbom'
}

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

ext {
    pomTitle = 'Grails Wrapper Downloader'
    pomDescription = 'Downloads necessary libraries for the Grails Wrapper'
    pomDevelopers = [
            'graemerocher'  : 'Graeme Rocher',
            'jeffscottbrown': 'Jeff Brown',
            'puneetbehl'    : 'Puneet Behl',
            'jdaugherty'    : 'James Daugherty'
    ]
    cliProject = true
    startMainClass = 'grails.init.Start'
    sbomProjectType = org.cyclonedx.model.Component.Type.APPLICATION.name()
}

dependencies {
    // Intentionally no implementation dependencies to prevent bloat

    testImplementation platform(project(':grails-bom'))
    testImplementation 'org.apache.groovy:groovy'
    testImplementation 'org.spockframework:spock-core'
    testImplementation 'org.apache.groovy:groovy-test-junit5'
    testImplementation 'org.junit.jupiter:junit-jupiter-api'
    testImplementation 'org.junit.platform:junit-platform-runner'
    // for easier setting of environment variables in tests
    testImplementation 'uk.org.webcompere:system-stubs-core:2.1.8'
}

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

// It's surprisingly hard to generate start scripts and *not* have them nested into a bin / lib directory
// rather than defining the application and then having to add tasks to rework it, just define a distribution and
// create the scripts in the right location from the start
TaskProvider<Jar> jarTask = tasks.named('jar', Jar)
jarTask.configure { Jar it ->
    it.archiveFileName = "${project.name}.jar" as String
    it.manifest {
        attributes 'Main-Class': project.property('startMainClass')
    }
}

TaskProvider<CreateStartScripts> startScripts = tasks.register('createStartScripts', CreateStartScripts)
startScripts.configure { CreateStartScripts t ->
    t.outputDir = layout.buildDirectory.dir('generated-scripts').get().asFile
    t.applicationName = 'grailsw'
    t.mainClass = project.findProperty('startMainClass') as String
    t.classpath = files(jarTask)

    // See gradle tickets such as for lib https://github.com/gradle/gradle/issues/7033
    doLast {
        t.unixScript.text = t.unixScript.text
                .replace('$APP_HOME/lib/', '$APP_HOME/')
                .replace('"${APP_HOME:-./}.."', '"${APP_HOME:-./}"')

        t.windowsScript.text = t.windowsScript.text
                .replace('%APP_HOME%\\lib\\', '%APP_HOME%\\')
                .replace('"%APP_HOME%\\lib\\', '"%APP_HOME%\\')
                .replace('%DIRNAME%..', '%DIRNAME%')
    }
}

project.extensions.getByType(DistributionContainer).configureEach {
    // WARNING: changing the distro name / classifier requires updating the task in grails-profiles-base to copy from the correct location
    it.distributionBaseName = 'apache-grails-wrapper'
    it.distributionClassifier = 'bin'
    it.contents {
        from(jarTask) {
            into ''
        }

        from(startScripts) {
            into ''
            filePermissions { permissions ->
                permissions.unix(0755)
            }
        }

        from(layout.projectDirectory.dir('distribution-artifacts')) {
            into ''
        }
    }
}

tasks.named('assemble').configure {
    it.dependsOn(startScripts)
}
tasks.named('installDist').configure {
    it.dependsOn startScripts
}
tasks.named('distZip').configure {
    dependsOn startScripts
}
tasks.named('distTar').configure {
    dependsOn startScripts
}