Search in sources :

Example 1 with InvalidVersionSpecificationException

use of org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException in project SpongeVanilla by SpongePowered.

the class PluginCandidate method verifyVersionRange.

private boolean verifyVersionRange(String id, @Nullable String expectedRange, @Nullable String version) {
    if (expectedRange == null) {
        return true;
    }
    // Don't check version again if it already failed
    if (expectedRange.equals(this.missingRequirements.get(id))) {
        return false;
    }
    // Don't check version again if it was already checked
    if (expectedRange.equals(this.versions.get(id))) {
        return true;
    }
    if (version != null) {
        try {
            VersionRange range = VersionRange.createFromVersionSpec(expectedRange);
            DefaultArtifactVersion installedVersion = new DefaultArtifactVersion(version);
            if (range.containsVersion(installedVersion)) {
                String currentRange = this.versions.get(id);
                if (currentRange != null) {
                    // This should almost never happen because it means the plugin is
                    // depending on two different versions of another plugin
                    // We need to merge the ranges
                    VersionRange otherRange;
                    try {
                        otherRange = VersionRange.createFromVersionSpec(currentRange);
                    } catch (InvalidVersionSpecificationException e) {
                        // Should never happen because we already parsed it once
                        throw new AssertionError(e);
                    }
                    expectedRange = otherRange.restrict(range).toString();
                }
                this.versions.put(id, expectedRange);
                if (range.getRecommendedVersion() instanceof DefaultArtifactVersion) {
                    BigInteger majorExpected = ((DefaultArtifactVersion) range.getRecommendedVersion()).getVersion().getFirstInteger();
                    if (majorExpected != null) {
                        BigInteger majorInstalled = installedVersion.getVersion().getFirstInteger();
                        // or if the installed version is lower than the recommended version
                        if (majorInstalled != null && (!majorExpected.equals(majorInstalled) || installedVersion.compareTo(range.getRecommendedVersion()) < 0)) {
                            VanillaLaunch.getLogger().warn("Plugin {} from {} was designed for {} {}. It may not work properly.", this.id, this.source, id, range.getRecommendedVersion());
                        }
                    }
                }
                return true;
            }
        } catch (InvalidVersionSpecificationException e) {
            VanillaLaunch.getLogger().error("Failed to parse version range {} for dependency {} of plugin {} from {}: {}", version, id, this.id, this.source, e.getMessage());
            this.invalid = true;
        }
    } else {
        if (this.dependenciesWithUnknownVersion.add(id)) {
            VanillaLaunch.getLogger().warn("Cannot check version of dependency {} for plugin {} from {}: Version of dependency unknown", id, this.id, this.source);
        }
        return true;
    }
    return false;
}
Also used : InvalidVersionSpecificationException(org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException) DefaultArtifactVersion(org.spongepowered.plugin.meta.version.DefaultArtifactVersion) BigInteger(java.math.BigInteger) VersionRange(org.spongepowered.plugin.meta.version.VersionRange)

Example 2 with InvalidVersionSpecificationException

use of org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException in project SpongeAPI by SpongePowered.

the class PluginElement method apply.

void apply(Messager messager) {
    String value = this.annotation.get().id();
    if (!ID_PATTERN.matcher(value).matches()) {
        messager.printMessage(ERROR, "Plugin ID '" + value + "' must match pattern '" + ID_PATTERN.pattern() + "'. " + "It should be lower case, start with an alphabetic character and may only contain alphanumeric characters, underscores or " + "dashes.", this.element, this.annotation.getMirror(), this.annotation.getValue("id"));
    }
    value = this.annotation.get().name();
    if (value.isEmpty()) {
        if (this.metadata.getName() == null) {
            messager.printMessage(WARNING, "Missing plugin name", this.element, this.annotation.getMirror());
        }
    } else {
        this.metadata.setName(value);
    }
    value = this.annotation.get().version();
    if (value.isEmpty()) {
        if (this.metadata.getVersion() == null) {
            messager.printMessage(WARNING, "Missing plugin version", this.element, this.annotation.getMirror());
        }
    } else {
        this.metadata.setVersion(value);
    }
    value = this.annotation.get().description();
    if (value.isEmpty()) {
        if (this.metadata.getDescription() == null) {
            messager.printMessage(WARNING, "Missing plugin description", this.element, this.annotation.getMirror());
        }
    } else {
        this.metadata.setDescription(value);
    }
    value = this.annotation.get().url();
    if (!value.isEmpty()) {
        if (!isLikelyValidUrl(value)) {
            messager.printMessage(ERROR, "Invalid URL: " + value, this.element, this.annotation.getMirror(), this.annotation.getValue("url"));
        }
        this.metadata.setUrl(value);
    } else if ((value = this.metadata.getUrl()) != null) {
        if (!isLikelyValidUrl(value)) {
            messager.printMessage(ERROR, "Invalid URL: " + value + " in extra metadata files", this.element, this.annotation.getMirror());
        }
    }
    String[] authors = this.annotation.get().authors();
    if (authors.length > 0) {
        this.metadata.getAuthors().clear();
        for (String author : authors) {
            if (author.isEmpty()) {
                messager.printMessage(ERROR, "Empty author is not allowed", this.element, this.annotation.getMirror(), this.annotation.getValue("authors"));
                continue;
            }
            this.metadata.addAuthor(author);
        }
    }
    checkDependencies(this.metadata.getDependencies(), messager);
    Set<String> addedDependencies = new HashSet<>();
    Dependency[] dependencies = this.annotation.get().dependencies();
    if (dependencies.length > 0) {
        for (Dependency dependency : dependencies) {
            final String id = dependency.id();
            if (id.isEmpty()) {
                messager.printMessage(ERROR, "Dependency ID should not be empty", this.element, this.annotation.getMirror(), this.annotation.getValue("dependencies"));
                continue;
            }
            if (id.equals("*")) {
                messager.printMessage(ERROR, "Wildcard dependencies are not supported on Sponge", this.element, this.annotation.getMirror(), this.annotation.getValue("dependencies"));
                continue;
            }
            final String version = dependency.version();
            if (!version.isEmpty()) {
                try {
                    VersionRange.createFromVersionSpec(version);
                } catch (InvalidVersionSpecificationException e) {
                    messager.printMessage(ERROR, "Invalid dependency version range: " + version + " (" + e.getMessage() + ") Please check the Javadocs of @Dependency.version() for details.", this.element, this.annotation.getMirror(), this.annotation.getValue("dependencies"));
                }
            }
            if (addedDependencies.add(id)) {
                // TODO: Load order
                this.metadata.replaceDependency(new PluginDependency(PluginDependency.LoadOrder.BEFORE, id, dependency.version(), dependency.optional()));
            } else {
                messager.printMessage(ERROR, "Duplicate dependency with plugin ID: " + id, this.element, this.annotation.getMirror(), this.annotation.getValue("dependencies"));
            }
        }
    }
    if (this.metadata.getDependency(Platform.API_ID) == null) {
        // Add SpongeAPI as required dependency to the metadata
        this.metadata.addDependency(new PluginDependency(PluginDependency.LoadOrder.BEFORE, Platform.API_ID, API_VERSION, false));
    }
}
Also used : InvalidVersionSpecificationException(org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException) PluginDependency(org.spongepowered.plugin.meta.PluginDependency) PluginDependency(org.spongepowered.plugin.meta.PluginDependency) Dependency(org.spongepowered.api.plugin.Dependency) HashSet(java.util.HashSet)

Example 3 with InvalidVersionSpecificationException

use of org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException in project LanternServer by LanternPowered.

the class PluginCandidate method verifyVersionRange.

private boolean verifyVersionRange(String id, @Nullable String expectedRange, @Nullable String version) {
    if (expectedRange == null) {
        return true;
    }
    // Don't check version again if it already failed
    if (expectedRange.equals(this.missingRequirements.get(id))) {
        return false;
    }
    // Don't check version again if it was already checked
    if (expectedRange.equals(this.versions.get(id))) {
        return true;
    }
    if (version != null) {
        try {
            final VersionRange range = VersionRange.createFromVersionSpec(expectedRange);
            final DefaultArtifactVersion installedVersion = new DefaultArtifactVersion(version);
            if (range.containsVersion(installedVersion)) {
                final String currentRange = this.versions.get(id);
                if (currentRange != null) {
                    // This should almost never happen because it means the plugin is
                    // depending on two different versions of another plugin
                    // We need to merge the ranges
                    final VersionRange otherRange;
                    try {
                        otherRange = VersionRange.createFromVersionSpec(currentRange);
                    } catch (InvalidVersionSpecificationException e) {
                        // Should never happen because we already parsed it once
                        throw new AssertionError(e);
                    }
                    expectedRange = otherRange.restrict(range).toString();
                }
                this.versions.put(id, expectedRange);
                if (range.getRecommendedVersion() instanceof DefaultArtifactVersion) {
                    final BigInteger majorExpected = ((DefaultArtifactVersion) range.getRecommendedVersion()).getVersion().getFirstInteger();
                    if (majorExpected != null) {
                        final BigInteger majorInstalled = installedVersion.getVersion().getFirstInteger();
                        // or if the installed version is lower than the recommended version
                        if (majorInstalled != null && (!majorExpected.equals(majorInstalled) || installedVersion.compareTo(range.getRecommendedVersion()) < 0)) {
                            Lantern.getLogger().warn("Plugin {} from {} was designed for {} {}. It may not work properly.", this.id, this.source.map(Path::toString).orElse("unknown"), id, range.getRecommendedVersion());
                        }
                    }
                }
                return true;
            }
        } catch (InvalidVersionSpecificationException e) {
            Lantern.getLogger().error("Failed to parse version range {} for dependency {} of plugin {} from {}: {}", version, id, this.id, getDisplaySource(), e.getMessage());
            this.invalid = true;
        }
    } else {
        if (this.dependenciesWithUnknownVersion.add(id)) {
            Lantern.getLogger().warn("Cannot check version of dependency {} for plugin {} from {}: Unknown dependency version.", id, this.id, this.source.map(Path::toString).orElse("unknown"));
        }
        return true;
    }
    return false;
}
Also used : Path(java.nio.file.Path) InvalidVersionSpecificationException(org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException) DefaultArtifactVersion(org.spongepowered.plugin.meta.version.DefaultArtifactVersion) BigInteger(java.math.BigInteger) VersionRange(org.spongepowered.plugin.meta.version.VersionRange)

Aggregations

InvalidVersionSpecificationException (org.spongepowered.plugin.meta.version.InvalidVersionSpecificationException)3 BigInteger (java.math.BigInteger)2 DefaultArtifactVersion (org.spongepowered.plugin.meta.version.DefaultArtifactVersion)2 VersionRange (org.spongepowered.plugin.meta.version.VersionRange)2 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 Dependency (org.spongepowered.api.plugin.Dependency)1 PluginDependency (org.spongepowered.plugin.meta.PluginDependency)1