Search in sources :

Example 11 with ProjectDescription

use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescription in project n4js by eclipse.

the class DtsUtils method isDtsExportableReference.

/**
 * Tells whether references (e.g. imports, type references) to the given resource and its contents can be exported
 * to .d.ts. Iff this returns <code>false</code>, the .d.ts export will be cut off at this reference.
 */
public static boolean isDtsExportableReference(Resource resource, TranspilerState state) {
    if (resource == null) {
        return false;
    }
    if (N4Scheme.isResourceWithN4Scheme(resource)) {
        return true;
    }
    N4JSProjectConfigSnapshot project = state.workspaceAccess.findProjectContaining(resource);
    ProjectDescription pd = project != null ? project.getProjectDescription() : null;
    return pd != null && pd.hasN4JSNature() && (N4JSLanguageUtils.isDtsGenerationActive(pd) || isDefinition(pd) || isRuntime(pd));
}
Also used : N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)

Example 12 with ProjectDescription

use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescription in project n4js by eclipse.

the class NodeModulesDiscoveryHelper method getWorkspacesOfYarnWorkspaceProject.

private List<String> getWorkspacesOfYarnWorkspaceProject(File yarnProjectFolder, Map<Path, ProjectDescription> pdCache) {
    // obtain value of property "workspaces" in package.json located in folder 'candidate'
    final ProjectDescription prjDescr = pdCache.computeIfAbsent(yarnProjectFolder.toPath(), // load value from package.json
    p -> {
        FileURI location = new FileURI(yarnProjectFolder);
        // yarn projects do not have a related root, hence location is given
        ProjectDescription pd = projectDescriptionLoader.loadProjectDescriptionAtLocation(location, null);
        return pd;
    });
    final List<String> workspaces = (prjDescr != null && prjDescr.isYarnWorkspaceRoot()) ? prjDescr.getWorkspaces() : null;
    return workspaces;
}
Also used : FileURI(org.eclipse.n4js.workspace.locations.FileURI) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)

Example 13 with ProjectDescription

use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescription in project n4js by eclipse.

the class ProjectDiscoveryHelper method getOrCreateProjectDescription.

/**
 * @return the potentially cached {@link ProjectDescription} for the project in the given directory or creates it.
 *         In case creation fails, null is returned.
 */
private ProjectDescription getOrCreateProjectDescription(Path path, Path relatedRoot, Map<Path, ProjectDescription> cache) {
    if (!cache.containsKey(path) || cache.get(path).getRelatedRootLocation() == null) {
        FileURI location = new FileURI(path.toFile());
        FileURI relatedRootLocation = relatedRoot == null ? null : new FileURI(relatedRoot.toFile());
        ProjectDescription pd = projectDescriptionLoader.loadProjectDescriptionAtLocation(location, relatedRootLocation);
        cache.put(path, pd);
    }
    ProjectDescription pd = cache.get(path);
    if (pd != null && relatedRoot != null && pd.getRelatedRootLocation() != null && !Objects.equals(relatedRoot, pd.getRelatedRootLocation().toPath())) {
        // recompute project description in case the nodeModulesDiscoveryHelper added it
        ProjectDescriptionBuilder pdb = pd.change();
        pd = pdb.setRelatedRootLocation(new FileURI(relatedRoot.toFile())).setId(pdb.computeProjectID()).build();
        cache.put(path, pd);
    }
    return pd;
}
Also used : FileURI(org.eclipse.n4js.workspace.locations.FileURI) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) ProjectDescriptionBuilder(org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)

Example 14 with ProjectDescription

use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescription in project n4js by eclipse.

the class ProjectDiscoveryHelper method findDependencies.

private void findDependencies(Set<Path> allProjectDirs, Path prjDir, NodeModulesFolder nodeModulesFolder, Map<Path, ProjectDescription> pdCache, Set<Path> workList, Set<Path> dependencies) {
    Path relatedRoot = nodeModulesFolder.getRelatedRoot();
    Path prjNodeModules = prjDir.resolve(N4JSGlobals.NODE_MODULES);
    ProjectDescription prjDescr = getOrCreateProjectDescription(prjDir, relatedRoot, pdCache);
    if (prjDescr == null) {
        return;
    }
    for (ProjectDependency dependency : prjDescr.getProjectDependencies()) {
        String depName = dependency.getPackageName();
        Path depLocation = findDependencyLocation(nodeModulesFolder, prjNodeModules, depName);
        if (isNewDependency(allProjectDirs, prjDir, pdCache, depLocation, relatedRoot)) {
            addDependency(depLocation, relatedRoot, pdCache, workList, dependencies);
        }
    }
}
Also used : Path(java.nio.file.Path) ProjectDependency(org.eclipse.n4js.packagejson.projectDescription.ProjectDependency) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)

Example 15 with ProjectDescription

use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescription in project n4js by eclipse.

the class ProjectDiscoveryHelper method collectGlobMatches.

private void collectGlobMatches(String glob, Path yarnRoot, Map<Path, ProjectDescription> pdCache, Set<Path> allProjectDirs) {
    int depth = glob.contains("**") ? Integer.MAX_VALUE : glob.split("/").length + 1;
    @SuppressWarnings("resource") PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + yarnRoot.resolve(glob));
    try {
        EnumSet<FileVisitOption> options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
        Files.walkFileTree(yarnRoot, options, depth, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
                if (dir.endsWith(N4JSGlobals.NODE_MODULES)) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                if (pathMatcher.matches(dir)) {
                    Path dirName = dir.getName(dir.getNameCount() - 1);
                    if (dirName.toString().startsWith("@")) {
                        // note: project names must not start with '@' (unless it is a parent folder)
                        collectProjects(dir, yarnRoot, false, pdCache, allProjectDirs);
                        return FileVisitResult.SKIP_SUBTREE;
                    } else {
                        File pckJson = dir.resolve(N4JSGlobals.PACKAGE_JSON).toFile();
                        if (pckJson.isFile()) {
                            ProjectDescription projectDescription = getOrCreateProjectDescription(dir, yarnRoot, pdCache);
                            // #removeUnnecessaryPlainjsProjects() below)
                            if (projectDescription != null) {
                                allProjectDirs.add(dir);
                            }
                            return FileVisitResult.SKIP_SUBTREE;
                        }
                    }
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) {
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) FileVisitOption(java.nio.file.FileVisitOption) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) PathMatcher(java.nio.file.PathMatcher) File(java.io.File) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

ProjectDescription (org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)19 Path (java.nio.file.Path)8 FileURI (org.eclipse.n4js.workspace.locations.FileURI)6 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 File (java.io.File)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 ProjectDependency (org.eclipse.n4js.packagejson.projectDescription.ProjectDependency)2 ProjectDescriptionBuilder (org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)2 ProjectType (org.eclipse.n4js.packagejson.projectDescription.ProjectType)2 N4JSSourceFolderSnapshot (org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)2 FluentIterable (com.google.common.collect.FluentIterable)1 HashMultimap (com.google.common.collect.HashMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1 IOException (java.io.IOException)1 FileVisitOption (java.nio.file.FileVisitOption)1