|
| 1 | +/* |
| 2 | + * Copyright 2017-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.spring.javaformat.cli; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.nio.charset.Charset; |
| 21 | +import java.nio.file.FileSystems; |
| 22 | + |
| 23 | +import org.jspecify.annotations.Nullable; |
| 24 | +import picocli.CommandLine.ITypeConverter; |
| 25 | +import picocli.CommandLine.Option; |
| 26 | +import picocli.CommandLine.Parameters; |
| 27 | +import picocli.CommandLine.TypeConversionException; |
| 28 | + |
| 29 | +/** |
| 30 | + * Shared options for commands that scan and format files. |
| 31 | + * |
| 32 | + * @author Tim Sparg |
| 33 | + */ |
| 34 | +@PicocliManaged |
| 35 | +public class InputOptions { |
| 36 | + |
| 37 | + /** Base directory to scan. */ |
| 38 | + @Parameters(index = "0", arity = "0..1", defaultValue = ".", paramLabel = "PATH", |
| 39 | + description = "Base directory to scan. Default: ${DEFAULT-VALUE}", converter = DirectoryConverter.class) |
| 40 | + public File path; |
| 41 | + |
| 42 | + /** Glob patterns for files to include. */ |
| 43 | + @Option(names = { "-i", "--includes" }, split = ",", defaultValue = "**/*.java", |
| 44 | + description = "Repeatable or comma-separated include patterns. Default: ${DEFAULT-VALUE}", |
| 45 | + converter = GlobConverter.class) |
| 46 | + public String[] includes; |
| 47 | + |
| 48 | + /** Glob patterns for files to exclude. */ |
| 49 | + @Option(names = { "-x", "--excludes" }, split = ",", |
| 50 | + defaultValue = "**/target/**,**/generated-sources/**,**/generated-test-sources/**", |
| 51 | + description = "Repeatable or comma-separated exclude patterns. Default: ${DEFAULT-VALUE}", |
| 52 | + converter = GlobConverter.class) |
| 53 | + public String[] excludes; |
| 54 | + |
| 55 | + /** File encoding to use when reading and writing files. */ |
| 56 | + @Option(names = { "-e", "--encoding" }, defaultValue = "UTF-8", |
| 57 | + description = "File encoding. Default: ${DEFAULT-VALUE}") |
| 58 | + public Charset encoding; |
| 59 | + |
| 60 | + @Option(names = { "-l", "--line-separator" }, |
| 61 | + description = "Line separator (${COMPLETION-CANDIDATES}). If not specified, the existing line separator in each file is preserved.") |
| 62 | + @Nullable |
| 63 | + LineSeparator lineSeparator; |
| 64 | + |
| 65 | + @Nullable |
| 66 | + public String resolveLineSeparator() { |
| 67 | + return (this.lineSeparator != null) ? this.lineSeparator.value : null; |
| 68 | + } |
| 69 | + |
| 70 | + enum LineSeparator { |
| 71 | + |
| 72 | + CR("\r"), LF("\n"), CRLF("\r\n"); |
| 73 | + |
| 74 | + private final String value; |
| 75 | + |
| 76 | + LineSeparator(String value) { |
| 77 | + this.value = value; |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + static final class GlobConverter implements ITypeConverter<String> { |
| 83 | + |
| 84 | + @Override |
| 85 | + public String convert(String value) throws TypeConversionException { |
| 86 | + try { |
| 87 | + FileSystems.getDefault().getPathMatcher("glob:" + value); |
| 88 | + } |
| 89 | + catch (IllegalArgumentException ex) { |
| 90 | + throw new TypeConversionException("Invalid glob pattern '" + value + "': " + ex.getMessage()); |
| 91 | + } |
| 92 | + return value; |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + static final class DirectoryConverter implements ITypeConverter<File> { |
| 98 | + |
| 99 | + @Override |
| 100 | + public File convert(String value) throws TypeConversionException { |
| 101 | + File dir = new File(value).getAbsoluteFile(); |
| 102 | + if (!dir.exists()) { |
| 103 | + throw new TypeConversionException("Directory does not exist: " + dir); |
| 104 | + } |
| 105 | + if (!dir.isDirectory()) { |
| 106 | + throw new TypeConversionException("Not a directory: " + dir); |
| 107 | + } |
| 108 | + return dir; |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments