Search in sources :

Example 6 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class ConvertedIdeTest method importProject.

/**
 * Imports a project into the running JUnit test workspace. Usage:
 *
 * <pre>
 * IProject project = ProjectTestsUtils.importProject(new File(&quot;probands&quot;), &quot;TestProject&quot;, n4jsLibs);
 * </pre>
 *
 * @param probandsFolder
 *            the parent folder in which the test project is found
 * @param projectName
 *            the name of the test project, must be folder contained in probandsFolder
 * @param n4jsLibs
 *            names of N4JS libraries to install from the local <code>n4js-libs</code> top-level folder (see
 *            {@link N4jsLibsAccess#installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...)}).
 * @return the imported project
 * @see <a href=
 *      "http://stackoverflow.com/questions/12484128/how-do-i-import-an-eclipse-project-from-a-zip-file-programmatically">
 *      stackoverflow: from zip</a>
 */
protected File importProject(File probandsFolder, N4JSPackageName projectName, Collection<N4JSPackageName> n4jsLibs) {
    if (!testWorkspaceManager.isCreated()) {
        throw new IllegalStateException("the test workspace is not yet created");
    }
    Path projectNameAsRelativePath = projectName.getProjectNameAsRelativePath();
    File projectSourceFolder = probandsFolder.toPath().resolve(projectNameAsRelativePath).toFile();
    if (!projectSourceFolder.exists()) {
        throw new IllegalArgumentException("proband not found in " + projectSourceFolder);
    }
    File projectFolder = projectName.getLocation(getProjectLocation().toPath());
    installN4jsLibs(projectName, n4jsLibs.toArray(new N4JSPackageName[0]));
    // copy project into workspace
    // (need to do that manually to properly handle NPM scopes, because the Eclipse import functionality won't put
    // those projects into an "@myScope" subfolder)
    final List<Path> filesCopied = new ArrayList<>();
    try {
        projectFolder.mkdirs();
        FileCopier.copy(projectSourceFolder.toPath(), projectFolder.toPath(), path -> filesCopied.add(path));
    } catch (IOException e) {
        throw new WrappedException("exception while copying project into workspace", e);
    }
    // create symbolic link in node_modules folder of root project (if necessary)
    if (isYarnWorkspace()) {
        try {
            File nodeModulesFolder = getNodeModulesFolder(projectName);
            Path from = nodeModulesFolder.toPath().resolve(projectNameAsRelativePath);
            Files.createDirectories(from.getParent());
            Files.createSymbolicLink(from, projectFolder.toPath());
        } catch (IOException e) {
            throw new WrappedException("exception while creating symbolic link from node_modules folder to project folder", e);
        }
    }
    // notify LSP server
    DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams(FluentIterable.from(filesCopied).transform(path -> new FileURI(path.toFile())).transform(fileURI -> new FileEvent(fileURI.toString(), FileChangeType.Created)).toList());
    languageServer.didChangeWatchedFiles(params);
    joinServerRequests();
    return projectFolder;
}
Also used : Path(java.nio.file.Path) UserDataMapper(org.eclipse.n4js.resource.UserDataMapper) ProcessResult(org.eclipse.n4js.cli.helper.ProcessResult) IssueCodes(org.eclipse.n4js.validation.IssueCodes) CliTools(org.eclipse.n4js.cli.helper.CliTools) FileTime(java.nio.file.attribute.FileTime) AbstractIdeTest(org.eclipse.n4js.ide.tests.helper.server.AbstractIdeTest) Diagnostic(org.eclipse.lsp4j.Diagnostic) Multimap(com.google.common.collect.Multimap) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) TestWorkspaceManager(org.eclipse.n4js.ide.tests.helper.server.TestWorkspaceManager) URIUtils(org.eclipse.n4js.utils.URIUtils) ArrayList(java.util.ArrayList) FileEvent(org.eclipse.lsp4j.FileEvent) HashSet(java.util.HashSet) FileChangeType(org.eclipse.lsp4j.FileChangeType) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) FluentIterable(com.google.common.collect.FluentIterable) Optional(com.google.common.base.Optional) Path(java.nio.file.Path) ProjectDescriptionUtils(org.eclipse.n4js.utils.ProjectDescriptionUtils) FileURI(org.eclipse.n4js.workspace.locations.FileURI) Files(java.nio.file.Files) FileCopier(org.eclipse.n4js.utils.io.FileCopier) Collection(java.util.Collection) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) Set(java.util.Set) IOException(java.io.IOException) WrappedException(org.eclipse.emf.common.util.WrappedException) Collectors(java.util.stream.Collectors) File(java.io.File) Sets(com.google.common.collect.Sets) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) ConcurrentIndex(org.eclipse.n4js.xtext.ide.server.build.ConcurrentIndex) List(java.util.List) IterableExtensions(org.eclipse.xtext.xbase.lib.IterableExtensions) N4jsLibsAccess(org.eclipse.n4js.cli.helper.N4jsLibsAccess) TypesPackage(org.eclipse.n4js.ts.types.TypesPackage) Entry(java.util.Map.Entry) Assert(org.junit.Assert) Collections(java.util.Collections) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) WrappedException(org.eclipse.emf.common.util.WrappedException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileEvent(org.eclipse.lsp4j.FileEvent) FileURI(org.eclipse.n4js.workspace.locations.FileURI) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) File(java.io.File) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams)

Example 7 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class ConvertedIdeTest method importProband.

/**
 * Creates an empty yarn workspace project with
 * {@link TestWorkspaceManager#createTestOnDisk(org.eclipse.xtext.xbase.lib.Pair...) the test workspace manager},
 * starts the LSP server, and then imports all projects in the given proband folder. Each sub-folder of the given
 * proband folder is assumed to be a project and will be imported.
 */
protected List<N4JSPackageName> importProband(File probandFolder, Collection<N4JSPackageName> n4jsLibs) {
    if (testWorkspaceManager.isCreated()) {
        throw new IllegalStateException("the test workspace has already been created");
    }
    // this will create an empty yarn workspace
    testWorkspaceManager.createTestOnDisk();
    startAndWaitForLspServer();
    // import the projects
    final List<N4JSPackageName> importedProjects = new ArrayList<>();
    boolean needToCopyLibs = true;
    for (final File child : probandFolder.listFiles()) {
        if (child.isDirectory()) {
            if (child.getName().startsWith(ProjectDescriptionUtils.NPM_SCOPE_PREFIX)) {
                for (final File grandChild : child.listFiles()) {
                    if (grandChild.isDirectory()) {
                        final N4JSPackageName name = new N4JSPackageName(child.getName(), grandChild.getName());
                        importProject(probandFolder, name, needToCopyLibs ? n4jsLibs : Collections.emptyList());
                        importedProjects.add(name);
                        needToCopyLibs = false;
                    }
                }
            } else {
                final N4JSPackageName name = new N4JSPackageName(child.getName());
                importProject(probandFolder, name, needToCopyLibs ? n4jsLibs : Collections.emptyList());
                importedProjects.add(name);
                needToCopyLibs = false;
            }
        }
    }
    cleanBuildAndWait();
    // ensure that all projects have been imported properly
    final Set<String> expectedProjects = importedProjects.stream().map(pn -> "yarn-test-project/packages/" + pn.getRawName()).collect(Collectors.toSet());
    final Set<String> actualProjects = concurrentIndex.entries().stream().map(Entry::getKey).collect(Collectors.toSet());
    final Set<String> missingProjects = new HashSet<>(Sets.difference(expectedProjects, actualProjects));
    Assert.assertTrue("some projects were not correctly imported: " + missingProjects, missingProjects.isEmpty());
    return importedProjects;
}
Also used : UserDataMapper(org.eclipse.n4js.resource.UserDataMapper) ProcessResult(org.eclipse.n4js.cli.helper.ProcessResult) IssueCodes(org.eclipse.n4js.validation.IssueCodes) CliTools(org.eclipse.n4js.cli.helper.CliTools) FileTime(java.nio.file.attribute.FileTime) AbstractIdeTest(org.eclipse.n4js.ide.tests.helper.server.AbstractIdeTest) Diagnostic(org.eclipse.lsp4j.Diagnostic) Multimap(com.google.common.collect.Multimap) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) TestWorkspaceManager(org.eclipse.n4js.ide.tests.helper.server.TestWorkspaceManager) URIUtils(org.eclipse.n4js.utils.URIUtils) ArrayList(java.util.ArrayList) FileEvent(org.eclipse.lsp4j.FileEvent) HashSet(java.util.HashSet) FileChangeType(org.eclipse.lsp4j.FileChangeType) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) FluentIterable(com.google.common.collect.FluentIterable) Optional(com.google.common.base.Optional) Path(java.nio.file.Path) ProjectDescriptionUtils(org.eclipse.n4js.utils.ProjectDescriptionUtils) FileURI(org.eclipse.n4js.workspace.locations.FileURI) Files(java.nio.file.Files) FileCopier(org.eclipse.n4js.utils.io.FileCopier) Collection(java.util.Collection) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) Set(java.util.Set) IOException(java.io.IOException) WrappedException(org.eclipse.emf.common.util.WrappedException) Collectors(java.util.stream.Collectors) File(java.io.File) Sets(com.google.common.collect.Sets) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) ConcurrentIndex(org.eclipse.n4js.xtext.ide.server.build.ConcurrentIndex) List(java.util.List) IterableExtensions(org.eclipse.xtext.xbase.lib.IterableExtensions) N4jsLibsAccess(org.eclipse.n4js.cli.helper.N4jsLibsAccess) TypesPackage(org.eclipse.n4js.ts.types.TypesPackage) Entry(java.util.Map.Entry) Assert(org.junit.Assert) Collections(java.util.Collections) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) ArrayList(java.util.ArrayList) File(java.io.File) HashSet(java.util.HashSet)

Example 8 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class DtsAfterBuildListener method createLibValue.

private String createLibValue() {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    sb.append("\"es2019\", \"es2020\"");
    for (ProjectReference requiredLibRef : projectConfig.getProjectDescription().getRequiredRuntimeLibraries()) {
        N4JSPackageName requiredLibName = requiredLibRef.getN4JSProjectName();
        ImmutableSet<String> dtsLibNames = N4JSGlobals.N4JS_DTS_LIB_CORRESPONDENCE.get(requiredLibName);
        for (String dtsLibName : dtsLibNames) {
            sb.append(", \"");
            sb.append(dtsLibName);
            sb.append('"');
        }
    }
    sb.append("]");
    return sb.toString();
}
Also used : ProjectReference(org.eclipse.n4js.packagejson.projectDescription.ProjectReference) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName)

Example 9 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class RepoRelativePath method compute.

/**
 * Creates the RepoRelativePath from a given resource. Returns null, if resource is not contained in a repository.
 * If a repository is found, the simple name of the origin is used.
 */
public static RepoRelativePath compute(FileURI uriOfResource, WorkspaceAccess workspaceAccess, Notifier context) {
    N4JSProjectConfigSnapshot project = workspaceAccess.findProjectByNestedLocation(context, uriOfResource.toURI());
    if (project == null) {
        return null;
    }
    Path pathOfResource = uriOfResource.toFileSystemPath();
    Path pathOfProject = project.getPathAsFileURI().toFileSystemPath();
    String fileOfResourceInsideProject = pathOfProject.relativize(pathOfResource).toString();
    // strip anchor part if present, i.e. path to type within the resource
    int anchorIndex = fileOfResourceInsideProject.indexOf("#");
    if (anchorIndex >= 0)
        fileOfResourceInsideProject = fileOfResourceInsideProject.substring(0, anchorIndex);
    File absolutePathOfResource = pathOfProject.toAbsolutePath().resolve(fileOfResourceInsideProject).toFile();
    if (!absolutePathOfResource.exists()) {
        return null;
    }
    // note: for retrieving the repo relative folder name, we must not rely on single path segments as they
    // may appear more than once. E.g. "n4js" maybe the folder of the oomph installation, the simple name of the
    // repository folder and a folder representing the package n4js.
    File repoFolder = getRepoFolder(absolutePathOfResource);
    // for resolving the repo relative path,
    // we only care about the repo folder name, since the folder may be named differently
    String pathOfProjectInRepo = getRepoPath(repoFolder, pathOfProject.getParent().toFile());
    if (pathOfProjectInRepo == null) {
        return null;
    }
    String pathOfResourceInProject = '/' + fileOfResourceInsideProject;
    // ensure slashes
    if (File.separatorChar != '/') {
        pathOfResourceInProject = pathOfResourceInProject.replace(File.separatorChar, '/');
        pathOfProjectInRepo = pathOfProjectInRepo.replace(File.separatorChar, '/');
    }
    N4JSPackageName projName = project.getN4JSPackageName();
    String repoName = getRepoName(repoFolder);
    return new // repo relative
    RepoRelativePath(// repo relative
    repoName, // repo relative
    pathOfProjectInRepo, // project relative
    projName, // project relative
    pathOfResourceInProject, -1);
}
Also used : Path(java.nio.file.Path) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) File(java.io.File)

Example 10 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class JSONIdeContentProposalProvider method proposeLocalPackages.

private void proposeLocalPackages(ContentAssistContext context, IIdeContentProposalAcceptor acceptor, List<String> namePath) {
    if (!namePath.isEmpty()) {
        // somewhat poor heuristic: propose all projects that are known in the current workspace
        String last = namePath.get(namePath.size() - 1);
        if (PackageJsonProperties.DEPENDENCIES.name.equals(last) || PackageJsonProperties.DEV_DEPENDENCIES.name.equals(last)) {
            for (N4JSProjectConfigSnapshot project : workspaceAccess.findAllProjects(context.getResource())) {
                N4JSPackageName projectName = project.getN4JSPackageName();
                ContentAssistEntry entryForModule = getProposalCreator().createProposal('"' + projectName.getRawName() + '"', context, ContentAssistEntry.KIND_MODULE, null);
                if (entryForModule != null) {
                    acceptor.accept(entryForModule, getProposalPriorities().getDefaultPriority(entryForModule));
                }
            }
        }
    }
}
Also used : ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)

Aggregations

N4JSPackageName (org.eclipse.n4js.workspace.utils.N4JSPackageName)23 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)9 Path (java.nio.file.Path)8 File (java.io.File)5 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 HashMap (java.util.HashMap)3 Entry (java.util.Map.Entry)3 Set (java.util.Set)3 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)3 Optional (com.google.common.base.Optional)2 FluentIterable (com.google.common.collect.FluentIterable)2 Multimap (com.google.common.collect.Multimap)2 Sets (com.google.common.collect.Sets)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 FileTime (java.nio.file.attribute.FileTime)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2