Search in sources :

Example 1 with ProjectDescriptionBuilder

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;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) ProjectDescriptionBuilder(org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)

Example 2 with ProjectDescriptionBuilder

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);
            }
        }
    }
}
Also used : JSONModelUtils.asStringsInArrayOrEmpty(org.eclipse.n4js.json.model.utils.JSONModelUtils.asStringsInArrayOrEmpty) Inject(com.google.inject.Inject) JSONValue(org.eclipse.n4js.json.JSON.JSONValue) VERSION(org.eclipse.n4js.packagejson.PackageJsonProperties.VERSION) SourceContainerDescription(org.eclipse.n4js.packagejson.projectDescription.SourceContainerDescription) VENDOR_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_ID) DependencyType(org.eclipse.n4js.packagejson.projectDescription.DependencyType) HashSet(java.util.HashSet) ProjectDependency(org.eclipse.n4js.packagejson.projectDescription.ProjectDependency) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) PROJECT_TYPE(org.eclipse.n4js.packagejson.PackageJsonProperties.PROJECT_TYPE) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) PackageJsonUtils.asProjectReferencesInArrayOrEmpty(org.eclipse.n4js.packagejson.PackageJsonUtils.asProjectReferencesInArrayOrEmpty) MAIN_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.MAIN_MODULE) ProjectDescriptionBuilder(org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder) PackageJsonUtils.asProjectReferenceOrNull(org.eclipse.n4js.packagejson.PackageJsonUtils.asProjectReferenceOrNull) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) JSONModelUtils.asNameValuePairsOrEmpty(org.eclipse.n4js.json.model.utils.JSONModelUtils.asNameValuePairsOrEmpty) JSONModelUtils.asNonEmptyStringOrNull(org.eclipse.n4js.json.model.utils.JSONModelUtils.asNonEmptyStringOrNull) ProjectDescriptionUtils(org.eclipse.n4js.utils.ProjectDescriptionUtils) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) GENERATOR_SOURCE_MAPS(org.eclipse.n4js.packagejson.PackageJsonProperties.GENERATOR_SOURCE_MAPS) OUTPUT(org.eclipse.n4js.packagejson.PackageJsonProperties.OUTPUT) PACKAGES(org.eclipse.n4js.packagejson.PackageJsonProperties.PACKAGES) PackageJsonUtils.asModuleFiltersInObjectOrEmpty(org.eclipse.n4js.packagejson.PackageJsonUtils.asModuleFiltersInObjectOrEmpty) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) Set(java.util.Set) PackageJsonUtils.parseProjectType(org.eclipse.n4js.packagejson.PackageJsonUtils.parseProjectType) VersionNumber(org.eclipse.n4js.semver.Semver.VersionNumber) JSONModelUtils.getProperty(org.eclipse.n4js.json.model.utils.JSONModelUtils.getProperty) Collectors(java.util.stream.Collectors) GENERATOR_DTS(org.eclipse.n4js.packagejson.PackageJsonProperties.GENERATOR_DTS) Objects(java.util.Objects) SemverHelper(org.eclipse.n4js.semver.SemverHelper) List(java.util.List) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) PackageJsonUtils.asSourceContainerDescriptionsOrEmpty(org.eclipse.n4js.packagejson.PackageJsonUtils.asSourceContainerDescriptionsOrEmpty) MAIN(org.eclipse.n4js.packagejson.PackageJsonProperties.MAIN) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONModelUtils.asStringOrNull(org.eclipse.n4js.json.model.utils.JSONModelUtils.asStringOrNull) JSONModelUtils.asBooleanOrDefault(org.eclipse.n4js.json.model.utils.JSONModelUtils.asBooleanOrDefault) GENERATOR_REWRITE_CJS_IMPORTS(org.eclipse.n4js.packagejson.PackageJsonProperties.GENERATOR_REWRITE_CJS_IMPORTS) Collections(java.util.Collections) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) ProjectDependency(org.eclipse.n4js.packagejson.projectDescription.ProjectDependency) HashSet(java.util.HashSet)

Example 3 with ProjectDescriptionBuilder

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;
    }
}
Also used : ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) ProjectDescriptionBuilder(org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)

Example 4 with ProjectDescriptionBuilder

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;
}
Also used : FileURI(org.eclipse.n4js.workspace.locations.FileURI) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) ProjectDescriptionBuilder(org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)

Example 5 with ProjectDescriptionBuilder

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;
}
Also used : URI(org.eclipse.emf.common.util.URI) ProjectDescriptionBuilder(org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)

Aggregations

ProjectDescriptionBuilder (org.eclipse.n4js.packagejson.projectDescription.ProjectDescriptionBuilder)5 ProjectDescription (org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)3 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)2 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)2 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)2 Inject (com.google.inject.Inject)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 URI (org.eclipse.emf.common.util.URI)1 EcoreUtil (org.eclipse.emf.ecore.util.EcoreUtil)1 N4JSGlobals (org.eclipse.n4js.N4JSGlobals)1 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)1 JSONModelUtils.asBooleanOrDefault (org.eclipse.n4js.json.model.utils.JSONModelUtils.asBooleanOrDefault)1 JSONModelUtils.asNameValuePairsOrEmpty (org.eclipse.n4js.json.model.utils.JSONModelUtils.asNameValuePairsOrEmpty)1 JSONModelUtils.asNonEmptyStringOrNull (org.eclipse.n4js.json.model.utils.JSONModelUtils.asNonEmptyStringOrNull)1 JSONModelUtils.asStringOrNull (org.eclipse.n4js.json.model.utils.JSONModelUtils.asStringOrNull)1