All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.gradle_plugins.toolkit.build_logic.properties.gradle Maven / Gradle / Ivy

import java.util.stream.Stream
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Constants
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.eclipse.jgit.transport.URIish
import org.gradle.util.GradleVersion

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

buildscript {
    dependencies {
        classpath('org.eclipse.jgit:org.eclipse.jgit:5.13.3.202401111512-r') {
            exclude(group: 'org.slf4j')
        }
    }
    repositories {
        mavenCentral()
    }
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

allprojects {
    File rootProjectDir = project.rootProjectDir

    if (project.isBuildSrcProject) {
        Properties props = new Properties()
        File gradlePropertiesFile = new File(rootProjectDir, 'gradle.properties')
        if (gradlePropertiesFile.isFile()) {
            gradlePropertiesFile.newInputStream().withCloseable { props.load(it) }
        }
        props.forEach { key, value ->
            if (!rootProject.hasProperty(key)) {
                rootProject.ext[key] = value
            }
        }
    }

    File ciPropertiesFile = new File(rootProjectDir, 'build/gradle-ci.properties')
    synchronized (rootProject) {
        if (!rootProject.hasProperty('ciProperties')) {
            Properties props = new Properties()
            rootProject.ext['ciProperties'] = props
            if (ciPropertiesFile.isFile()) {
                ciPropertiesFile.newInputStream().withCloseable { props.load(it) }
            }
        }
    }

    Closure getCiProperty = project.ext.getCiProperty = { String name, Object defaultValue = null, boolean storeValue = false ->
        Properties ciProperties = rootProject.ext['ciProperties']

        String value = Stream.of(name)
            .flatMap { Stream.of(it, 'remal-gradle-plugins.' + it) }
            .flatMap {
                Stream.of(
                    ciProperties.getProperty(name),
                    project.findProperty(it)?.toString(),
                    System.getProperty(it),
                    System.getenv(it.replaceAll(/\W/, '_').toUpperCase()),
                )
            }
            .filter { it != null }
            .findFirst()
            .orElse(null)

        if (value != null) {
            if (storeValue) {
                ciProperties.setProperty(name, value)
                synchronized (rootProject) {
                    ciPropertiesFile.parentFile.mkdirs()
                    ciPropertiesFile.newOutputStream().withCloseable { ciProperties.store(it, null) }
                }
            }
            return value
        }

        defaultValue = project.unwrapProviders(defaultValue)
        return defaultValue?.toString()
    }

    Closure withGitRepository = { Closure action ->
        File gitDir = new File(rootProjectDir, Constants.DOT_GIT)
        if (!gitDir.exists()) {
            return null
        }

        FileRepositoryBuilder.create(gitDir).withCloseable { Repository repository ->
            return action(repository)
        }
    }
    Closure withGit = { Closure action ->
        withGitRepository { Repository repository ->
            new Git(repository).withCloseable { Git git ->
                return action(git)
            }
        }
    }
    Closure getGitRemoteUri = { String remoteName = Constants.DEFAULT_REMOTE_NAME ->
        withGit { Git git ->
            return git.remoteList().call().stream()
                .filter { it.name == remoteName }
                .flatMap { it.getURIs().stream() }
                .filter { it != null }
                .findFirst()
                .orElse(null)
        }
    }
    Closure getRepositoryFullNameFromGitRemote = { String remoteName = Constants.DEFAULT_REMOTE_NAME ->
        URIish uri = getGitRemoteUri(remoteName)
        if (uri == null) {
            return null
        }
        String fullName = uri.path
        fullName = fullName.replaceFirst(/\.git$/, '')
        return fullName
    }


    if (rootProject.findProperty('isJavaRuntimeVersionSet') == null) {
        rootProject.ext.isJavaRuntimeVersionSet = getCiProperty('java-runtime.version', null) != null
    }
    if (rootProject.findProperty('isGradleApiVersionSet') == null) {
        rootProject.ext.isGradleApiVersionSet = getCiProperty('gradle-api.version', null) != null
    }

    project.ext['java-runtime.version'] = getCiProperty('java-runtime.version', JavaVersion.current().majorVersion)
    project.ext['java-runtime.min-version'] = getCiProperty('java-runtime.min-version', JavaVersion.current().majorVersion)
    project.ext['gradle-api.version'] = getCiProperty('gradle-api.version', GradleVersion.current().version)
    project.ext['gradle-api.min-version'] = getCiProperty('gradle-api.min-version', GradleVersion.current().version)

    project.ext.javaRuntimeVersion = JavaVersion.toVersion(project.ext['java-runtime.version'])
    project.ext.javaRuntimeMinVersion = JavaVersion.toVersion(project.ext['java-runtime.min-version'])
    project.ext.gradleApiVersion = GradleVersion.version(project.ext['gradle-api.version'])
    project.ext.gradleApiMinVersion = GradleVersion.version(project.ext['gradle-api.min-version'])

    project.ext['git-ref'] = getCiProperty('git-ref', '')
    project.ext['git-sha'] = getCiProperty('git-sha', '')

    project.ext['github-server-url'] = getCiProperty('github-server-url', 'https://github.com/')
    project.ext['github-api-url'] = getCiProperty('github-api-url', 'https://api.github.com/')
    project.ext['github-actions-token'] = getCiProperty('push-back-token', { getCiProperty('github-actions-token', '') })
    project.ext['github-actions-run-id'] = getCiProperty('github-actions-run-id', '')
    project.ext['github-actions-run-attempt'] = getCiProperty('github-actions-run-attempt', '')
    project.ext['github-actions-job'] = getCiProperty('github-actions-job', '')
    project.ext['github-actions-job-index'] = getCiProperty('github-actions-job-index', '')

    project.ext['disable-compilation'] = getCiProperty('disable-compilation', 'false')
    project.ext['disable-tests'] = getCiProperty('disable-tests', 'false')
    project.ext['disable-verification'] = getCiProperty('disable-verification', 'false')
    project.ext['disable-verification-except-tests'] = getCiProperty('disable-verification-except-tests', 'false')

    project.ext['repository-full-name'] = getCiProperty('repository-full-name', { getRepositoryFullNameFromGitRemote() ?: '' }, true)
    project.ext['repository-name'] = getCiProperty('repository-name', { project.ext['repository-full-name'].replaceFirst('^.*/', '') }, true)
    project.ext['repository-api-url'] = getCiProperty('repository-api-url', { project.ext['github-api-url'].replaceFirst('/+$', '') + '/repos/' + project.ext['repository-full-name'].replaceFirst('^/+', '') }, true)
    project.ext['repository-html-url'] = getCiProperty('repository-html-url', { project.ext['github-server-url'].replaceFirst('/+$', '') + '/' + project.ext['repository-full-name'].replaceFirst('^/+', '') }, true)
    project.ext['repository-description'] = getCiProperty('repository-description', '', true)
    project.ext['repository-topics'] = getCiProperty('repository-topics', '', true)
    project.ext['repository-is-template'] = getCiProperty('repository-is-template', '', true)
    project.ext['repository-owner-name'] = getCiProperty('repository-name', { project.ext['repository-full-name'].replaceFirst('/.*$', '') }, true)
    project.ext['repository-license-name'] = getCiProperty('repository-license-name', '', true)
    project.ext['repository-license-html-url'] = getCiProperty('repository-license-html-url', '', true)

    String currentVersion = project.version.toString()
    if (currentVersion == 'unspecified') {
        project.version = '0-SNAPSHOT'
        project.ext['majorVersion'] = '0'
    } else if (currentVersion.matches(/^(0|[1-9]\d*)(\D.*)?$/)) {
        project.ext['majorVersion'] = currentVersion.replaceFirst(/^(\d+).*/, '$1')
    } else {
        throw new GradleException("Can't extract major version for version '$currentVersion'")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy