use of org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot in project n4js by eclipse.
the class XpectN4JSES5TranspilerHelper method compileImplementationOfN4JSDFile.
private void compileImplementationOfN4JSDFile(final Path projectFolder, StringBuilder errorResult, Resource dep, GeneratorOption[] options, boolean replaceQuotes) {
Script script = (Script) dep.getContents().get(0);
if (AnnotationDefinition.PROVIDED_BY_RUNTIME.hasAnnotation(script)) {
return;
}
Pair<N4JSProjectConfigSnapshot, N4JSSourceFolderSnapshot> pair = workspaceAccess.findProjectAndSourceFolderContaining(dep, dep.getURI());
N4JSProjectConfigSnapshot project = pair.getKey();
N4JSSourceFolderSnapshot source = pair.getValue();
if (project != null && source != null) {
for (N4JSSourceFolderSnapshot c : project.getSourceFolders()) {
if (c.isExternal()) {
String sourceRelativePath = dep.getURI().toString().replace(source.getPathAsFileURI().toString(), "");
String[] potentialExternalSourceRelativeURISegments = null;
String potentialExternalSourceRelativePath = sourceRelativePath.replace("." + N4JSGlobals.N4JSD_FILE_EXTENSION, "." + N4JSGlobals.JS_FILE_EXTENSION);
potentialExternalSourceRelativeURISegments = URI.createURI(potentialExternalSourceRelativePath).segments();
if (potentialExternalSourceRelativeURISegments != null) {
URI potentialExternalSourceURI = c.getPathAsFileURI().appendSegments(potentialExternalSourceRelativeURISegments).toURI();
try {
Resource externalDep = dep.getResourceSet().getResource(potentialExternalSourceURI, true);
script = (Script) externalDep.getContents().get(0);
if (xpectGenerator.isCompilable(externalDep, errorResult)) {
createTempJsFileWithScript(projectFolder, script, options, replaceQuotes);
}
} catch (Exception e) {
throw new RuntimeException("Couldn't load " + potentialExternalSourceURI + ".", e);
}
}
}
}
}
}
use of org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot in project n4js by eclipse.
the class TestDiscoveryHelper method collectTestLocations.
/**
* Low-level method to collect all test modules, i.e. N4JS files containing classes containing at least one method
* annotated with @Test, as {@link IResourceDescription}s.
*/
private Stream<URI> collectTestLocations(N4JSWorkspaceConfigSnapshot ws, final IResourceDescriptions index, final ResourceSet resSet, URI location) {
if (null == location) {
return Stream.empty();
}
// does location point to an N4JS project?
if (isProject(ws, location)) {
// yes
// --> collect all test modules (files containing test classes) located in source containers of type "test"
final N4JSProjectConfigSnapshot p = ws.findProjectByPath(location);
return p.getSourceFolders().stream().filter(N4JSSourceFolderSnapshot::isTest).map(N4JSSourceFolderSnapshot::getContents).flatMap(TestDiscoveryHelper::stream).filter(uri -> isTestFile(uri) && isTestModule(resSet, index.getResourceDescription(uri)));
}
// does location point to an n4js file?
final IResourceDescription resDesc = index.getResourceDescription(location.trimFragment());
if (resDesc != null) {
// yes --> is it a test module? (i.e. does it contain test classes and the class is not abstract?)
if (isTestModule(resSet, resDesc)) {
// yes --> is it contained in a source container of type "test"?
final N4JSSourceFolderSnapshot srcContainer = ws.findSourceFolderContaining(location.trimFragment());
if (srcContainer != null && srcContainer.isTest()) {
// return location with fragment! (if any)
return Stream.of(location);
}
}
return Stream.empty();
}
// does location point to a source container (or sub-folder)?
final N4JSSourceFolderSnapshot srcContainer = ws.findSourceFolderContaining(location);
if (srcContainer != null) {
// yes --> is this a source container of type "test"?
if (srcContainer.isTest()) {
// yes --> collect all test modules (files containing test classes) in this source container
final String locationStr = location.toString();
return stream(srcContainer.getContents()).filter(uri -> uri.toString().startsWith(locationStr) && isTestModule(resSet, index.getResourceDescription(uri)));
}
return Stream.empty();
}
// invalid location URI
return Stream.empty();
}
use of org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot in project n4js by eclipse.
the class ModuleFilterUtils method getProjectRelativeLocation.
private static String getProjectRelativeLocation(N4JSProjectConfigSnapshot project, URI location, ModuleFilterSpecifier spec) {
N4JSSourceFolderSnapshot sourceContainer = project.findSourceFolderContaining(location);
if (sourceContainer == null) {
return null;
}
Path prjLocationPath = Paths.get(project.getPath().toString());
Path locationPath = Paths.get(location.toString());
Preconditions.checkState(locationPath.startsWith(prjLocationPath));
Path prjRelativeLocationPath = prjLocationPath.relativize(locationPath);
String filterSrcCont = spec.getSourcePath();
if (filterSrcCont == null) {
// e.g. noValidate { "**/*" }
for (N4JSSourceFolderSnapshot srcCont : project.getSourceFolders()) {
Path srcContLocationPath = Paths.get(srcCont.getRelativeLocation());
if (prjRelativeLocationPath.startsWith(srcContLocationPath)) {
Path srcRelativeLocationPath = srcContLocationPath.relativize(prjRelativeLocationPath);
return srcRelativeLocationPath.toString();
}
}
} else {
// e.g. noValidate { "**/*" in "src" }
if (filterSrcCont.equals(sourceContainer.getRelativeLocation())) {
Path srcContLocationPath = Paths.get(sourceContainer.getRelativeLocation());
Preconditions.checkState(prjRelativeLocationPath.startsWith(srcContLocationPath));
Path srcRelativeLocationPath = srcContLocationPath.relativize(prjRelativeLocationPath);
return srcRelativeLocationPath.toString();
}
}
return null;
}
use of org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot in project n4js by eclipse.
the class ProjectResolveHelper method resolvePackageAndFileName.
/**
* Resolves package and filename from provided {@link URI} against {@link N4JSProjectConfigSnapshot}. In returned
* string file extension of the actual file is trimmed.
*/
public String resolvePackageAndFileName(URI uri, N4JSProjectConfigSnapshot project) {
final String msg = "Cannot locate source container for module " + uri + ".";
if (null == project) {
throw new RuntimeException(msg + " Provided project was null.");
}
final N4JSSourceFolderSnapshot sourceContainer = project.findSourceFolderContaining(uri);
if (sourceContainer == null) {
throw new RuntimeException(msg);
}
URI deresolvedUri = uri.deresolve(sourceContainer.getPathAsFileURI().withTrailingPathDelimiter().toURI());
return URIUtils.trimFileExtension(deresolvedUri).toString();
}
use of org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot in project n4js by eclipse.
the class NullUndefinedValidator method isInTestFolder.
private boolean isInTestFolder(EObject eobj) {
Resource resource = eobj.eResource();
URI location = resource.getURI();
final N4JSSourceFolderSnapshot c = workspaceAccess.findSourceFolderContaining(resource, location);
return c != null && c.isTest();
}
Aggregations