|
| 1 | +package com.gradle; |
| 2 | + |
| 3 | +import com.gradle.ccud.adapters.BuildCacheAdapter; |
| 4 | +import com.gradle.ccud.adapters.DevelocityAdapter; |
| 5 | +import com.gradle.ccud.adapters.develocity.DevelocityBuildCacheAdapter; |
| 6 | +import com.gradle.ccud.adapters.develocity.DevelocityConfigurationAdapter; |
| 7 | +import com.gradle.ccud.adapters.enterprise.BuildScanExtension_1_X_Adapter; |
| 8 | +import com.gradle.ccud.adapters.enterprise.GradleEnterpriseBuildCacheAdapter; |
| 9 | +import com.gradle.ccud.adapters.enterprise.GradleEnterpriseExtensionAdapter; |
| 10 | +import com.gradle.ccud.adapters.enterprise.proxies.GradleEnterpriseBuildCacheProxy; |
| 11 | +import com.gradle.ccud.adapters.reflection.ProxyFactory; |
| 12 | +import com.gradle.develocity.agent.gradle.buildcache.DevelocityBuildCache; |
| 13 | +import org.gradle.caching.configuration.AbstractBuildCache; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import static com.gradle.Utils.isGradle5OrNewer; |
| 19 | + |
| 20 | +class AdapterFactory { |
| 21 | + |
| 22 | + private AdapterFactory() { |
| 23 | + } |
| 24 | + |
| 25 | + private static final String DEVELOCITY_CONFIGURATION = "com.gradle.develocity.agent.gradle.DevelocityConfiguration"; |
| 26 | + private static final String GRADLE_ENTERPRISE_EXTENSION = "com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension"; |
| 27 | + private static final String BUILD_SCAN_EXTENSION = "com.gradle.scan.plugin.BuildScanExtension"; |
| 28 | + |
| 29 | + static DevelocityAdapter createDevelocityAdapter(Object gradleEnterpriseOrDevelocity) { |
| 30 | + if (isDevelocityConfiguration(gradleEnterpriseOrDevelocity)) { |
| 31 | + return new DevelocityConfigurationAdapter(gradleEnterpriseOrDevelocity); |
| 32 | + } else if (isGradleEnterpriseExtension(gradleEnterpriseOrDevelocity)) { |
| 33 | + return new GradleEnterpriseExtensionAdapter(gradleEnterpriseOrDevelocity); |
| 34 | + } else if (!isGradle5OrNewer() && isBuildScanExtension(gradleEnterpriseOrDevelocity)) { |
| 35 | + // Build Scan plugin only exposes the buildScan extension with a limited functionality |
| 36 | + return new BuildScanExtension_1_X_Adapter(gradleEnterpriseOrDevelocity); |
| 37 | + } |
| 38 | + |
| 39 | + throw new IllegalArgumentException("Provided extension of type '" + gradleEnterpriseOrDevelocity.getClass().getName() + "' is neither the Develocity configuration nor the Gradle Enterprise extension"); |
| 40 | + } |
| 41 | + |
| 42 | + static boolean isDevelocityConfiguration(Object gradleEnterpriseOrDevelocity) { |
| 43 | + return implementsInterface(gradleEnterpriseOrDevelocity, DEVELOCITY_CONFIGURATION); |
| 44 | + } |
| 45 | + |
| 46 | + static boolean isGradleEnterpriseExtension(Object gradleEnterpriseOrDevelocity) { |
| 47 | + return implementsInterface(gradleEnterpriseOrDevelocity, GRADLE_ENTERPRISE_EXTENSION); |
| 48 | + } |
| 49 | + |
| 50 | + static boolean isBuildScanExtension(Object gradleEnterpriseOrDevelocity) { |
| 51 | + return implementsInterface(gradleEnterpriseOrDevelocity, BUILD_SCAN_EXTENSION); |
| 52 | + } |
| 53 | + |
| 54 | + static boolean implementsInterface(Object object, String interfaceName) { |
| 55 | + Class<?> clazz = object.getClass(); |
| 56 | + while (clazz != null) { |
| 57 | + Class<?>[] interfaces = clazz.getInterfaces(); |
| 58 | + boolean implementsInterface = Stream.concat( |
| 59 | + Arrays.stream(interfaces), |
| 60 | + Arrays.stream(interfaces).flatMap(it -> Arrays.stream(it.getInterfaces())) |
| 61 | + ).anyMatch(it -> interfaceName.equals(it.getName())); |
| 62 | + |
| 63 | + if (implementsInterface) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + clazz = clazz.getSuperclass(); |
| 68 | + } |
| 69 | + |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + static BuildCacheAdapter createBuildCacheAdapter(AbstractBuildCache cache, Class<? extends AbstractBuildCache> reportedCacheClass) { |
| 74 | + if (reportedCacheClass.getName().toLowerCase().contains("develocity")) { |
| 75 | + return new DevelocityBuildCacheAdapter((DevelocityBuildCache) cache); |
| 76 | + } |
| 77 | + |
| 78 | + return new GradleEnterpriseBuildCacheAdapter(ProxyFactory.createProxy(cache, GradleEnterpriseBuildCacheProxy.class)); |
| 79 | + } |
| 80 | +} |
0 commit comments