/*
 *  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 org.apache.tools.ant.DirectoryScanner
import org.gradle.api.initialization.resolve.RepositoriesMode

pluginManagement {
    includeBuild('./grails-gradle') {
        name = 'grails-gradle'
    }
    includeBuild('./build-logic') {
        name = 'build-logic'
    }
    repositories {
        // because our publish plugin uses other plugins, those dependencies have to be resolvable
        // mavenLocal()
        mavenCentral()
        gradlePluginPortal()
        maven {
            url = 'https://repository.apache.org/content/groups/snapshots'
            content {
                includeVersionByRegex('org[.]apache[.]grails[.]gradle.*', '.*', '.*-SNAPSHOT')
            }
            mavenContent {
                snapshotsOnly()
            }
        }
        maven {
            url = 'https://central.sonatype.com/repository/maven-snapshots'
            content {
                includeVersionByRegex('cloud[.]wondrify.*', '.*', '.*-SNAPSHOT')
            }
            mavenContent {
                snapshotsOnly()
            }
        }
        maven {
            url = 'https://repository.apache.org/content/groups/staging'
            content {
                includeModuleByRegex('org[.]apache[.]grails[.]gradle', 'grails-publish')
            }
            mavenContent {
                releasesOnly()
            }
        }
    }
}

plugins {
    id 'com.gradle.develocity' version '4.1.1'
    id 'com.gradle.common-custom-user-data-gradle-plugin' version '2.3'
}

def isCI = System.getenv().containsKey('CI')
def isLocal = !isCI
def isReproducibleBuild = System.getenv('SOURCE_DATE_EPOCH') != null
if (isReproducibleBuild) {
    gradle.settingsEvaluated {
        logger.warn('*************** Remote Build Cache Disabled due to Reproducible Build ********************')
        logger.warn("Build date will be set to (SOURCE_DATE_EPOCH=${System.getenv("SOURCE_DATE_EPOCH")})")
    }
}

develocity {
    server = 'https://ge.grails.org'
    buildScan {
        tag('grails')
        tag('grails-core')
        publishing.onlyIf { it.authenticated }
        uploadInBackground = isLocal
    }
}

buildCache {
    local { enabled = (isLocal && !isReproducibleBuild) || (isCI && isReproducibleBuild) }
    remote(develocity.buildCache) {
        push = isCI
        enabled = !isReproducibleBuild
    }
}

rootProject.name = 'grails.core.ROOT'

include(
        'grails-bootstrap',
        'grails-cache',
        'grails-codecs-core',
        'grails-codecs',
        'grails-common',
        'grails-console',
        'grails-controllers',
        'grails-converters',
        'grails-core',
        'grails-databinding-core',
        'grails-databinding',
        'grails-datasource',
        'grails-domain-class',
        'grails-encoder',
        'grails-fields',
        'grails-geb',
        'grails-i18n',
        'grails-interceptors',
        'grails-logging',
        'grails-mimetypes',
        'grails-rest-transforms',
        'grails-scaffolding',
        'grails-services',
        'grails-shell-cli',
        'grails-spring',
        'grails-test-core',
        'grails-testing-support-core',
        'grails-testing-support-mongodb',
        'grails-testing-support-views-gson',
        'grails-testing-support-web',
        'grails-url-mappings',
        'grails-validation',
        'grails-views-core',
        'grails-views-gson',
        'grails-views-markup',
        'grails-web-core',
        'grails-web-common',
        'grails-web-boot',
        'grails-web-databinding',
        'grails-web-mvc',
        'grails-web-url-mappings',

        // POM / BOMs
        'grails-dependencies-starter-web',
        'grails-dependencies-test',
        'grails-dependencies-assets',
        'grails-bom',

        // Docs
        'grails-doc',

        // Test Suite
        'grails-test-suite-base',
        'grails-test-suite-persistence',
        'grails-test-suite-uber',
        'grails-test-suite-web',

        // events
        'grails-events-core',
        'grails-events-compat',
        'grails-events-transforms',
        'grails-events-gpars',
        'grails-events-spring',
        'grails-events-rxjava',
        'grails-events-rxjava2',
        'grails-events-rxjava3',
        'grails-events',

        // async
        'grails-async-core',
        'grails-async-gpars',
        'grails-async-rxjava',
        'grails-async-rxjava2',
        'grails-async-rxjava3',
        'grails-async',

        // gsp
        'grails-gsp-core',
        'grails-gsp',
        'grails-sitemesh3',
        'grails-layout',
        'grails-taglib',
        'grails-web-gsp',
        'grails-web-gsp-taglib',
        'grails-web-jsp',
        'grails-web-taglib',
        'grails-gsp-spring-boot',

        'grails-datastore-core',
        'grails-datastore-web',

        // Core GORM Implementation projects
        'grails-datamapping-core',
        'grails-datastore-async',
        'grails-datamapping-async',
        'grails-datamapping-validation',
        'grails-datamapping-support',
        'grails-datamapping-tck',
        'grails-datamapping-core-test',

        // RX projects
        // until this can be finished ported to grails 7, we are disabling
        //'grails-datamapping-rx',

        // Testing Support
        'grails-testing-support-datamapping',

        // Test Report
        'grails-data-test-report',

        // wrapper
        'grails-wrapper',

        // micronaut plugin for integration with Grails
        'grails-micronaut',
)

// Instead of using : to separate nested projects, fix the project directories by setting the projectDir explicitly
// (Preserve the parent directory name as part of the nested project name)
project(':grails-events-core').projectDir = file('grails-events/core')
project(':grails-events-compat').projectDir = file('grails-events/compat')
project(':grails-events-transforms').projectDir = file('grails-events/transforms')
project(':grails-events-gpars').projectDir = file('grails-events/gpars')
project(':grails-events-spring').projectDir = file('grails-events/spring')
project(':grails-events-rxjava').projectDir = file('grails-events/rxjava')
project(':grails-events-rxjava2').projectDir = file('grails-events/rxjava2')
project(':grails-events-rxjava3').projectDir = file('grails-events/rxjava3')
project(':grails-events').projectDir = file('grails-events/plugin')

project(':grails-async-core').projectDir = file('grails-async/core')
project(':grails-async-gpars').projectDir = file('grails-async/gpars')
project(':grails-async-rxjava').projectDir = file('grails-async/rxjava')
project(':grails-async-rxjava2').projectDir = file('grails-async/rxjava2')
project(':grails-async-rxjava3').projectDir = file('grails-async/rxjava3')
project(':grails-async').projectDir = file('grails-async/plugin')

project(':grails-dependencies-starter-web').projectDir = file('grails-dependencies/starter-web')
project(':grails-dependencies-test').projectDir = file('grails-dependencies/test')
project(':grails-dependencies-assets').projectDir = file('grails-dependencies/assets')

project(':grails-gsp-core').projectDir = file('grails-gsp/core')
project(':grails-gsp').projectDir = file('grails-gsp/plugin')
project(':grails-sitemesh3').projectDir = file('grails-gsp/grails-sitemesh3')
project(':grails-layout').projectDir = file('grails-gsp/grails-layout')
project(':grails-taglib').projectDir = file('grails-gsp/grails-taglib')
project(':grails-web-gsp').projectDir = file('grails-gsp/grails-web-gsp')
project(':grails-web-gsp-taglib').projectDir = file('grails-gsp/grails-web-gsp-taglib')
project(':grails-web-jsp').projectDir = file('grails-gsp/grails-web-jsp')
project(':grails-web-taglib').projectDir = file('grails-gsp/grails-web-taglib')
project(':grails-gsp-spring-boot').projectDir = file('grails-gsp/spring-boot')

// Data Mapping - Documentation
include 'grails-data-docs-guide-developer'
project(':grails-data-docs-guide-developer').projectDir = file('grails-data-docs/guide-developer')
// until this can be finished ported to grails 7, we are disabling
// include 'grails-data-docs-guide-rx'
// project(':grails-data-docs-guide-rx').projectDir = file('docs/guide-rx')
include 'grails-data-docs-guide-whats-new'
project(':grails-data-docs-guide-whats-new').projectDir = file('grails-data-docs/guide-whats-new')
include 'grails-data-docs-stage'
project(':grails-data-docs-stage').projectDir = file('grails-data-docs/stage')

// Data Mapping - Simple (for simple gorm usage, unit tests, and base implementation testing)
include 'grails-data-simple'
findProject(':grails-data-simple').projectDir = file('grails-data-simple')

// Data Mapping - Hibernate 5
// Core
include 'grails-data-hibernate5-core'
findProject(':grails-data-hibernate5-core').projectDir = file('grails-data-hibernate5/core')

// Documentation
include 'grails-data-hibernate5-docs'
findProject(':grails-data-hibernate5-docs').projectDir = new File(settingsDir, 'grails-data-hibernate5/docs')

// Plugins
include ':grails-data-hibernate5'
findProject(':grails-data-hibernate5').projectDir = new File(settingsDir, 'grails-data-hibernate5/grails-plugin')

include 'grails-data-hibernate5-dbmigration'
findProject(':grails-data-hibernate5-dbmigration').projectDir = new File(settingsDir, 'grails-data-hibernate5/dbmigration')

include 'grails-data-hibernate5-spring-boot'
findProject(':grails-data-hibernate5-spring-boot').projectDir = new File(settingsDir, 'grails-data-hibernate5/boot-plugin')

// functional tests - Hibernate5 examples
include 'grails-test-examples-hibernate5-grails-hibernate'
project(':grails-test-examples-hibernate5-grails-hibernate').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/grails-hibernate')

include 'grails-test-examples-hibernate5-grails-multiple-datasources'
project(':grails-test-examples-hibernate5-grails-multiple-datasources').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/grails-multiple-datasources')

include 'grails-test-examples-hibernate5-grails-database-per-tenant'
project(':grails-test-examples-hibernate5-grails-database-per-tenant').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/grails-database-per-tenant')

include 'grails-test-examples-hibernate5-grails-schema-per-tenant'
project(':grails-test-examples-hibernate5-grails-schema-per-tenant').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/grails-schema-per-tenant')

include 'grails-test-examples-hibernate5-grails-partitioned-multi-tenancy'
project(':grails-test-examples-hibernate5-grails-partitioned-multi-tenancy').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/grails-partitioned-multi-tenancy')

include 'grails-test-examples-hibernate5-standalone-hibernate'
project(':grails-test-examples-hibernate5-standalone-hibernate').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/standalone-hibernate')

include 'grails-test-examples-hibernate5-spring-boot-hibernate'
project(':grails-test-examples-hibernate5-spring-boot-hibernate').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/spring-boot-hibernate')

include 'grails-test-examples-hibernate5-grails-data-service'
project(':grails-test-examples-hibernate5-grails-data-service').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/grails-data-service')

include 'grails-test-examples-hibernate5-grails-hibernate-groovy-proxy'
project(':grails-test-examples-hibernate5-grails-hibernate-groovy-proxy').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/grails-hibernate-groovy-proxy')

include 'grails-test-examples-hibernate5-issue450'
project(':grails-test-examples-hibernate5-issue450').projectDir = new File(settingsDir, 'grails-test-examples/hibernate5/issue450')

// Mongodb
// core
include 'grails-data-mongodb-core'
project(':grails-data-mongodb-core').projectDir = new File(settingsDir, 'grails-data-mongodb/core')

include 'grails-data-mongodb-bson'
project(':grails-data-mongodb-bson').projectDir = new File(settingsDir, 'grails-data-mongodb/bson')

include 'grails-data-mongodb-ext'
project(':grails-data-mongodb-ext').projectDir = new File(settingsDir, 'grails-data-mongodb/ext')

// documentation
include 'grails-data-mongodb-docs'
project(':grails-data-mongodb-docs').projectDir = new File(settingsDir, 'grails-data-mongodb/docs')

// plugins
include 'grails-data-mongodb-spring-boot'
project(':grails-data-mongodb-spring-boot').projectDir = new File(settingsDir, 'grails-data-mongodb/boot-plugin')

include 'grails-data-mongodb'
project(':grails-data-mongodb').projectDir = new File(settingsDir, 'grails-data-mongodb/grails-plugin')

include 'grails-data-mongodb-gson-templates'
project(':grails-data-mongodb-gson-templates').projectDir = new File(settingsDir, 'grails-data-mongodb/gson-templates')

// functional tests - mongodb examples
include 'grails-test-examples-mongodb-base'
project(':grails-test-examples-mongodb-base').projectDir = new File(settingsDir, 'grails-test-examples/mongodb/base')

include 'grails-test-examples-mongodb-database-per-tenant'
project(':grails-test-examples-mongodb-database-per-tenant').projectDir = new File(settingsDir, 'grails-test-examples/mongodb/database-per-tenant')

include 'grails-test-examples-mongodb-hibernate5'
project(':grails-test-examples-mongodb-hibernate5').projectDir = new File(settingsDir, 'grails-test-examples/mongodb/hibernate5')

include 'grails-test-examples-mongodb-springboot'
project(':grails-test-examples-mongodb-springboot').projectDir = new File(settingsDir, 'grails-test-examples/mongodb/springboot')

include 'grails-test-examples-mongodb-test-data-service'
project(':grails-test-examples-mongodb-test-data-service').projectDir = new File(settingsDir, 'grails-test-examples/mongodb/test-data-service')

include 'grails-test-examples-mongodb-gson-templates'
project(':grails-test-examples-mongodb-gson-templates').projectDir = new File(settingsDir, 'grails-test-examples/mongodb/gson-templates')

// profiles
include 'grails-profiles-base'
project(':grails-profiles-base').projectDir = file('grails-profiles/base')

include 'grails-profiles-profile'
project(':grails-profiles-profile').projectDir = file('grails-profiles/profile')

include 'grails-profiles-plugin'
project(':grails-profiles-plugin').projectDir = file('grails-profiles/plugin')

include 'grails-profiles-rest-api'
project(':grails-profiles-rest-api').projectDir = file('grails-profiles/rest-api')

include 'grails-profiles-rest-api-plugin'
project(':grails-profiles-rest-api-plugin').projectDir = file('grails-profiles/rest-api-plugin')

include 'grails-profiles-web'
project(':grails-profiles-web').projectDir = file('grails-profiles/web')

include 'grails-profiles-web-plugin'
project(':grails-profiles-web-plugin').projectDir = file('grails-profiles/web-plugin')

// Functional Tests
include(
        'grails-test-examples-async-events-pubsub-demo',
        'grails-test-examples-app1',
        'grails-test-examples-app2',
        'grails-test-examples-app3',
        'grails-test-examples-external-configuration',
        'grails-test-examples-exploded',
        'grails-test-examples-datasources',
        'grails-test-examples-namespaces',
        'grails-test-examples-geb',
        'grails-test-examples-geb-gebconfig',
        'grails-test-examples-gorm',
        'grails-test-examples-gsp-layout',
        'grails-test-examples-gsp-sitemesh3',
        'grails-test-examples-gsp-spring-boot',
        'grails-test-examples-issue-698-domain-save-npe',
        'grails-test-examples-hyphenated',
        'grails-test-examples-issue-views-182',
        'grails-test-examples-issue-11102',
        'grails-test-examples-demo33',
        'grails-test-examples-micronaut',
        'grails-test-examples-plugins-loadfirst',
        'grails-test-examples-plugins-loadsecond',
        'grails-test-examples-plugins-loadafter',
        'grails-test-examples-plugins-issue11005',
        'grails-test-examples-issue-11767',
        'grails-test-examples-plugins-exploded',
        'grails-test-examples-plugins-issue-11767',
        'grails-test-examples-cache',
        'grails-test-examples-scaffolding',
        'grails-test-examples-views-functional-tests',
        'grails-test-examples-views-functional-tests-plugin'
)
project(':grails-test-examples-async-events-pubsub-demo').projectDir = file('grails-test-examples/async-events-pubsub-demo')
project(':grails-test-examples-app1').projectDir = file('grails-test-examples/app1')
project(':grails-test-examples-app2').projectDir = file('grails-test-examples/app2')
project(':grails-test-examples-app3').projectDir = file('grails-test-examples/app3')
project(':grails-test-examples-external-configuration').projectDir = file('grails-test-examples/external-configuration')
project(':grails-test-examples-exploded').projectDir = file('grails-test-examples/exploded')
project(':grails-test-examples-datasources').projectDir = file('grails-test-examples/datasources')
project(':grails-test-examples-geb').projectDir = file('grails-test-examples/geb')
project(':grails-test-examples-geb-gebconfig').projectDir = file('grails-test-examples/geb-gebconfig')
project(':grails-test-examples-namespaces').projectDir = file('grails-test-examples/namespaces')
project(':grails-test-examples-gorm').projectDir = file('grails-test-examples/gorm')
project(':grails-test-examples-gsp-layout').projectDir = file('grails-test-examples/gsp-layout')
project(':grails-test-examples-gsp-sitemesh3').projectDir = file('grails-test-examples/gsp-sitemesh3')
project(':grails-test-examples-gsp-spring-boot').projectDir = file('grails-test-examples/gsp-spring-boot/app')
project(':grails-test-examples-issue-698-domain-save-npe').projectDir = file('grails-test-examples/issue-698-domain-save-npe')
project(':grails-test-examples-hyphenated').projectDir = file('grails-test-examples/hyphenated')
project(':grails-test-examples-issue-views-182').projectDir = file('grails-test-examples/issue-views-182')
project(':grails-test-examples-issue-11102').projectDir = file('grails-test-examples/issue-11102')
project(':grails-test-examples-demo33').projectDir = file('grails-test-examples/demo33')
project(':grails-test-examples-micronaut').projectDir = file('grails-test-examples/micronaut')
project(':grails-test-examples-plugins-loadfirst').projectDir = file('grails-test-examples/plugins/loadfirst')
project(':grails-test-examples-plugins-loadsecond').projectDir = file('grails-test-examples/plugins/loadsecond')
project(':grails-test-examples-plugins-loadafter').projectDir = file('grails-test-examples/plugins/loadafter')
project(':grails-test-examples-plugins-issue11005').projectDir = file('grails-test-examples/plugins/issue11005')
project(':grails-test-examples-issue-11767').projectDir = file('grails-test-examples/issue-11767')
project(':grails-test-examples-plugins-exploded').projectDir = file('grails-test-examples/plugins/exploded')
project(':grails-test-examples-plugins-issue-11767').projectDir = file('grails-test-examples/plugins/issue-11767')
project(':grails-test-examples-cache').projectDir = file('grails-test-examples/cache')
project(':grails-test-examples-scaffolding').projectDir = file('grails-test-examples/scaffolding')
project(':grails-test-examples-views-functional-tests').projectDir = file('grails-test-examples/views-functional-tests')
project(':grails-test-examples-views-functional-tests-plugin').projectDir = file('grails-test-examples/views-functional-tests-plugin')

includeBuild('./grails-gradle') {
    name = 'grails-gradle'
}

// Due to https://github.com/gradle/gradle/issues/2986 , we can't change the global exclude for profiles, it must be done everywhere
for (String pattern in DirectoryScanner.defaultExcludes) {
    if (pattern.contains('gitignore') || pattern.contains('gitattributes')) {
        DirectoryScanner.removeDefaultExclude(pattern)
    }
}

dependencyResolutionManagement {
    repositoriesMode = RepositoriesMode.FAIL_ON_PROJECT_REPOS
    repositories {
        // mavenLocal() // Keep, this will be uncommented and used by CI (groovy-joint-workflow)
        maven { url = 'https://repo.grails.org/grails/restricted' }
        maven {
            url = 'https://repository.apache.org/content/groups/snapshots'
            content {
                includeVersionByRegex('org[.]apache[.]grails.*', '.*', '.*-SNAPSHOT')
                includeVersionByRegex('org[.]apache[.]groovy.*', '.*', '.*-SNAPSHOT')
            }
        }
        maven {
            url = 'https://central.sonatype.com/repository/maven-snapshots'
            content {
                includeVersionByRegex('cloud[.]wondrify.*', '.*', '.*-SNAPSHOT')
            }
            mavenContent {
                snapshotsOnly()
            }
        }
        maven {
            url = 'https://repository.apache.org/content/groups/staging'
            content {
                includeModuleByRegex('org[.]apache[.]grails[.]gradle', 'grails-publish')
                includeModuleByRegex('org[.]apache[.]groovy[.]geb', 'geb-.*')
            }
            mavenContent {
                releasesOnly()
            }
        }
    }
}
