Skip to content

Commit

Permalink
Move code from mojo to generator and generate missing p2 data if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Feb 2, 2024
1 parent a444b09 commit d421113
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Map;

import org.apache.maven.project.MavenProject;
import org.eclipse.tycho.IArtifactFacade;

public interface P2Generator {
Expand All @@ -32,4 +33,17 @@ public Map<String, IP2Artifact> generateMetadata(List<IArtifactFacade> artifacts
File targetDir) throws IOException;

void persistMetadata(Map<String, IP2Artifact> metadata, File unitsXml, File artifactsXml) throws IOException;

FileInfo persistMetadata(Map<String, IP2Artifact> metadata, MavenProject project) throws IOException;

void writeArtifactLocations(MavenProject project) throws IOException;

Map<String, IP2Artifact> generateMetadata(MavenProject project, boolean generateDownloadStatsProperty,
boolean generateChecksums) throws IOException;

record FileInfo(File metadata, File artifacts) {

}

void generateMetaData(MavenProject mavenProject) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class ModuleArtifactRepository extends ArtifactRepositoryBaseImpl<ModuleA

// BEGIN construction

static boolean canAttemptRead(File repositoryDir) {
public static boolean canAttemptRead(File repositoryDir) {
File requiredP2ArtifactsFile = new File(repositoryDir, TychoConstants.FILE_NAME_P2_ARTIFACTS);
File requiredLocalArtifactsFile = new File(repositoryDir, TychoConstants.FILE_NAME_LOCAL_ARTIFACTS);
return requiredP2ArtifactsFile.isFile() && requiredLocalArtifactsFile.isFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ File getPersistenceFile() {
return storage;
}

static boolean canAttemptRead(File repositoryDir) {
public static boolean canAttemptRead(File repositoryDir) {
File requiredP2MetadataFile = getStorageFile(repositoryDir);
return requiredP2MetadataFile.isFile();
}

private static File getStorageFile(File repositoryDir) {
static File getStorageFile(File repositoryDir) {
return new File(repositoryDir, TychoConstants.FILE_NAME_P2_METADATA);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
Expand Down Expand Up @@ -56,6 +61,7 @@
import org.eclipse.osgi.framework.util.CaseInsensitiveDictionaryMap;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.util.ManifestElement;
import org.eclipse.tycho.ArtifactType;
import org.eclipse.tycho.BuildProperties;
import org.eclipse.tycho.BuildPropertiesParser;
import org.eclipse.tycho.IArtifactFacade;
Expand Down Expand Up @@ -425,4 +431,88 @@ public void setBuildPropertiesParser(BuildPropertiesParser propertiesParserForTe
buildPropertiesParser = propertiesParserForTesting;
}

@Override
public Map<String, IP2Artifact> generateMetadata(MavenProject project, boolean generateDownloadStatsProperty,
boolean generateChecksums) throws IOException {
File targetDir = new File(project.getBuild().getDirectory());
ArtifactFacade projectDefaultArtifact = new ArtifactFacade(project.getArtifact());
List<IArtifactFacade> artifacts = new ArrayList<>();

artifacts.add(projectDefaultArtifact);

for (Artifact attachedArtifact : project.getAttachedArtifacts()) {
if (attachedArtifact.getFile() != null && (attachedArtifact.getFile().getName().endsWith(".jar")
|| (attachedArtifact.getFile().getName().endsWith(".zip")
&& project.getPackaging().equals(ArtifactType.TYPE_INSTALLABLE_UNIT)))) {
artifacts.add(new ArtifactFacade(attachedArtifact));
}
}

PublisherOptions options = new PublisherOptions();
options.setGenerateDownloadStats(generateDownloadStatsProperty);
options.setGenerateChecksums(generateChecksums);
Map<String, IP2Artifact> generatedMetadata = generateMetadata(artifacts, options, targetDir);
return generatedMetadata;
}

@Override
public FileInfo persistMetadata(Map<String, IP2Artifact> metadata, MavenProject project) throws IOException {
File targetDir = new File(project.getBuild().getDirectory());
File contentsXml = new File(targetDir, TychoConstants.FILE_NAME_P2_METADATA);
File artifactsXml = new File(targetDir, TychoConstants.FILE_NAME_P2_ARTIFACTS);
persistMetadata(metadata, contentsXml, artifactsXml);
return new FileInfo(contentsXml, artifactsXml);
}

@Override
public void writeArtifactLocations(MavenProject project) throws IOException {
File localArtifactsFile = new File(project.getBuild().getDirectory(), TychoConstants.FILE_NAME_LOCAL_ARTIFACTS);
writeArtifactLocations(localArtifactsFile, getAllProjectArtifacts(project));
}

static void writeArtifactLocations(File outputFile, Map<String, File> artifactLocations) throws IOException {
Properties outputProperties = new Properties();

for (Entry<String, File> entry : artifactLocations.entrySet()) {
if (entry.getKey() == null) {
outputProperties.put(TychoConstants.KEY_ARTIFACT_MAIN, entry.getValue().getAbsolutePath());
} else {
outputProperties.put(TychoConstants.KEY_ARTIFACT_ATTACHED + entry.getKey(),
entry.getValue().getAbsolutePath());
}
}

writeProperties(outputProperties, outputFile);
}

private static void writeProperties(Properties properties, File outputFile) throws IOException {
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
properties.store(outputStream, null);
}
}

/**
* Returns a map from classifiers to artifact files of the given project. The classifier
* <code>null</code> is mapped to the project's main artifact.
*/
private static Map<String, File> getAllProjectArtifacts(MavenProject project) {
Map<String, File> artifacts = new HashMap<>();
Artifact mainArtifact = project.getArtifact();
if (mainArtifact != null) {
artifacts.put(null, mainArtifact.getFile());
}
for (Artifact attachedArtifact : project.getAttachedArtifacts()) {
artifacts.put(attachedArtifact.getClassifier(), attachedArtifact.getFile());
}
return artifacts;
}

@Override
public void generateMetaData(MavenProject mavenProject) throws IOException {
//TODO we probably should get the active execution here and derive the data from the config of the p2 plugin that applies here
Map<String, IP2Artifact> generatedMetadata = generateMetadata(mavenProject, false, false);
persistMetadata(generatedMetadata, mavenProject);
writeArtifactLocations(mavenProject);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@
*******************************************************************************/
package org.eclipse.tycho.p2resolver;

import java.io.File;

import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.ReactorProjectIdentities;
import org.eclipse.tycho.p2.metadata.P2Generator;
import org.eclipse.tycho.p2.repository.PublishingRepository;
import org.eclipse.tycho.p2.repository.module.ModuleArtifactRepository;
import org.eclipse.tycho.p2.repository.module.ModuleMetadataRepository;
import org.eclipse.tycho.p2.repository.module.PublishingRepositoryImpl;
import org.eclipse.tycho.repository.registry.facade.ReactorRepositoryManager;

Expand All @@ -27,9 +34,30 @@ public class ReactorRepositoryManagerImpl implements ReactorRepositoryManager {
@Requirement
IProvisioningAgent agent;

@Requirement
P2Generator p2generator;

@Override
public PublishingRepository getPublishingRepository(ReactorProjectIdentities project) {
return new PublishingRepositoryImpl(agent, project);
}

@Override
public PublishingRepository getPublishingRepository(ReactorProject project) {

File targetDir = project.getBuildDirectory().getLocation();
if (!ModuleMetadataRepository.canAttemptRead(targetDir)
|| !ModuleArtifactRepository.canAttemptRead(targetDir)) {
//no metadata there so just generate it...
try {
agent.getService(Object.class); //needed to make checksum computation work see https://github.com/eclipse-equinox/p2/issues/214
p2generator.generateMetaData(project.adapt(MavenProject.class));
} catch (Exception e) {
// can't do anything then...
}
}

return getPublishingRepository(project.getIdentities());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import org.eclipse.tycho.core.DependencyResolverConfiguration;
import org.eclipse.tycho.core.TargetPlatformConfiguration;
import org.eclipse.tycho.core.TychoProjectManager;
import org.eclipse.tycho.core.osgitools.DefaultReactorProject;
import org.eclipse.tycho.p2.repository.RepositoryBlackboardKey;
import org.eclipse.tycho.p2.resolver.ResolverException;
import org.eclipse.tycho.p2.tools.RepositoryReferences;
import org.eclipse.tycho.repository.registry.facade.ReactorRepositoryManager;

/**
* Tool to obtain the list of p2 repositories that contain the dependencies of a module.
Expand All @@ -60,6 +62,9 @@ public class RepositoryReferenceTool {
@Requirement
private TychoProjectManager projectManager;

@Requirement
private ReactorRepositoryManager repositoryManager;

/**
* Returns the list of visible p2 repositories for the build of the current module. The list
* includes the p2 repositories of the referenced reactor modules, the target platform, and
Expand Down Expand Up @@ -90,6 +95,8 @@ public RepositoryReferences getVisibleRepositories(MavenProject module, MavenSes
RepositoryReferences repositories = new RepositoryReferences();

if ((flags & REPOSITORIES_INCLUDE_CURRENT_MODULE) != 0) {
//This is to enforce the repository is there e.g. if no p2 metadata is generated yet it will init it now
repositoryManager.getPublishingRepository(DefaultReactorProject.adapt(module));
File publisherResults = new File(module.getBuild().getDirectory());
repositories.addMetadataRepository(publisherResults);
repositories.addArtifactRepository(publisherResults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public interface ReactorRepositoryManager {
*/
PublishingRepository getPublishingRepository(ReactorProjectIdentities project);

default PublishingRepository getPublishingRepository(ReactorProject project) {
return getPublishingRepository(project.getIdentities());
}
PublishingRepository getPublishingRepository(ReactorProject project);

}
Loading

0 comments on commit d421113

Please sign in to comment.