Search in sources :

Example 6 with ProjectType

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

the class TestWorkspaceManager method createSimpleProject.

private Project createSimpleProject(String projectName, Map<String, ? extends CharSequence> modulesContents, Multimap<String, String> dependencies, ProjectKind projectKind) {
    if (projectName.equals(N4JS_RUNTIME) && (modulesContents == null || modulesContents.isEmpty())) {
        return N4JS_RUNTIME_FAKE;
    }
    ProjectType prjType = projectName.equals(N4JS_RUNTIME) ? ProjectType.RUNTIME_ENVIRONMENT : projectType;
    Project project = new Project(projectName, VENDOR, VENDOR_NAME, prjType);
    CharSequence customSourceFolderName = modulesContents.get(CFG_SOURCE_FOLDER);
    Folder sourceFolder = project.createSourceFolder(customSourceFolderName != null ? customSourceFolderName.toString() : DEFAULT_SOURCE_FOLDER);
    for (String moduleName : modulesContents.keySet()) {
        String contents = modulesContents.get(moduleName).toString();
        if (moduleName.equals(CFG_DEPENDENCIES)) {
            String[] allDeps = contents.split(",");
            for (String dependency : allDeps) {
                String dependencyTrimmed = dependency.trim();
                dependencies.put(projectName, dependencyTrimmed);
                project.addProjectDependency(dependencyTrimmed);
            }
        } else if (moduleName.equals(CFG_MAIN_MODULE)) {
            project.setMainModule(contents);
        } else if (moduleName.equals(CFG_SOURCE_FOLDER)) {
        // ignore (already processed above)
        } else if (moduleName.equals(N4JSGlobals.PACKAGE_JSON)) {
            project.setProjectDescriptionContent(contents);
        } else if (moduleName.startsWith(CFG_NODE_MODULES)) {
            int length = CFG_NODE_MODULES.length();
            int endIdx = moduleName.indexOf(NAME_DIVIDER, length);
            String nmName = moduleName.substring(length, endIdx < 0 ? moduleName.length() : endIdx);
            if (moduleName.equals(CFG_NODE_MODULES + N4JS_RUNTIME)) {
                project.addNodeModuleProject(N4JS_RUNTIME_FAKE);
                project.addProjectDependency(N4JS_RUNTIME_FAKE.getName());
            } else if (moduleName.indexOf(CFG_SRC, length) > 0) {
                int indexOfSrc = moduleName.indexOf(CFG_SRC);
                String nmModuleName = moduleName.substring(indexOfSrc + CFG_SRC.length());
                Project nmProject = project.getNodeModuleProject(nmName);
                if (nmProject == null) {
                    nmProject = new Project(nmName, VENDOR, VENDOR + "_name", prjType);
                    nmProject.createSourceFolder(DEFAULT_SOURCE_FOLDER);
                    project.addNodeModuleProject(nmProject);
                    project.addProjectDependency(nmProject.getName());
                }
                Folder nmSourceFolder = nmProject.getSourceFolders().iterator().next();
                createAndAddModule(contents, nmModuleName, nmSourceFolder);
            } else if (moduleName.indexOf(CFG_DEPENDENCIES, length) > 0) {
                Project nmProject = project.getNodeModuleProject(nmName);
                String[] allDeps = contents.split(",");
                for (String dependency : allDeps) {
                    String dependencyTrimmed = dependency.trim();
                    dependencies.put(nmName, dependencyTrimmed);
                    nmProject.addProjectDependency(dependencyTrimmed);
                }
            } else if (moduleName.indexOf(CFG_NODE_MODULES, length) > 0) {
                Project nmProject = project.getNodeModuleProject(nmName);
                int startIdx = moduleName.indexOf(CFG_NODE_MODULES, length) + CFG_NODE_MODULES.length();
                String nmnmName = moduleName.substring(startIdx);
                Project nmnmProject = new Project(nmnmName, VENDOR, VENDOR + "_name", prjType);
                nmnmProject.createSourceFolder(DEFAULT_SOURCE_FOLDER);
                nmProject.addNodeModuleProject(nmnmProject);
                nmProject.addProjectDependency(nmnmProject.getName());
            } else {
                throw new IllegalArgumentException("Unknown specifier: " + moduleName);
            }
        } else {
            if (moduleName.startsWith("#")) {
                throw new IllegalArgumentException("unknown reserved string: " + moduleName);
            }
            Folder moduleSpecificSourceFolder = sourceFolder;
            if (moduleName.startsWith("/")) {
                // use first segment as source folder
                int endOfFirstSegment = moduleName.indexOf("/", 1);
                String sourceFolderName = moduleName.subSequence(1, endOfFirstSegment).toString();
                moduleName = moduleName.substring(endOfFirstSegment + 1);
                moduleSpecificSourceFolder = project.createSourceFolder(sourceFolderName);
            }
            createAndAddModule(contents, moduleName, moduleSpecificSourceFolder);
        }
    }
    // apply default values
    if (N4JSGlobals.PROJECT_TYPES_REQUIRING_N4JS_RUNTIME.contains(project.getType())) {
        // add dependency to n4js-runtime (if not already present)
        project.addProjectDependency(N4JS_RUNTIME);
        // add fake n4js-runtime to node_modules folder (if not already present)
        if (projectKind == ProjectKind.TopLevel) {
            if (project.getNodeModuleProject(N4JS_RUNTIME) == null) {
                project.addNodeModuleProject(N4JS_RUNTIME_FAKE);
            }
        }
    }
    return project;
}
Also used : Project(org.eclipse.n4js.tests.codegen.Project) YarnWorkspaceProject(org.eclipse.n4js.tests.codegen.YarnWorkspaceProject) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) Folder(org.eclipse.n4js.tests.codegen.Folder)

Example 7 with ProjectType

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

the class ProjectDiscoveryHelper method removeUnnecessaryPlainjsProjects.

/**
 * Removes from <code>projects</code> all "unnecessary" {@link ProjectType#PLAINJS PLAINJS} projects. A PLAINJS
 * project P is "unnecessary" if there does not exist an N4JS project P' in <code>projects</code> that has a
 * dependency to P.
 */
private void removeUnnecessaryPlainjsProjects(Set<Path> projects, Map<Path, ProjectDescription> pdCache) {
    Map<String, Path> plainjsProjects = new HashMap<>();
    Set<String> projectsRequiredByAnN4JSProject = new HashSet<>();
    for (Path project : projects) {
        // if it is null it was not added before
        ProjectDescription pd = getProjectDescription(project, pdCache);
        if (pd == null) {
            continue;
        }
        ProjectType type = pd.getType();
        if (type == ProjectType.PLAINJS) {
            // note: in case a name occurs twice yarn would throw an error
            plainjsProjects.put(pd.getPackageName(), project);
        } else {
            List<String> deps = pd.getProjectDependencies().stream().map(ProjectReference::getPackageName).collect(Collectors.toList());
            projectsRequiredByAnN4JSProject.addAll(deps);
        }
    }
    plainjsProjects.keySet().removeAll(projectsRequiredByAnN4JSProject);
    for (String plainJsName : plainjsProjects.keySet()) {
        projects.remove(plainjsProjects.get(plainJsName));
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 8 with ProjectType

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

the class DefinitionProjectMap method addProject.

/**
 * Add a project.
 */
public void addProject(ProjectDescription pd) {
    N4JSPackageName name = pd.getN4JSProjectName();
    ProjectType type = pd.getType();
    if (name == null || type == null) {
        // ignore invalid project descriptions
        return;
    }
    N4JSPackageName definedProjectName = pd.getDefinesPackage() != null ? new N4JSPackageName(pd.getDefinesPackage()) : null;
    addProject(name, type, definedProjectName);
}
Also used : ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType)

Aggregations

ProjectType (org.eclipse.n4js.packagejson.projectDescription.ProjectType)8 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)2 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)1 PackageJsonUtils.parseProjectType (org.eclipse.n4js.packagejson.PackageJsonUtils.parseProjectType)1 ProjectDescription (org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)1 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)1 Folder (org.eclipse.n4js.tests.codegen.Folder)1 Project (org.eclipse.n4js.tests.codegen.Project)1 YarnWorkspaceProject (org.eclipse.n4js.tests.codegen.YarnWorkspaceProject)1