/*
 *  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 'application'
    id 'groovy'
    id 'java-library'
    id 'project-report'
    id 'org.apache.grails.buildsrc.publish'
    id 'org.apache.grails.buildsrc.sbom'
}

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

configurations.register('shellCliDependencies').configure {
    it.transitive = true

    configurations.runtimeClasspath.extendsFrom(it)
}

ext {
    sbomProjectType = org.cyclonedx.model.Component.Type.APPLICATION.name()
}

dependencies {

    implementation platform(project(':grails-bom'))

    implementation 'org.apache.groovy:groovy'
    implementation 'org.apache.groovy:groovy-templates'
    implementation 'org.apache.groovy:groovy-xml'
    implementation 'org.apache.groovy:groovy-ant'
    implementation 'org.apache.groovy:groovy-json'

    api project(':grails-bootstrap')
    api 'org.apache.grails.gradle:grails-gradle-model'
    api 'org.apache.ant:ant'
    api 'org.fusesource.jansi:jansi'
    api 'jline:jline'
    api "org.gradle:gradle-tooling-api:$gradleToolingApiVersion"
    
    compileOnly 'org.springframework.boot:spring-boot'
    compileOnly 'org.springframework.boot:spring-boot-loader-tools'
    compileOnly 'org.springframework:spring-web'
    api 'org.springframework.boot:spring-boot-cli'
    compileOnly 'jakarta.servlet:jakarta.servlet-api'
    compileOnly "net.sf.jopt-simple:jopt-simple:$joptSimpleVersion"

    // Maven Resolver API & friends
    implementation "org.apache.maven.resolver:maven-resolver-api:$apacheMavenResolverVersion"
    implementation "org.apache.maven.resolver:maven-resolver-spi:$apacheMavenResolverVersion"
    implementation "org.apache.maven.resolver:maven-resolver-util:$apacheMavenResolverVersion"
    implementation "org.apache.maven.resolver:maven-resolver-impl:$apacheMavenResolverVersion"
    implementation "org.apache.maven.resolver:maven-resolver-connector-basic:$apacheMavenResolverVersion"
    implementation "org.apache.maven.resolver:maven-resolver-transport-file:$apacheMavenResolverVersion"
    implementation "org.apache.maven.resolver:maven-resolver-transport-http:$apacheMavenResolverVersion"
    implementation "org.apache.maven:maven-resolver-provider:$apacheMavenVersion"
    implementation "org.apache.maven:maven-model-builder:$apacheMavenVersion"
    implementation "org.apache.maven:maven-settings-builder:$apacheMavenVersion"
    //implementation "org.eclipse.sisu:org.eclipse.sisu.inject:0.9.0.M3" // upstream from the maven resolver examples project

    testImplementation "net.sf.expectit:expectit-core:$expectitCoreVersion"
    testImplementation "com.github.jnr:jnr-posix:$jnrPosixVersion"
    testImplementation 'org.apache.groovy:groovy-xml'
    testImplementation 'org.apache.groovy:groovy-json'
    testImplementation 'org.apache.groovy:groovy-templates'
    testImplementation 'org.apache.groovy:groovy-ant'

    runtimeOnly 'org.slf4j:slf4j-simple'

    testImplementation 'org.apache.groovy:groovy-test-junit5'
    testImplementation 'org.junit.jupiter:junit-jupiter-api'
    testImplementation 'org.junit.platform:junit-platform-runner'

    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

    // Logging
    api 'org.slf4j:slf4j-api'
    api 'org.slf4j:jcl-over-slf4j'

    // Testing
    testImplementation 'org.slf4j:slf4j-simple'
    testImplementation('org.spockframework:spock-core') { transitive = false }
    // Required by Spock's Mocking
    testRuntimeOnly 'net.bytebuddy:byte-buddy'
    testImplementation 'org.objenesis:objenesis'

    // any project that should be included in the end distribution should be included here
    // historically these were the included projects so we have trimmed them back down to pre7.0
    shellCliDependencies project(':grails-codecs-core')
    shellCliDependencies project(':grails-codecs')
    shellCliDependencies project(':grails-console')
    shellCliDependencies project(':grails-controllers')
    shellCliDependencies project(':grails-converters')
    shellCliDependencies project(':grails-core')
    shellCliDependencies project(':grails-databinding-core')
    shellCliDependencies project(':grails-datasource')
    shellCliDependencies project(':grails-domain-class')
    shellCliDependencies project(':grails-i18n')
    shellCliDependencies project(':grails-interceptors')
    shellCliDependencies project(':grails-mimetypes')
    shellCliDependencies project(':grails-rest-transforms')
    shellCliDependencies project(':grails-services')
    shellCliDependencies project(':grails-test-core')
    shellCliDependencies project(':grails-testing-support-core')
    shellCliDependencies project(':grails-url-mappings')
    shellCliDependencies project(':grails-validation')
    shellCliDependencies project(':grails-web-boot')
    shellCliDependencies project(':grails-web-common')
    shellCliDependencies project(':grails-web-core')
    shellCliDependencies project(':grails-web-databinding')
    shellCliDependencies project(':grails-web-url-mappings')
}

apply {
    // java-configuration must be applied first since tasks are now lazy registered
    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/docs-config.gradle')
    from rootProject.layout.projectDirectory.file('gradle/test-config.gradle')
}

extensions.configure(JavaApplication) {
    it.applicationName = 'grails-shell-cli'
    it.mainClass = 'org.grails.cli.GrailsCli'
    it.applicationDefaultJvmArgs = ['-XX:+TieredCompilation', '-XX:TieredStopAtLevel=1', '-XX:CICompilerCount=3']
    it.applicationDistribution.with {
        it.from(rootProject.layout.projectDirectory.file('LICENSE')) { it.into '' }
        it.from(rootProject.layout.projectDirectory.file('NOTICE')) { it.into '' }
        it.from(rootProject.layout.projectDirectory.file('INSTALL')) { it.into '' }
    }
}

tasks.named('startScripts').configure { it ->
    def batFile = project.layout.buildDirectory.file('scripts/grails-shell-cli.bat')
    it.doLast {
        File win = batFile.get().asFile
        win.text = win.text.replaceFirst(/(?m)^set CLASSPATH=.*$/,'set CLASSPATH=%APP_HOME%\\\\lib\\\\*')
    }
}