Search in sources :

Example 6 with SourceContainerType

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

the class PackageJsonValidatorExtension method createInSourceContainerTypeClause.

/**
 * Constructs in-clause (e.g. "in external, test") for sections in which a source container path can be declared.
 *
 * If the source container type of {@code issueTarget} is the only section (e.g. external) in which
 * {@code duplicates} appear, the in-clause is empty. Otherwise the in-clause lists all source container types for
 * which duplicates have been declared.
 */
private String createInSourceContainerTypeClause(JSONStringLiteral issueTarget, List<JSONStringLiteral> duplicates) {
    final SourceContainerType targetContainerType = getSourceContainerType(issueTarget);
    final Set<SourceContainerType> otherTypes = duplicates.stream().filter(d -> d != issueTarget).map(d -> getSourceContainerType(d)).collect(Collectors.toSet());
    // if issueTarget's type is the only type for which there have been declared duplicate paths
    if (otherTypes.size() == 1 && otherTypes.iterator().next() == targetContainerType) {
        // do not use an in-clause
        return "";
    }
    // otherwise list all other types for which the path of issueTarget has been declared as well
    return " in " + otherTypes.stream().map(PackageJsonUtils::getSourceContainerTypeStringRepresentation).collect(Collectors.joining(", "));
}
Also used : REQUIRED_RUNTIME_LIBRARIES(org.eclipse.n4js.packagejson.PackageJsonProperties.REQUIRED_RUNTIME_LIBRARIES) IssueCodes(org.eclipse.n4js.validation.IssueCodes) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral) Inject(com.google.inject.Inject) NV_SOURCE_CONTAINER(org.eclipse.n4js.packagejson.PackageJsonProperties.NV_SOURCE_CONTAINER) NAME(org.eclipse.n4js.packagejson.PackageJsonProperties.NAME) FileUtils(org.eclipse.n4js.utils.io.FileUtils) ProjectNameInfo(org.eclipse.n4js.utils.ProjectDescriptionUtils.ProjectNameInfo) URLVersionRequirement(org.eclipse.n4js.semver.Semver.URLVersionRequirement) VENDOR_NAME(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_NAME) HashMultimap(com.google.common.collect.HashMultimap) PackageJsonUtils(org.eclipse.n4js.packagejson.PackageJsonUtils) DEPENDENCIES(org.eclipse.n4js.packagejson.PackageJsonProperties.DEPENDENCIES) ModuleFilterType(org.eclipse.n4js.packagejson.projectDescription.ModuleFilterType) Optional(com.google.common.base.Optional) Map(java.util.Map) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) VersionRangeSetRequirement(org.eclipse.n4js.semver.Semver.VersionRangeSetRequirement) INode(org.eclipse.xtext.nodemodel.INode) Check(org.eclipse.xtext.validation.Check) Path(java.nio.file.Path) PackageJsonProperties(org.eclipse.n4js.packagejson.PackageJsonProperties) JSONPackage(org.eclipse.n4js.json.JSON.JSONPackage) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) IParseResult(org.eclipse.xtext.parser.IParseResult) FileURI(org.eclipse.n4js.workspace.locations.FileURI) DEFINES_PACKAGE(org.eclipse.n4js.packagejson.PackageJsonProperties.DEFINES_PACKAGE) N4JS(org.eclipse.n4js.packagejson.PackageJsonProperties.N4JS) Collection(java.util.Collection) IMPLEMENTATION_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTATION_ID) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) TagVersionRequirement(org.eclipse.n4js.semver.Semver.TagVersionRequirement) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Collectors(java.util.stream.Collectors) GitHubVersionRequirement(org.eclipse.n4js.semver.Semver.GitHubVersionRequirement) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) List(java.util.List) Stream(java.util.stream.Stream) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement) SimpleVersion(org.eclipse.n4js.semver.Semver.SimpleVersion) GENERATOR_REWRITE_MODULE_SPECIFIERS(org.eclipse.n4js.packagejson.PackageJsonProperties.GENERATOR_REWRITE_MODULE_SPECIFIERS) IMPLEMENTED_PROJECTS(org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTED_PROJECTS) Entry(java.util.Map.Entry) Resource(org.eclipse.emf.ecore.resource.Resource) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) Singleton(com.google.inject.Singleton) DEV_DEPENDENCIES(org.eclipse.n4js.packagejson.PackageJsonProperties.DEV_DEPENDENCIES) URI(org.eclipse.emf.common.util.URI) VersionRangeConstraint(org.eclipse.n4js.semver.Semver.VersionRangeConstraint) JSONValue(org.eclipse.n4js.json.JSON.JSONValue) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Multimap(com.google.common.collect.Multimap) VERSION(org.eclipse.n4js.packagejson.PackageJsonProperties.VERSION) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) PROVIDED_RUNTIME_LIBRARIES(org.eclipse.n4js.packagejson.PackageJsonProperties.PROVIDED_RUNTIME_LIBRARIES) TESTED_PROJECTS(org.eclipse.n4js.packagejson.PackageJsonProperties.TESTED_PROJECTS) VENDOR_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_ID) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InvalidPathException(java.nio.file.InvalidPathException) EClass(org.eclipse.emf.ecore.EClass) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) WorkspaceAccess(org.eclipse.n4js.workspace.WorkspaceAccess) PROJECT_TYPE(org.eclipse.n4js.packagejson.PackageJsonProperties.PROJECT_TYPE) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) MAIN_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.MAIN_MODULE) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) JSONModelUtils.asNonEmptyStringOrNull(org.eclipse.n4js.json.model.utils.JSONModelUtils.asNonEmptyStringOrNull) ProjectDescriptionUtils(org.eclipse.n4js.utils.ProjectDescriptionUtils) MODULE_FILTERS(org.eclipse.n4js.packagejson.PackageJsonProperties.MODULE_FILTERS) NV_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.NV_MODULE) OUTPUT(org.eclipse.n4js.packagejson.PackageJsonProperties.OUTPUT) Iterator(java.util.Iterator) EXTENDED_RUNTIME_ENVIRONMENT(org.eclipse.n4js.packagejson.PackageJsonProperties.EXTENDED_RUNTIME_ENVIRONMENT) SOURCES(org.eclipse.n4js.packagejson.PackageJsonProperties.SOURCES) File(java.io.File) LocalPathVersionRequirement(org.eclipse.n4js.semver.Semver.LocalPathVersionRequirement) SemverHelper(org.eclipse.n4js.semver.SemverHelper) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) Issue(org.eclipse.xtext.validation.Issue) Paths(java.nio.file.Paths) SemverSerializer(org.eclipse.n4js.semver.model.SemverSerializer) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) PackageJsonUtils(org.eclipse.n4js.packagejson.PackageJsonUtils) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)

Example 7 with SourceContainerType

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

the class PackageJsonValidatorExtension method doGetSourceContainers.

/**
 * Validates the correct structure of a {@link PackageJsonProperties#SOURCES} section and returns a map between the
 * declared source container types and corresponding {@link JSONStringLiteral}s that specify the various source
 * container paths.
 */
private Multimap<SourceContainerType, List<JSONStringLiteral>> doGetSourceContainers() {
    final Collection<JSONValue> sourcesValues = getDocumentValues(SOURCES);
    // first check whether n4js.sources section has been defined at all
    if (sourcesValues.isEmpty()) {
        // return an empty map
        return ImmutableMultimap.<SourceContainerType, List<JSONStringLiteral>>of();
    }
    // first check type of all occuring 'sources' sections
    if (!checkIsType(sourcesValues, JSONPackage.Literals.JSON_OBJECT, "as source container section")) {
        // return an empty map
        return ImmutableMultimap.<SourceContainerType, List<JSONStringLiteral>>of();
    }
    // only consider the first n4js.sources section for further validation (in case of duplicates)
    final JSONValue sourcesValue = sourcesValues.iterator().next();
    final JSONObject sourceContainerObject = (JSONObject) sourcesValue;
    final Multimap<SourceContainerType, List<JSONStringLiteral>> sourceContainerValues = HashMultimap.create();
    for (NameValuePair pair : sourceContainerObject.getNameValuePairs()) {
        final String sourceContainerType = pair.getName();
        // compute type of source container sub-section
        final SourceContainerType containerType = PackageJsonUtils.parseSourceContainerType(pair.getName());
        // check that sourceContainerType represents a valid source container type
        if (containerType == null) {
            addIssue(IssueCodes.getMessageForPKGJ_INVALID_SOURCE_CONTAINER_TYPE(sourceContainerType), pair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_INVALID_SOURCE_CONTAINER_TYPE);
            continue;
        }
        // check type of RHS (list of source paths)
        if (!checkIsType(pair.getValue(), JSONPackage.Literals.JSON_ARRAY, "as source container list")) {
            continue;
        }
        final JSONArray sourceContainerSpecifiers = (JSONArray) pair.getValue();
        // collect all source container paths in this list
        final List<JSONStringLiteral> specifierLiterals = new ArrayList<>();
        for (JSONValue specifier : sourceContainerSpecifiers.getElements()) {
            if (!checkIsType(specifier, JSONPackage.Literals.JSON_STRING_LITERAL, "as source container specifier")) {
                continue;
            }
            specifierLiterals.add((JSONStringLiteral) specifier);
        }
        // This may override a value in case of a duplicate containerType (e.g. two external sections).
        // However, this will also issue an appropriate warning for a duplicate key and
        // is therefore not handled here.
        sourceContainerValues.put(containerType, specifierLiterals);
    }
    return sourceContainerValues;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Aggregations

SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)4 Optional (com.google.common.base.Optional)3 HashMultimap (com.google.common.collect.HashMultimap)3 Multimap (com.google.common.collect.Multimap)3 File (java.io.File)3 Path (java.nio.file.Path)3 HashSet (java.util.HashSet)3 Entry (java.util.Map.Entry)3 Set (java.util.Set)3 URI (org.eclipse.emf.common.util.URI)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 Inject (com.google.inject.Inject)2 Singleton (com.google.inject.Singleton)2 InvalidPathException (java.nio.file.InvalidPathException)2 Paths (java.nio.file.Paths)2 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)2