Skip to content

Commit

Permalink
Introduce installation beacon and refactor representation of Gradle i…
Browse files Browse the repository at this point in the history
…nstallation

- Installation discovery by finding JAR in lib directory
- Minor improvements

+review REVIEW-5867
  • Loading branch information
Benjamin Muschko committed Mar 16, 2016
1 parent c0d8ba3 commit 6e6cc48
Show file tree
Hide file tree
Showing 46 changed files with 488 additions and 333 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ configurations {
dependencies {
runtime project(':launcher')
runtime project(':wrapper')
runtime project(":installationBeacon")
sonar libraries.logback_classic
gradlePlugins pluginProjects
gradlePlugins project(':dependencyManagement')
Expand Down
2 changes: 1 addition & 1 deletion gradle/buildSplits.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def buckets = [
":resourcesS3", ":wrapper", ":resourcesSftp", ":signing", ":modelGroovy", ":baseServices"],

"4": [":docs", ":integTest", ":core", ":languageNative", ":ide", ":platformJvm", ":platformNative",
":modelCore", ":sonar", ":maven", ":testingNative", ":ideNative", ":native"]
":modelCore", ":sonar", ":maven", ":testingNative", ":ideNative", ":native", ":installationBeacon"]
]

//Make sure all projects are accounted for
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ include 'testingNative'
include 'testingJvm'
include 'platformPlay'
include 'testKit'
include 'installationBeacon'

rootProject.name = 'gradle'
rootProject.children.each {project ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package org.gradle.api.plugins.announce

import org.gradle.api.Project
import org.gradle.api.internal.GradleDistributionLocator
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.plugins.announce.internal.AnnouncerFactory
import org.gradle.api.plugins.announce.internal.DefaultAnnouncerFactory
import org.gradle.api.plugins.announce.internal.DefaultIconProvider
import org.gradle.internal.installation.CurrentGradleInstallation

class AnnouncePluginExtension {
private static final Logger logger = Logging.getLogger(AnnouncePlugin)
Expand All @@ -43,7 +43,7 @@ class AnnouncePluginExtension {

AnnouncePluginExtension(ProjectInternal project) {
this.project = project
this.announcerFactory = new DefaultAnnouncerFactory(this, project, new DefaultIconProvider(project.services.get(GradleDistributionLocator)))
this.announcerFactory = new DefaultAnnouncerFactory(this, project, new DefaultIconProvider(project.services.get(CurrentGradleInstallation).getInstallation()))
this.onDemandLocalAnnouncer = new LocalAnnouncer(this)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
*/
package org.gradle.api.plugins.announce.internal

import org.gradle.api.internal.GradleDistributionLocator
import org.gradle.internal.installation.GradleInstallation

class DefaultIconProvider implements IconProvider {
private final GradleDistributionLocator locator
private final GradleInstallation gradleInstallation

DefaultIconProvider(GradleDistributionLocator locator) {
this.locator = locator
DefaultIconProvider(GradleInstallation gradleInstallation) {
this.gradleInstallation = gradleInstallation
}

File getIcon(int width, int height) {
def homeDir = locator.gradleHome
if (!homeDir) {
if (gradleInstallation == null) {
return null
}
def candidate = new File(homeDir, "media/gradle-icon-${width}x${height}.png")
def candidate = new File(gradleInstallation.gradleHome, "media/gradle-icon-${width}x${height}.png")
return candidate.file ? candidate : null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,41 @@
*/
package org.gradle.api.plugins.announce.internal

import org.gradle.api.internal.GradleDistributionLocator
import org.gradle.internal.installation.GradleInstallation
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.junit.Rule
import spock.lang.Specification

class DefaultIconProviderTest extends Specification {
@Rule final TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider()
final GradleDistributionLocator locator = Mock()
final DefaultIconProvider provider = new DefaultIconProvider(locator)

@Rule
final TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider()
GradleInstallation installation

private DefaultIconProvider createProvider() {
new DefaultIconProvider(installation)
}

def "returns png file that matches specified dimensions exactly"() {
given:
def homeDir = tmpDir.testDirectory
def pngFile = tmpDir.createFile("media/gradle-icon-48x18.png")
_ * locator.gradleHome >> homeDir
installation = new GradleInstallation(homeDir)

expect:
provider.getIcon(48, 18) == pngFile
createProvider().getIcon(48, 18) == pngFile
}

def "returns null when no distribution home"() {
given:
_ * locator.gradleHome >> null

expect:
provider.getIcon(48, 18) == null
createProvider().getIcon(48, 18) == null
}

def "returns null when no icon with exact dimension"() {
given:
def homeDir = tmpDir.testDirectory
_ * locator.gradleHome >> homeDir
installation = new GradleInstallation(tmpDir.testDirectory)

expect:
provider.getIcon(48, 18) == null
createProvider().getIcon(48, 18) == null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.gradle.cache.internal.*
import org.gradle.cache.internal.locklistener.NoOpFileLockContentionHandler
import org.gradle.internal.event.ListenerBroadcast
import org.gradle.internal.id.LongIdGenerator
import org.gradle.internal.installation.CurrentGradleInstallation
import org.gradle.internal.jvm.Jvm
import org.gradle.messaging.remote.MessagingServer
import org.gradle.messaging.remote.internal.MessagingServices
Expand Down Expand Up @@ -59,7 +60,7 @@ class PathLimitationIntegTest extends Specification {
private final ProcessMetaDataProvider metaDataProvider = new DefaultProcessMetaDataProvider(NativeServicesTestFixture.getInstance().get(org.gradle.internal.nativeintegration.ProcessEnvironment.class));
private final CacheFactory factory = new DefaultCacheFactory(new DefaultFileLockManager(metaDataProvider, new NoOpFileLockContentionHandler()));
private final CacheRepository cacheRepository = new DefaultCacheRepository(new DefaultCacheScopeMapping(tmpDir.getTestDirectory(), null, GradleVersion.current()), factory);
private final ModuleRegistry moduleRegistry = new DefaultModuleRegistry();
private final ModuleRegistry moduleRegistry = new DefaultModuleRegistry(CurrentGradleInstallation.get());
private final ClassPathRegistry classPathRegistry = new DefaultClassPathRegistry(new DefaultClassPathProvider(moduleRegistry), new WorkerProcessClassPathProvider(cacheRepository, moduleRegistry));
private final DefaultWorkerProcessFactory workerFactory = new DefaultWorkerProcessFactory(LogLevel.INFO, server, classPathRegistry, new LongIdGenerator(), null, new TmpDirTemporaryFileProvider(), TestFiles.execHandleFactory(tmpDir.getTestDirectory()));
private final ListenerBroadcast<TestListenerInterface> broadcast = new ListenerBroadcast<TestListenerInterface>(TestListenerInterface.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.gradle.internal.Actions;
import org.gradle.internal.event.ListenerBroadcast;
import org.gradle.internal.id.LongIdGenerator;
import org.gradle.internal.installation.CurrentGradleInstallation;
import org.gradle.internal.nativeintegration.ProcessEnvironment;
import org.gradle.messaging.remote.MessagingServer;
import org.gradle.messaging.remote.ObjectConnectionBuilder;
Expand Down Expand Up @@ -75,7 +76,7 @@ public class WorkerProcessIntegrationTest {
private final CacheFactory factory = new DefaultCacheFactory(new DefaultFileLockManager(metaDataProvider, new NoOpFileLockContentionHandler()));
private final CacheScopeMapping scopeMapping = new DefaultCacheScopeMapping(tmpDir.getTestDirectory(), null, GradleVersion.current());
private final CacheRepository cacheRepository = new DefaultCacheRepository(scopeMapping, factory);
private final ModuleRegistry moduleRegistry = new DefaultModuleRegistry();
private final ModuleRegistry moduleRegistry = new DefaultModuleRegistry(CurrentGradleInstallation.get());
private final ClassPathRegistry classPathRegistry = new DefaultClassPathRegistry(new DefaultClassPathProvider(moduleRegistry), new WorkerProcessClassPathProvider(cacheRepository, moduleRegistry));
private final DefaultWorkerProcessFactory workerFactory = new DefaultWorkerProcessFactory(LogLevel.INFO, server, classPathRegistry, new LongIdGenerator(), null, new TmpDirTemporaryFileProvider(), TestFiles.execHandleFactory(tmpDir.getTestDirectory()));
private final ListenerBroadcast<TestListenerInterface> broadcast = new ListenerBroadcast<TestListenerInterface>(TestListenerInterface.class);
Expand Down
10 changes: 8 additions & 2 deletions subprojects/core/src/main/groovy/org/gradle/StartParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.gradle.api.Incubating;
import org.gradle.api.internal.classpath.DefaultGradleDistributionLocator;
import org.gradle.initialization.BuildLayoutParameters;
import org.gradle.initialization.CompositeInitScriptFinder;
import org.gradle.initialization.DistributionInitScriptFinder;
import org.gradle.initialization.UserHomeInitScriptFinder;
import org.gradle.internal.DefaultTaskExecutionRequest;
import org.gradle.internal.installation.CurrentGradleInstallation;
import org.gradle.internal.installation.GradleInstallation;
import org.gradle.logging.LoggingConfiguration;
import org.gradle.util.GFileUtils;

Expand Down Expand Up @@ -97,7 +98,12 @@ public File getProjectCacheDir() {
* Creates a {@code StartParameter} with default values. This is roughly equivalent to running Gradle on the command-line with no arguments.
*/
public StartParameter() {
gradleHomeDir = new DefaultGradleDistributionLocator().getGradleHome();
GradleInstallation gradleInstallation = CurrentGradleInstallation.get();
if (gradleInstallation == null) {
gradleHomeDir = null;
} else {
gradleHomeDir = gradleInstallation.getGradleHome();
}

BuildLayoutParameters layoutParameters = new BuildLayoutParameters();
searchUpwards = layoutParameters.getSearchUpwards();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public ClassPath findClassPath(String name) {
if (name.equals("GRADLE_BASE_SERVICES")) {
return moduleRegistry.getModule("gradle-base-services").getImplementationClasspath();
}
if (name.equals("GRADLE_INSTALLATION_BEACON")) {
return moduleRegistry.getModule("gradle-installation-beacon").getImplementationClasspath();
}
if (name.equals("COMMONS_CLI")) {
return moduleRegistry.getExternalModule("commons-cli").getClasspath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ public ClassPath findClassPath(String name) {
return classpath;
}
if (name.equals(GRADLE_TEST_KIT.name())) {
return moduleRegistry.getModule("gradle-test-kit").getImplementationClasspath();
return moduleRegistry.getModule("gradle-test-kit").getClasspath();
}
if (name.equals(LOCAL_GROOVY.name())) {
return moduleRegistry.getExternalModule("groovy-all").getClasspath();
}

return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package org.gradle.api.internal.classpath;

import org.gradle.api.Nullable;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.internal.GradleDistributionLocator;
import org.gradle.internal.classpath.ClassPath;
import org.gradle.internal.classpath.DefaultClassPath;
import org.gradle.internal.installation.GradleInstallation;
import org.gradle.util.GUtil;

import java.io.File;
Expand All @@ -33,25 +34,21 @@
* Determines the classpath for a module by looking for a '${module}-classpath.properties' resource with 'name' set to the name of the module.
*/
public class DefaultModuleRegistry implements ModuleRegistry {
private final GradleDistributionLocator gradleDistributionLocator;
private final GradleInstallation gradleInstallation;
private final Map<String, Module> modules = new HashMap<String, Module>();
private final List<File> classpath = new ArrayList<File>();
private final Map<String, File> classpathJars = new LinkedHashMap<String, File>();

public DefaultModuleRegistry() {
this(new DefaultClassPath());
public DefaultModuleRegistry(@Nullable GradleInstallation gradleInstallation) {
this(new DefaultClassPath(), gradleInstallation);
}

public DefaultModuleRegistry(ClassPath additionalModuleClassPath) {
this(DefaultModuleRegistry.class.getClassLoader(), additionalModuleClassPath, new DefaultGradleDistributionLocator(DefaultModuleRegistry.class));
public DefaultModuleRegistry(ClassPath additionalModuleClassPath, @Nullable GradleInstallation gradleInstallation) {
this(DefaultModuleRegistry.class.getClassLoader(), additionalModuleClassPath, gradleInstallation);
}

DefaultModuleRegistry(ClassLoader classLoader, File distDir) {
this(classLoader, new DefaultClassPath(), new DefaultGradleDistributionLocator(distDir));
}

private DefaultModuleRegistry(ClassLoader classLoader, ClassPath additionalModuleClassPath, GradleDistributionLocator gradleDistributionLocator) {
this.gradleDistributionLocator = gradleDistributionLocator;
private DefaultModuleRegistry(ClassLoader classLoader, ClassPath additionalModuleClassPath, @Nullable GradleInstallation gradleInstallation) {
this.gradleInstallation = gradleInstallation;

for (File classpathFile : new EffectiveClassPath(classLoader).plus(additionalModuleClassPath).getAsFiles()) {
classpath.add(classpathFile);
Expand All @@ -63,13 +60,13 @@ private DefaultModuleRegistry(ClassLoader classLoader, ClassPath additionalModul

@Override
public ClassPath getAdditionalClassPath() {
return gradleDistributionLocator.getGradleHome() == null ? new DefaultClassPath(classpath) : new DefaultClassPath();
return gradleInstallation == null ? new DefaultClassPath(classpath) : new DefaultClassPath();
}

public Module getExternalModule(String name) {
File externalJar = findJar(name);
if (externalJar == null) {
throw new UnknownModuleException(String.format("Cannot locate JAR for module '%s' in distribution directory '%s'.", name, gradleDistributionLocator.getGradleHome()));
throw new UnknownModuleException(String.format("Cannot locate JAR for module '%s' in distribution directory '%s'.", name, gradleInstallation.getGradleHome()));
}
return new DefaultModule(name, Collections.singleton(externalJar), Collections.<File>emptySet(), Collections.<Module>emptySet());
}
Expand Down Expand Up @@ -105,10 +102,10 @@ private Module loadModule(String moduleName) {
}
}

if (gradleDistributionLocator.getGradleHome() == null) {
if (gradleInstallation == null) {
throw new UnknownModuleException(String.format("Cannot locate manifest for module '%s' in classpath.", moduleName));
}
throw new UnknownModuleException(String.format("Cannot locate JAR for module '%s' in distribution directory '%s'.", moduleName, gradleDistributionLocator.getGradleHome()));
throw new UnknownModuleException(String.format("Cannot locate JAR for module '%s' in distribution directory '%s'.", moduleName, gradleInstallation.getGradleHome()));
}

private Module module(String moduleName, Properties properties, Set<File> implementationClasspath) {
Expand Down Expand Up @@ -192,10 +189,12 @@ private Properties loadModuleProperties(String name, File jarFile) {

private File findJar(String name) {
Pattern pattern = Pattern.compile(Pattern.quote(name) + "-\\d.+\\.jar");
for (File libDir : gradleDistributionLocator.getLibDirs()) {
for (File file : libDir.listFiles()) {
if (pattern.matcher(file.getName()).matches()) {
return file;
if (gradleInstallation != null) {
for (File libDir : gradleInstallation.getLibDirs()) {
for (File file : libDir.listFiles()) {
if (pattern.matcher(file.getName()).matches()) {
return file;
}
}
}
}
Expand All @@ -212,16 +211,16 @@ private File findDependencyJar(String module, String name) {
if (jarFile != null) {
return jarFile;
}
if (gradleDistributionLocator.getGradleHome() == null) {
if (gradleInstallation == null) {
throw new IllegalArgumentException(String.format("Cannot find JAR '%s' required by module '%s' using classpath.", name, module));
}
for (File libDir : gradleDistributionLocator.getLibDirs()) {
for (File libDir : gradleInstallation.getLibDirs()) {
jarFile = new File(libDir, name);
if (jarFile.isFile()) {
return jarFile;
}
}
throw new IllegalArgumentException(String.format("Cannot find JAR '%s' required by module '%s' using classpath or distribution directory '%s'", name, module, gradleDistributionLocator.getGradleHome()));
throw new IllegalArgumentException(String.format("Cannot find JAR '%s' required by module '%s' using classpath or distribution directory '%s'", name, module, gradleInstallation.getGradleHome()));
}

private static class DefaultModule implements Module {
Expand Down
Loading

0 comments on commit 6e6cc48

Please sign in to comment.