org.jreleaser.ant.tasks.AbstractJReleaserTask Maven / Gradle / Ivy
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2024 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.ant.tasks;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.jreleaser.ant.tasks.internal.JReleaserLoggerAdapter;
import org.jreleaser.config.JReleaserConfigParser;
import org.jreleaser.engine.context.ContextCreator;
import org.jreleaser.logging.JReleaserLogger;
import org.jreleaser.model.JReleaserVersion;
import org.jreleaser.model.api.JReleaserContext.Mode;
import org.jreleaser.model.internal.JReleaserContext;
import org.jreleaser.util.Env;
import org.jreleaser.util.PlatformUtils;
import org.jreleaser.util.StringUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.ServiceLoader;
import java.util.Set;
import static java.util.stream.Collectors.toList;
import static org.jreleaser.util.FileUtils.resolveOutputDirectory;
import static org.jreleaser.util.IoUtils.newPrintWriter;
import static org.jreleaser.util.StringUtils.isBlank;
import static org.jreleaser.util.StringUtils.isNotBlank;
/**
* @author Andres Almiray
* @since 0.1.0
*/
abstract class AbstractJReleaserTask extends Task {
protected File basedir;
protected File configFile;
protected Boolean dryrun;
protected Boolean gitRootSearch;
protected Boolean strict;
protected boolean skip;
protected Path outputDir;
protected JReleaserLogger logger;
protected Path actualConfigFile;
protected Path actualBasedir;
public void setBasedir(File basedir) {
this.basedir = basedir;
}
public void setConfigFile(File configFile) {
this.configFile = configFile;
}
public void setDryrun(Boolean dryrun) {
this.dryrun = dryrun;
}
public void setGitRootSearch(Boolean gitRootSearch) {
this.gitRootSearch = gitRootSearch;
}
public void setStrict(Boolean strict) {
this.strict = strict;
}
public void setSkip(boolean skip) {
this.skip = skip;
}
public void setOutputDir(Path outputDir) {
this.outputDir = outputDir;
}
@Override
public void execute() throws BuildException {
Banner.display(newPrintWriter(System.err));
if (skip) return;
resolveConfigFile();
resolveBasedir();
initLogger();
PlatformUtils.resolveCurrentPlatform(logger);
logger.info("JReleaser {}", JReleaserVersion.getPlainVersion());
JReleaserVersion.banner(logger.getTracer());
logger.info("Configuring with {}", actualConfigFile);
logger.info(" - basedir set to {}", actualBasedir.toAbsolutePath());
logger.info(" - outputdir set to {}", getOutputDirectory().toAbsolutePath());
doExecute(createContext());
}
private void resolveConfigFile() {
if (null != configFile) {
actualConfigFile = configFile.toPath();
} else {
ServiceLoader parsers = ServiceLoader.load(JReleaserConfigParser.class,
JReleaserConfigParser.class.getClassLoader());
for (JReleaserConfigParser parser : parsers) {
Path file = Paths.get(".").normalize()
.resolve("jreleaser." + parser.getPreferredFileExtension());
if (Files.exists(file)) {
actualConfigFile = file;
break;
}
}
}
if (null == actualConfigFile || !Files.exists(actualConfigFile)) {
throw new BuildException("Missing required option 'configFile' " +
"or local file named jreleaser[" +
String.join("|", getSupportedConfigFormats()) + "]");
}
}
private void resolveBasedir() {
String resolvedBasedir = Env.resolve(org.jreleaser.model.api.JReleaserContext.BASEDIR, null != basedir ? basedir.getPath() : "");
actualBasedir = (isNotBlank(resolvedBasedir) ? Paths.get(resolvedBasedir) : actualConfigFile.toAbsolutePath().getParent()).normalize();
}
protected abstract void doExecute(JReleaserContext context);
protected JReleaserLogger initLogger() {
if (null == logger) {
logger = new JReleaserLoggerAdapter(createTracer(), getProject());
}
return logger;
}
protected PrintWriter createTracer() {
try {
Files.createDirectories(getOutputDirectory());
return newPrintWriter(new FileOutputStream(
getOutputDirectory().resolve("trace.log").toFile()));
} catch (IOException e) {
throw new IllegalStateException("Could not initialize trace file", e);
}
}
protected Path getOutputDirectory() {
return resolveOutputDirectory(actualBasedir, outputDir, "build");
}
protected JReleaserContext createContext() {
return ContextCreator.create(
logger,
resolveConfigurer(actualConfigFile),
getMode(),
actualConfigFile,
actualBasedir,
getOutputDirectory(),
resolveBoolean(org.jreleaser.model.api.JReleaserContext.DRY_RUN, dryrun),
resolveBoolean(org.jreleaser.model.api.JReleaserContext.GIT_ROOT_SEARCH, gitRootSearch),
resolveBoolean(org.jreleaser.model.api.JReleaserContext.STRICT, strict),
collectSelectedPlatforms(),
collectRejectedPlatforms());
}
protected boolean resolveBoolean(String key, Boolean value) {
if (null != value) return value;
String resolvedValue = Env.resolve(key, "");
return isNotBlank(resolvedValue) && Boolean.parseBoolean(resolvedValue);
}
protected List resolveCollection(String key, List values) {
if (!values.isEmpty()) return values;
String resolvedValue = Env.resolve(key, "");
if (isBlank(resolvedValue)) return Collections.emptyList();
return Arrays.stream(resolvedValue.trim().split(","))
.map(String::trim)
.filter(StringUtils::isNotBlank)
.collect(toList());
}
protected JReleaserContext.Configurer resolveConfigurer(Path configFile) {
switch (StringUtils.getFilenameExtension(configFile.getFileName().toString())) {
case "yml":
case "yaml":
return JReleaserContext.Configurer.CLI_YAML;
case "toml":
return JReleaserContext.Configurer.CLI_TOML;
case "json":
return JReleaserContext.Configurer.CLI_JSON;
default:
// should not happen!
throw new IllegalArgumentException("Invalid configuration format: " + configFile.getFileName());
}
}
private Set getSupportedConfigFormats() {
Set extensions = new LinkedHashSet<>();
ServiceLoader parsers = ServiceLoader.load(JReleaserConfigParser.class,
JReleaserConfigParser.class.getClassLoader());
for (JReleaserConfigParser parser : parsers) {
extensions.add("." + parser.getPreferredFileExtension());
}
return extensions;
}
protected Mode getMode() {
return Mode.FULL;
}
protected List collectSelectedPlatforms() {
return Collections.emptyList();
}
protected List collectRejectedPlatforms() {
return Collections.emptyList();
}
protected List collectEntries(List input) {
return collectEntries(input, false);
}
protected List collectEntries(List input, boolean lowerCase) {
List list = new ArrayList<>();
if (null != input && !input.isEmpty()) {
for (String s : input) {
if (isNotBlank(s)) {
if (!s.contains("-") && lowerCase) {
s = StringUtils.getHyphenatedName(s);
}
list.add(lowerCase ? s.toLowerCase(Locale.ENGLISH) : s);
}
}
}
return list;
}
protected Collection expandAndCollect(String input) {
if (isBlank(input)) return Collections.emptyList();
if (input.contains(",")) {
return Arrays.stream(input.split(","))
.map(String::trim)
.filter(StringUtils::isNotBlank)
.collect(toList());
}
return Collections.singletonList(input.trim());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy