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));
}
}
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;
}
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);
}
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);
}
}
}
}
}
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;
}
Aggregations