Skip to content

Commit

Permalink
target repository location can contain env_var and system_property va…
Browse files Browse the repository at this point in the history
…riables
  • Loading branch information
Vaclav Hala committed May 22, 2023
1 parent 765d33e commit 298b9d1
Show file tree
Hide file tree
Showing 21 changed files with 370 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public TargetDefinitionContent resolveContentWithExceptions(TargetDefinition def
}
List<URITargetDefinitionContent> locations = new ArrayList<>();
for (Repository repository : installableUnitLocation.getRepositories()) {
URI location = repository.getLocation();
URI location = resolveRepositoryLocation(repository.getLocation());
String key = location.normalize().toASCIIString();
locations.add(uriRepositories.computeIfAbsent(key,
s -> new URITargetDefinitionContent(provisioningAgent, location, repository.getId())));
Expand Down Expand Up @@ -274,6 +274,21 @@ public IArtifactRepository getArtifactRepository() {
};
}

protected URI resolveRepositoryLocation(String location) {
location = resolvePattern(location, SYSTEM_PROPERTY_PATTERN,
key -> mavenContext.getSessionProperties().getProperty(key, ""));
location = resolvePattern(location, ENV_VAR_PATTERN, key -> {
String env = System.getenv(key);
return env == null ? "" : env;
});

try {
return new URI(location);
} catch (URISyntaxException e) {
throw new TargetDefinitionSyntaxException("Invalid URI: " + location);
}
}

/**
* Converts a "raw" URI string into one that can be used to parse it as an {@link URI}. The
* conversion is especially for converting file URIs constructed using maven properties that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*******************************************************************************/
package org.eclipse.tycho.p2resolver;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -117,6 +118,11 @@ private void debugCacheMiss(ResolutionArguments arguments) {
}
}

public URI resolveRepositoryLocation(String location) {
TargetDefinitionResolver resolver = new TargetDefinitionResolver(null, null, null, mavenContext, null);
return resolver.resolveRepositoryLocation(location);
}

// setter for DS
public void setMavenContext(MavenContext mavenContext) {
this.mavenContext = mavenContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;

import org.eclipse.tycho.core.resolver.shared.IncludeSourceMode;
Expand All @@ -44,7 +43,7 @@ public void testTarget() throws Exception {

InstallableUnitLocation location = (InstallableUnitLocation) locations.get(0);
assertEquals(1, location.getRepositories().size());
assertEquals(URI.create("https://download.eclipse.org/eclipse/updates/3.5/"),
assertEquals("https://download.eclipse.org/eclipse/updates/3.5/",
location.getRepositories().get(0).getLocation());
assertEquals(1, location.getUnits().size());
assertEquals("org.eclipse.platform.sdk", location.getUnits().get(0).getId());
Expand All @@ -53,9 +52,8 @@ public void testTarget() throws Exception {
InstallableUnitLocation l02 = (InstallableUnitLocation) locations.get(1);
assertEquals(5, l02.getUnits().size());
assertEquals(2, l02.getRepositories().size());
assertEquals(URI.create("http://subclipse.tigris.org/update_1.6.x/"),
l02.getRepositories().get(0).getLocation());
assertEquals(URI.create("https://download.eclipse.org/tools/mylyn/update/e3.4/"),
assertEquals("http://subclipse.tigris.org/update_1.6.x/", l02.getRepositories().get(0).getLocation());
assertEquals("https://download.eclipse.org/tools/mylyn/update/e3.4/",
l02.getRepositories().get(1).getLocation());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
import org.eclipse.tycho.core.resolver.target.TargetDefinitionContent;
import org.eclipse.tycho.core.test.utils.ResourceUtil;
import org.eclipse.tycho.targetplatform.TargetDefinition;
import org.eclipse.tycho.targetplatform.TargetDefinitionResolutionException;
import org.eclipse.tycho.targetplatform.TargetDefinitionSyntaxException;
import org.eclipse.tycho.targetplatform.TargetDefinition.IncludeMode;
import org.eclipse.tycho.targetplatform.TargetDefinition.InstallableUnitLocation;
import org.eclipse.tycho.targetplatform.TargetDefinition.Location;
import org.eclipse.tycho.targetplatform.TargetDefinition.Repository;
import org.eclipse.tycho.targetplatform.TargetDefinition.Unit;
import org.eclipse.tycho.targetplatform.TargetDefinitionResolutionException;
import org.eclipse.tycho.targetplatform.TargetDefinitionSyntaxException;
import org.eclipse.tycho.test.util.LogVerifier;
import org.eclipse.tycho.test.util.MockMavenContext;
import org.eclipse.tycho.testing.TychoPlexusTestCase;
Expand Down Expand Up @@ -383,12 +383,12 @@ public RepositoryStub(String repository) {
}

@Override
public URI getLocation() {
public String getLocation() {
if (repository != null) {
File repo = ResourceUtil.resourceFile(basedir + repository + "/content.xml").getParentFile();
return repo.toURI();
return repo.toURI().toString();
}
return URI.create("invalid:hello");
return URI.create("invalid:hello").toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.tycho.p2.target.facade.TargetPlatformConfigurationStub;
import org.eclipse.tycho.p2.tools.RepositoryReferences;
import org.eclipse.tycho.p2.tools.director.shared.DirectorRuntime;
import org.eclipse.tycho.p2resolver.TargetDefinitionResolverService;
import org.eclipse.tycho.targetplatform.TargetDefinition.InstallableUnitLocation;
import org.eclipse.tycho.targetplatform.TargetDefinition.Location;
import org.eclipse.tycho.targetplatform.TargetDefinition.Repository;
Expand Down Expand Up @@ -117,6 +118,9 @@ public class TPValidationMojo extends AbstractMojo {
@Component
private P2ResolverFactory factory;

@Component(role = TargetDefinitionResolverService.class)
private TargetDefinitionResolverService definitionResolver;

public void execute() throws MojoExecutionException {

List<TPError> errors = new ArrayList<>();
Expand Down Expand Up @@ -194,8 +198,9 @@ private void validateTarget(File targetFile) throws TPError {
for (Location location : targetDefinition.getLocations()) {
if (location instanceof InstallableUnitLocation p2Loc) {
for (Repository repo : p2Loc.getRepositories()) {
ref.addArtifactRepository(repo.getLocation());
ref.addMetadataRepository(repo.getLocation());
var repoUri = definitionResolver.resolveRepositoryLocation(repo.getLocation());
ref.addArtifactRepository(repoUri);
ref.addMetadataRepository(repoUri);
}
for (Unit unit : p2Loc.getUnits()) {
if (checkDependencies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.tycho.versionbump;

import static java.util.stream.Collectors.toList;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand All @@ -25,10 +27,12 @@

import javax.xml.parsers.ParserConfigurationException;

import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.core.resolver.P2ResolutionResult;
import org.eclipse.tycho.p2resolver.TargetDefinitionResolverService;
import org.eclipse.tycho.targetplatform.TargetDefinition;
import org.eclipse.tycho.targetplatform.TargetDefinition.IncludeMode;
import org.eclipse.tycho.targetplatform.TargetDefinition.InstallableUnitLocation;
Expand All @@ -49,6 +53,9 @@ public class UpdateTargetMojo extends AbstractUpdateMojo {
@Parameter(property = "target")
private File targetFile;

@Component(role = TargetDefinitionResolverService.class)
private TargetDefinitionResolverService definitionResolver;

@Override
protected void doUpdate() throws IOException, URISyntaxException, ParserConfigurationException, SAXException {

Expand All @@ -57,7 +64,7 @@ protected void doUpdate() throws IOException, URISyntaxException, ParserConfigur
target = TargetDefinitionFile.parseDocument(input);
TargetDefinitionFile parsedTarget = TargetDefinitionFile.parse(target, targetFile.getAbsolutePath());
resolutionContext.setEnvironments(Collections.singletonList(TargetEnvironment.getRunningEnvironment()));
resolutionContext.addTargetDefinition(new LatestVersionTarget(parsedTarget));
resolutionContext.addTargetDefinition(new LatestVersionTarget(parsedTarget, definitionResolver));
P2ResolutionResult result = p2.getTargetPlatformAsResolutionResult(resolutionContext, executionEnvironment);

Map<String, String> ius = new HashMap<>();
Expand Down Expand Up @@ -90,16 +97,18 @@ protected File getFileToBeUpdated() {
private static final class LatestVersionTarget implements TargetDefinition {

private TargetDefinitionFile delegate;
private TargetDefinitionResolverService varResolver;

public LatestVersionTarget(TargetDefinitionFile delegate) {
public LatestVersionTarget(TargetDefinitionFile delegate, TargetDefinitionResolverService varResolver) {
this.delegate = delegate;
this.varResolver = varResolver;
}

@Override
public List<? extends Location> getLocations() {
return delegate.getLocations().stream().map(location -> {
if (location instanceof InstallableUnitLocation iuLocation) {
return new LatestVersionLocation(iuLocation);
return new LatestVersionLocation(iuLocation, varResolver);
} else {
return location;
}
Expand All @@ -126,14 +135,19 @@ public String getTargetEE() {
private static final class LatestVersionLocation implements InstallableUnitLocation {

private InstallableUnitLocation delegate;
private TargetDefinitionResolverService varResolver;

public LatestVersionLocation(InstallableUnitLocation delegate) {
public LatestVersionLocation(InstallableUnitLocation delegate, TargetDefinitionResolverService varResolver) {
this.delegate = delegate;
this.varResolver = varResolver;
}

@Override
public List<? extends Repository> getRepositories() {
return delegate.getRepositories();
return delegate.getRepositories().stream().map(repo -> {
var resolvedLocation = varResolver.resolveRepositoryLocation(repo.getLocation());
return TargetDefinitionFile.repository(repo.getId(), resolvedLocation.toString());
}).collect(toList());
}

@Override
Expand Down
59 changes: 59 additions & 0 deletions tycho-its/projects/target.variables-env/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tycho-its-project.variables.env</groupId>
<artifactId>aggregator</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>target-platform-configuration</artifactId>
<version>${tycho-version}</version>
<configuration>
<resolver>p2</resolver>
<pomDependencies>consider</pomDependencies>
<executionEnvironment>JavaSE-11</executionEnvironment>
<target>
<artifact>
<groupId>tycho-its-project.variables.env</groupId>
<artifactId>targetplatform</artifactId>
<version>1.0.0</version>
</artifact>
</target>
<environments>
<environment>
<os>win32</os>
<ws>win32</ws>
<arch>x86_64</arch>
</environment>
<environment>
<os>linux</os>
<ws>gtk</ws>
<arch>x86_64</arch>
</environment>
</environments>
</configuration>
</plugin>
</plugins>
</build>

<modules>
<module>targetplatform</module>
<module>project</module>
</modules>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: project
Bundle-Version: 1.0.0
Automatic-Module-Name: project
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: javax.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source.. = src/
bin.includes = META-INF/,.
15 changes: 15 additions & 0 deletions tycho-its/projects/target.variables-env/project/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>project</artifactId>
<version>1.0.0</version>
<packaging>eclipse-plugin</packaging>
<parent>
<groupId>tycho-its-project.variables.env</groupId>
<artifactId>aggregator</artifactId>
<version>1.0.0</version>
</parent>

</project>
34 changes: 34 additions & 0 deletions tycho-its/projects/target.variables-env/targetplatform/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>targetplatform</artifactId>
<packaging>eclipse-target-definition</packaging>
<parent>
<groupId>tycho-its-project.variables.env</groupId>
<artifactId>aggregator</artifactId>
<version>1.0.0</version>
</parent>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho.extras</groupId>
<artifactId>target-platform-validation-plugin</artifactId>
<version>${tycho-version}</version>
<executions>
<execution>
<goals>
<goal>validate-target-platform</goal>
</goals>
<configuration>
<checkDependencies>true</checkDependencies>
<checkProvisioning>true</checkProvisioning>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?pde?>
<target name="tycho-its-project.variables.env" sequenceNumber="1">
<locations>
<location includeAllPlatforms="false" includeConfigurePhase="true" includeMode="planner" includeSource="true" type="InstallableUnit">
<repository location="${env_var:MY_MIRROR}/repo"/>

<unit id="javax.xml" version="0.0.0"/>
</location>
</locations>
</target>
Loading

0 comments on commit 298b9d1

Please sign in to comment.