Skip to content

Commit

Permalink
Prototype on how to auto-accept ToS for build scans
Browse files Browse the repository at this point in the history
Fixes #1173
  • Loading branch information
donat committed Jun 2, 2022
1 parent 710b27e commit 859d66a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -133,7 +136,7 @@ private static List<String> collectArguments(List<String> baseArgs, boolean buil
arguments.add("--offline");
}
arguments.addAll(CorePlugin.invocationCustomizer().getExtraArguments());
arguments.addAll(getInitScriptArguments());
arguments.addAll(getInitScriptArguments(buildScansEnabled));
return arguments;
}

Expand All @@ -145,31 +148,63 @@ private static String buildScanArgumentFor(BuildEnvironment environment) {
}
}

private static List<String> getInitScriptArguments() {
File initScript = getEclipsePluginInitScriptLocation();
private static List<String> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@
initscript {
allprojects {
apply plugin: "eclipse"
afterEvaluate {
if (project.parent == null) {
BUILD_SCANS_CONFIGURATION
}
}
}
}

0 comments on commit 859d66a

Please sign in to comment.