Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sortpom to 4.0.0 #2115

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
* Bump default `ktlint` version to latest `1.1.1` -> `1.2.1`. ([#2057](https://github.com/diffplug/spotless/pull/2057))
* Bump default `sortpom` version to latest `3.4.0` -> `3.4.1`. ([#2078](https://github.com/diffplug/spotless/pull/2078))
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0` and support versions back to `3.2.1`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
### Removed
* **BREAKING** Remove `JarState.getMavenCoordinate(String prefix)`. ([#1945](https://github.com/diffplug/spotless/pull/1945))
* **BREAKING** Replace `PipeStepPair` with `FenceStep`. ([#1954](https://github.com/diffplug/spotless/pull/1954))
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ dependencies {
// scalafmt
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:3.7.3"
// sortPom
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.4.1'
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:4.0.0'
sortPomCompileOnly 'org.slf4j:slf4j-api:2.0.12'
// zjsonPatch
zjsonPatchCompileOnly 'com.flipkart.zjsonpatch:zjsonpatch:0.4.14'
Expand Down
4 changes: 3 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class SortPomCfg implements Serializable {
private static final long serialVersionUID = 1L;

public String version = "3.4.1";
public String version = "4.0.0";

public String encoding = "UTF-8";

Expand All @@ -41,6 +41,8 @@ public class SortPomCfg implements Serializable {

public boolean indentSchemaLocation = false;

public String indentAttribute = null;

public String predefinedSortOrder = "recommended_2008_06";

public String sortOrderFile = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.diffplug.spotless.glue.pom;

import java.io.*;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Files;

Expand Down Expand Up @@ -46,17 +47,45 @@ public String apply(String input) throws Exception {
writer.write(input);
}
SortPomImpl sortPom = new SortPomImpl();
sortPom.setup(new MySortPomLogger(), PluginParameters.builder()
PluginParameters.Builder builder = PluginParameters.builder()
.setPomFile(pom)
.setFileOutput(false, null, null, false)
.setEncoding(cfg.encoding)
.setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement, cfg.keepBlankLines, cfg.endWithNewline)
.setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation)
.setEncoding(cfg.encoding);
try {
builder = builder
.setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement,
cfg.keepBlankLines, cfg.endWithNewline);
} catch (NoSuchMethodError e) {
try {
Method method = PluginParameters.Builder.class
.getMethod("setFormatting", String.class, boolean.class, boolean.class, boolean.class);
builder = (PluginParameters.Builder) method
.invoke(builder, cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement,
cfg.keepBlankLines);
} catch (ReflectiveOperationException | RuntimeException ignore) {
throw e;
}
}
try {
builder = builder
.setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation,
cfg.indentAttribute);
} catch (NoSuchMethodError e) {
try {
Method method = PluginParameters.Builder.class
.getMethod("setIndent", int.class, boolean.class, boolean.class);
builder = (PluginParameters.Builder) method
.invoke(builder, cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation);
} catch (ReflectiveOperationException | RuntimeException ignore) {
throw e;
}
}
builder = builder
.setSortOrder(cfg.sortOrderFile, cfg.predefinedSortOrder)
.setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortDependencyManagement,
cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions)
.setIgnoreLineSeparators(false)
.build());
.setIgnoreLineSeparators(false);
sortPom.setup(new MySortPomLogger(), builder.build());
sortPom.sortPom();
return Files.readString(pom.toPath(), Charset.forName(cfg.encoding));
}
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
* Bump default `ktlint` version to latest `1.1.1` -> `1.2.1`. ([#2057](https://github.com/diffplug/spotless/pull/2057))
* Bump default `sortpom` version to latest `3.4.0` -> `3.4.1`. ([#2078](https://github.com/diffplug/spotless/pull/2078))
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0` and support versions back to `3.2.1`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
### Added
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
* Add support for formatting and sorting Maven POMs ([#2082](https://github.com/diffplug/spotless/issues/2082))
Expand Down
3 changes: 2 additions & 1 deletion plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ All configuration settings are optional, they are described in detail [here](htt
```gradle
spotless {
pom {
sortPom('3.4.0')
sortPom('4.0.0')
.encoding('UTF-8') // The encoding of the pom files
.lineSeparator(System.getProperty('line.separator')) // line separator to use
.expandEmptyElements(true) // Should empty elements be expanded
Expand All @@ -710,6 +710,7 @@ spotless {
.nrOfIndentSpace(2) // Indentation
.indentBlankLines(false) // Should empty lines be indented
.indentSchemaLocation(false) // Should schema locations be indented
.indentAttribute(null) // Should the xml attributes be indented
.predefinedSortOrder('recommended_2008_06') // Sort order of elements: https://github.com/Ekryd/sortpom/wiki/PredefinedSortOrderProfiles
.sortOrderFile(null) // Custom sort order of elements: https://raw.githubusercontent.com/Ekryd/sortpom/master/sorter/src/main/resources/custom_1.xml
.sortDependencies(null) // Sort dependencies: https://github.com/Ekryd/sortpom/wiki/SortDependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public SortPomGradleConfig indentSchemaLocation(boolean indentSchemaLocation) {
return this;
}

public SortPomGradleConfig indentAttribute(String indentAttribute) {
cfg.indentAttribute = indentAttribute;
return this;
}

public SortPomGradleConfig predefinedSortOrder(String predefinedSortOrder) {
cfg.predefinedSortOrder = predefinedSortOrder;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.diffplug.gradle.spotless;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class SortPomGradleTest extends GradleIntegrationHarness {
@Test
Expand Down Expand Up @@ -63,8 +65,9 @@ void sortPomWithTarget() throws Exception {
assertFile("test.xml").sameAsResource("pom/pom_clean_default.xml");
}

@Test
void sortPomWithVersion() throws Exception {
@ParameterizedTest
@ValueSource(strings = {"3.2.1", "3.3.0", "3.4.1", "4.0.0"})
void sortPomWithVersion(String version) throws Exception {
// given
setFile("build.gradle").toLines(
"plugins {",
Expand All @@ -73,7 +76,7 @@ void sortPomWithVersion() throws Exception {
"repositories { mavenCentral() }",
"spotless {",
" pom {",
" sortPom '3.4.0'",
" sortPom '" + version + "'",
" }",
"}");
setFile("pom.xml").toResource("pom/pom_dirty.xml");
Expand Down Expand Up @@ -105,6 +108,7 @@ void sortPomWithParameters() throws Exception {
" .nrOfIndentSpace(2)",
" .indentBlankLines(false)",
" .indentSchemaLocation(false)",
" .indentAttribute(null)",
" .predefinedSortOrder('recommended_2008_06')",
" .sortOrderFile(null)",
" .sortDependencies(null)",
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `shfmt` version to latest `3.7.0` -> `3.8.0`. ([#2050](https://github.com/diffplug/spotless/pull/2050))
* Bump default `ktlint` version to latest `1.1.1` -> `1.2.1`. ([#2057](https://github.com/diffplug/spotless/pull/2057))
* Bump default `sortpom` version to latest `3.4.0` -> `3.4.1`. ([#2078](https://github.com/diffplug/spotless/pull/2078))
* Bump default `sortpom` version to latest `3.4.1` -> `4.0.0` and support versions back to `3.2.1`. ([#2115](https://github.com/diffplug/spotless/pull/2115))
### Added
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
* Skip execution in M2E (incremental) builds by default ([#1814](https://github.com/diffplug/spotless/issues/1814), [#2037](https://github.com/diffplug/spotless/issues/2037))
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ All configuration settings are optional, they are described in detail [here](htt

<indentSchemaLocation>false</indentSchemaLocation> <!-- Should schema locations be indented -->

<indentAttribute></indentAttribute> <!-- Should the xml attributes be indented -->

<predefinedSortOrder>recommended_2008_06</predefinedSortOrder> <!-- Sort order of elements: https://github.com/Ekryd/sortpom/wiki/PredefinedSortOrderProfiles-->

<sortOrderFile></sortOrderFile> <!-- Custom sort order of elements: https://raw.githubusercontent.com/Ekryd/sortpom/master/sorter/src/main/resources/custom_1.xml -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class SortPom implements FormatterStepFactory {
@Parameter
boolean indentSchemaLocation = defaultValues.indentSchemaLocation;

@Parameter
String indentAttribute = defaultValues.indentAttribute;

@Parameter
String predefinedSortOrder = defaultValues.predefinedSortOrder;

Expand Down Expand Up @@ -96,6 +99,7 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
cfg.nrOfIndentSpace = nrOfIndentSpace;
cfg.indentBlankLines = indentBlankLines;
cfg.indentSchemaLocation = indentSchemaLocation;
cfg.indentAttribute = indentAttribute;
cfg.predefinedSortOrder = predefinedSortOrder;
cfg.sortOrderFile = sortOrderFile;
cfg.sortDependencies = sortDependencies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.diffplug.spotless.pom;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.diffplug.spotless.*;

Expand All @@ -27,10 +29,11 @@ public void testSortPomWithDefaultConfig() {
StepHarness.forStep(step).testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
}

@Test
public void testSortPomWithVersion() {
@ParameterizedTest
@ValueSource(strings = {"3.2.1", "3.3.0", "3.4.1", "4.0.0"})
public void testSortPomWithVersion(String version) {
SortPomCfg cfg = new SortPomCfg();
cfg.version = "3.4.1";
cfg.version = version;
FormatterStep step = SortPomStep.create(cfg, TestProvisioner.mavenCentral());
StepHarness.forStep(step).testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
}
Expand Down
Loading