|
| 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.check; |
| 18 | + |
| 19 | +import java.util.Properties; |
| 20 | +import java.util.concurrent.Callable; |
| 21 | + |
| 22 | +import org.springframework.stereotype.Component; |
| 23 | +import picocli.CommandLine.ArgGroup; |
| 24 | +import picocli.CommandLine.Command; |
| 25 | +import picocli.CommandLine.Mixin; |
| 26 | +import picocli.CommandLine.Model.CommandSpec; |
| 27 | +import picocli.CommandLine.Option; |
| 28 | +import picocli.CommandLine.ParameterException; |
| 29 | +import picocli.CommandLine.Spec; |
| 30 | + |
| 31 | +import io.spring.javaformat.cli.InputOptions; |
| 32 | + |
| 33 | +/** |
| 34 | + * Check formatting and checkstyle violations. |
| 35 | + * |
| 36 | + * @author Tim Sparg |
| 37 | + */ |
| 38 | +@Component |
| 39 | +@Command(name = "check", mixinStandardHelpOptions = true, |
| 40 | + description = "Check the codebase for formatting and checkstyle violations") |
| 41 | +public class CheckCommand implements Callable<Integer> { |
| 42 | + |
| 43 | + @Spec |
| 44 | + private CommandSpec spec; |
| 45 | + |
| 46 | + @Mixin |
| 47 | + private InputOptions formatOptions; |
| 48 | + |
| 49 | + @Option(names = { "-t", "--header-type" }, defaultValue = "APACHE2", |
| 50 | + description = "Header type (${COMPLETION-CANDIDATES}). Default: ${DEFAULT-VALUE}") |
| 51 | + private HeaderType headerType; |
| 52 | + |
| 53 | + @Option(names = { "-c", "--header-copyright-pattern" }, defaultValue = "20\\d\\d(-20\\d\\d|-present)?", |
| 54 | + description = "Copyright year regex pattern. Default: ${DEFAULT-VALUE}") |
| 55 | + private String headerCopyrightPattern; |
| 56 | + |
| 57 | + @Option(names = { "-f", "--header-file" }, defaultValue = "", |
| 58 | + description = "Path to a custom header file. Required when --header-type is 'file'.") |
| 59 | + private String headerFile; |
| 60 | + |
| 61 | + @Option(names = { "-r", "--project-root-package" }, defaultValue = "org.springframework", |
| 62 | + description = "Root package used for import ordering. Default: ${DEFAULT-VALUE}") |
| 63 | + private String projectRootPackage; |
| 64 | + |
| 65 | + @Option(names = { "-s", "--avoid-static-import-excludes" }, split = ",", defaultValue = "", |
| 66 | + description = "Repeatable or comma-separated static import patterns to allow.") |
| 67 | + private String[] avoidStaticImportExcludes; |
| 68 | + |
| 69 | + @ArgGroup |
| 70 | + private RunMode runMode; |
| 71 | + |
| 72 | + private final CheckRunner checkRunner; |
| 73 | + |
| 74 | + private final CheckReportRenderer checkReportRenderer; |
| 75 | + |
| 76 | + @SuppressWarnings("NullAway.Init") |
| 77 | + CheckCommand(CheckRunner checkRunner, CheckReportRenderer checkReportRenderer) { |
| 78 | + this.checkRunner = checkRunner; |
| 79 | + this.checkReportRenderer = checkReportRenderer; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Integer call() { |
| 84 | + CheckRunner.Inputs inputs = createInputs(); |
| 85 | + CheckReport report = this.checkRunner.run(inputs); |
| 86 | + this.checkReportRenderer.render(this.spec.commandLine(), report); |
| 87 | + return report.exitCode(); |
| 88 | + } |
| 89 | + |
| 90 | + private CheckRunner.Inputs createInputs() { |
| 91 | + boolean skipCheckstyle = this.runMode != null && this.runMode.skipCheckstyle; |
| 92 | + if (!skipCheckstyle && this.headerType == HeaderType.FILE && this.headerFile.isEmpty()) { |
| 93 | + throw new ParameterException(this.spec.commandLine(), |
| 94 | + "--header-file is required when --header-type is FILE"); |
| 95 | + } |
| 96 | + return new CheckRunner.Inputs(this.formatOptions, buildCheckstyleProperties(), |
| 97 | + this.runMode != null && this.runMode.skipFormat, skipCheckstyle); |
| 98 | + } |
| 99 | + |
| 100 | + private Properties buildCheckstyleProperties() { |
| 101 | + Properties properties = new Properties(); |
| 102 | + properties.setProperty("headerType", this.headerType.value); |
| 103 | + properties.setProperty("headerCopyrightPattern", this.headerCopyrightPattern); |
| 104 | + if (!this.headerFile.isEmpty()) { |
| 105 | + properties.setProperty("headerFile", this.headerFile); |
| 106 | + } |
| 107 | + properties.setProperty("projectRootPackage", this.projectRootPackage); |
| 108 | + properties.setProperty("avoidStaticImportExcludes", String.join(",", |
| 109 | + java.util.Arrays.stream(this.avoidStaticImportExcludes).filter((s) -> !s.isEmpty()).toList())); |
| 110 | + return properties; |
| 111 | + } |
| 112 | + |
| 113 | + static class RunMode { |
| 114 | + |
| 115 | + @Option(names = { "-S", "--skip-checkstyle" }, defaultValue = "false", |
| 116 | + description = "Skip checkstyle checks, only check source formatting") |
| 117 | + boolean skipCheckstyle; |
| 118 | + |
| 119 | + @Option(names = { "-F", "--skip-format" }, defaultValue = "false", |
| 120 | + description = "Skip source formatting check, only run checkstyle") |
| 121 | + boolean skipFormat; |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + enum HeaderType { |
| 126 | + |
| 127 | + APACHE2("apache2"), NONE("none"), UNCHECKED("unchecked"), REGEXP("regexp"), FILE("file"); |
| 128 | + |
| 129 | + private final String value; |
| 130 | + |
| 131 | + HeaderType(String value) { |
| 132 | + this.value = value; |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments