Skip to content

Commit

Permalink
Do not cache local project names when configuring on demand
Browse files Browse the repository at this point in the history
Closes gh-321
  • Loading branch information
wilkinsona committed May 10, 2023
1 parent 1be5ed9 commit 09caf96
Showing 1 changed file with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,13 +44,15 @@ class VersionConfiguringAction implements Action<DependencyResolveDetails> {

private final Configuration configuration;

private Set<String> directDependencies;
private final LocalProjects localProjects;

private Set<String> localProjectNames;
private Set<String> directDependencies;

VersionConfiguringAction(Project project, DependencyManagementContainer dependencyManagementContainer,
Configuration configuration) {
this.project = project;
this.localProjects = project.getGradle().getStartParameter().isConfigureOnDemand()
? new StandardLocalProjects(project) : new CachingLocalProjects(project);
this.dependencyManagementContainer = dependencyManagementContainer;
this.configuration = configuration;
}
Expand Down Expand Up @@ -90,14 +92,7 @@ private boolean isDirectDependency(DependencyResolveDetails details) {
}

private boolean isDependencyOnLocalProject(Project project, DependencyResolveDetails details) {
if (this.localProjectNames == null) {
Set<String> names = new HashSet<>();
for (Project localProject : project.getRootProject().getAllprojects()) {
names.add(localProject.getGroup() + ":" + localProject.getName());
}
this.localProjectNames = names;
}
return this.localProjectNames.contains(getId(details));
return this.localProjects.getNames().contains(getId(details));
}

private String getId(DependencyResolveDetails details) {
Expand All @@ -108,4 +103,51 @@ ResolutionStrategy applyTo(Configuration c) {
return c.getResolutionStrategy().eachDependency(this);
}

private interface LocalProjects {

Set<String> getNames();

}

private static final class StandardLocalProjects implements LocalProjects {

private final Project project;

private StandardLocalProjects(Project project) {
this.project = project;
}

@Override
public Set<String> getNames() {
Set<String> names = new HashSet<>();
for (Project localProject : this.project.getRootProject().getAllprojects()) {
names.add(localProject.getGroup() + ":" + localProject.getName());
}
return names;
}

}

private static final class CachingLocalProjects implements LocalProjects {

private final LocalProjects delegate;

private Set<String> localProjectNames;

private CachingLocalProjects(Project project) {
this.delegate = new StandardLocalProjects(project);
}

@Override
public Set<String> getNames() {
Set<String> names = this.localProjectNames;
if (names == null) {
names = this.delegate.getNames();
this.localProjectNames = names;
}
return names;
}

}

}

0 comments on commit 09caf96

Please sign in to comment.