|
| 1 | +/* |
| 2 | + * Copyright 2024 DiffPlug |
| 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 | + * http://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 | +package com.diffplug.gradle.spotless; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +import javax.inject.Inject; |
| 21 | + |
| 22 | +import com.diffplug.spotless.FormatterStep; |
| 23 | +import com.diffplug.spotless.pom.SortPomCfg; |
| 24 | +import com.diffplug.spotless.pom.SortPomStep; |
| 25 | + |
| 26 | +public class PomExtension extends FormatExtension { |
| 27 | + private static final String POM_FILE = "pom.xml"; |
| 28 | + |
| 29 | + static final String NAME = "pom"; |
| 30 | + |
| 31 | + @Inject |
| 32 | + public PomExtension(SpotlessExtension spotless) { |
| 33 | + super(spotless); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void setupTask(SpotlessTask task) { |
| 38 | + if (target == null) { |
| 39 | + target = parseTarget(POM_FILE); |
| 40 | + } |
| 41 | + super.setupTask(task); |
| 42 | + } |
| 43 | + |
| 44 | + public SortPomGradleConfig sortPom() { |
| 45 | + return new SortPomGradleConfig(); |
| 46 | + } |
| 47 | + |
| 48 | + public SortPomGradleConfig sortPom(String version) { |
| 49 | + Objects.requireNonNull(version); |
| 50 | + return new SortPomGradleConfig(version); |
| 51 | + } |
| 52 | + |
| 53 | + public class SortPomGradleConfig { |
| 54 | + private final SortPomCfg cfg = new SortPomCfg(); |
| 55 | + |
| 56 | + SortPomGradleConfig() { |
| 57 | + addStep(createStep()); |
| 58 | + } |
| 59 | + |
| 60 | + SortPomGradleConfig(String version) { |
| 61 | + this(); |
| 62 | + cfg.version = Objects.requireNonNull(version); |
| 63 | + } |
| 64 | + |
| 65 | + public SortPomGradleConfig encoding(String encoding) { |
| 66 | + cfg.encoding = encoding; |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public SortPomGradleConfig lineSeparator(String lineSeparator) { |
| 71 | + cfg.lineSeparator = lineSeparator; |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + public SortPomGradleConfig expandEmptyElements(boolean expandEmptyElements) { |
| 76 | + cfg.expandEmptyElements = expandEmptyElements; |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + public SortPomGradleConfig spaceBeforeCloseEmptyElement(boolean spaceBeforeCloseEmptyElement) { |
| 81 | + cfg.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public SortPomGradleConfig keepBlankLines(boolean keepBlankLines) { |
| 86 | + cfg.keepBlankLines = keepBlankLines; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + public SortPomGradleConfig endWithNewline(boolean endWithNewline) { |
| 91 | + cfg.endWithNewline = endWithNewline; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + public SortPomGradleConfig nrOfIndentSpace(int nrOfIndentSpace) { |
| 96 | + cfg.nrOfIndentSpace = nrOfIndentSpace; |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + public SortPomGradleConfig indentBlankLines(boolean indentBlankLines) { |
| 101 | + cfg.indentBlankLines = indentBlankLines; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + public SortPomGradleConfig indentSchemaLocation(boolean indentSchemaLocation) { |
| 106 | + cfg.indentSchemaLocation = indentSchemaLocation; |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + public SortPomGradleConfig predefinedSortOrder(String predefinedSortOrder) { |
| 111 | + cfg.predefinedSortOrder = predefinedSortOrder; |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + public SortPomGradleConfig sortOrderFile(String sortOrderFile) { |
| 116 | + cfg.sortOrderFile = sortOrderFile; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public SortPomGradleConfig sortDependencies(String sortDependencies) { |
| 121 | + cfg.sortDependencies = sortDependencies; |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + public SortPomGradleConfig sortDependencyManagement(String sortDependencyManagement) { |
| 126 | + cfg.sortDependencyManagement = sortDependencyManagement; |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + public SortPomGradleConfig sortDependencyExclusions(String sortDependencyExclusions) { |
| 131 | + cfg.sortDependencyExclusions = sortDependencyExclusions; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + public SortPomGradleConfig sortPlugins(String sortPlugins) { |
| 136 | + cfg.sortPlugins = sortPlugins; |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + public SortPomGradleConfig sortProperties(boolean sortProperties) { |
| 141 | + cfg.sortProperties = sortProperties; |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + public SortPomGradleConfig sortModules(boolean sortModules) { |
| 146 | + cfg.sortModules = sortModules; |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + public SortPomGradleConfig sortExecutions(boolean sortExecutions) { |
| 151 | + cfg.sortExecutions = sortExecutions; |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + private FormatterStep createStep() { |
| 156 | + return SortPomStep.create(cfg, provisioner()); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments