Skip to content

Commit

Permalink
feat(core): Add a screenshots section to project. Resolves #883
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Aug 11, 2022
1 parent e0f2fff commit 2261f8d
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class Project extends AbstractModelObject<Project> implements Domain, Ext
private final Links links = new Links();
private final Java java = new Java();
private final Snapshot snapshot = new Snapshot();
private final List<Screenshot> screenshots = new ArrayList<>();
private String name;
private String version;
private VersionPattern versionPattern = new VersionPattern();
Expand All @@ -81,6 +82,7 @@ public void freeze() {
java.freeze();
snapshot.freeze();
versionPattern.freeze();
screenshots.forEach(ModelObject::freeze);
}

@Override
Expand All @@ -102,6 +104,7 @@ public void merge(Project project) {
setTags(merge(this.tags, project.tags));
setExtraProperties(merge(this.extraProperties, project.extraProperties));
setLinks(project.links);
setScreenshots(merge(this.screenshots, project.screenshots));
}

@Override
Expand Down Expand Up @@ -277,6 +280,23 @@ public void setStereotype(String str) {
setStereotype(Stereotype.of(str));
}

public List<Screenshot> getScreenshots() {
return freezeWrap(screenshots);
}

public void setScreenshots(List<Screenshot> screenshots) {
freezeCheck();
this.screenshots.clear();
this.screenshots.addAll(screenshots);
}

public void addScreenshot(Screenshot screenshot) {
freezeCheck();
if (null != screenshot) {
this.screenshots.add(screenshot);
}
}

public Java getJava() {
return java;
}
Expand Down Expand Up @@ -348,6 +368,12 @@ public Map<String, Object> asMap(boolean full) {
map.put("tags", tags);
map.put("stereotype", stereotype);
map.put("links", links.asMap(full));
Map<String, Map<String, Object>> sm = new LinkedHashMap<>();
int i = 0;
for (Screenshot screenshot : screenshots) {
sm.put("screenshot " + (i++), screenshot.asMap(full));
}
map.put("screenshots", sm);
map.put("extraProperties", getResolvedExtraProperties());
if (java.isEnabled()) {
map.put("java", java.asMap(full));
Expand Down
133 changes: 133 additions & 0 deletions core/jreleaser-model/src/main/java/org/jreleaser/model/Screenshot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2022 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.model;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import static org.jreleaser.util.StringUtils.isBlank;

/**
* @author Andres Almiray
* @since 1.2.0
*/
public class Screenshot extends AbstractModelObject<Screenshot> implements Domain {
private Type type = Type.SOURCE;
private Boolean primary;
private String url;
private String caption;
private Integer width;
private Integer height;

@Override
public void merge(Screenshot registry) {
freezeCheck();
this.type = merge(this.type, registry.type);
this.primary = merge(this.primary, registry.primary);
this.url = merge(this.url, registry.url);
this.caption = merge(this.caption, registry.caption);
this.width = merge(this.width, registry.width);
this.height = merge(this.height, registry.height);
}

public Type getType() {
return type;
}

public void setType(Type type) {
freezeCheck();
this.type = type;
}

public void setType(String str) {
setType(Type.of(str));
}

public Boolean isPrimary() {
return primary != null && primary;
}

public void setPrimary(Boolean primary) {
freezeCheck();
this.primary = primary;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
freezeCheck();
this.url = url;
}

public String getCaption() {
return caption;
}

public void setCaption(String caption) {
freezeCheck();
this.caption = caption;
}

public Integer getWidth() {
return width;
}

public void setWidth(Integer width) {
freezeCheck();
this.width = width;
}

public Integer getHeight() {
return height;
}

public void setHeight(Integer height) {
freezeCheck();
this.height = height;
}

@Override
public Map<String, Object> asMap(boolean full) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("type", type);
map.put("url", url);
if (isPrimary()) map.put("primary", isPrimary());
map.put("caption", caption);
map.put("width", width);
map.put("height", height);
return map;
}

public enum Type {
SOURCE,
THUMBNAIL;

@Override
public String toString() {
return name().toLowerCase(Locale.ENGLISH);
}

public static Type of(String str) {
if (isBlank(str)) return null;
return Type.valueOf(str.toUpperCase(Locale.ENGLISH).trim());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import org.jreleaser.model.JReleaserContext;
import org.jreleaser.model.LicenseId;
import org.jreleaser.model.Project;
import org.jreleaser.model.Screenshot;
import org.jreleaser.model.VersionPattern;
import org.jreleaser.util.Errors;

import java.util.List;

import static org.jreleaser.model.Project.DEFAULT_SNAPSHOT_LABEL;
import static org.jreleaser.model.Project.DEFAULT_SNAPSHOT_PATTERN;
import static org.jreleaser.model.Project.PROJECT_NAME;
Expand Down Expand Up @@ -111,6 +114,8 @@ public static void validateProject(JReleaserContext context, JReleaserContext.Mo
if ((mode.validateConfig() && javaDistributions) || javaAssemblers) {
validateJava(context, project, errors);
}

validateScreenshots(context, mode, project.getScreenshots(), errors, "project");
}

public static void postValidateProject(JReleaserContext context, JReleaserContext.Mode mode, Errors errors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import org.jreleaser.model.OwnerAware;
import org.jreleaser.model.Packager;
import org.jreleaser.model.RepositoryTap;
import org.jreleaser.model.Screenshot;
import org.jreleaser.model.TimeoutAware;
import org.jreleaser.util.Env;
import org.jreleaser.util.Errors;

import java.util.Collection;
import java.util.List;

import static org.jreleaser.util.StringUtils.isBlank;
import static org.jreleaser.util.StringUtils.isNotBlank;
Expand Down Expand Up @@ -246,4 +248,38 @@ static void validateFileSet(JReleaserContext context, JReleaserContext.Mode mode
errors.configuration(RB.$("validation_must_not_be_null", assembler.getType() + "." + assembler.getName() + ".fileSet[" + index + "].input"));
}
}

static void validateScreenshots(JReleaserContext context, JReleaserContext.Mode mode, List<Screenshot> screenshots, Errors errors, String base) {
if (screenshots.size() == 1) {
screenshots.get(0).setPrimary(true);
}

if (screenshots.stream()
.mapToInt(s -> s.isPrimary() ? 1 : 0)
.sum() > 1) {
errors.configuration(RB.$("validation_multiple_primary_screenshots", base));
}

for (int i = 0; i < screenshots.size(); i++) {
Screenshot screenshot = screenshots.get(i);
if (isBlank(screenshot.getUrl())) {
errors.configuration(RB.$("validation_must_not_be_blank", base + ".screenshots[" + i + "].url"));
}

if (screenshot.getType() == Screenshot.Type.THUMBNAIL) {
if (null == screenshot.getWidth()) {
errors.configuration(RB.$("validation_must_not_be_null", base + ".screenshots[" + i + "].width"));
}
if (null == screenshot.getHeight()) {
errors.configuration(RB.$("validation_must_not_be_null", base + ".screenshots[" + i + "].height"));
}
} else {
if (null == screenshot.getWidth() && null != screenshot.getHeight()) {
errors.configuration(RB.$("validation_must_not_be_null", base + ".screenshots[" + i + "].width"));
} else if (null != screenshot.getWidth() && null == screenshot.getHeight()) {
errors.configuration(RB.$("validation_must_not_be_null", base + ".screenshots[" + i + "].height"));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ ERROR_artifacts_unexpected_error_globs = Unexpected error when resolving globs
ERROR_artifacts_unexpected_error_path = Unexpected error visiting path {}
ERROR_artifacts_download_url_missing = cannot resolve downloadUrl for {}. Using default settings from {}

validation_multiple_primary_screenshots = There's more than 1 primary screenshot in {}
validation_java_home_missing = Java home could not be found
validation_s3_missing_download_url = {} defines a custom endpoint but no downloadUrl
validation_gitlab_non_matching_uploader = Uploader {}:{} is not configured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ interface Project extends ExtraProperties {

void snapshot(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Snapshot) Closure<Void> action)

void screenshot(Action<? super Screenshot> action)

void screenshot(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Screenshot) Closure<Void> action)

interface Snapshot {
Property<String> getPattern()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2022 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.gradle.plugin.dsl

import groovy.transform.CompileStatic
import org.gradle.api.provider.Property

/**
*
* @author Andres Almiray
* @since 1.2.0
*/
@CompileStatic
interface Screenshot {
void setScreenshotType(String str)

Property<org.jreleaser.model.Screenshot.Type> getScreenshotType()

Property<Boolean> getPrimary()

Property<String> getUrl()

Property<String> getCaption()

Property<Integer> getWidth()

Property<Integer> getHeight()
}

0 comments on commit 2261f8d

Please sign in to comment.