Search in sources :

Example 1 with NPMVersionRequirement

use of org.eclipse.n4js.semver.Semver.NPMVersionRequirement in project n4js by eclipse.

the class N4JSCommandService method installNpm.

/**
 * Install the given NPM into the workspace.
 *
 * @param cancelIndicator
 *            not required.
 */
@SuppressWarnings("unused")
@ExecutableCommandHandler(JSONCodeActionService.INSTALL_NPM)
public Void installNpm(String packageName, String version, String fileUri, ILanguageServerAccess access, CancelIndicator cancelIndicator) {
    builderFrontend.asyncRunBuildTask("InstallNpm", () -> {
        return (ci) -> {
            // FIXME: Use CliTools in favor of npmCli
            NPMVersionRequirement versionRequirement = semverHelper.parse(version);
            if (versionRequirement == null) {
                versionRequirement = SemverUtils.createEmptyVersionRequirement();
            }
            // @formatter:off
            access.getLanguageClient().showMessage(new MessageParams(MessageType.Warning, "Installation of npm packages is disabled. Use command line."));
            // @formatter:on
            return new CoarseGrainedChangeEvent();
        };
    }).whenComplete((a, b) -> builderFrontend.reinitWorkspace());
    return null;
}
Also used : CoarseGrainedChangeEvent(org.eclipse.xtext.resource.impl.CoarseGrainedChangeEvent) MessageParams(org.eclipse.lsp4j.MessageParams) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement)

Example 2 with NPMVersionRequirement

use of org.eclipse.n4js.semver.Semver.NPMVersionRequirement in project n4js by eclipse.

the class PackageJsonValidatorExtension method checkVersion.

/**
 * Check the version property.
 */
@CheckProperty(property = VERSION)
public void checkVersion(JSONValue versionValue) {
    if (!checkIsType(versionValue, JSONPackage.Literals.JSON_STRING_LITERAL, "as package version")) {
        return;
    }
    JSONStringLiteral versionJsonString = (JSONStringLiteral) versionValue;
    String versionString = versionJsonString.getValue();
    IParseResult parseResult = validateSemver(versionValue, versionString);
    NPMVersionRequirement npmVersion = semverHelper.parse(parseResult);
    VersionRangeSetRequirement vrs = semverHelper.parseVersionRangeSet(parseResult);
    if (vrs == null) {
        String reason = "Cannot parse given string";
        if (npmVersion != null) {
            reason = "Given string is parsed as " + getVersionRequirementType(npmVersion);
        }
        String msg = IssueCodes.getMessageForPKGJ_INVALID_VERSION_NUMBER(versionString, reason);
        addIssue(msg, versionValue, IssueCodes.PKGJ_INVALID_VERSION_NUMBER);
        return;
    }
    if (!vrs.getRanges().isEmpty() && vrs.getRanges().get(0) instanceof VersionRangeConstraint) {
        VersionRangeConstraint vrc = (VersionRangeConstraint) vrs.getRanges().get(0);
        SimpleVersion simpleVersion = vrc.getVersionConstraints().get(0);
        if (!simpleVersion.getComparators().isEmpty()) {
            String comparator = SemverSerializer.serialize(simpleVersion.getComparators().get(0));
            String reason = "Version numbers must not have comparators: '" + comparator + "'";
            String msg = IssueCodes.getMessageForPKGJ_INVALID_VERSION_NUMBER(versionString, reason);
            addIssue(msg, versionValue, IssueCodes.PKGJ_INVALID_VERSION_NUMBER);
            return;
        }
    }
}
Also used : VersionRangeConstraint(org.eclipse.n4js.semver.Semver.VersionRangeConstraint) VersionRangeSetRequirement(org.eclipse.n4js.semver.Semver.VersionRangeSetRequirement) SimpleVersion(org.eclipse.n4js.semver.Semver.SimpleVersion) IParseResult(org.eclipse.xtext.parser.IParseResult) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement)

Example 3 with NPMVersionRequirement

use of org.eclipse.n4js.semver.Semver.NPMVersionRequirement in project n4js by eclipse.

the class PackageJsonHelper method convertDependencies.

private void convertDependencies(ProjectDescriptionBuilder target, List<NameValuePair> depPairs, boolean avoidDuplicates, DependencyType type) {
    Objects.requireNonNull(type);
    Set<String> existingProjectNames = new HashSet<>();
    if (avoidDuplicates) {
        for (ProjectDependency pd : target.getDependencies()) {
            existingProjectNames.add(pd.getPackageName());
        }
    }
    for (NameValuePair pair : depPairs) {
        String projectName = pair.getName();
        boolean addProjectDependency = true;
        addProjectDependency &= projectName != null && !projectName.isEmpty();
        addProjectDependency &= !(avoidDuplicates && existingProjectNames.contains(projectName));
        existingProjectNames.add(projectName);
        if (addProjectDependency) {
            JSONValue value = pair.getValue();
            String valueStr = asStringOrNull(value);
            NPMVersionRequirement versionRequirement = valueStr != null ? semverHelper.parse(valueStr) : null;
            ProjectDependency dep = new ProjectDependency(projectName, type, valueStr, versionRequirement);
            target.addDependency(dep);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) ProjectDependency(org.eclipse.n4js.packagejson.projectDescription.ProjectDependency) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement) HashSet(java.util.HashSet)

Aggregations

NPMVersionRequirement (org.eclipse.n4js.semver.Semver.NPMVersionRequirement)3 HashSet (java.util.HashSet)1 MessageParams (org.eclipse.lsp4j.MessageParams)1 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)1 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)1 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)1 ProjectDependency (org.eclipse.n4js.packagejson.projectDescription.ProjectDependency)1 SimpleVersion (org.eclipse.n4js.semver.Semver.SimpleVersion)1 VersionRangeConstraint (org.eclipse.n4js.semver.Semver.VersionRangeConstraint)1 VersionRangeSetRequirement (org.eclipse.n4js.semver.Semver.VersionRangeSetRequirement)1 IParseResult (org.eclipse.xtext.parser.IParseResult)1 CoarseGrainedChangeEvent (org.eclipse.xtext.resource.impl.CoarseGrainedChangeEvent)1