-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gradle support for Maven POM sorting/formatting
- Loading branch information
Showing
7 changed files
with
344 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
plugin-gradle/src/main/java/com/diffplug/gradle/spotless/PomExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2024 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.gradle.spotless; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.inject.Inject; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.pom.SortPomCfg; | ||
import com.diffplug.spotless.pom.SortPomStep; | ||
|
||
public class PomExtension extends FormatExtension { | ||
private static final String POM_FILE = "pom.xml"; | ||
|
||
static final String NAME = "pom"; | ||
|
||
@Inject | ||
public PomExtension(SpotlessExtension spotless) { | ||
super(spotless); | ||
} | ||
|
||
@Override | ||
protected void setupTask(SpotlessTask task) { | ||
if (target == null) { | ||
target = parseTarget(POM_FILE); | ||
} | ||
super.setupTask(task); | ||
} | ||
|
||
public SortPomGradleConfig sortPom() { | ||
return new SortPomGradleConfig(); | ||
} | ||
|
||
public SortPomGradleConfig sortPom(String version) { | ||
Objects.requireNonNull(version); | ||
return new SortPomGradleConfig(version); | ||
} | ||
|
||
public class SortPomGradleConfig { | ||
final SortPomCfg cfg = new SortPomCfg(); | ||
|
||
SortPomGradleConfig() { | ||
addStep(createStep()); | ||
} | ||
|
||
SortPomGradleConfig(String version) { | ||
this(); | ||
cfg.version = Objects.requireNonNull(version); | ||
} | ||
|
||
public SortPomGradleConfig encoding(String encoding) { | ||
cfg.encoding = encoding; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig lineSeparator(String lineSeparator) { | ||
cfg.lineSeparator = lineSeparator; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig expandEmptyElements(boolean expandEmptyElements) { | ||
cfg.expandEmptyElements = expandEmptyElements; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig spaceBeforeCloseEmptyElement(boolean spaceBeforeCloseEmptyElement) { | ||
cfg.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig keepBlankLines(boolean keepBlankLines) { | ||
cfg.keepBlankLines = keepBlankLines; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig endWithNewline(boolean endWithNewline) { | ||
cfg.endWithNewline = endWithNewline; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig nrOfIndentSpace(int nrOfIndentSpace) { | ||
cfg.nrOfIndentSpace = nrOfIndentSpace; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig indentBlankLines(boolean indentBlankLines) { | ||
cfg.indentBlankLines = indentBlankLines; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig indentSchemaLocation(boolean indentSchemaLocation) { | ||
cfg.indentSchemaLocation = indentSchemaLocation; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig predefinedSortOrder(String predefinedSortOrder) { | ||
cfg.predefinedSortOrder = predefinedSortOrder; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig sortOrderFile(String sortOrderFile) { | ||
cfg.sortOrderFile = sortOrderFile; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig sortDependencies(String sortDependencies) { | ||
cfg.sortDependencies = sortDependencies; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig sortDependencyManagement(String sortDependencyManagement) { | ||
cfg.sortDependencyManagement = sortDependencyManagement; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig sortDependencyExclusions(String sortDependencyExclusions) { | ||
cfg.sortDependencyExclusions = sortDependencyExclusions; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig sortPlugins(String sortPlugins) { | ||
cfg.sortPlugins = sortPlugins; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig sortProperties(boolean sortProperties) { | ||
cfg.sortProperties = sortProperties; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig sortModules(boolean sortModules) { | ||
cfg.sortModules = sortModules; | ||
return this; | ||
} | ||
|
||
public SortPomGradleConfig sortExecutions(boolean sortExecutions) { | ||
cfg.sortExecutions = sortExecutions; | ||
return this; | ||
} | ||
|
||
private FormatterStep createStep() { | ||
return SortPomStep.create(cfg, provisioner()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SortPomGradleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2024 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.gradle.spotless; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class SortPomGradleTest extends GradleIntegrationHarness { | ||
@Test | ||
void sortPom() throws Exception { | ||
// given | ||
setFile("build.gradle").toLines( | ||
"plugins {", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"repositories { mavenCentral() }", | ||
"spotless {", | ||
" pom {", | ||
" sortPom()", | ||
" }", | ||
"}"); | ||
setFile("pom.xml").toResource("pom/pom_dirty.xml"); | ||
|
||
// when | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
|
||
// then | ||
assertFile("pom.xml").sameAsResource("pom/pom_clean_default.xml"); | ||
} | ||
|
||
@Test | ||
void sortPomWithTarget() throws Exception { | ||
// given | ||
setFile("build.gradle").toLines( | ||
"plugins {", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"repositories { mavenCentral() }", | ||
"spotless {", | ||
" pom {", | ||
" target('test.xml')", | ||
" sortPom()", | ||
" }", | ||
"}"); | ||
setFile("test.xml").toResource("pom/pom_dirty.xml"); | ||
|
||
// when | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
|
||
// then | ||
assertFile("test.xml").sameAsResource("pom/pom_clean_default.xml"); | ||
} | ||
|
||
@Test | ||
void sortPomWithVersion() throws Exception { | ||
// given | ||
setFile("build.gradle").toLines( | ||
"plugins {", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"repositories { mavenCentral() }", | ||
"spotless {", | ||
" pom {", | ||
" sortPom '3.4.0'", | ||
" }", | ||
"}"); | ||
setFile("pom.xml").toResource("pom/pom_dirty.xml"); | ||
|
||
// when | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
|
||
// then | ||
assertFile("pom.xml").sameAsResource("pom/pom_clean_default.xml"); | ||
} | ||
|
||
@Test | ||
void sortPomWithParameters() throws Exception { | ||
// given | ||
setFile("build.gradle").toLines( | ||
"plugins {", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"repositories { mavenCentral() }", | ||
"spotless {", | ||
" pom {", | ||
" sortPom()", | ||
" .encoding('UTF-8')", | ||
" .lineSeparator(System.getProperty('line.separator'))", | ||
" .expandEmptyElements(true)", | ||
" .spaceBeforeCloseEmptyElement(false)", | ||
" .keepBlankLines(true)", | ||
" .endWithNewline(true)", | ||
" .nrOfIndentSpace(2)", | ||
" .indentBlankLines(false)", | ||
" .indentSchemaLocation(false)", | ||
" .predefinedSortOrder('recommended_2008_06')", | ||
" .sortOrderFile(null)", | ||
" .sortDependencies(null)", | ||
" .sortDependencyManagement(null)", | ||
" .sortDependencyExclusions(null)", | ||
" .sortPlugins(null)", | ||
" .sortProperties(false)", | ||
" .sortModules(false)", | ||
" .sortExecutions(false)", | ||
" }", | ||
"}"); | ||
setFile("pom.xml").toResource("pom/pom_dirty.xml"); | ||
|
||
// when | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
|
||
// then | ||
assertFile("pom.xml").sameAsResource("pom/pom_clean_default.xml"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters