use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.
the class ProjectCompareHelper method compareModules.
/**
* Get the comparison for a module in a specific implementation specified by it's ID.
*
* @param module
* can be either Implementation or API
* @param implementationID
* the current implementation to compare with
*
* @param includePolyfills
* {@code true} if polyfills should be considered as the part of the API and the implementation.
* Otherwise {@code false}.
* @return a comparison between API and implementation with implementationID, or <code>null</code> if no project
* could be found, the module is not part of an API/IMPL ...
*/
public ProjectComparisonEntry compareModules(TModule module, N4JSPackageName implementationID, final boolean includePolyfills) {
Resource resource = module.eResource();
N4JSWorkspaceConfigSnapshot wc = workspaceAccess.getWorkspaceConfig(resource);
N4JSProjectConfigSnapshot project = workspaceAccess.findProjectContaining(resource);
if (project == null) {
return null;
}
N4JSProjectConfigSnapshot implProject = null;
N4JSProjectConfigSnapshot apiProject = null;
TModule apiModule = null;
TModule apiImplModule = null;
if (project.getImplementationId() == null) {
// this is NOT an implementation project, so assume we have the api
// need to load the correct implementation. Since there might be multiple implementors
ApiImplMapping mapping = ApiImplMapping.of(wc);
implProject = mapping.getImpl(project.getN4JSPackageName(), implementationID);
if (implProject == null) {
// no implementation found.
return null;
}
apiProject = project;
apiModule = module;
URI impUri = artifactHelper.findArtifact(implProject, apiModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JS_FILE_EXTENSION));
if (impUri != null) {
IResourceDescriptions xtextIndex = workspaceAccess.getXtextIndex(module).orNull();
IResourceDescription resourceDescription = xtextIndex != null ? xtextIndex.getResourceDescription(impUri) : null;
if (resourceDescription != null) {
apiImplModule = workspaceAccess.loadModuleFromIndex(module.eResource().getResourceSet(), resourceDescription, false);
} else {
if (logger.isDebugEnabled()) {
logger.debug("...ouch nothing in index for " + impUri);
}
Resource implResource = module.eResource().getResourceSet().getResource(impUri, true);
apiImplModule = (TModule) implResource.getContents().get(1);
// .get(1);
if (logger.isDebugEnabled()) {
logger.debug("TModule loaded from Resource: " + apiImplModule);
}
}
} else {
// No implementation present.
if (logger.isDebugEnabled()) {
logger.debug("No implementation given. For " + apiModule.getQualifiedName());
}
}
} else {
// check that the implementation ID matches.
if (implementationID.equals(new N4JSPackageName(project.getImplementationId()))) {
implProject = project;
apiImplModule = module;
List<N4JSProjectConfigSnapshot> apiProjects = new ArrayList<>();
for (ProjectReference apiProjectRef : implProject.getProjectDescription().getImplementedProjects()) {
N4JSProjectConfigSnapshot currApiProject = wc.findProjectByID(apiProjectRef.getPackageName());
if (currApiProject != null) {
apiProjects.add(currApiProject);
}
}
labelA: for (N4JSProjectConfigSnapshot ap : apiProjects) {
URI apiURI = artifactHelper.findArtifact(ap, apiImplModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JSD_FILE_EXTENSION));
if (apiURI != null) {
IResourceDescriptions xtextIndex = workspaceAccess.getXtextIndex(apiImplModule).orNull();
IResourceDescription resourceDescription = xtextIndex.getResourceDescription(apiURI);
if (resourceDescription != null) {
apiModule = workspaceAccess.loadModuleFromIndex(apiImplModule.eResource().getResourceSet(), resourceDescription, false);
if (apiModule != null)
break labelA;
}
}
}
} else {
return null;
}
}
if (apiModule != null) {
return compareModules(apiProject, apiModule, implProject, apiImplModule, includePolyfills);
} else {
// no apiModule --> this is not an implementation of API.
return null;
}
}
use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.
the class ProjectCompareHelper method createEntries.
// may be made public later
private ProjectComparisonEntry createEntries(ProjectComparison root, N4JSProjectConfigSnapshot api, N4JSProjectConfigSnapshot[] impls, ResourceSet resourceSet, IResourceDescriptions index) {
final ProjectComparisonEntry entry = new ProjectComparisonEntry(root, api, impls);
for (N4JSSourceFolderSnapshot currSrcFolder : api.getSourceFolders()) {
for (URI uri : currSrcFolder.getContents()) {
final String uriStr = uri.toString();
if (uriStr.endsWith("." + N4JSGlobals.N4JS_FILE_EXTENSION) || uriStr.endsWith("." + N4JSGlobals.N4JSD_FILE_EXTENSION)) {
final IResourceDescription resDesc = index.getResourceDescription(uri);
final TModule moduleApi = getModuleFrom(resourceSet, resDesc);
if (moduleApi != null) {
final TModule[] moduleImpls = new TModule[impls.length];
for (int idx = 0; idx < impls.length; idx++) {
final N4JSProjectConfigSnapshot projectImpl = impls[idx];
if (projectImpl != null)
moduleImpls[idx] = findImplementation(moduleApi, projectImpl, resourceSet, index);
else
moduleImpls[idx] = null;
}
createEntries(entry, -1, moduleApi, moduleImpls, false);
}
}
}
}
return entry;
}
use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.
the class ProjectCompareHelper method getImplementationID.
/**
* Retrieves the implementation ID for an implementor of some API
*
* @param apiImplModule
* the implementor
* @return implementation ID if applicable - unset if the parameter is not implementing an API or is itself from an
* API definition.
*/
public Optional<N4JSPackageName> getImplementationID(TModule apiImplModule) {
N4JSProjectConfigSnapshot implProject = workspaceAccess.findProjectContaining(apiImplModule);
String implId = implProject != null ? implProject.getImplementationId() : null;
return implId != null ? Optional.of(new N4JSPackageName(implId)) : Optional.absent();
}
use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.
the class ProjectCompareHelper method createComparison.
/**
* First step in comparing API and implementation projects. This method will collect all API projects, the types
* declared there and their members and relate them to the corresponding types and members in all available
* implementation projects.
* <p>
* The second step of comparison is to compare an individual type/member to a corresponding type/member of a
* particular implementation via method {@link #compareApiImpl(ProjectComparisonEntry, int)}.
* <p>
* Usually, this second step is performed lazily when method <code>#compareApiImpl()</code> is invoked; however, by
* setting <code>fullCompare</code> to <code>true</code>, this is performed up-front here causing all results to be
* stored in a cache in the entries, and then subsequent calls to <code>#compareApiImpl()</code> will just read from
* that cache.
* <p>
* Returns <code>null</code> in case of error and adds human-readable error messages to the given list.
*/
public ProjectComparison createComparison(boolean fullCompare, List<String> addErrorMessagesHere) {
final ResourceSet resourceSet = workspaceAccess.createResourceSet();
final N4JSWorkspaceConfigSnapshot wc = workspaceAccess.getWorkspaceConfig(resourceSet);
final IResourceDescriptions index = workspaceAccess.getXtextIndex(resourceSet).orNull();
if (index == null) {
addErrorMessagesHere.add("failed to create a new resource set with properly configured workspace configuration and index");
return null;
}
final ApiImplMapping mapping = ApiImplMapping.of(wc);
if (mapping.hasErrors()) {
if (addErrorMessagesHere != null)
addErrorMessagesHere.addAll(mapping.getErrorMessages());
return null;
}
final List<N4JSPackageName> allImplIds = mapping.getAllImplIds();
final int implCount = allImplIds.size();
final ProjectComparison root = new ProjectComparison(allImplIds.toArray(new N4JSPackageName[implCount]));
for (N4JSPackageName currApiId : mapping.getApiIds()) {
final N4JSProjectConfigSnapshot currApi = mapping.getApi(currApiId);
final N4JSProjectConfigSnapshot[] currImpls = new N4JSProjectConfigSnapshot[implCount];
for (int idx = 0; idx < implCount; idx++) {
final N4JSPackageName currImplId = allImplIds.get(idx);
currImpls[idx] = mapping.getImpl(currApiId, currImplId);
}
createEntries(root, currApi, currImpls, resourceSet, index);
}
// compare all implementations in all entries and store in cache (if requested)
if (fullCompare) {
root.getAllEntries().forEach(currE -> {
for (int implIdx = 0; implIdx < implCount; implIdx++) compareApiImpl(currE, implIdx);
});
}
return root;
}
use of org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot in project n4js by eclipse.
the class ApiImplMapping method put.
/**
* Add a single API -> implementation association to the receiving mapping (if it is not present already).
*/
public void put(N4JSProjectConfigSnapshot api, N4JSProjectConfigSnapshot impl) {
final N4JSPackageName apiId = api.getN4JSPackageName();
if (apiId == null)
// just ignore (complaining about this problem is not our business)
return;
final String implIdStr = impl.getImplementationId();
final N4JSPackageName implId = implIdStr != null ? new N4JSPackageName(implIdStr) : null;
if (implId == null) {
projectsWithUndefImplIds.add(impl);
return;
}
ApiImplMapping.ApiImplAssociation assoc = assocs.get(apiId);
if (assoc == null) {
assoc = new ApiImplAssociation(api);
assocs.put(apiId, assoc);
}
final N4JSProjectConfigSnapshot replaced = assoc.putImpl(impl);
if (replaced != null && !Objects.equals(replaced, impl)) {
// ooops! we have several projects defining an implementation for project 'api' with the same
// implementation id -> remember them!
putConflict(apiId, implId, replaced, impl);
}
}
Aggregations