Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
chore: merge upstream branch gradle/master into main
Browse files Browse the repository at this point in the history
This commit synchronizes the latest 2 changes from the https://github.com/gradle/gradle/tree/master. The changes include:

29ca585: gradle/gradle#27166 Adopt ValueState in DefaultConfigurableFileCollection
cfcf6b0: Adopt ValueState in DefaultConfigurableFileCollection
  • Loading branch information
meowool-bot committed Nov 28, 2023
2 parents dba00b8 + 29ca585 commit 620f887
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class FileCollectionLifecycleIntegrationTest extends AbstractIntegrationSpec imp
fails('broken')

then:
failure.assertHasCause("The value for this file collection is final and cannot be changed.")
failure.assertHasCause("The value for this file collection is final and cannot be changed any further.")
}

def "can disallow changes to file collection without finalizing value"() {
Expand All @@ -165,7 +165,7 @@ class FileCollectionLifecycleIntegrationTest extends AbstractIntegrationSpec imp
fails('broken')

then:
failure.assertHasCause("The value for this file collection cannot be changed.")
failure.assertHasCause("The value for this file collection cannot be changed any further.")
}

def "can write but cannot read strict project file collection instance before project configuration completes"() {
Expand Down Expand Up @@ -233,11 +233,11 @@ class FileCollectionLifecycleIntegrationTest extends AbstractIntegrationSpec imp
run("show")

then:
outputContains("get files failed with: Cannot query the value for this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("get elements failed with: Cannot query the value for this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("get files in afterEvaluate failed with: Cannot query the value for this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("get elements in afterEvaluate failed with: Cannot query the value for this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("set after read failed with: The value for this file collection is final and cannot be changed.")
outputContains("get files failed with: Cannot query the value of this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("get elements failed with: Cannot query the value of this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("get files in afterEvaluate failed with: Cannot query the value of this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("get elements in afterEvaluate failed with: Cannot query the value of this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("set after read failed with: The value for this file collection is final and cannot be changed any further.")
output.count("value = [${file('some-file-2')}]") == 2
output.count("elements = [${file('some-file-2')}]") == 2
}
Expand Down Expand Up @@ -274,7 +274,7 @@ class FileCollectionLifecycleIntegrationTest extends AbstractIntegrationSpec imp
run("show")

then:
outputContains("set failed with: The value for this file collection is final and cannot be changed.")
outputContains("set failed with: The value for this file collection is final and cannot be changed any further.")
output.count("value = [${file('some-file')}]") == 2
}

Expand Down Expand Up @@ -313,7 +313,7 @@ class FileCollectionLifecycleIntegrationTest extends AbstractIntegrationSpec imp
run("show")

then:
outputContains("finalize failed with: Cannot finalize the value for this file collection because configuration of root project 'broken' has not completed yet.")
outputContains("finalize failed with: Cannot finalize the value of this file collection because configuration of root project 'broken' has not completed yet.")
output.count("value = [${file('some-file')}]") == 2
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.gradle.api.internal.file.UnionFileCollection;
import org.gradle.api.internal.provider.HasConfigurableValueInternal;
import org.gradle.api.internal.provider.PropertyHost;
import org.gradle.api.internal.provider.ValueState;
import org.gradle.api.internal.provider.ValueSupplier;
import org.gradle.api.internal.provider.support.LazyGroovySupport;
import org.gradle.api.internal.tasks.DefaultTaskDependency;
import org.gradle.api.internal.tasks.TaskDependencyFactory;
Expand Down Expand Up @@ -55,29 +57,23 @@
* A {@link org.gradle.api.file.FileCollection} which resolves a set of paths relative to a {@link org.gradle.api.internal.file.FileResolver}.
*/
public class DefaultConfigurableFileCollection extends CompositeFileCollection implements ConfigurableFileCollection, Managed, HasConfigurableValueInternal, LazyGroovySupport {
public static final EmptyCollector EMPTY_COLLECTOR = new EmptyCollector();

private enum State {
Mutable, ImplicitFinalizeNextQuery, FinalizeNextQuery, Final
}

private static final EmptyCollector EMPTY_COLLECTOR = new EmptyCollector();
private final PathSet filesWrapper;
private final String displayName;
private final PathToFileResolver resolver;
private final TaskDependencyFactory dependencyFactory;
private final PropertyHost host;
private final DefaultTaskDependency buildDependency;
private State state = State.Mutable;
private boolean disallowChanges;
private boolean disallowUnsafeRead;
private ValueCollector value = EMPTY_COLLECTOR;
private ValueState<ValueCollector> valueState;

public DefaultConfigurableFileCollection(@Nullable String displayName, PathToFileResolver fileResolver, TaskDependencyFactory dependencyFactory, Factory<PatternSet> patternSetFactory, PropertyHost host) {
super(dependencyFactory, patternSetFactory);
this.displayName = displayName;
this.resolver = fileResolver;
this.dependencyFactory = dependencyFactory;
this.host = host;
this.valueState = ValueState.newState(host);
filesWrapper = new PathSet();
buildDependency = dependencyFactory.configurableDependency();
}
Expand All @@ -99,46 +95,40 @@ public Object unpackState() {

@Override
public void finalizeValue() {
if (state == State.Final) {
return;
}
if (disallowUnsafeRead) {
String reason = host.beforeRead(null);
if (reason != null) {
throw new IllegalStateException("Cannot finalize the value for " + displayNameForThisCollection() + " because " + reason + ".");
}
if (valueState.shouldFinalize(this::displayNameForThisCollection, null)) {
finalizeNow();
}
}

private void finalizeNow() {
calculateFinalizedValue();
state = State.Final;
disallowChanges = true;
valueState = valueState.finalState();
}

public boolean isFinalizing() {
return state != State.Mutable;
return valueState.isFinalizing();
}

@Override
public void disallowChanges() {
disallowChanges = true;
valueState.disallowChanges();
}

@Override
public void finalizeValueOnRead() {
if (state == State.Mutable || state == State.ImplicitFinalizeNextQuery) {
state = State.FinalizeNextQuery;
}
valueState.finalizeOnNextGet();
}

@Override
public void implicitFinalizeValue() {
if (state == State.Mutable) {
state = State.ImplicitFinalizeNextQuery;
}
// Property prevents reads *and* mutations,
// however CFCs only want automatic finalization on query,
// so we do not #disallowChanges().
valueState.finalizeOnNextGet();
}

public void disallowUnsafeRead() {
disallowUnsafeRead = true;
finalizeValueOnRead();
valueState.disallowUnsafeRead();
}

public int getFactoryId() {
Expand Down Expand Up @@ -255,13 +245,7 @@ public FileCollectionInternal replace(FileCollectionInternal original, Supplier<
}

private void assertMutable() {
if (state == State.Final && disallowChanges) {
throw new IllegalStateException("The value for " + displayNameForThisCollection() + " is final and cannot be changed.");
} else if (disallowChanges) {
throw new IllegalStateException("The value for " + displayNameForThisCollection() + " cannot be changed.");
} else if (state == State.Final) {
throw new IllegalStateException("The value for " + displayNameForThisCollection() + " is final and cannot be changed.");
}
valueState.beforeMutate(this::displayNameForThisCollection);
}

private String displayNameForThisCollection() {
Expand Down Expand Up @@ -311,20 +295,7 @@ public void visitFileTreeBackedByFile(File file, FileTreeInternal fileTree, File

@Override
protected void visitChildren(Consumer<FileCollectionInternal> visitor) {
if (disallowUnsafeRead && state != State.Final) {
String reason = host.beforeRead(null);
if (reason != null) {
throw new IllegalStateException("Cannot query the value for " + displayNameForThisCollection() + " because " + reason + ".");
}
}
if (state == State.ImplicitFinalizeNextQuery) {
calculateFinalizedValue();
state = State.Final;
} else if (state == State.FinalizeNextQuery) {
calculateFinalizedValue();
state = State.Final;
disallowChanges = true;
}
valueState.finalizeOnReadIfNeeded(this::displayNameForThisCollection, null, ValueSupplier.ValueConsumer.IgnoreUnsafeRead, unused -> finalizeNow());
value.visitContents(visitor);
}

Expand Down
Loading

0 comments on commit 620f887

Please sign in to comment.