Skip to content

Commit

Permalink
Call the API tools directly without using ApiAnalysisApplication
Browse files Browse the repository at this point in the history
This replaces ApiAnalysisApplication usage by a more direct approach
that do not require us to pass file lists around and directly class the
ApiTools.
This is also much more flexible in regard to debugging and additional
printouts but still retains that we can consume dynamic implementation
from P2 repositories.
  • Loading branch information
laeubi committed Oct 7, 2023
1 parent e154832 commit d51aedc
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.apitools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.osgi.service.resolver.ResolverError;
import org.eclipse.pde.api.tools.internal.FilterStore;
import org.eclipse.pde.api.tools.internal.builder.BaseApiAnalyzer;
import org.eclipse.pde.api.tools.internal.builder.BuildContext;
import org.eclipse.pde.api.tools.internal.model.ApiModelFactory;
import org.eclipse.pde.api.tools.internal.model.BundleComponent;
import org.eclipse.pde.api.tools.internal.model.SystemLibraryApiComponent;
import org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore;
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline;
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent;
import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

public class ApiAnalysis implements Serializable, Callable<ApiAnalysisResult> {

private Collection<String> baselineBundles;
private String baselineName;
private String apiFilterFile;
private String bundleFile;
private boolean debug;
private Collection<String> compareBundles;
private String apiPreferences;

public ApiAnalysis(Collection<Path> baselineBundles, Collection<Path> dependencyBundles, String baselineName,
Path apiFilterFile, Path apiPreferences, Path bundleFile, boolean debug) {
this.compareBundles = Stream.concat(Stream.of(bundleFile), dependencyBundles.stream()).map(Path::toAbsolutePath)
.map(Path::toString).toList();
this.baselineBundles = baselineBundles.stream().map(Path::toAbsolutePath).map(Path::toString).toList();
this.baselineName = baselineName;
this.apiFilterFile = apiFilterFile == null ? null : apiFilterFile.toAbsolutePath().toString();
this.apiPreferences = apiPreferences == null ? null : apiPreferences.toAbsolutePath().toString();
this.bundleFile = bundleFile.toAbsolutePath().toString();
this.debug = debug;
}

@Override
public ApiAnalysisResult call() throws Exception {
printVersion();
IApiBaseline baseline = createBaseline(baselineBundles, baselineName + " - baseline");
IApiBaseline compare = createBaseline(compareBundles, baselineName + " - compare");
BundleComponent bundle = getBundleOfInterest(compare);
ResolverError[] resolverErrors = bundle.getErrors();
if (resolverErrors != null && resolverErrors.length > 0) {
throw new RuntimeException("The bundle has resolve errors");
}

IApiFilterStore filterStore = getApiFilterStore(bundle);
BaseApiAnalyzer analyzer = new BaseApiAnalyzer();
try {
analyzer.setContinueOnResolverError(true);
analyzer.analyzeComponent(null, filterStore, getPreferences(), baseline, bundle, new BuildContext(),
new NullProgressMonitor());
IApiProblem[] problems = analyzer.getProblems();
for (IApiProblem problem : problems) {
// TODO add to the result...
System.out.println(problem);
}
} finally {
analyzer.dispose();
}
return new ApiAnalysisResult();
}

private Properties getPreferences() throws IOException {
Properties properties = new Properties();
if (apiPreferences != null) {
try (FileInputStream stream = new FileInputStream(apiPreferences)) {
properties.load(stream);
}
}
return properties;
}

private BundleComponent getBundleOfInterest(IApiBaseline baseline) {
for (IApiComponent component : baseline.getApiComponents()) {
String location = component.getLocation();
if (bundleFile.equals(location)) {
if (component instanceof BundleComponent bundle) {
return bundle;
}
}
}
throw new RuntimeException("Can't find bundle in baseline!");
}

private void printVersion() {
Bundle apiToolsBundle = FrameworkUtil.getBundle(ApiModelFactory.class);
if (apiToolsBundle != null) {
debug("API Tools version: " + apiToolsBundle.getVersion());
}
}

private IApiBaseline createBaseline(Collection<String> bundles, String name) throws CoreException {
debug("==== " + name + " ====");
IApiBaseline baseline = ApiModelFactory.newApiBaseline(name);
List<IApiComponent> baselineComponents = new ArrayList<IApiComponent>();
for (String baselineBundle : bundles) {
IApiComponent component = ApiModelFactory.newApiComponent(baseline, baselineBundle);
if (component != null) {
debug(component.getSymbolicName() + " " + component.getVersion() + " -- "
+ new File(component.getLocation()).getName());
baselineComponents.add(component);
}
}
baseline.addApiComponents(baselineComponents.toArray(IApiComponent[]::new));
for (IApiComponent component : baseline.getApiComponents()) {
if (component instanceof SystemLibraryApiComponent systemLibrary) {
debug("System Component:");
debug("\tVersion: " + systemLibrary.getVersion());
debug("\tLocation: " + systemLibrary.getLocation());
for (String ee : systemLibrary.getExecutionEnvironments()) {
debug("\tExecution Environment: " + ee);
}
}

}
return baseline;
}

private IApiFilterStore getApiFilterStore(BundleComponent bundle) {
return new FilterStore(bundle) {
@Override
protected synchronized void initializeApiFilters() {
if (fFilterMap == null) {
fFilterMap = new HashMap<>(5);
if (apiFilterFile != null) {
Path path = Path.of(apiFilterFile);
if (Files.isRegularFile(path)) {
try (InputStream stream = Files.newInputStream(path)) {
readFilterFile(stream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
};
}

private void debug(String string) {
if (debug) {
System.out.println(string);
}
}

}
Loading

0 comments on commit d51aedc

Please sign in to comment.