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;
}
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));
}
}
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);
}
Aggregations