From 859d66ae7bf05fc9d2e0c2c5383b8bc2e64e9e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Don=C3=A1t=20Csik=C3=B3s?= Date: Thu, 2 Jun 2022 05:54:12 -0700 Subject: [PATCH] Prototype on how to auto-accept ToS for build scans Fixes #1173 --- .../configuration/GradleArguments.java | 73 ++++++++++++++----- .../configuration/eclipsePlugin.gradle | 5 ++ 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/org.eclipse.buildship.core/src/main/java/org/eclipse/buildship/core/internal/configuration/GradleArguments.java b/org.eclipse.buildship.core/src/main/java/org/eclipse/buildship/core/internal/configuration/GradleArguments.java index 234f093818..67e0385147 100644 --- a/org.eclipse.buildship.core/src/main/java/org/eclipse/buildship/core/internal/configuration/GradleArguments.java +++ b/org.eclipse.buildship.core/src/main/java/org/eclipse/buildship/core/internal/configuration/GradleArguments.java @@ -14,6 +14,7 @@ import java.io.InputStream; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; @@ -27,6 +28,7 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; +import com.google.common.io.ByteStreams; import com.google.common.io.Files; import org.eclipse.buildship.core.GradleDistribution; @@ -45,7 +47,8 @@ */ public final class GradleArguments { - private static final String INITSCRIPT_LOCATION="/org/eclipse/buildship/core/internal/configuration/eclipsePlugin.gradle"; + private static final String INITSCRIPT_NAME = "eclipsePlugin.gradle"; + private static final String INITSCRIPT_LOCATION="/org/eclipse/buildship/core/internal/configuration/" + INITSCRIPT_NAME; private final File rootDir; private final GradleDistribution gradleDistribution; @@ -133,7 +136,7 @@ private static List collectArguments(List baseArgs, boolean buil arguments.add("--offline"); } arguments.addAll(CorePlugin.invocationCustomizer().getExtraArguments()); - arguments.addAll(getInitScriptArguments()); + arguments.addAll(getInitScriptArguments(buildScansEnabled)); return arguments; } @@ -145,31 +148,63 @@ private static String buildScanArgumentFor(BuildEnvironment environment) { } } - private static List getInitScriptArguments() { - File initScript = getEclipsePluginInitScriptLocation(); + private static List getInitScriptArguments(boolean buildScansEnabled) { + File initScriptLocation = getEclipsePluginInitScriptLocation(); try { - if (!initScript.exists()) { - Files.createParentDirs(initScript); - Files.touch(initScript); - URL resource = GradleVersion.class.getResource(INITSCRIPT_LOCATION); - if (resource == null) { - throw new GradlePluginsRuntimeException(String.format("Resource '%s' not found.", INITSCRIPT_LOCATION)); - } - - URLConnection connection = resource.openConnection(); - try (InputStream inputStream = connection.getInputStream()) { - Files.asByteSink(initScript).writeFrom(inputStream); - } - + String existingInitScriptText = readInitScriptContent(initScriptLocation); + String newInitScriptContent = createInitScriptContent(buildScansEnabled); + if (existingInitScriptText == null || !existingInitScriptText.equals(newInitScriptContent)) { + writeInitScript(initScriptLocation, newInitScriptContent); } - return Arrays.asList("--init-script", initScript.getAbsolutePath()); + return Arrays.asList("--init-script", initScriptLocation.getAbsolutePath()); } catch (IOException e) { throw new GradlePluginsRuntimeException("Failed to create init script", e); } } + + + private static void writeInitScript(File initScript, String content) throws IOException { + Files.createParentDirs(initScript); + Files.touch(initScript); + URL resource = GradleVersion.class.getResource(INITSCRIPT_LOCATION); + if (resource == null) { + throw new GradlePluginsRuntimeException(String.format("Resource '%s' not found.", INITSCRIPT_LOCATION)); + } + + URLConnection connection = resource.openConnection(); + try (InputStream inputStream = connection.getInputStream()) { + Files.asByteSink(initScript).write(content.getBytes()); + } + } + + private static String readInitScriptContent(File initScript) throws IOException { + if (initScript.exists() && initScript.isFile()) { + return Files.asCharSource(initScript, StandardCharsets.UTF_8).read(); + } else { + return null; + } + } + + private static String createInitScriptContent(boolean buildScansEnabled) throws IOException { + URL resource = GradleVersion.class.getResource(INITSCRIPT_LOCATION); + if (resource == null) { + throw new GradlePluginsRuntimeException(String.format("Resource '%s' not found.", INITSCRIPT_LOCATION)); + } + + URLConnection connection = resource.openConnection(); + try (InputStream inputStream = connection.getInputStream()) { + String buildScansConfig = !buildScansEnabled ? "" : "buildScan {\n" + + " termsOfServiceUrl = 'https://gradle.com/terms-of-service'\n" + + " termsOfServiceAgree = 'yes'\n" + + "}\n"; + + return new String(ByteStreams.toByteArray(inputStream)).replace("BUILD_SCANS_CONFIGURATION", buildScansConfig); + } + } + private static File getEclipsePluginInitScriptLocation() { - return CorePlugin.getInstance().getStateLocation().append("init.d").append("eclipsePlugin.gradle").toFile(); + return CorePlugin.getInstance().getStateLocation().append("init.d").append(INITSCRIPT_NAME).toFile(); } } diff --git a/org.eclipse.buildship.core/src/main/resources/org/eclipse/buildship/core/internal/configuration/eclipsePlugin.gradle b/org.eclipse.buildship.core/src/main/resources/org/eclipse/buildship/core/internal/configuration/eclipsePlugin.gradle index d30c9240dd..2f1a421a07 100644 --- a/org.eclipse.buildship.core/src/main/resources/org/eclipse/buildship/core/internal/configuration/eclipsePlugin.gradle +++ b/org.eclipse.buildship.core/src/main/resources/org/eclipse/buildship/core/internal/configuration/eclipsePlugin.gradle @@ -10,5 +10,10 @@ initscript { allprojects { apply plugin: "eclipse" + afterEvaluate { + if (project.parent == null) { + BUILD_SCANS_CONFIGURATION + } + } } } \ No newline at end of file