Skip to content

Commit

Permalink
Update sortpom plugin to newest version (#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored May 16, 2023
2 parents 245867f + c5d88ad commit f631f09
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changes
* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675))

### Added
* `Jvm.Support` now accepts `-SNAPSHOT` versions, treated as the non`-SNAPSHOT`. ([#1583](https://github.com/diffplug/spotless/issues/1583))
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ dependencies {
// scalafmt
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:3.7.3"
// sortPom
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0'
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.2.1'
sortPomCompileOnly 'org.slf4j:slf4j-api:2.0.0'
}

Expand Down
6 changes: 5 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,8 @@
public class SortPomCfg implements Serializable {
private static final long serialVersionUID = 1L;

public String version = "3.2.1";

public String encoding = "UTF-8";

public String lineSeparator = System.getProperty("line.separator");
Expand All @@ -43,6 +45,8 @@ public class SortPomCfg implements Serializable {

public String sortDependencies = null;

public String sortDependencyManagement = null;

public String sortDependencyExclusions = null;

public String sortPlugins = null;
Expand Down
6 changes: 4 additions & 2 deletions lib/src/main/java/com/diffplug/spotless/pom/SortPomStep.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,8 @@

public class SortPomStep {
public static final String NAME = "sortPom";
static final String PACKAGE = "com.github.ekryd.sortpom";
static final String MAVEN_COORDINATE = PACKAGE + ":sortpom-sorter:";

private SortPomStep() {}

Expand All @@ -42,7 +44,7 @@ static class State implements Serializable {

public State(SortPomCfg cfg, Provisioner provisioner) throws IOException {
this.cfg = cfg;
this.jarState = JarState.from("com.github.ekryd.sortpom:sortpom-sorter:3.0.0", provisioner);
this.jarState = JarState.from(MAVEN_COORDINATE + cfg.version, provisioner);
}

FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 DiffPlug
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,11 +15,10 @@
*/
package com.diffplug.spotless.glue.pom;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -40,10 +39,12 @@ public SortPomFormatterFunc(SortPomCfg cfg) {

@Override
public String apply(String input) throws Exception {
// SortPom expects a file to sort, so we write the inpout into a temporary file
// SortPom expects a file to sort, so we write the input into a temporary file
File pom = File.createTempFile("pom", ".xml");
pom.deleteOnExit();
IOUtils.write(input, new FileOutputStream(pom), cfg.encoding);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(pom, Charset.forName(cfg.encoding)))) {
writer.write(input);
}
SortPomImpl sortPom = new SortPomImpl();
sortPom.setup(new MySortPomLogger(), PluginParameters.builder()
.setPomFile(pom)
Expand All @@ -52,11 +53,12 @@ public String apply(String input) throws Exception {
.setFormatting(cfg.lineSeparator, cfg.expandEmptyElements, cfg.spaceBeforeCloseEmptyElement, cfg.keepBlankLines)
.setIndent(cfg.nrOfIndentSpace, cfg.indentBlankLines, cfg.indentSchemaLocation)
.setSortOrder(cfg.sortOrderFile, cfg.predefinedSortOrder)
.setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions)
.setTriggers(false)
.setSortEntities(cfg.sortDependencies, cfg.sortDependencyExclusions, cfg.sortDependencyManagement,
cfg.sortPlugins, cfg.sortProperties, cfg.sortModules, cfg.sortExecutions)
.setIgnoreLineSeparators(false)
.build());
sortPom.sortPom();
return IOUtils.toString(new FileInputStream(pom), cfg.encoding);
return Files.readString(pom.toPath(), Charset.forName(cfg.encoding));
}

private static class MySortPomLogger implements SortPomLogger {
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
### Fixed
* Added `@DisableCachingByDefault` to `RegisterDependenciesTask`.
* When P2 download fails, indicate the responsible formatter. ([#1698](https://github.com/diffplug/spotless/issues/1698))
### Changes
* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675))

## [6.18.0] - 2023-04-06
### Added
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changes
* Bump default sortpom version to latest `3.0.0` -> `3.2.1`. ([#1675](https://github.com/diffplug/spotless/pull/1675))
### Fixed
* `palantir` step now accepts a `style` parameter, which is documentation had already claimed to do. ([#1694](https://github.com/diffplug/spotless/pull/1694))
* When P2 download fails, indicate the responsible formatter. ([#1698](https://github.com/diffplug/spotless/issues/1698))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,9 @@
public class SortPom implements FormatterStepFactory {
private final SortPomCfg defaultValues = new SortPomCfg();

@Parameter
String version = defaultValues.version;

@Parameter
String encoding = defaultValues.encoding;

Expand Down Expand Up @@ -59,6 +62,9 @@ public class SortPom implements FormatterStepFactory {
@Parameter
String sortDependencies = defaultValues.sortDependencies;

@Parameter
String sortDependencyManagement = defaultValues.sortDependencyManagement;

@Parameter
String sortDependencyExclusions = defaultValues.sortDependencyExclusions;

Expand All @@ -77,6 +83,7 @@ public class SortPom implements FormatterStepFactory {
@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
SortPomCfg cfg = new SortPomCfg();
cfg.version = version;
cfg.encoding = encoding;
cfg.lineSeparator = lineSeparator;
cfg.expandEmptyElements = expandEmptyElements;
Expand All @@ -88,6 +95,7 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
cfg.predefinedSortOrder = predefinedSortOrder;
cfg.sortOrderFile = sortOrderFile;
cfg.sortDependencies = sortDependencies;
cfg.sortDependencyManagement = sortDependencyManagement;
cfg.sortDependencyExclusions = sortDependencyExclusions;
cfg.sortPlugins = sortPlugins;
cfg.sortProperties = sortProperties;
Expand Down
21 changes: 13 additions & 8 deletions testlib/src/test/java/com/diffplug/spotless/pom/SortPomTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 DiffPlug
* Copyright 2021-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,16 +17,21 @@

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.TestProvisioner;
import com.diffplug.spotless.*;

public class SortPomTest {
public class SortPomTest extends ResourceHarness {
@Test
public void testSortPomWithDefaultConfig() throws Exception {
SortPomCfg cfg = new SortPomCfg();
Provisioner provisioner = TestProvisioner.mavenCentral();
StepHarness harness = StepHarness.forStep(SortPomStep.create(cfg, provisioner));
harness.testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
FormatterStep step = SortPomStep.create(cfg, TestProvisioner.mavenCentral());
StepHarness.forStep(step).testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
}

@Test
public void testSortPomWithVersion() throws Exception {
SortPomCfg cfg = new SortPomCfg();
cfg.version = "3.2.1";
FormatterStep step = SortPomStep.create(cfg, TestProvisioner.mavenCentral());
StepHarness.forStep(step).testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
}
}

0 comments on commit f631f09

Please sign in to comment.