Search in sources :

Example 1 with SourceContainerType

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

the class PackageJsonModificationUtils method addSourceFoldersToPackageJsonFile.

/**
 * Adds the given source folders, assuming <code>root</code> is the root object of a <code>package.json</code> file.
 * Throws an exception if one of those source folders is already contained in the <code>package.json</code>.
 */
public static void addSourceFoldersToPackageJsonFile(JsonElement root, SourceContainerType type, String... srcFolders) {
    Objects.requireNonNull(srcFolders);
    if (srcFolders.length == 0) {
        throw new IllegalArgumentException("no source folders given");
    }
    JsonArray srcFolderArray = JsonUtils.getOrCreateArrayDeepFailFast(root, UtilN4.PACKAGE_JSON__N4JS, UtilN4.PACKAGE_JSON__SOURCES, PackageJsonUtils.getSourceContainerTypeStringRepresentation(type));
    Set<String> existingSrcFolders = FluentIterable.from(srcFolderArray).filter(JsonPrimitive.class).transform(prim -> prim.getAsString()).toSet();
    for (String srcFolder : srcFolders) {
        if (existingSrcFolders.contains(srcFolder)) {
            throw new IllegalStateException("package.json file already contains this source folder: " + srcFolder);
        }
        srcFolderArray.add(new JsonPrimitive(srcFolder));
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Iterables(com.google.common.collect.Iterables) UtilN4(org.eclipse.n4js.utils.UtilN4) Multimap(com.google.common.collect.Multimap) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) Stack(java.util.Stack) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JsonElement(com.google.gson.JsonElement) Matcher(java.util.regex.Matcher) HashMultimap(com.google.common.collect.HashMultimap) FluentIterable(com.google.common.collect.FluentIterable) Optional(com.google.common.base.Optional) Gson(com.google.gson.Gson) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) JsonPrimitive(com.google.gson.JsonPrimitive) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Files(java.nio.file.Files) FileWriter(java.io.FileWriter) StandardOpenOption(java.nio.file.StandardOpenOption) Set(java.util.Set) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) FileVisitResult(java.nio.file.FileVisitResult) Strings(org.eclipse.n4js.utils.Strings) List(java.util.List) JsonArray(com.google.gson.JsonArray) FileVisitOption(java.nio.file.FileVisitOption) Entry(java.util.Map.Entry) Pattern(java.util.regex.Pattern) Pair(org.eclipse.xtext.xbase.lib.Pair) Collections(java.util.Collections) JsonUtils(org.eclipse.n4js.utils.JsonUtils) Joiner(com.google.common.base.Joiner) JsonPrimitive(com.google.gson.JsonPrimitive)

Example 2 with SourceContainerType

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

the class PackageJsonUtils method asSourceContainerDescriptionOrNull.

/**
 * Converts given name/value pair to a {@link SourceContainerDescription}; returns <code>null</code> if not
 * possible.
 * <p>
 * Expected format of argument:
 *
 * <pre>
 * "source": [
 *     "src1",
 *     "src2"
 * ]
 *
 * // or:
 *
 * "external": [
 *     "src-ext"
 * ]
 * </pre>
 */
public static SourceContainerDescription asSourceContainerDescriptionOrNull(NameValuePair pair) {
    SourceContainerType type = parseSourceContainerType(pair.getName());
    List<String> paths = asNonEmptyStringsInArrayOrEmpty(pair.getValue());
    if (type != null && !paths.isEmpty()) {
        return new SourceContainerDescription(type, paths);
    }
    return null;
}
Also used : SourceContainerDescription(org.eclipse.n4js.packagejson.projectDescription.SourceContainerDescription) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)

Example 3 with SourceContainerType

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

the class PackageJsonValidatorExtension method checkSourceContainers.

/**
 * Validates the source container section of N4JS package.json files
 */
@CheckProperty(property = SOURCES)
public void checkSourceContainers() {
    // obtain source-container-related content of the section and validate its structure
    Multimap<SourceContainerType, List<JSONStringLiteral>> sourceContainers = getSourceContainers();
    final List<JSONStringLiteral> allDeclaredSourceContainers = sourceContainers.entries().stream().flatMap(entry -> entry.getValue().stream()).collect(Collectors.toList());
    // check each source container sub-section (e.g. sources, external, etc.)
    final List<JSONStringLiteral> validSourceContainerLiterals = allDeclaredSourceContainers.stream().filter(l -> internalCheckSourceContainerLiteral(l)).collect(Collectors.toList());
    // find all groups of duplicate paths
    final List<List<JSONStringLiteral>> containerDuplicates = findPathDuplicates(allDeclaredSourceContainers);
    for (List<JSONStringLiteral> duplicateGroup : containerDuplicates) {
        // indicates whether the duplicates are spread across multiple container types (e.g. external, sources)
        final String normalizedPath = FileUtils.normalize(duplicateGroup.get(0).getValue());
        for (JSONStringLiteral duplicate : duplicateGroup) {
            final String inClause = createInSourceContainerTypeClause(duplicate, duplicateGroup);
            addIssue(IssueCodes.getMessageForPKGJ_DUPLICATE_SOURCE_CONTAINER(normalizedPath, inClause), duplicate, IssueCodes.PKGJ_DUPLICATE_SOURCE_CONTAINER);
        }
    }
    // check for nested source containers (within valid source container literals)
    internalCheckNoNestedSourceContainers(validSourceContainerLiterals);
}
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) List(java.util.List) ArrayList(java.util.ArrayList) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Example 4 with SourceContainerType

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

the class PackageJsonValidatorExtension method internalCheckOutput.

/**
 * Checks the given {@code outputPath} for validity.
 *
 * @param astOutputValue
 *            If present, the ast representation. May be {@code null} if {@code outputPath} is a default value.
 */
private void internalCheckOutput(String outputPath, Optional<JSONValue> astOutputValue) {
    final Resource resource = getDocument().eResource();
    final URI absoluteOutputLocation = getResourceRelativeURI(resource, outputPath);
    // forbid output folder for 'definition' projects
    final ProjectType projectType = getProjectType();
    if (projectType == ProjectType.DEFINITION && astOutputValue.isPresent()) {
        String message = IssueCodes.getMessageForPKGJ_DEFINES_PROPERTY(projectType.name(), "not ", "output");
        addIssue(message, astOutputValue.get().eContainer(), IssueCodes.PKGJ_DEFINES_PROPERTY);
    }
    // do not perform check for projects which do not require an output folder
    if (!isRequiresOutputAndSourceFolder(projectType)) {
        return;
    }
    final Multimap<SourceContainerType, List<JSONStringLiteral>> sourceContainers = getSourceContainers();
    for (Entry<SourceContainerType, List<JSONStringLiteral>> sourceContainerType : sourceContainers.entries()) {
        // iterate over all source container paths (in terms of string literals)
        for (JSONStringLiteral sourceContainerSpecifier : sourceContainerType.getValue()) {
            // compute absolute source container location
            final URI absoluteSourceLocation = getResourceRelativeURI(resource, sourceContainerSpecifier.getValue());
            // obtain descriptive name of the current source container type
            final String srcFrgmtName = PackageJsonUtils.getSourceContainerTypeStringRepresentation(sourceContainerType.getKey());
            // handle case that source container is nested within output directory (or equal)
            if (isContainedOrEqual(absoluteSourceLocation, absoluteOutputLocation)) {
                final String containingFolder = ("A " + srcFrgmtName + " folder");
                final String nestedFolder = astOutputValue.isPresent() ? "the output folder" : "the default output folder \"" + OUTPUT.defaultValue + "\"";
                final String message = IssueCodes.getMessageForOUTPUT_AND_SOURCES_FOLDER_NESTING(containingFolder, nestedFolder);
                addIssue(message, sourceContainerSpecifier, IssueCodes.OUTPUT_AND_SOURCES_FOLDER_NESTING);
            }
            // if "output" AST element is available (outputPath is not a default value)
            if (astOutputValue.isPresent()) {
                // handle case that output path is nested within a source folder (or equal)
                if (isContainedOrEqual(absoluteOutputLocation, absoluteSourceLocation)) {
                    final String containingFolder = "The output folder";
                    final String nestedFolder = ("a " + srcFrgmtName + " folder");
                    final String message = IssueCodes.getMessageForOUTPUT_AND_SOURCES_FOLDER_NESTING(containingFolder, nestedFolder);
                    addIssue(message, astOutputValue.get(), IssueCodes.OUTPUT_AND_SOURCES_FOLDER_NESTING);
                }
            }
        }
    }
}
Also used : ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) Resource(org.eclipse.emf.ecore.resource.Resource) List(java.util.List) ArrayList(java.util.ArrayList) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) FileURI(org.eclipse.n4js.workspace.locations.FileURI) URI(org.eclipse.emf.common.util.URI) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Example 5 with SourceContainerType

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

the class N4JSProjectConfig method createSourceFolders.

/**
 * Create source folders from the information in the given project description. Does not update the state of this
 * project configuration.
 */
protected Set<? extends IN4JSSourceFolder> createSourceFolders(ProjectDescription pd) {
    Set<IN4JSSourceFolder> result = new LinkedHashSet<>();
    for (SourceContainerDescription scd : pd.getSourceContainers()) {
        SourceContainerType type = scd.getType();
        for (String relPath : ProjectDescriptionUtils.getPathsNormalized(scd)) {
            result.add(new N4JSSourceFolder(this, type, relPath));
        }
    }
    result.add(new N4JSSourceFolderForPackageJson(this));
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SourceContainerDescription(org.eclipse.n4js.packagejson.projectDescription.SourceContainerDescription) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)

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