Search in sources :

Example 1 with ProjectType

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

the class JSONIdeContentProposalProvider method proposeProjectTypes.

private void proposeProjectTypes(ContentAssistContext context, IIdeContentProposalAcceptor acceptor, List<String> namePath) {
    if (namePath.size() >= 2) {
        String n4js = namePath.get(namePath.size() - 2);
        String projectType = namePath.get(namePath.size() - 1);
        if (PackageJsonProperties.PROJECT_TYPE.name.equals(projectType) || PackageJsonProperties.N4JS.name.equals(n4js)) {
            for (ProjectType type : ProjectType.values()) {
                String asString = PackageJsonUtils.getProjectTypeStringRepresentation(type);
                if (asString.equals(asString.toUpperCase())) {
                    asString = asString.toLowerCase();
                }
                ContentAssistEntry entryForProjectType = getProposalCreator().createProposal('"' + asString + '"', context, ContentAssistEntry.KIND_KEYWORD, null);
                if (entryForProjectType != null) {
                    acceptor.accept(entryForProjectType, getProposalPriorities().getDefaultPriority(entryForProjectType));
                }
            }
        }
    }
}
Also used : ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType)

Example 2 with ProjectType

use of org.eclipse.n4js.packagejson.projectDescription.ProjectType 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 3 with ProjectType

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

the class N4JSResourceValidator method isValidFileTypeForProjectType.

private boolean isValidFileTypeForProjectType(Resource resource, N4JSProjectConfigSnapshot project) {
    final ResourceType resourceType = ResourceType.getResourceType(resource);
    final ProjectType projectType = project.getType();
    if (resourceType == ResourceType.JS || resourceType == ResourceType.JSX || resourceType == ResourceType.N4JS || resourceType == ResourceType.N4JSX) {
        // we have a .js or .n4js file ...
        if (projectType == ProjectType.RUNTIME_LIBRARY || projectType == ProjectType.DEFINITION) {
            // --> invalid!
            return false;
        }
    }
    return true;
}
Also used : ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) ResourceType(org.eclipse.n4js.utils.ResourceType)

Example 4 with ProjectType

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

the class PackageJsonValidatorExtension method checkProjectType.

/**
 * Check the projectType value structure and limitations.
 */
@CheckProperty(property = PROJECT_TYPE)
public void checkProjectType(JSONValue projectTypeValue) {
    if (!checkIsType(projectTypeValue, JSONPackage.Literals.JSON_STRING_LITERAL)) {
        return;
    }
    if (!checkIsNonEmptyString((JSONStringLiteral) projectTypeValue, PROJECT_TYPE)) {
        return;
    }
    // check whether the given value represents a valid project type
    final String projectTypeString = ((JSONStringLiteral) projectTypeValue).getValue();
    final ProjectType type = PackageJsonUtils.parseProjectType(projectTypeString);
    // check type can be parsed successfully
    if (type == null) {
        addIssue(IssueCodes.getMessageForPKGJ_INVALID_PROJECT_TYPE(projectTypeString), projectTypeValue, IssueCodes.PKGJ_INVALID_PROJECT_TYPE);
        return;
    }
    // check limitations of specific project types
    final boolean isDefType = type == ProjectType.DEFINITION;
    final JSONValue propDefinesPck = getSingleDocumentValue(DEFINES_PACKAGE);
    final boolean hasDefPck = propDefinesPck != null;
    if (isDefType != hasDefPck) {
        EObject issueObj = propDefinesPck == null ? projectTypeValue : propDefinesPck.eContainer();
        String not = propDefinesPck == null ? "" : "not ";
        String msg = IssueCodes.getMessageForPKGJ_DEFINES_PROPERTY(type.toString(), not, "definesPackage");
        addIssue(msg, issueObj, IssueCodes.PKGJ_DEFINES_PROPERTY);
    }
    if (isRequiresOutputAndSourceFolder(type)) {
        // make sure non-validation projects always declare an output and at least one source folder
        final boolean hasSources = getSingleDocumentValue(SOURCES) != null;
        final boolean hasOutput = getSingleDocumentValue(OUTPUT) != null;
        if (!hasSources || !hasOutput) {
            String msg = IssueCodes.getMessageForPKGJ_PROJECT_TYPE_MANDATORY_OUTPUT_AND_SOURCES(projectTypeString);
            addIssue(msg, projectTypeValue, IssueCodes.PKGJ_PROJECT_TYPE_MANDATORY_OUTPUT_AND_SOURCES);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) EObject(org.eclipse.emf.ecore.EObject) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Example 5 with ProjectType

use of org.eclipse.n4js.packagejson.projectDescription.ProjectType 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)

Aggregations

ProjectType (org.eclipse.n4js.packagejson.projectDescription.ProjectType)8 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)2 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)1 PackageJsonUtils.parseProjectType (org.eclipse.n4js.packagejson.PackageJsonUtils.parseProjectType)1 ProjectDescription (org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)1 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)1 Folder (org.eclipse.n4js.tests.codegen.Folder)1 Project (org.eclipse.n4js.tests.codegen.Project)1 YarnWorkspaceProject (org.eclipse.n4js.tests.codegen.YarnWorkspaceProject)1