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

fix: Search gradle executable to infer gradle home #95

Merged
merged 3 commits into from
Oct 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@

package com.microsoft.java.bs.core.internal.gradle;

import static com.microsoft.java.bs.core.Launcher.LOGGER;

import java.io.File;
import java.net.URI;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.gradle.internal.impldep.org.apache.commons.lang.StringUtils;
import org.gradle.tooling.BuildException;
import org.gradle.tooling.BuildLauncher;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ModelBuilder;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.build.BuildEnvironment;
import org.gradle.util.GradleVersion;

import com.microsoft.java.bs.core.Launcher;
Expand Down Expand Up @@ -188,12 +187,53 @@ static File getGradleUserHomeFile(String gradleUserHome) {
return getFileFromEnvOrProperty(GRADLE_USER_HOME);
}

/**
* Infer the Gradle Home.
* TODO: simplify the method.
*/
static File getGradleHome(String gradleHome) {
File gradleHomeFolder = null;
if (StringUtils.isNotBlank(gradleHome)) {
return new File(gradleHome);
gradleHomeFolder = new File(gradleHome);
} else {
// find if there is a gradle executable in PATH
String path = System.getenv("PATH");
if (StringUtils.isNotBlank(path)) {
for (String p : path.split(File.pathSeparator)) {
File gradle = new File(p, "gradle");
if (gradle.exists() && gradle.isFile()) {
File gradleBinFolder = gradle.getParentFile();
if (gradleBinFolder != null && gradleBinFolder.isDirectory()
&& gradleBinFolder.getName().equals("bin")) {
File gradleLibFolder = new File(gradleBinFolder.getParent(), "lib");
if (gradleLibFolder.isDirectory()) {
File[] files = gradleLibFolder.listFiles();
if (files != null) {
Optional<File> gradleLauncherJar = Arrays.stream(files)
.filter(file -> file.isFile() && file.getName().startsWith("gradle-launcher-")
&& file.getName().endsWith(".jar"))
.findFirst();
if (gradleLauncherJar.isPresent()) {
gradleHomeFolder = gradleBinFolder.getParentFile();
break;
}
}
}
}
}
}
}
}

if (gradleHomeFolder == null) {
gradleHomeFolder = getFileFromEnvOrProperty(GRADLE_HOME);
}

if (gradleHomeFolder != null && gradleHomeFolder.isDirectory()) {
return gradleHomeFolder;
}

return getFileFromEnvOrProperty(GRADLE_HOME);
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ void testPreferencesPriority_gradleVersionSet() {
void testPreferencesPriority_gradleHomeSet() {
Preferences preferences = mock(Preferences.class);
when(preferences.isWrapperEnabled()).thenReturn(false);
when(preferences.getGradleHome()).thenReturn(new File("").getAbsolutePath());
when(preferences.getGradleHome()).thenReturn(new File(System.getProperty("java.io.tmpdir"))
.getAbsolutePath());
assertEquals(GradleBuildKind.SPECIFIED_INSTALLATION,
Utils.getEffectiveBuildKind(projectDir, preferences));
}
Expand Down
Loading