Skip to content

Commit

Permalink
Add properties to allow plugins to make suggestions to others
Browse files Browse the repository at this point in the history
  • Loading branch information
timja committed May 26, 2022
1 parent 34702bc commit 2aba575
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/main/java/io/jenkins/plugins/thememanager/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import static java.util.Collections.singletonList;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.Beta;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand All @@ -26,11 +30,13 @@ public class Theme {
private final List<String> cssUrls;
private final List<String> javascriptUrls;
private final boolean blueOceanCompatible;
private final Map<String, String> properties;

private Theme(List<String> cssUrls, List<String> javascriptUrls, boolean blueOceanCompatible) {
private Theme(List<String> cssUrls, List<String> javascriptUrls, boolean blueOceanCompatible, Map<String, String> properties) {
this.cssUrls = cssUrls;
this.javascriptUrls = javascriptUrls;
this.blueOceanCompatible = blueOceanCompatible;
this.properties = properties;
}

@Restricted(NoExternalUse.class)
Expand Down Expand Up @@ -77,6 +83,32 @@ public boolean isBlueOceanCompatible() {
return blueOceanCompatible;
}

/**
* Additional information that theme authors can provide to influence other plugins
*
* e.g. the Prism API plugin can read properties and use a default theme based on this information.
* @param artifactId the plugin to retrieve the properties for
* @return the properties associated with the plugin requested
*/
public List<String> getProperties(String artifactId) {
return properties.entrySet().stream()
.filter(k -> k.getKey().startsWith(artifactId + ":"))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}

/**
* Additional information that theme authors can provide to influence other plugins
*
* e.g. the Prism API plugin can read properties and use a default theme based on this information.
* @param artifactId the plugin to retrieve the properties for
* @param propertyName the property to retrieve
* @return the properties associated with the plugin requested
*/
public Optional<String> getProperty(String artifactId, String propertyName) {
return Optional.ofNullable(properties.get(artifactId + ":" + propertyName));
}

/**
* Constructs the builder for the theme.
*
Expand All @@ -91,6 +123,7 @@ public static class Builder {
private List<String> cssUrls = emptyList();
private List<String> javascriptUrls = emptyList();
private boolean blueOceanCompatible = false;
private final Map<String, String> properties = new HashMap<>();

Builder() {}

Expand Down Expand Up @@ -148,6 +181,20 @@ public Builder disableOnBlueOcean() {
return this;
}

/**
* Properties are a way a theme author can provide extra information to plugins.
* e.g. the Prism API plugin can read properties and use a default theme based on this information.
*
* @param pluginId artifact ID of the plugin the property is associated with
* @param name property name this will be namespaced with the artifactId automatically
* @param value the property value
* @return the current builder with the new property
*/
public Builder withProperty(String pluginId, String name, String value) {
this.properties.put(pluginId + ":" + name, value);
return this;
}

/**
* A URL to a JavaScript file, this can be served by Jenkins or remote.
*
Expand Down Expand Up @@ -188,7 +235,7 @@ public Builder withJavascriptUrls(List<String> javascriptUrls) {
* @return the theme.
*/
public Theme build() {
return new Theme(cssUrls, javascriptUrls, blueOceanCompatible);
return new Theme(cssUrls, javascriptUrls, blueOceanCompatible, properties);
}
}
}
28 changes: 28 additions & 0 deletions src/test/java/io/jenkins/plugins/thememanager/ThemeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.jenkins.plugins.thememanager;

import java.util.Optional;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class ThemeTest {

@Test
public void getPropertyReturnsKeyIfPresent() {
Theme theme = Theme.builder()
.withProperty("prism-api", "theme", "some-theme")
.build();

Optional<String> property = theme.getProperty("prism-api", "theme");
assertThat(property,is(Optional.of("some-theme")));
}

@Test
public void getPropertyReturnsOptionalIfNotPresent() {
Theme theme = Theme.builder().build();

Optional<String> property = theme.getProperty("prism-api", "theme");
assertThat(property, is(Optional.empty()));
}
}

0 comments on commit 2aba575

Please sign in to comment.