Search in sources :

Example 16 with JReleaserException

use of org.jreleaser.util.JReleaserException in project jreleaser by jreleaser.

the class Init method execute.

protected void execute() {
    try {
        outputDirectory = null != basedir ? basedir : Paths.get(".").normalize();
        initLogger();
        if (!getSupportedConfigFormats().contains(format)) {
            spec.commandLine().getErr().println(spec.commandLine().getColorScheme().errorText($("jreleaser.init.ERROR_invalid_format", String.join("|", getSupportedConfigFormats()))));
            spec.commandLine().usage(parent.out);
            throw new HaltExecutionException();
        }
        Path outputFile = outputDirectory.resolve("jreleaser." + format);
        Reader template = TemplateUtils.resolveTemplate(logger, "jreleaser." + format + ".tpl");
        StringWriter sw = new StringWriter();
        try (Scanner scanner = new Scanner(template)) {
            while (scanner.hasNextLine()) {
                sw.write(scanner.nextLine() + System.lineSeparator());
            }
        }
        String content = sw.toString();
        LocalDate now = LocalDate.now();
        content = content.replaceAll("@year@", now.getYear() + "");
        logger.info($("jreleaser.init.TEXT_writing_file"), outputFile.toAbsolutePath());
        try {
            Files.write(outputFile, content.getBytes(), (overwrite ? CREATE : CREATE_NEW), WRITE, TRUNCATE_EXISTING);
        } catch (FileAlreadyExistsException e) {
            logger.error($("jreleaser.init.ERROR_file_exists"), outputFile.toAbsolutePath());
            return;
        }
        if (!quiet) {
            logger.info($("jreleaser.init.TEXT_success"), outputDirectory.toAbsolutePath());
        }
    } catch (IllegalStateException | IOException e) {
        throw new JReleaserException($("ERROR_unexpected_error"), e);
    }
}
Also used : Path(java.nio.file.Path) Scanner(java.util.Scanner) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) StringWriter(java.io.StringWriter) Reader(java.io.Reader) IOException(java.io.IOException) LocalDate(java.time.LocalDate) JReleaserException(org.jreleaser.util.JReleaserException)

Example 17 with JReleaserException

use of org.jreleaser.util.JReleaserException in project jreleaser by jreleaser.

the class TemplateUtils method resolveTemplates.

public static Map<String, Reader> resolveTemplates(String distributionType, String toolName, boolean snapshot, Path templateDirectory) {
    Map<String, Reader> templates = new LinkedHashMap<>();
    Path snapshotTemplateDirectory = templateDirectory.resolveSibling(templateDirectory.getFileName() + "-snapshot");
    Path directory = templateDirectory;
    if (snapshot && snapshotTemplateDirectory.toFile().exists()) {
        directory = snapshotTemplateDirectory;
    }
    Path actualTemplateDirectory = directory;
    try {
        Files.walkFileTree(actualTemplateDirectory, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                templates.put(actualTemplateDirectory.relativize(file).toString(), Files.newBufferedReader(file));
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        String distributionTypeName = distributionType.toLowerCase().replace('_', '-');
        throw new JReleaserException(RB.$("ERROR_unexpected_reading_templates_distribution", distributionTypeName, toolName, actualTemplateDirectory.toAbsolutePath()));
    }
    return templates;
}
Also used : Path(java.nio.file.Path) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) LinkedHashMap(java.util.LinkedHashMap) JReleaserException(org.jreleaser.util.JReleaserException)

Example 18 with JReleaserException

use of org.jreleaser.util.JReleaserException in project jreleaser by jreleaser.

the class Artifacts method resolveFiles.

public static Set<Artifact> resolveFiles(JReleaserLogger logger, Path basedir, Collection<String> globs) throws JReleaserException {
    if (null == globs || globs.isEmpty()) {
        return Collections.emptySet();
    }
    FileSystem fileSystem = FileSystems.getDefault();
    List<PathMatcher> matchers = new ArrayList<>();
    for (String glob : globs) {
        matchers.add(fileSystem.getPathMatcher(glob));
    }
    GlobResolver resolver = new GlobResolver(logger, basedir, matchers);
    try {
        java.nio.file.Files.walkFileTree(basedir, resolver);
        if (resolver.failed) {
            throw new JReleaserException(RB.$("ERROR_artifacts_glob_resolution"));
        }
        return Artifact.sortArtifacts(resolver.artifacts);
    } catch (IOException e) {
        throw new JReleaserException(RB.$("ERROR_artifacts_unexpected_error_globs"), e);
    }
}
Also used : PathMatcher(java.nio.file.PathMatcher) FileSystem(java.nio.file.FileSystem) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JReleaserException(org.jreleaser.util.JReleaserException)

Example 19 with JReleaserException

use of org.jreleaser.util.JReleaserException in project jreleaser by jreleaser.

the class Artifacts method resolveFiles.

public static Set<Artifact> resolveFiles(JReleaserLogger logger, Map<String, Object> props, Path basedir, Collection<String> globs) throws JReleaserException {
    if (null == globs || globs.isEmpty()) {
        return Collections.emptySet();
    }
    FileSystem fileSystem = FileSystems.getDefault();
    List<PathMatcher> matchers = new ArrayList<>();
    for (String glob : globs) {
        matchers.add(fileSystem.getPathMatcher(resolveTemplate(glob, props)));
    }
    GlobResolver resolver = new GlobResolver(logger, basedir, matchers);
    try {
        java.nio.file.Files.walkFileTree(basedir, resolver);
        if (resolver.failed) {
            throw new JReleaserException(RB.$("ERROR_artifacts_glob_resolution"));
        }
        return Artifact.sortArtifacts(resolver.artifacts);
    } catch (IOException e) {
        throw new JReleaserException(RB.$("ERROR_artifacts_unexpected_error_globs"), e);
    }
}
Also used : PathMatcher(java.nio.file.PathMatcher) FileSystem(java.nio.file.FileSystem) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JReleaserException(org.jreleaser.util.JReleaserException)

Example 20 with JReleaserException

use of org.jreleaser.util.JReleaserException in project jreleaser by jreleaser.

the class Artifacts method copyFile.

public static void copyFile(JReleaserContext context, Path src, Path dest) throws JReleaserException {
    try {
        java.nio.file.Files.createDirectories(dest.getParent());
        java.nio.file.Files.copy(src, dest, REPLACE_EXISTING, COPY_ATTRIBUTES);
    } catch (IOException e) {
        throw new JReleaserException(RB.$("ERROR_artifacts_unexpected_error_copying", context.relativizeToBasedir(src), context.relativizeToBasedir(dest)));
    }
}
Also used : IOException(java.io.IOException) JReleaserException(org.jreleaser.util.JReleaserException)

Aggregations

JReleaserException (org.jreleaser.util.JReleaserException)31 IOException (java.io.IOException)23 Path (java.nio.file.Path)22 Reader (java.io.Reader)15 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 Artifact (org.jreleaser.model.Artifact)3 PrintWriter (java.io.PrintWriter)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 FileSystem (java.nio.file.FileSystem)2 PathMatcher (java.nio.file.PathMatcher)2 Map (java.util.Map)2 Scanner (java.util.Scanner)2 JReleaserModel (org.jreleaser.model.JReleaserModel)2 ResolvedType (com.fasterxml.classmate.ResolvedType)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 Option (com.github.victools.jsonschema.generator.Option)1 OptionPreset (com.github.victools.jsonschema.generator.OptionPreset)1 SchemaGenerationContext (com.github.victools.jsonschema.generator.SchemaGenerationContext)1