fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-codegen / src / main / java / wtf / metio / yosql / codegen / files / DefaultFileParser.java
blob9839022d2989ab39e8e711e81d4e2e7820ce0de9
1 /*
2 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at https://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
8 package wtf.metio.yosql.codegen.files;
10 import org.slf4j.cal10n.LocLogger;
11 import wtf.metio.yosql.codegen.lifecycle.ApplicationErrors;
12 import wtf.metio.yosql.codegen.lifecycle.FileLifecycle;
13 import wtf.metio.yosql.codegen.orchestration.ExecutionErrors;
14 import wtf.metio.yosql.models.immutables.FilesConfiguration;
15 import wtf.metio.yosql.models.immutables.SqlStatement;
17 import java.io.IOException;
18 import java.nio.file.FileVisitOption;
19 import java.nio.file.Files;
20 import java.util.List;
22 /**
23 * Default SQL file parser that starts at {@link FilesConfiguration#inputBaseDirectory()
24 * the given source}, walks into every subdirectory and returns all statements found in
25 * {@link FilesConfiguration#sqlFilesSuffix() .sql files}.
27 public final class DefaultFileParser implements FileParser {
29 private final LocLogger logger;
30 private final ParserPreconditions preconditions;
31 private final FilesConfiguration fileConfiguration;
32 private final ExecutionErrors errors;
33 private final SqlStatementParser fileParser;
35 public DefaultFileParser(
36 final LocLogger logger,
37 final ParserPreconditions preconditions,
38 final FilesConfiguration fileConfiguration,
39 final ExecutionErrors errors,
40 final SqlStatementParser fileParser) {
41 this.logger = logger;
42 this.preconditions = preconditions;
43 this.fileConfiguration = fileConfiguration;
44 this.errors = errors;
45 this.fileParser = fileParser;
48 @Override
49 public List<SqlStatement> parseFiles() {
50 final var source = fileConfiguration.inputBaseDirectory();
51 logger.trace(FileLifecycle.READ_FILES, source);
52 preconditions.directoryIsReadable(source);
54 if (!errors.hasErrors()) {
55 try (final var files = Files.walk(source, FileVisitOption.FOLLOW_LINKS).parallel()) {
56 return files.peek(path -> logger.trace(FileLifecycle.ENCOUNTER_FILE, path))
57 .filter(Files::isRegularFile)
58 .filter(path -> path.toString().endsWith(fileConfiguration.sqlFilesSuffix()))
59 .peek(path -> logger.trace(FileLifecycle.CONSIDER_FILE, path))
60 .flatMap(fileParser::parse)
61 .toList();
62 } catch (final IOException | SecurityException exception) {
63 logger.error(ApplicationErrors.READ_FILES_FAILED, exception.getLocalizedMessage());
64 errors.add(exception);
68 return List.of();