Skip to content

Commit

Permalink
fix: resolve gradle URL deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Sep 9, 2023
1 parent e5baa13 commit 0608917
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,96 @@
*/
package org.spongepowered.gradle.plugin.config;

import org.gradle.api.GradleException;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.inject.Inject;

public class PluginLinksConfiguration {

private final Property<URL> homepage;
private final Property<URL> source;
private final Property<URL> issues;
private final Property<URI> homepage;
private final Property<URI> source;
private final Property<URI> issues;

private final Property<URL> legacyHomepage;
private final Property<URL> legacySource;
private final Property<URL> legacyIssues;

@Inject
public PluginLinksConfiguration(final ObjectFactory factory) {
this.homepage = factory.property(URL.class);
this.source = factory.property(URL.class);
this.issues = factory.property(URL.class);
this.legacyHomepage = factory.property(URL.class);
this.legacySource = factory.property(URL.class);
this.legacyIssues = factory.property(URL.class);

this.homepage = factory.property(URI.class).convention(urlToUri(this.legacyHomepage));
this.source = factory.property(URI.class).convention(urlToUri(this.legacySource));
this.issues = factory.property(URI.class).convention(urlToUri(this.legacyIssues));
}

private Provider<URI> urlToUri(final Property<URL> url) {
return url.map(old -> {
try {
return old.toURI();
} catch (final URISyntaxException ex) {
throw new GradleException("Failed to convert the provided URL to a URI, please set the URI directly", ex);
}
});
}

@Input
@Optional
public Property<URL> getHomepage() {
public Property<URI> getHomepageLink() {
return this.homepage;
}

public void homepage(final String homepage) throws MalformedURLException {
this.homepage.set(new URL(homepage));
@Deprecated
@Internal
public Property<URL> getHomepage() {
return this.legacyHomepage;
}

public void homepage(final String homepage) throws URISyntaxException {
this.homepage.set(new URI(homepage));
}

@Input
@Optional
public Property<URL> getSource() {
public Property<URI> getSourceLink() {
return this.source;
}

public void source(final String source) throws MalformedURLException {
this.source.set(new URL(source));
@Internal
@Deprecated
public Property<URL> getSource() {
return this.legacySource;
}

public void source(final String source) throws URISyntaxException {
this.source.set(new URI(source));
}

@Input
@Optional
public Property<URL> getIssues() {
public Property<URI> getIssuesLink() {
return this.issues;
}

public void issues(final String issues) throws MalformedURLException {
this.issues.set(new URL(issues));
@Deprecated
@Internal
public Property<URL> getIssues() {
return this.legacyIssues;
}

public void issues(final String issues) throws URISyntaxException {
this.issues.set(new URI(issues));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.gson.Gson;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.Property;
Expand Down Expand Up @@ -54,6 +55,7 @@
import org.spongepowered.plugin.metadata.builtin.model.StandardPluginLinks;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;

Expand Down Expand Up @@ -122,16 +124,20 @@ private <T extends StandardInheritable.AbstractBuilder<?, T>> T populateBuilder(

final StandardPluginLinks.Builder linksBuilder = StandardPluginLinks.builder();
final PluginLinksConfiguration linksConfiguration = src.getLinks();
if (linksConfiguration.getHomepage().isPresent()) {
linksBuilder.homepage(linksConfiguration.getHomepage().get());
}
if (linksConfiguration.getSource().isPresent()) {
linksBuilder.source(linksConfiguration.getSource().get());
}
if (linksConfiguration.getIssues().isPresent()) {
linksBuilder.issues(linksConfiguration.getIssues().get());
try {
if (linksConfiguration.getHomepageLink().isPresent()) {
linksBuilder.homepage(linksConfiguration.getHomepageLink().get().toURL());
}
if (linksConfiguration.getSourceLink().isPresent()) {
linksBuilder.source(linksConfiguration.getSourceLink().get().toURL());
}
if (linksConfiguration.getIssuesLink().isPresent()) {
linksBuilder.issues(linksConfiguration.getIssuesLink().get().toURL());
}
builder.links(linksBuilder.build());
} catch (final MalformedURLException ex) {
throw new GradleException("Failed to convert URIs back to URLs for writing to plugin meta file");
}
builder.links(linksBuilder.build());

// TODO: validate paths here?
final StandardPluginBranding.Builder brandingBuilder = StandardPluginBranding.builder();
Expand Down

0 comments on commit 0608917

Please sign in to comment.