Skip to content

Commit

Permalink
Add new option to API tools verify goal to runAsJob
Browse files Browse the repository at this point in the history
This allows to configure if the analysis should run as a workspace job.
  • Loading branch information
laeubi committed Nov 4, 2024
1 parent dfab8b7 commit f784331
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ public class ApiAnalysis implements Serializable, Callable<ApiAnalysisResult> {
private String apiPreferences;
private String binaryArtifact;
private String outputDir;
private boolean runAsJob;

ApiAnalysis(Collection<Path> baselineBundles, Collection<Path> dependencyBundles, String baselineName,
Path apiFilterFile, Path apiPreferences, Path projectDir, boolean debug, Path binaryArtifact,
Path outputDir) {
Path outputDir, boolean runAsJob) {
this.runAsJob = runAsJob;
this.targetBundles = dependencyBundles.stream().map(ApiAnalysis::pathAsString).toList();
this.baselineBundles = baselineBundles.stream().map(ApiAnalysis::pathAsString).toList();
this.baselineName = baselineName;
Expand Down Expand Up @@ -163,45 +165,23 @@ public void aboutToRun(IJobChangeEvent event) {
IPath projectPath = IPath.fromOSString(projectDir);
IProject project = getProject(projectPath);
ApiAnalysisResult result = new ApiAnalysisResult(getVersion());
WorkspaceJob job = new WorkspaceJob("Tycho API Analysis") {
IStatus status;
if (runAsJob) {
WorkspaceJob job = new WorkspaceJob("Tycho API Analysis") {

@Override
public IStatus runInWorkspace(IProgressMonitor monitor) {
try {
BundleComponent projectComponent = getApiComponent(project, projectPath);
IApiBaseline baseline = createBaseline(baselineBundles, baselineName + " - baseline");
ResolverError[] resolverErrors = projectComponent.getErrors();
if (resolverErrors != null && resolverErrors.length > 0) {
for (ResolverError error : resolverErrors) {
result.addResolverError(error);
}
}
IApiFilterStore filterStore = getApiFilterStore(projectComponent);
Properties preferences = getPreferences();
BaseApiAnalyzer analyzer = new BaseApiAnalyzer();
try {
analyzer.setContinueOnResolverError(true);
analyzer.analyzeComponent(null, filterStore, preferences, baseline, projectComponent,
new BuildContext(), new NullProgressMonitor());
IApiProblem[] problems = analyzer.getProblems();
for (IApiProblem problem : problems) {
result.addProblem(problem, project);
debug(String.valueOf(problem));
}
} finally {
analyzer.dispose();
ResourcesPlugin.getWorkspace().save(true, new NullProgressMonitor());
}
} catch (Exception e) {
return Status.error("Api Analysis failed", e);
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) {
return performAPIAnalysis(project, projectPath, result);
}
return Status.OK_STATUS;
}
};
job.setRule(ResourcesPlugin.getWorkspace().getRoot());
job.schedule();
job.join();
IStatus status = job.getResult();

};
job.setRule(ResourcesPlugin.getWorkspace().getRoot());
job.schedule();
job.join();
status = job.getResult();
} else {
status = performAPIAnalysis(project, projectPath, result);
}
JRTUtil.reset(); // reclaim space due to loaded multiple JRTUtil should better be fixed to not
// use that much space
if (!status.isOK() && status.getException() instanceof Exception error) {
Expand All @@ -210,6 +190,38 @@ public IStatus runInWorkspace(IProgressMonitor monitor) {
return result;
}

private IStatus performAPIAnalysis(IProject project, IPath projectPath, ApiAnalysisResult result) {
try {
BundleComponent projectComponent = getApiComponent(project, projectPath);
IApiBaseline baseline = createBaseline(baselineBundles, baselineName + " - baseline");
ResolverError[] resolverErrors = projectComponent.getErrors();
if (resolverErrors != null && resolverErrors.length > 0) {
for (ResolverError error : resolverErrors) {
result.addResolverError(error);
}
}
IApiFilterStore filterStore = getApiFilterStore(projectComponent);
Properties preferences = getPreferences();
BaseApiAnalyzer analyzer = new BaseApiAnalyzer();
try {
analyzer.setContinueOnResolverError(true);
analyzer.analyzeComponent(null, filterStore, preferences, baseline, projectComponent,
new BuildContext(), new NullProgressMonitor());
IApiProblem[] problems = analyzer.getProblems();
for (IApiProblem problem : problems) {
result.addProblem(problem, project);
debug(String.valueOf(problem));
}
} finally {
analyzer.dispose();
ResourcesPlugin.getWorkspace().save(true, new NullProgressMonitor());
}
} catch (Exception e) {
return Status.error("Api Analysis failed", e);
}
return Status.OK_STATUS;
}

private String getVersion() {
Bundle apiToolsBundle = FrameworkUtil.getBundle(ApiModelFactory.class);
if (apiToolsBundle != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ public class ApiAnalysisMojo extends AbstractMojo {
@Parameter(defaultValue = "false", property = "tycho.apitools.enhanceLogs")
private boolean enhanceLogs;

/**
* Configures if the API Analysis should run as a workspace job, this ensure
* that no other actions are allowed to run in parallel what sometimes can
* result in failures to execute the api-analysis
*/
@Parameter(defaultValue = "true", property = "tycho.apitools.runAsJob")
private boolean runAsJob;

@Component
private EclipseWorkspaceManager workspaceManager;

Expand Down Expand Up @@ -350,7 +358,7 @@ private ApiAnalysisResult performAnalysis(Collection<Path> baselineBundles, Coll
ApiAnalysis analysis = new ApiAnalysis(baselineBundles, dependencyBundles, project.getName(),
eclipseProject.getFile(fileToPath(apiFilter)), eclipseProject.getFile(fileToPath(apiPreferences)),
fileToPath(project.getBasedir()), debug, fileToPath(project.getArtifact().getFile()),
stringToPath(project.getBuild().getOutputDirectory()));
stringToPath(project.getBuild().getOutputDirectory()), runAsJob);
return eclipseFramework.execute(analysis);
} catch (Exception e) {
throw new MojoExecutionException("Execute ApiApplication failed", e);
Expand Down

0 comments on commit f784331

Please sign in to comment.