Skip to content

Commit

Permalink
Use a lazy lookup of the session instead of an inner session scoped bean
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Sep 18, 2024
1 parent b168dbe commit 8685029
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/
package org.apache.maven.internal.impl;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.api.Session;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.SessionScoped;
import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.di.Typed;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.model.Parent;
import org.apache.maven.api.services.Lookup;
Expand All @@ -37,6 +36,8 @@
@Singleton
public class CIFriendlyVersionModelTransformer implements ModelTransformer {

private static final Pattern REGEX = Pattern.compile("\\$\\{([^}]+)}");

private final Lookup lookup;

@Inject
Expand All @@ -46,60 +47,37 @@ public CIFriendlyVersionModelTransformer(Lookup lookup) {

@Override
public Model transformFileModel(Model model) throws ModelTransformerException {
return lookup.lookup(CIFriendlyVersionInner.class).transformFileModel(model);
return model.with()
.version(replaceCiFriendlyVersion(model.getVersion()))
.parent(replaceParent(model.getParent()))
.build();
}

public interface CIFriendlyVersionInner {
Model transformFileModel(Model model) throws ModelTransformerException;
private Parent replaceParent(Parent parent) {
return parent != null ? parent.withVersion(replaceCiFriendlyVersion(parent.getVersion())) : null;
}

@SessionScoped
@Named
@Typed
public static class CIFriendlyVersionInnerImpl implements CIFriendlyVersionInner {

private static final Pattern REGEX = Pattern.compile("\\$\\{([^}]+)}");

private final Session session;

@Inject
public CIFriendlyVersionInnerImpl(Session session) {
this.session = session;
}

@Override
public Model transformFileModel(Model model) throws ModelTransformerException {
return model.with()
.version(replaceCiFriendlyVersion(model.getVersion()))
.parent(replaceParent(model.getParent()))
.build();
}

private Parent replaceParent(Parent parent) {
return parent != null ? parent.withVersion(replaceCiFriendlyVersion(parent.getVersion())) : null;
}

protected String replaceCiFriendlyVersion(String version) {
// TODO: we're using a simple regex here, but we should probably use
// a proper interpolation service to do the replacements
// once one is available in maven-api-impl
// https://issues.apache.org/jira/browse/MNG-8262
if (version != null && version.contains("${")) {
protected String replaceCiFriendlyVersion(String version) {
// TODO: we're using a simple regex here, but we should probably use
// a proper interpolation service to do the replacements
// once one is available in maven-api-impl
// https://issues.apache.org/jira/browse/MNG-8262
if (version != null) {
Matcher matcher = REGEX.matcher(version);
if (matcher.find()) {
Map<String, String> properties = lookup.lookup(Session.class).getUserProperties();
StringBuilder result = new StringBuilder();
Matcher matcher = REGEX.matcher(version);
while (matcher.find()) {
String key = matcher.group(1); // extract the key inside ${}
String replacement = session.getUserProperties()
.getOrDefault(
key,
matcher.group(
0)); // get replacement from the map, or use the original ${xy} if not found
do {
// extract the key inside ${}
String key = matcher.group(1);
// get replacement from the map, or use the original ${xy} if not found
String replacement = properties.getOrDefault(key, "\\" + matcher.group(0));
matcher.appendReplacement(result, replacement);
}
} while (matcher.find());
matcher.appendTail(result); // Append the remaining part of the string
return result.toString();
}
return version;
}
return version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@
import org.apache.maven.api.DownloadedArtifact;
import org.apache.maven.api.Node;
import org.apache.maven.api.Session;
import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Priority;
import org.apache.maven.api.di.Typed;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.ModelBuilder;
import org.apache.maven.api.services.ModelBuilderRequest;
import org.apache.maven.api.services.ModelBuilderResult;
import org.apache.maven.api.services.ModelSource;
import org.apache.maven.api.services.ModelTransformerException;
import org.apache.maven.internal.impl.CIFriendlyVersionModelTransformer;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -66,18 +60,4 @@ void testStandalone() {
assertNotNull(node);
assertEquals(8, node.getChildren().size());
}

/**
* Disables the CIFriendlyVersionModelTransformer as it requires a session scope
* which is not available in this test.
*/
@Named
@Typed
@Priority(100)
public static class CIFriendlyVersionInnerImpl implements CIFriendlyVersionModelTransformer.CIFriendlyVersionInner {
@Override
public Model transformFileModel(Model model) throws ModelTransformerException {
return model;
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;

import org.apache.maven.SimpleLookup;
import org.apache.maven.api.Session;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.DefaultMavenExecutionResult;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -60,6 +61,7 @@ public abstract class AbstractRepositoryTestCase {
@BeforeEach
public void setUp() throws Exception {
session = newMavenRepositorySystemSession(system);
container.addComponent(InternalSession.from(session), Session.class, null);
}

protected PlexusContainer getContainer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import java.nio.file.StandardCopyOption;
import java.util.List;

import org.apache.maven.api.Session;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;

import static org.apache.maven.project.ProjectBuildingResultWithProblemMessageMatcher.projectBuildingResultWithProblemMessage;
import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
Expand Down Expand Up @@ -58,6 +60,9 @@ class DefaultMavenProjectBuilderTest extends AbstractMavenProjectTestCase {
@BeforeEach
public void setUp() throws Exception {
projectBuilder = getContainer().lookup(ProjectBuilder.class);

Session session = Mockito.mock(Session.class);
getContainer().addComponent(session, Session.class, null);
}

protected MavenProject getProject(Artifact pom, boolean allowStub) throws Exception {
Expand Down

0 comments on commit 8685029

Please sign in to comment.