forked from eclipse-tycho/tycho
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue eclipse-tycho#1711 refactor based on CR, Changelog entry
- Loading branch information
Vaclav Hala
committed
May 23, 2023
1 parent
298b9d1
commit 12fee83
Showing
26 changed files
with
189 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
tycho-core/src/main/java/org/eclipse/tycho/p2resolver/TargetDefinitionVariableResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.eclipse.tycho.p2resolver; | ||
|
||
import java.util.function.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.codehaus.plexus.component.annotations.Component; | ||
import org.codehaus.plexus.component.annotations.Requirement; | ||
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; | ||
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; | ||
import org.eclipse.tycho.ReactorProject; | ||
import org.eclipse.tycho.core.shared.MavenContext; | ||
import org.eclipse.tycho.core.shared.MavenLogger; | ||
|
||
@Component(role = TargetDefinitionVariableResolver.class) | ||
public class TargetDefinitionVariableResolver implements Initializable { | ||
|
||
private static final Pattern SYSTEM_PROPERTY_PATTERN = createVariablePatternArgument("system_property"); | ||
private static final Pattern PROJECT_LOC_PATTERN = createVariablePatternArgument("project_loc"); | ||
private static final Pattern ENV_VAR_PATTERN = createVariablePatternArgument("env_var"); | ||
|
||
@Requirement | ||
private MavenContext mavenContext; | ||
private MavenLogger logger; | ||
|
||
/** for test */ | ||
public TargetDefinitionVariableResolver(MavenContext mavenContext) { | ||
this.mavenContext = mavenContext; | ||
this.logger = mavenContext.getLogger(); | ||
} | ||
|
||
/** for injection */ | ||
public TargetDefinitionVariableResolver() { | ||
} | ||
|
||
@Override | ||
public void initialize() throws InitializationException { | ||
this.logger = mavenContext.getLogger(); | ||
} | ||
|
||
public String resolve(String path) { | ||
path = resolvePattern(path, SYSTEM_PROPERTY_PATTERN, | ||
key -> mavenContext.getSessionProperties().getProperty(key, "")); | ||
path = resolvePattern(path, ENV_VAR_PATTERN, key -> { | ||
String env = System.getenv(key); | ||
return env == null ? "" : env; | ||
}); | ||
path = resolvePattern(path, PROJECT_LOC_PATTERN, this::findProjectLocation); | ||
return path; | ||
} | ||
|
||
private String findProjectLocation(String projectName) { | ||
if (projectName.startsWith("/")) { | ||
projectName = projectName.substring(1); | ||
} | ||
logger.debug("Find project location for project " + projectName); | ||
for (ReactorProject project : mavenContext.getProjects()) { | ||
String name = project.getName(); | ||
logger.debug("check reactor project name: " + name); | ||
if (name.equals(projectName)) { | ||
return project.getBasedir().getAbsolutePath(); | ||
} | ||
} | ||
for (ReactorProject project : mavenContext.getProjects()) { | ||
String artifactId = project.getArtifactId(); | ||
logger.debug("check reactor project artifact id: " + artifactId); | ||
if (artifactId.equals(projectName)) { | ||
return project.getBasedir().getAbsolutePath(); | ||
} | ||
} | ||
for (ReactorProject project : mavenContext.getProjects()) { | ||
String name = project.getBasedir().getName(); | ||
logger.debug("check reactor project base directory: " + name); | ||
if (name.equals(projectName)) { | ||
return project.getBasedir().getAbsolutePath(); | ||
} | ||
} | ||
//if we can't resolve this, we will return the original one as this might be intentional to not include the project in the build | ||
String defaultValue = "${project_loc:" + projectName + "}"; | ||
logger.warn("Cannot resolve " + defaultValue + " target resolution might be incomplete"); | ||
return defaultValue; | ||
} | ||
|
||
private static String resolvePattern(String input, Pattern pattern, Function<String, String> parameterResolver) { | ||
Matcher matcher = pattern.matcher(input); | ||
StringBuffer sb = new StringBuffer(); | ||
while (matcher.find()) { | ||
String group = matcher.group(1); | ||
String resolved = parameterResolver.apply(group); | ||
matcher.appendReplacement(sb, Matcher.quoteReplacement(resolved)); | ||
} | ||
matcher.appendTail(sb); | ||
return sb.toString(); | ||
} | ||
|
||
private static Pattern createVariablePatternArgument(String variableName) { | ||
return Pattern.compile("\\$\\{" + variableName + ":([^}]+)\\}", Pattern.CASE_INSENSITIVE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.