Search in sources :

Example 1 with N4JSSourceFolderSnapshot

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);
                    }
                }
            }
        }
    }
}
Also used : Script(org.eclipse.n4js.n4JS.Script) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot) XtextResource(org.eclipse.xtext.resource.XtextResource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI) IOException(java.io.IOException)

Example 2 with N4JSSourceFolderSnapshot

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 &#64;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();
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)

Example 3 with N4JSSourceFolderSnapshot

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;
}
Also used : Path(java.nio.file.Path) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)

Example 4 with N4JSSourceFolderSnapshot

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();
}
Also used : N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot) URI(org.eclipse.emf.common.util.URI)

Example 5 with N4JSSourceFolderSnapshot

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();
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot) URI(org.eclipse.emf.common.util.URI)

Aggregations

N4JSSourceFolderSnapshot (org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)13 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)8 URI (org.eclipse.emf.common.util.URI)6 Resource (org.eclipse.emf.ecore.resource.Resource)4 N4JSResource (org.eclipse.n4js.resource.N4JSResource)4 FileURI (org.eclipse.n4js.workspace.locations.FileURI)4 Script (org.eclipse.n4js.n4JS.Script)3 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)3 ProjectDescription (org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)2 Type (org.eclipse.n4js.ts.types.Type)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 TModule (org.eclipse.n4js.ts.types.TModule)1 TVariable (org.eclipse.n4js.ts.types.TVariable)1 N4JSSourceFolderSnapshotForPackageJson (org.eclipse.n4js.workspace.N4JSSourceFolderSnapshotForPackageJson)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)1