use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder in project n4js by eclipse.
the class PackageJsonHelper method convertToProjectDescription.
/**
* Transform the given {@code packageJSON} into an equivalent {@link ProjectDescriptionBuilder} instance. If no
* further adjustments are required, the client can immediately invoke the {@code #build()} method to obtain the
* corresponding {@link ProjectDescription}.
* <p>
* Note: this method does not implement the package.json feature that a "main" path may point to a folder and then a
* file "index.js" in that folder will be used as main module (for details see
* {@link ProjectDescriptionUtils#convertMainPathToModuleSpecifier(String, List)}).
*
* @param packageJSON
* the JSON document to convert (should be the representation of a valid {@code package.json} file).
* @param applyDefaultValues
* whether default values should be applied to the project description after conversion.
* @param defaultProjectName
* the default project ID (will be ignored if {@code applyDefaultValues} is set to <code>false</code>.
* @return the project description converted from the given JSON document or <code>null</code> if the root value of
* the given JSON document is not a {@link JSONObject}.
*/
public ProjectDescriptionBuilder convertToProjectDescription(JSONDocument packageJSON, boolean applyDefaultValues, String defaultProjectName) {
JSONValue rootValue = packageJSON.getContent();
if (!(rootValue instanceof JSONObject)) {
return null;
}
ProjectDescriptionBuilder target = new ProjectDescriptionBuilder();
List<NameValuePair> rootPairs = ((JSONObject) rootValue).getNameValuePairs();
convertRootPairs(target, rootPairs);
String valueOfPropMain = asNonEmptyStringOrNull(getProperty((JSONObject) rootValue, MAIN.name).orElse(null));
adjustProjectDescriptionAfterConversion(target, applyDefaultValues, defaultProjectName, valueOfPropMain);
return target;
}
use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder in project n4js by eclipse.
the class PackageJsonHelper method adjustProjectDescriptionAfterConversion.
private void adjustProjectDescriptionAfterConversion(ProjectDescriptionBuilder target, boolean applyDefaultValues, String defaultProjectName, String valueOfTopLevelPropertyMain) {
// store whether target has a declared mainModule *before* applying the default values
boolean hasN4jsSpecificMainModule = target.getMainModule() != null;
// apply default values (if desired)
if (applyDefaultValues) {
applyDefaults(target, defaultProjectName);
}
// (note: this makes use of the source containers, so it possibly relies on default values having been applied)
if (valueOfTopLevelPropertyMain != null) {
if (!hasN4jsSpecificMainModule) {
// only if no N4JS-specific "mainModule" property was given
List<String> sourceContainerPaths = target.getSourceContainers().stream().flatMap(scd -> ProjectDescriptionUtils.getPathsNormalized(scd).stream()).collect(Collectors.toList());
String mainModulePath = ProjectDescriptionUtils.convertMainPathToModuleSpecifier(valueOfTopLevelPropertyMain, sourceContainerPaths);
if (mainModulePath != null) {
target.setMainModule(mainModulePath);
}
}
}
// sanitize dependencies: remove implementation projects from dependencies if API projects also given
// (this is a work-around for supporting the API/Impl concept with npm/yarn: in a client project, both the API
// and implementation projects will be specified as dependency in the package.json and the following code will
// filter out implementation projects to not confuse API/Impl logic in other places)
Set<String> projectNamesToRemove = new HashSet<>();
List<ProjectDependency> projectDependencies = target.getDependencies();
for (ProjectDependency dep : projectDependencies) {
String otherProject = dep.getPackageName();
for (String suffix : N4JSGlobals.API_PROJECT_NAME_SUFFIXES) {
if (otherProject.endsWith(suffix)) {
projectNamesToRemove.add(otherProject.substring(0, otherProject.length() - suffix.length()));
break;
}
}
}
if (!projectNamesToRemove.isEmpty()) {
for (int i = projectDependencies.size() - 1; i >= 0; i--) {
if (projectNamesToRemove.contains(projectDependencies.get(i).getPackageName())) {
projectDependencies.remove(i);
}
}
}
}
use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder in project n4js by eclipse.
the class ProjectDescriptionLoader method loadProjectDescriptionAtLocation.
/**
* Same as {@link #loadPackageJSONAtLocation(SafeURI)}.
*/
public ProjectDescription loadProjectDescriptionAtLocation(URI location, URI relatedRootLocation, JSONDocument packageJSON) {
if (location == null) {
return null;
}
adjustMainPath(location, packageJSON);
ProjectDescriptionBuilder pdbFromPackageJSON = packageJSON != null ? packageJsonHelper.convertToProjectDescription(packageJSON, true, null) : null;
if (pdbFromPackageJSON != null) {
setInformationFromFileSystem(location, pdbFromPackageJSON);
pdbFromPackageJSON.setLocation(location);
pdbFromPackageJSON.setRelatedRootLocation(relatedRootLocation);
ProjectDescription result = pdbFromPackageJSON.build();
return result;
} else {
return null;
}
}
use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder 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;
}
use of org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder in project n4js by eclipse.
the class AbstractPackageJSONValidatorExtension method isPckjsonOfPlainJS.
private boolean isPckjsonOfPlainJS(JSONDocument jsonDocument) {
URI uri = jsonDocument.eResource().getURI();
String fileExtension = fileExtensionCalculator.getFilenameWithoutXpectExtension(uri);
boolean isPckjson = fileExtension.equals(N4JSGlobals.PACKAGE_JSON);
if (isPckjson) {
ProjectDescriptionBuilder pdb = pckjsonHelper.convertToProjectDescription(jsonDocument, true, "xyz");
return pdb != null && pdb.build().getType() == ProjectType.PLAINJS;
}
return false;
}
Aggregations