Skip to content

Commit

Permalink
Slight simplification of dep replacement (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technici4n authored May 4, 2024
1 parent 2d1add3 commit 101ae05
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public abstract class DependencyReplacementsExtension implements ConfigurableDSL
private final Project project;
private final DependencyCreator dependencyCreator;
private final Set<ModuleReference> configuredReferences = Sets.newHashSet();
private final Set<ModuleReference> configuredGradleTasks = Sets.newHashSet();
private final Set<ModuleReference> configuredIdeTasks = Sets.newHashSet();
private final Set<ModuleReference> configuredTasks = Sets.newHashSet();
private final Table<Dependency, Configuration, Optional<DependencyReplacementResult>> dependencyReplacementInformation = HashBasedTable.create();
private final NamedDomainObjectContainer<DependencyReplacementHandler> dependencyReplacementHandlers;
private boolean hasBeenBaked = false;
Expand Down Expand Up @@ -154,7 +153,7 @@ void handleDependency(final Configuration configuration, final ModuleDependency
}
}

candidate.ifPresent(result -> handleDependencyReplacement(configuration, dependency, result, this::handleDependencyReplacementForIde, this::handleDependencyReplacementForGradle));
candidate.ifPresent(result -> handleDependencyReplacement(configuration, dependency, result));
}

/**
Expand All @@ -174,65 +173,32 @@ Set<Consumer<Project>> getAfterDefinitionBakeCallbacks() {
* @param configuration The configuration to handle the replacement in.
* @param dependency The dependency to replace.
* @param result The replacement result from one of the handlers.
* @param ideReplacer The replacer to use when the IDE is importing the project.
* @param gradleReplacer The replacer to use when the project is being built by Gradle.
* @implNote This method is responsible for removing the dependency from the configuration and adding the dependency provider task to the configuration.
* @implNote Currently the gradle importer is always used, the ide replacer is however only invoked when an IDE is detected.
*/
@VisibleForTesting
void handleDependencyReplacement(Configuration configuration, Dependency dependency, DependencyReplacementResult result, DependencyReplacer ideReplacer, DependencyReplacer gradleReplacer) {
void handleDependencyReplacement(Configuration configuration, Dependency dependency, DependencyReplacementResult result) {
configuration.getDependencies().remove(dependency);

final List<Configuration> targetConfigurations = result.getTargetConfiguration().orElseGet(() -> {
final List<Configuration> list = new ArrayList<>();
list.add(configuration);
return list;
});

final IdeManagementExtension ideManagementExtension = getProject().getExtensions().getByType(IdeManagementExtension.class);
if (ideManagementExtension.isIdeImportInProgress()) {
ideReplacer.handle(targetConfigurations, configuration, dependency, result);
}

gradleReplacer.handle(targetConfigurations, configuration, dependency, result);
}

private void handleDependencyReplacementForGradle(final List<Configuration> configurations, Configuration originalConfiguration, final Dependency dependency, final DependencyReplacementResult result) {
createDependencyReplacementResult(configurations, originalConfiguration, dependency, result, (repoBaseDir, entry) -> {
final ModuleReference reference = entry.toModuleReference();

final String artifactSelectionTaskName = result.getTaskNameBuilder().apply(CommonRuntimeUtils.buildTaskName("selectRawArtifact", reference));
if (configuredGradleTasks.contains(reference))
return new RepositoryEntryGenerationTasks(project.getTasks().named(artifactSelectionTaskName, ArtifactFromOutput.class));

configuredGradleTasks.add(reference);

return new RepositoryEntryGenerationTasks(project.getTasks().register(artifactSelectionTaskName, ArtifactFromOutput.class, artifactFromOutput -> {
artifactFromOutput.setGroup("neogradle/dependencies");
artifactFromOutput.setDescription(String.format("Selects the raw artifact from the %s dependency and puts it in the Ivy repository", dependency));

artifactFromOutput.getInput().set(result.getRawJarTaskProvider().flatMap(WithOutput::getOutput));
artifactFromOutput.getOutput().fileProvider(repoBaseDir.map(TransformerUtils.guard(dir -> entry.buildArtifactPath(dir.getAsFile().toPath()).toFile())));
artifactFromOutput.dependsOn(result.getRawJarTaskProvider());
}));
});
}

private void handleDependencyReplacementForIde(final List<Configuration> configurations, Configuration originalConfiguration, final Dependency dependency, final DependencyReplacementResult result) {
createDependencyReplacementResult(configurations, originalConfiguration, dependency, result, ((repoBaseDir, entry) -> {
createDependencyReplacementResult(targetConfigurations, configuration, dependency, result, ((repoBaseDir, entry) -> {
final ModuleReference reference = entry.toModuleReference();

final String rawArtifactSelectorName = result.getTaskNameBuilder().apply(CommonRuntimeUtils.buildTaskName("selectRawArtifact", reference));
final String sourceArtifactSelectorName = result.getTaskNameBuilder().apply(CommonRuntimeUtils.buildTaskName("selectSourceArtifact", reference));

if (configuredIdeTasks.contains(reference)) {
if (!configuredTasks.add(reference)) {
final TaskProvider<? extends WithOutput> rawProvider = project.getTasks().named(rawArtifactSelectorName, WithOutput.class);
final TaskProvider<? extends WithOutput> sourceProvider = project.getTasks().named(sourceArtifactSelectorName, WithOutput.class);

return new RepositoryEntryGenerationTasks(rawProvider, sourceProvider);
}

configuredIdeTasks.add(reference);
final TaskProvider<? extends WithOutput> rawProvider = project.getTasks().register(rawArtifactSelectorName, ArtifactFromOutput.class, artifactFromOutput -> {
artifactFromOutput.setGroup("neogradle/dependencies");
artifactFromOutput.setDescription(String.format("Selects the raw artifact from the %s dependency and puts it in the Ivy repository", dependency));
Expand Down Expand Up @@ -268,17 +234,17 @@ void createDependencyReplacementResult(final List<Configuration> configurations,
try {
extension.withDependency(
builder -> configureRepositoryReference(result, externalModuleDependency, builder),
reference -> processRepositoryReference(configurations, result, generator, repoBaseDir, reference),
reference -> processRepositoryReference(configurations, result, reference),
builder -> configureRepositoryEntry(result, externalModuleDependency, builder),
entry -> processRepositoryEntry(configurations, originalConfiguration, result, generator, repoBaseDir, entry),
entry -> processRepositoryEntry(originalConfiguration, result, generator, repoBaseDir, entry),
result.getProcessImmediately()
);
} catch (XMLStreamException | IOException e) {
throw new RuntimeException(String.format("Failed to create the dummy dependency for: %s", dependency), e);
}
}

private void processRepositoryReference(List<Configuration> configurations, DependencyReplacementResult result, TaskProviderGenerator generator, Provider<Directory> repoBaseDir, RepositoryReference entry) {
private void processRepositoryReference(List<Configuration> configurations, DependencyReplacementResult result, RepositoryReference entry) {
final Dependency replacedDependency = entry.toGradle(project);

configurations.forEach(configuration -> {
Expand All @@ -290,23 +256,23 @@ private void processRepositoryReference(List<Configuration> configurations, Depe
}


private void processRepositoryEntry(List<Configuration> configurations, Configuration originalConfiguration, DependencyReplacementResult result, TaskProviderGenerator generator, Provider<Directory> repoBaseDir, RepositoryEntry<?, ?> entry) {
private void processRepositoryEntry(Configuration originalConfiguration, DependencyReplacementResult result, TaskProviderGenerator generator, Provider<Directory> repoBaseDir, RepositoryEntry<?, ?> entry) {
final ModuleReference reference = entry.toModuleReference();
if (configuredReferences.contains(reference))
return;

configuredReferences.add(reference);

final RepositoryEntryGenerationTasks entryGenerationTasks = generator.generate(repoBaseDir, entry);
final Dependency replacedDependency = this.dependencyCreator.from(entryGenerationTasks.getRawJarProvider());
final Dependency replacedDependency = this.dependencyCreator.from(entryGenerationTasks.rawJarProvider);
originalConfiguration.getDependencies().add(replacedDependency);
result.getOnRepoWritingTaskRegisteredCallback().accept(entryGenerationTasks.getRawJarProvider());
result.getOnRepoWritingTaskRegisteredCallback().accept(entryGenerationTasks.rawJarProvider);

afterDefinitionBake(projectAfterBake -> {
final IdeManagementExtension ideManagementExtension = getProject().getExtensions().getByType(IdeManagementExtension.class);
if (ideManagementExtension.isIdeImportInProgress()) {
ideManagementExtension.registerTaskToRun(entryGenerationTasks.getRawJarProvider());
entryGenerationTasks.getSourceJarProvider().ifPresent(ideManagementExtension::registerTaskToRun);
ideManagementExtension.registerTaskToRun(entryGenerationTasks.rawJarProvider);
ideManagementExtension.registerTaskToRun(entryGenerationTasks.sourceJarProvider);
result.getAdditionalIdePostSyncTasks().forEach(ideManagementExtension::registerTaskToRun);
}
});
Expand Down Expand Up @@ -338,30 +304,14 @@ public void afterDefinitionBake(final Consumer<Project> callback) {
this.afterDefinitionBakeCallbacks.add(callback);
}

@VisibleForTesting
public static class RepositoryEntryGenerationTasks {
@NotNull
private static class RepositoryEntryGenerationTasks {
private final TaskProvider<? extends WithOutput> rawJarProvider;
@Nullable
private final TaskProvider<? extends WithOutput> sourceJarProvider;

public RepositoryEntryGenerationTasks(@NotNull TaskProvider<? extends WithOutput> rawJarProvider) {
this.rawJarProvider = rawJarProvider;
this.sourceJarProvider = null;
}

public RepositoryEntryGenerationTasks(@NotNull TaskProvider<? extends WithOutput> rawJarProvider, @Nullable TaskProvider<? extends WithOutput> sourceJarProvider) {
public RepositoryEntryGenerationTasks(TaskProvider<? extends WithOutput> rawJarProvider, TaskProvider<? extends WithOutput> sourceJarProvider) {
this.rawJarProvider = rawJarProvider;
this.sourceJarProvider = sourceJarProvider;
}

public TaskProvider<? extends WithOutput> getRawJarProvider() {
return rawJarProvider;
}

public Optional<TaskProvider<? extends WithOutput>> getSourceJarProvider() {
return Optional.ofNullable(sourceJarProvider);
}
}

@VisibleForTesting
Expand All @@ -370,11 +320,4 @@ interface TaskProviderGenerator {

RepositoryEntryGenerationTasks generate(final Provider<Directory> repoBaseDir, final RepositoryEntry<?,?> entry);
}


@VisibleForTesting
@FunctionalInterface
interface DependencyReplacer {
void handle(final List<Configuration> configurations, Configuration originalConfiguration, final Dependency dependency, final DependencyReplacementResult result);
}
}
Loading

0 comments on commit 101ae05

Please sign in to comment.