Skip to content

Commit

Permalink
Stop relying on Settings from BuildLayout
Browse files Browse the repository at this point in the history
As mentioned by @adammurdoch:

> Its root directory should be the root
> directory of the build, taken from the
> BuildLocations rather than the mutable project
> directory of the root project.

Issue: #26521
  • Loading branch information
abstratt committed Apr 22, 2024
1 parent 3f8dc7d commit 7307263
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
Expand Up @@ -77,6 +77,7 @@ class BuildLayoutIntegrationTest extends AbstractIntegrationSpec {
// setting a custom settings location is deprecated
executer.noDeprecationChecks()
groovyFile(customSettingsFile, """
// changing the root project dir does not impact the build dir obtained from BuildLayout
rootProject.projectDir = file('..')
${printLocations()}
""")
Expand All @@ -85,7 +86,7 @@ class BuildLayoutIntegrationTest extends AbstractIntegrationSpec {
run("help", "--settings-file", customSettingsPath)

then:
outputContains("settings root dir: " + testDirectory + ".")
outputContains("settings root dir: " + customSettingsDir + ".")
outputContains("settings dir: " + customSettingsDir + ".")
outputContains("settings source file: " + customSettingsFile + ".")
}
Expand Down
Expand Up @@ -18,25 +18,25 @@

import org.gradle.api.file.BuildLayout;
import org.gradle.api.file.Directory;
import org.gradle.api.initialization.Settings;
import org.gradle.initialization.layout.BuildLocations;


public class DefaultBuildLayout implements BuildLayout {
protected final FileFactory fileFactory;
private final Settings settings;
private final BuildLocations buildLocations;

public DefaultBuildLayout(Settings settings, FileFactory fileFactory) {
public DefaultBuildLayout(BuildLocations buildLocations, FileFactory fileFactory) {
this.fileFactory = fileFactory;
this.settings = settings;
this.buildLocations = buildLocations;
}

@Override
public Directory getSettingsDirectory() {
return fileFactory.dir(settings.getSettingsDir());
return fileFactory.dir(buildLocations.getSettingsDir());
}

@Override
public Directory getRootDirectory() {
return fileFactory.dir(settings.getRootDir());
return fileFactory.dir(buildLocations.getRootDirectory());
}
}
Expand Up @@ -36,6 +36,8 @@
import org.gradle.api.model.ObjectFactory;
import org.gradle.cache.internal.LegacyCacheCleanupEnablement;
import org.gradle.configuration.ConfigurationTargetIdentifier;
import org.gradle.initialization.layout.BuildLocations;
import org.gradle.internal.code.UserCodeApplicationContext;
import org.gradle.initialization.DefaultProjectDescriptorRegistry;
import org.gradle.internal.code.UserCodeApplicationContext;
import org.gradle.internal.instantiation.InstantiatorFactory;
Expand All @@ -60,8 +62,8 @@ public SettingsScopeServices(final ServiceRegistry parent, final SettingsInterna
});
}

protected BuildLayout createBuildLayout(FileFactory fileFactory) {
return new DefaultBuildLayout(settings, fileFactory);
protected BuildLayout createBuildLayout(FileFactory fileFactory, BuildLocations buildLocations) {
return new DefaultBuildLayout(buildLocations, fileFactory);
}

protected FileResolver createFileResolver(FileLookup fileLookup) {
Expand Down
Expand Up @@ -18,6 +18,7 @@ package org.gradle.api.internal.file

import org.gradle.api.internal.provider.PropertyHost
import org.gradle.api.internal.tasks.TaskDependencyFactory
import org.gradle.initialization.layout.BuildLocations
import org.gradle.internal.Factory
import org.gradle.test.fixtures.file.TestFile
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
Expand All @@ -36,7 +37,7 @@ class DefaultProjectLayoutTest extends Specification {

def setup() {
projectDir = tmpDir.createDir("project")
layout = new DefaultProjectLayout(projectDir, TestFiles.resolver(projectDir), Stub(TaskDependencyFactory), Stub(Factory), Stub(PropertyHost), TestFiles.fileCollectionFactory(projectDir), TestFiles.filePropertyFactory(projectDir), TestFiles.fileFactory())
layout = new DefaultProjectLayout(projectDir, TestFiles.resolver(projectDir), Stub(TaskDependencyFactory), Stub(Factory), Stub(PropertyHost), TestFiles.fileCollectionFactory(projectDir), TestFiles.filePropertyFactory(projectDir), TestFiles.fileFactory(), new BuildLocations(projectDir, null, null, null))
}

def "can query the project directory"() {
Expand Down
Expand Up @@ -80,6 +80,7 @@ import org.gradle.configuration.project.ProjectEvaluator
import org.gradle.groovy.scripts.EmptyScript
import org.gradle.groovy.scripts.ScriptSource
import org.gradle.initialization.ClassLoaderScopeRegistryListener
import org.gradle.initialization.layout.BuildLocations
import org.gradle.internal.Factory
import org.gradle.internal.instantiation.InstantiatorFactory
import org.gradle.internal.logging.LoggingManagerInternal
Expand Down Expand Up @@ -243,7 +244,7 @@ class DefaultProjectTest extends Specification {
ModelSchemaStore modelSchemaStore = Stub(ModelSchemaStore)
serviceRegistryMock.get((Type) ModelSchemaStore) >> modelSchemaStore
serviceRegistryMock.get(ModelSchemaStore) >> modelSchemaStore
serviceRegistryMock.get((Type) DefaultProjectLayout) >> new DefaultProjectLayout(rootDir, TestFiles.resolver(rootDir), Stub(TaskDependencyFactory), Stub(Factory), Stub(PropertyHost), Stub(FileCollectionFactory), TestFiles.filePropertyFactory(), TestFiles.fileFactory())
serviceRegistryMock.get((Type) DefaultProjectLayout) >> new DefaultProjectLayout(rootDir, TestFiles.resolver(rootDir), Stub(TaskDependencyFactory), Stub(Factory), Stub(PropertyHost), Stub(FileCollectionFactory), TestFiles.filePropertyFactory(), TestFiles.fileFactory(), new BuildLocations(rootDir, null, null, null))

build.getProjectEvaluationBroadcaster() >> Stub(ProjectEvaluationListener)
build.getParent() >> null
Expand Down
Expand Up @@ -44,6 +44,7 @@ import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.util.internal.PatternSets
import org.gradle.cache.internal.TestCrossBuildInMemoryCacheFactory
import org.gradle.initialization.layout.BuildLocations
import org.gradle.internal.hash.ChecksumService
import org.gradle.internal.hash.HashCode
import org.gradle.internal.hash.Hashing
Expand Down Expand Up @@ -158,7 +159,9 @@ class TestUtil {

ProjectLayout createProjectLayout() {
def filePropertyFactory = new DefaultFilePropertyFactory(PropertyHost.NO_OP, fileResolver, fileCollectionFactory)
return new DefaultProjectLayout(fileResolver.resolve("."), fileResolver, DefaultTaskDependencyFactory.withNoAssociatedProject(), PatternSets.getNonCachingPatternSetFactory(), PropertyHost.NO_OP, fileCollectionFactory, filePropertyFactory, filePropertyFactory)
def projectDir = fileResolver.resolve(".")
def buildLocations = new BuildLocations(projectDir, null, null, null)
return new DefaultProjectLayout(projectDir, fileResolver, DefaultTaskDependencyFactory.withNoAssociatedProject(), PatternSets.getNonCachingPatternSetFactory(), PropertyHost.NO_OP, fileCollectionFactory, filePropertyFactory, filePropertyFactory, buildLocations)
}

ChecksumService createChecksumService() {
Expand Down

0 comments on commit 7307263

Please sign in to comment.