package io.freefair.gradle.plugins.git;

import org.codehaus.groovy.runtime.ProcessGroovyMethods;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;

public class GitVersionPlugin implements Plugin<Project> {

    private Project project;
    private Logger logger;

    @Override
    public void apply(Project project) {
        this.project = project;
        this.logger = project.getLogger();
        project.setVersion(resolveVersion());

        project.allprojects(p -> p.setVersion(project.getVersion()));
    }

    private Object resolveVersion() {
        if (!"unspecified".equals(project.getVersion().toString())) {
            logger.lifecycle("Using explicit version {}", project.getVersion());
            return project.getVersion();
        }

        String travisTag = System.getenv("TRAVIS_TAG");
        if (travisTag != null && !travisTag.trim().isEmpty()) {
            logger.lifecycle("Using TRAVIS_TAG as version: {}", travisTag);
            return travisTag;
        }

        String travisBranch = System.getenv("TRAVIS_BRANCH");
        if (travisBranch != null) {
            travisBranch = travisBranch.replace("/", "-");
            String version = travisBranch + "-SNAPSHOT";
            logger.lifecycle("Using TRAVIS_BRANCH as version: {}", version);
            return version;
        }

        try {
            Process execute = ProcessGroovyMethods.execute("git describe --tags --exact-match");
            String gitTag = ProcessGroovyMethods.getText(execute).trim();

            if (!gitTag.isEmpty()) {
                logger.lifecycle("Using git tag as version: {}", gitTag);
                return gitTag;
            }
        } catch (Exception e) {
            logger.debug("Failed to get current git tag", e);
        }

        try {
            Process execute = ProcessGroovyMethods.execute("git symbolic-ref --short HEAD");
            String gitBranch = ProcessGroovyMethods.getText(execute).trim();

            if (!gitBranch.isEmpty()) {
                gitBranch = gitBranch.replace("/", "-");
                String version = gitBranch + "-SNAPSHOT";
                logger.lifecycle("Using git branch as version: {}", version);
                return version;
            }
        } catch (Exception e) {
            logger.debug("Failed to get current git branch", e);
        }

        return project.getVersion();
    }
}

@@ -1362,32 +1362,903 @@
 ion;%0A        %7D%0A%0A
+        if (%22true%22.equalsIgnoreCase(System.getenv(%22GITHUB_ACTIONS%22))) %7B%0A            String githubRef = System.getenv(%22GITHUB_REF%22);%0A            if (githubRef != null) %7B%0A                if (githubRef.startsWith(%22refs/tags/%22)) %7B%0A                    githubRef = githubRef.substring(%22refs/tags/%22.length());%0A                    logger.lifecycle(%22Using GitHub Tag as version: %7B%7D%22, githubRef);%0A                    return githubRef;%0A                %7D%0A                else if (githubRef.startsWith(%22refs/heads/%22)) %7B%0A                    githubRef = githubRef.substring(%22refs/heads/%22.length());%0A                    githubRef = githubRef.replace(%22/%22, %22-%22);%0A                    String version = githubRef + %22-SNAPSHOT%22;%0A                    logger.lifecycle(%22Using GitHub Branch as version: %7B%7D%22, version);%0A                    return version;%0A                %7D%0A            %7D%0A        %7D%0A%0A
         try %7B%0A  

Improve Github Actions support