Search in sources :

Example 6 with JSONValue

use of org.eclipse.n4js.json.JSON.JSONValue in project n4js by eclipse.

the class PackageJsonHelper method convertRootPairs.

private void convertRootPairs(ProjectDescriptionBuilder target, List<NameValuePair> rootPairs) {
    for (NameValuePair pair : rootPairs) {
        PackageJsonProperties property = PackageJsonProperties.valueOfNameValuePairOrNull(pair);
        if (property == null) {
            continue;
        }
        JSONValue value = pair.getValue();
        switch(property) {
            case NAME:
                target.setPackageName(asNonEmptyStringOrNull(value));
                break;
            case VERSION:
                target.setVersion(asVersionNumberOrNull(value));
                break;
            case TYPE:
                // legal values are 'commonjs' and 'module'
                target.setESM("module".equals(asNonEmptyStringOrNull(value)));
                break;
            case DEPENDENCIES:
                convertDependencies(target, asNameValuePairsOrEmpty(value), true, DependencyType.RUNTIME);
                break;
            case DEV_DEPENDENCIES:
                // for the moment, we do not separate devDependencies from ordinary dependencies in ProjectDescription
                convertDependencies(target, asNameValuePairsOrEmpty(value), true, DependencyType.DEVELOPMENT);
                break;
            case MAIN:
                // (see method #adjustProjectDescriptionAfterConversion())
                break;
            case MODULE:
                // we don't care about the actual value, just about the fact that property "module" is present
                target.setModuleProperty(true);
                break;
            case N4JS:
                // mark project with N4JS nature
                target.setN4JSNature(true);
                convertN4jsPairs(target, asNameValuePairsOrEmpty(value));
                break;
            case WORKSPACES_ARRAY:
                target.setYarnWorkspaceRoot(true);
                target.getWorkspaces().addAll(asStringsInArrayOrEmpty(value));
                break;
            case WORKSPACES_OBJECT:
                target.setYarnWorkspaceRoot(true);
                JSONObject workspaces = (JSONObject) value;
                for (NameValuePair pairOfWorkspaces : workspaces.getNameValuePairs()) {
                    PackageJsonProperties wProp = PackageJsonProperties.valueOfNameValuePairOrNull(pairOfWorkspaces);
                    if (wProp == PACKAGES) {
                        JSONValue packagesValue = pairOfWorkspaces.getValue();
                        target.getWorkspaces().addAll(asStringsInArrayOrEmpty(packagesValue));
                        break;
                    }
                }
                break;
            default:
                break;
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 7 with JSONValue

use of org.eclipse.n4js.json.JSON.JSONValue in project n4js by eclipse.

the class PackageJsonHelper method convertN4jsPairs.

private void convertN4jsPairs(ProjectDescriptionBuilder target, List<NameValuePair> n4jsPairs) {
    for (NameValuePair pair : n4jsPairs) {
        PackageJsonProperties property = PackageJsonProperties.valueOfNameValuePairOrNull(pair);
        if (property == null) {
            continue;
        }
        JSONValue value = pair.getValue();
        switch(property) {
            case PROJECT_TYPE:
                ProjectType projectType = parseProjectType(asNonEmptyStringOrNull(value));
                if (projectType != null) {
                    target.setType(projectType);
                }
                break;
            case VENDOR_ID:
                target.setVendorId(asNonEmptyStringOrNull(value));
                break;
            case VENDOR_NAME:
                target.setVendorName(asNonEmptyStringOrNull(value));
                break;
            case OUTPUT:
                target.setOutputPath(asNonEmptyStringOrNull(value));
                break;
            case SOURCES:
                target.getSourceContainers().addAll(asSourceContainerDescriptionsOrEmpty(value));
                break;
            case MODULE_FILTERS:
                target.getModuleFilters().addAll(asModuleFiltersInObjectOrEmpty(value));
                break;
            case MAIN_MODULE:
                target.setMainModule(asNonEmptyStringOrNull(value));
                break;
            case TESTED_PROJECTS:
                target.getTestedProjects().addAll(asProjectReferencesInArrayOrEmpty(value));
                break;
            case IMPLEMENTATION_ID:
                target.setImplementationId(asNonEmptyStringOrNull(value));
                break;
            case IMPLEMENTED_PROJECTS:
                target.getImplementedProjects().addAll(asProjectReferencesInArrayOrEmpty(value));
                break;
            case EXTENDED_RUNTIME_ENVIRONMENT:
                target.setExtendedRuntimeEnvironment(asProjectReferenceOrNull(value));
                break;
            case PROVIDED_RUNTIME_LIBRARIES:
                target.getProvidedRuntimeLibraries().addAll(asProjectReferencesInArrayOrEmpty(value));
                break;
            case REQUIRED_RUNTIME_LIBRARIES:
                target.getRequiredRuntimeLibraries().addAll(asProjectReferencesInArrayOrEmpty(value));
                break;
            case DEFINES_PACKAGE:
                target.setDefinesPackage(asStringOrNull(value));
                break;
            case GENERATOR:
                convertN4jsPairs(target, asNameValuePairsOrEmpty(value));
                break;
            case GENERATOR_SOURCE_MAPS:
                target.setGeneratorEnabledSourceMaps(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_SOURCE_MAPS.defaultValue));
                break;
            case GENERATOR_DTS:
                target.setGeneratorEnabledDts(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_DTS.defaultValue));
                break;
            case GENERATOR_REWRITE_MODULE_SPECIFIERS:
                for (NameValuePair nvp : asNameValuePairsOrEmpty(value)) {
                    String n = nvp.getName();
                    String v = asStringOrNull(nvp.getValue());
                    if (n != null && v != null) {
                        // note: we allow empty strings
                        target.getGeneratorRewriteModuleSpecifiers().put(n, v);
                    }
                }
                break;
            case GENERATOR_REWRITE_CJS_IMPORTS:
                target.setGeneratorEnabledRewriteCjsImports(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_REWRITE_CJS_IMPORTS.defaultValue));
                break;
            default:
                break;
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) PackageJsonUtils.parseProjectType(org.eclipse.n4js.packagejson.PackageJsonUtils.parseProjectType)

Example 8 with JSONValue

use of org.eclipse.n4js.json.JSON.JSONValue in project n4js by eclipse.

the class ProjectDescriptionLoader method adjustMainPath.

/**
 * Adjust the path value of the "main" property of the given package.json document as follows (in-place change of
 * the given JSON document):
 * <ol>
 * <li>if the path points to a folder, then "/index.js" will be appended,
 * <li>if neither a folder nor a file exist at the location the path points to and the path does not end in ".js",
 * then ".js" will be appended.
 * </ol>
 */
private void adjustMainPath(URI location, JSONDocument packageJSON) {
    JSONValue content = packageJSON.getContent();
    if (!(content instanceof JSONObject))
        return;
    JSONObject contentCasted = (JSONObject) content;
    NameValuePair mainProperty = JSONModelUtils.getNameValuePair(contentCasted, MAIN.name).orElse(null);
    if (mainProperty == null) {
        return;
    }
    String main = asNonEmptyStringOrNull(mainProperty.getValue());
    if (main == null) {
        return;
    }
    String[] mainSegments;
    if (File.separatorChar == '/') {
        List<String> splitted = Strings.split(main, '/');
        mainSegments = splitted.toArray(String[]::new);
    } else {
        mainSegments = windowsPattern.split(main);
    }
    URI locationWithMain = location.appendSegments(mainSegments);
    if (!main.endsWith(".js") && isFile(URIConverter.INSTANCE, locationWithMain.appendFileExtension("js"))) {
        main += ".js";
        mainProperty.setValue(JSONModelUtils.createStringLiteral(main));
    } else if (isDirectory(URIConverter.INSTANCE, locationWithMain)) {
        if (!(main.endsWith("/") || main.endsWith(File.separator))) {
            main += "/";
        }
        main += "index.js";
        mainProperty.setValue(JSONModelUtils.createStringLiteral(main));
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) URI(org.eclipse.emf.common.util.URI) SafeURI(org.eclipse.n4js.workspace.locations.SafeURI)

Example 9 with JSONValue

use of org.eclipse.n4js.json.JSON.JSONValue in project n4js by eclipse.

the class ProjectDescriptionLoader method loadVersionAndN4JSNatureFromProjectDescriptionAtLocation.

/**
 * Loads the project description of the N4JS project at the given {@code location} and returns the version string or
 * <code>null</code> if undefined or in case of error.
 */
public Pair<String, Boolean> loadVersionAndN4JSNatureFromProjectDescriptionAtLocation(SafeURI<?> location) {
    JSONDocument packageJSON = loadPackageJSONAtLocation(location);
    JSONValue versionValue = null;
    boolean hasN4JSNature = false;
    if (packageJSON != null) {
        versionValue = JSONModelUtils.getProperty(packageJSON, VERSION.name).orElse(null);
        hasN4JSNature = JSONModelUtils.getProperty(packageJSON, N4JS.name).isPresent();
    }
    Pair<String, Boolean> result = Tuples.create(asNonEmptyStringOrNull(versionValue), hasN4JSNature);
    return result;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument)

Example 10 with JSONValue

use of org.eclipse.n4js.json.JSON.JSONValue in project n4js by eclipse.

the class AbstractPackageJSONValidatorExtension method collectDocumentValues.

/**
 * Collects all key-path-value pairs that can be extracted from the given {@code object} and stores them in
 * {@code documentValues}.
 *
 * Key-paths are represented in terms of concatenated strings, separated by a {@code .} character (e.g.
 * "nested.name.value" => JSONValue).
 *
 * @param object
 *            The {@link JSONObject} to collect the values form.
 * @param documentValues
 *            The map to store the values to.
 * @param prefix
 *            The prefix to use for key-paths.
 * @param depth
 *            The depth up to which the given {@link JSONObject} should be traversed. If {@code -1} no depth limit
 *            is assumed.
 */
private Multimap<String, JSONValue> collectDocumentValues(JSONObject object, Multimap<String, JSONValue> documentValues, String prefix, int depth) {
    // For negative depths this will always evaluate to false -> no depth limit
    if (depth == 0) {
        return documentValues;
    }
    for (NameValuePair pair : object.getNameValuePairs()) {
        final String pairName = pair.getName();
        final String name = prefix.isEmpty() ? pairName : prefix + "." + pairName;
        final JSONValue value = pair.getValue();
        documentValues.put(name, value);
        if (value instanceof JSONObject) {
            // recursively collect all values from pair value
            collectDocumentValues((JSONObject) value, documentValues, name, depth - 1);
        }
    }
    return documentValues;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Aggregations

JSONValue (org.eclipse.n4js.json.JSON.JSONValue)27 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)19 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)17 EObject (org.eclipse.emf.ecore.EObject)7 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)7 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 URI (org.eclipse.emf.common.util.URI)4 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)3 Multimap (com.google.common.collect.Multimap)3 Inject (com.google.inject.Inject)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 EClass (org.eclipse.emf.ecore.EClass)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 N4JSGlobals (org.eclipse.n4js.N4JSGlobals)3 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)3