Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access git info via ValueSource for Gradle Configuration Cache Compatibility #187

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ out
*.iml
*.ipr
*.iws

.vscode/
bin/
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
34 changes: 21 additions & 13 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -83,10 +85,9 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -133,26 +134,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -197,11 +201,15 @@ if "$cygwin" || "$msys" ; then
done
fi

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
22 changes: 12 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
16 changes: 7 additions & 9 deletions src/main/java/org/shipkit/auto/version/AutoVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@ class AutoVersion {

private final static Logger LOG = Logging.getLogger(AutoVersion.class);

private final ProcessRunner runner;
private final GitValueSourceProviderFactory gitValueSourceProviderFactory;
private final File versionFile;

AutoVersion(ProcessRunner runner, File versionFile) {
this.runner = runner;
AutoVersion(GitValueSourceProviderFactory gitValueSourceProviderFactory, File versionFile) {
this.versionFile = versionFile;
}

AutoVersion(File projectDir) {
this(new ProcessRunner(projectDir), new File(projectDir, "version.properties"));
this.gitValueSourceProviderFactory = gitValueSourceProviderFactory;
}

/**
Expand All @@ -43,14 +39,16 @@ DeducedVersion deduceVersion(Logger log, String projectVersion) {
VersionConfig config = VersionConfig.parseVersionFile(versionFile);

try {
Collection<VersionNumber> versions = new VersionsProvider(runner).getAllVersions(config.getTagPrefix());
VersionsProvider versionsProvider = new VersionsProvider(gitValueSourceProviderFactory);
Collection<VersionNumber> versions = versionsProvider.getAllVersions(config.getTagPrefix());
PreviousVersionFinder previousVersionFinder = new PreviousVersionFinder();

if (config.getVersionSpec().isPresent()) {
previousVersion = previousVersionFinder.findPreviousVersion(versions, config);
}

String nextVersion = new NextVersionPicker(runner, log).pickNextVersion(previousVersion,
NextVersionPicker nextVersionPicker = new NextVersionPicker(gitValueSourceProviderFactory, log);
String nextVersion = nextVersionPicker.pickNextVersion(previousVersion,
config, projectVersion);

if (!config.getVersionSpec().isPresent()) {
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/org/shipkit/auto/version/AutoVersionPlugin.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package org.shipkit.auto.version;

import java.io.File;

import javax.inject.Inject;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.provider.ProviderFactory;

/**
* The plugin, ideally with zero business logic, but only the Gradle integration code
* The plugin, ideally with zero business logic, but only the Gradle integration
* code
*/
public class AutoVersionPlugin implements Plugin<Project> {

private final ProviderFactory providerFactory;

@Inject
public AutoVersionPlugin(ProviderFactory providerFactory) {
this.providerFactory = providerFactory;
}

public void apply(Project project) {
DeducedVersion version = new AutoVersion(project.getProjectDir()).deduceVersion(project.getVersion().toString());
File versionFile = new File(project.getProjectDir(), "version.properties");
GitValueSourceProviderFactory gitValueSourceProviderFactory = new GitValueSourceProviderFactory(project.getProjectDir(), providerFactory);
AutoVersion autoVersion = new AutoVersion(gitValueSourceProviderFactory, versionFile);
DeducedVersion version = autoVersion.deduceVersion(project.getVersion().toString());

project.allprojects(p -> p.setVersion(version.getVersion()));
project.getExtensions().getExtraProperties().set("shipkit-auto-version.previous-version", version.getPreviousVersion());
project.getExtensions().getExtraProperties().set("shipkit-auto-version.previous-version",
version.getPreviousVersion());
project.getExtensions().getExtraProperties().set("shipkit-auto-version.previous-tag", version.getPreviousTag());
}
}
59 changes: 59 additions & 0 deletions src/main/java/org/shipkit/auto/version/GitValueSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.shipkit.auto.version;

import static java.lang.String.join;
import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.inject.Inject;

import org.gradle.api.provider.ValueSource;
import org.gradle.process.ExecOperations;
import org.gradle.process.ExecResult;

public abstract class GitValueSource implements ValueSource<String, GitValueSourceParameters> {

private final ExecOperations execOperations;

@Inject
public GitValueSource(ExecOperations execOperations) {
this.execOperations = execOperations;
}

@Override
public String obtain() {
File workDir = getParameters().getWorkingDirectory().get();

String[] commands = getParameters().getCommands().get();
String[] commandsArray = new String[commands.length + 1];
commandsArray[0] = "git";
System.arraycopy(commands, 0, commandsArray, 1, commands.length);

int exitValue;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ExecResult result;

try {
result = execOperations.exec(execSpec -> {
execSpec.commandLine((Object[]) commandsArray);
execSpec.workingDir(workDir);
execSpec.setStandardOutput(outputStream);
execSpec.setErrorOutput(outputStream);
});
} catch (Exception e) {
String cmdLine = join(" ", commandsArray);
throw new ShipkitAutoVersionException("Problems executing command:\n " + cmdLine, e);
}

String output = outputStream.toString();
exitValue = result.getExitValue();

if (exitValue != 0) {
String cmdLine = join(" ", commandsArray);
throw new ShipkitAutoVersionException(
"Problems executing command (exit code: " + exitValue + "): " + cmdLine + "\n" +
"Output:\n" + output);
}

return output;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.shipkit.auto.version;

import java.io.File;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.ValueSourceParameters;

public interface GitValueSourceParameters extends ValueSourceParameters {
Property<File> getWorkingDirectory();
Property<String[]> getCommands();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.shipkit.auto.version;

import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;

import java.io.File;

public class GitValueSourceProviderFactory {

private final File workingDirectory;
private final ProviderFactory providerFactory;

public GitValueSourceProviderFactory(File workingDirectory, ProviderFactory providerFactory) {
this.workingDirectory = workingDirectory;
this.providerFactory = providerFactory;
}

Provider<String> getProvider(String[] params) {
return providerFactory.of(GitValueSource.class, spec -> {
spec.parameters(parameters -> {
parameters.getCommands().set(params);
parameters.getWorkingDirectory().set(workingDirectory);
});
});
}

}
18 changes: 11 additions & 7 deletions src/main/java/org/shipkit/auto/version/NextVersionPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.provider.Provider;

import java.util.Optional;
import java.util.regex.Matcher;
Expand All @@ -14,11 +15,12 @@
* Picks the next version to use.
*/
class NextVersionPicker {
private final ProcessRunner runner;

private final Logger log;
private final GitValueSourceProviderFactory gitValueSourceProviderFactory;

NextVersionPicker(ProcessRunner runner, Logger log) {
this.runner = runner;
public NextVersionPicker(GitValueSourceProviderFactory gitValueSourceProviderFactory, Logger log) {
this.gitValueSourceProviderFactory = gitValueSourceProviderFactory;
this.log = log;
}

Expand All @@ -36,7 +38,8 @@ String pickNextVersion(Optional<VersionNumber> previousVersion, VersionConfig co
String result;

try {
tag = runner.run("git", "describe", "--tags").trim();
Provider<String> tagDescriptionProvider = gitValueSourceProviderFactory.getProvider(new String[]{"describe", "--tags"});
tag = tagDescriptionProvider.get().trim();
} catch (Exception e) {
result = "0.0.1-SNAPSHOT";
log.info("Process 'git describe --tags' exited with non-zero exit value. Assuming there are no tags. " +
Expand Down Expand Up @@ -65,15 +68,16 @@ String pickNextVersion(Optional<VersionNumber> previousVersion, VersionConfig co
}

if (!config.isWildcard()) {
//if there is no wildcard we will use the version 'as is'
// if there is no wildcard we will use the version 'as is'
explainVersion(log, config.getVersionSpec().get(), "uses verbatim version from version file");
return config.getVersionSpec().get();
}

if (previousVersion.isPresent() && previousVersion.get().satisfies(config.getVersionSpec().get())) {
VersionNumber prev = previousVersion.get();
String gitOutput = runner.run(
"git", "log", "--pretty=oneline", TagConvention.tagFor(prev.toString(), config.getTagPrefix()) + "..HEAD");
String[] params = { "log", "--pretty=oneline", TagConvention.tagFor(prev.toString(), config.getTagPrefix()) + "..HEAD" };
Provider<String> prettyLogProvider = gitValueSourceProviderFactory.getProvider(params);
String gitOutput = prettyLogProvider.get();
int commitCount = new CommitCounter().countCommitDelta(gitOutput);
String result = prev.incrementBy(commitCount).toString();
explainVersion(log, result, "deduced version based on previous tag: '" + prev + "'");
Expand Down
Loading
Loading