Skip to content

Commit

Permalink
Rename SortPomState to SortPomCfg, and:
Browse files Browse the repository at this point in the history
- SortPomCfg needs no dependencies, just Strings and booleans
- SortPomCfg now has the default values, which simplifies plugin-maven/.../SortPom.java
- SortPomStep does the JarState and classloader stuff
  • Loading branch information
nedtwigg committed Sep 29, 2021
1 parent 4a80576 commit f2c0e4a
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 145 deletions.
55 changes: 55 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/pom/SortPomCfg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2021 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.spotless.pom;

import java.io.Serializable;

// Class and members must be public, otherwise we get failed to access class com.diffplug.spotless.pom.SortPomInternalState from class com.diffplug.spotless.pom.SortPomFormatterFunc (com.diffplug.spotless.pom.SortPomInternalState is in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @682bd3c4; com.diffplug.spotless.pom.SortPomFormatterFunc is in unnamed module of loader com.diffplug.spotless.pom.DelegatingClassLoader @573284a5)
public class SortPomCfg implements Serializable {
private static final long serialVersionUID = 1L;

public String encoding = "UTF-8";

public String lineSeparator = System.getProperty("line.separator");

public boolean expandEmptyElements = true;

public boolean spaceBeforeCloseEmptyElement = false;

public boolean keepBlankLines = true;

public int nrOfIndentSpace = 2;

public boolean indentBlankLines = false;

public boolean indentSchemaLocation = false;

public String predefinedSortOrder = "recommended_2008_06";

public String sortOrderFile = null;

public String sortDependencies = null;

public String sortDependencyExclusions = null;

public String sortPlugins = null;

public boolean sortProperties = false;

public boolean sortModules = false;

public boolean sortExecutions = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
if (name.equals(FormatterFunc.class.getName())) {
return FormatterFunc.class;
}
if (name.equals(SortPomState.class.getName())) {
return SortPomState.class;
if (name.equals(SortPomCfg.class.getName())) {
return SortPomCfg.class;
}
// all other loaded classes need to be associated with this classloader, so we need to load them as resources
String path = name.replace('.', '/') + ".class";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@

class SortPomFormatterFunc implements FormatterFunc {
private static final Logger logger = Logger.getLogger(SortPomFormatterFunc.class.getName());
private final SortPomState state;
private final SortPomCfg cfg;

public SortPomFormatterFunc(SortPomState state) {
this.state = state;
public SortPomFormatterFunc(SortPomCfg cfg) {
this.cfg = cfg;
}

@Override
public String apply(String input) throws Exception {
// SortPom expects a file to sort, so we write the inpout into a temporary file
File pom = File.createTempFile("pom", ".xml");
pom.deleteOnExit();
IOUtils.write(input, new FileOutputStream(pom), state.encoding);
IOUtils.write(input, new FileOutputStream(pom), cfg.encoding);
SortPomImpl sortPom = new SortPomImpl();
sortPom.setup(new MySortPomLogger(), PluginParameters.builder()
.setPomFile(pom)
.setFileOutput(false, null, null, false)
.setEncoding(state.encoding)
.setFormatting(state.lineSeparator, state.expandEmptyElements, state.spaceBeforeCloseEmptyElement, state.keepBlankLines)
.setIndent(state.nrOfIndentSpace, state.indentBlankLines, state.indentSchemaLocation)
.setSortOrder(state.sortOrderFile, state.predefinedSortOrder)
.setSortEntities(state.sortDependencies, state.sortDependencyExclusions, state.sortPlugins, state.sortProperties, state.sortModules, state.sortExecutions)
.setEncoding(cfg.encoding)
.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)
.build());
sortPom.sortPom();
return IOUtils.toString(new FileInputStream(pom), state.encoding);
return IOUtils.toString(new FileInputStream(pom), cfg.encoding);
}

private static class MySortPomLogger implements SortPomLogger {
Expand Down
93 changes: 0 additions & 93 deletions lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomState.java

This file was deleted.

32 changes: 30 additions & 2 deletions lib/src/sortPom/java/com/diffplug/spotless/pom/SortPomStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
*/
package com.diffplug.spotless.pom;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.security.AccessController;
import java.security.PrivilegedAction;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;

public class SortPomStep {
Expand All @@ -24,7 +33,26 @@ public class SortPomStep {

private SortPomStep() {}

public static FormatterStep create(String encoding, String lineSeparator, boolean expandEmptyElements, boolean spaceBeforeCloseEmptyElement, boolean keepBlankLines, int nrOfIndentSpace, boolean indentBlankLines, boolean indentSchemaLocation, String predefinedSortOrder, String sortOrderFile, String sortDependencies, String sortDependencyExclusions, String sortPlugins, boolean sortProperties, boolean sortModules, boolean sortExecutions, Provisioner provisioner) {
return FormatterStep.createLazy(NAME, () -> new SortPomState(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions, provisioner), SortPomState::createFormat);
private SortPomCfg cfg;

public static FormatterStep create(SortPomCfg cfg, Provisioner provisioner) {
return FormatterStep.createLazy(NAME, () -> new State(cfg, provisioner), State::createFormat);
}

static class State implements Serializable {
SortPomCfg cfg;
JarState jarState;

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);
}

FormatterFunc createFormat() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
ClassLoader classLoader = AccessController.doPrivileged((PrivilegedAction<DelegatingClassLoader>) () -> new DelegatingClassLoader(this.getClass().getClassLoader(), jarState.getClassLoader()));
Constructor<?> constructor = classLoader.loadClass(SortPomFormatterFunc.class.getName()).getConstructor(classLoader.loadClass(SortPomCfg.class.getName()));
constructor.setAccessible(true);
return (FormatterFunc) constructor.newInstance(cfg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,79 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;
import com.diffplug.spotless.pom.SortPomCfg;
import com.diffplug.spotless.pom.SortPomStep;

public class SortPom implements FormatterStepFactory {
private final SortPomCfg defaultValues = new SortPomCfg();

@Parameter
String encoding = "UTF-8";
String encoding = defaultValues.encoding;

@Parameter
String lineSeparator = System.getProperty("line.separator");
String lineSeparator = defaultValues.lineSeparator;

@Parameter
boolean expandEmptyElements = true;
boolean expandEmptyElements = defaultValues.expandEmptyElements;

@Parameter
boolean spaceBeforeCloseEmptyElement = false;
boolean spaceBeforeCloseEmptyElement = defaultValues.spaceBeforeCloseEmptyElement;

@Parameter
boolean keepBlankLines = true;
boolean keepBlankLines = defaultValues.keepBlankLines;

@Parameter
int nrOfIndentSpace = 2;
int nrOfIndentSpace = defaultValues.nrOfIndentSpace;

@Parameter
boolean indentBlankLines = false;
boolean indentBlankLines = defaultValues.indentBlankLines;

@Parameter
boolean indentSchemaLocation = false;
boolean indentSchemaLocation = defaultValues.indentSchemaLocation;

@Parameter
String predefinedSortOrder = "recommended_2008_06";
String predefinedSortOrder = defaultValues.predefinedSortOrder;

@Parameter
String sortOrderFile;
String sortOrderFile = defaultValues.sortOrderFile;

@Parameter
String sortDependencies;
String sortDependencies = defaultValues.sortDependencies;

@Parameter
String sortDependencyExclusions;
String sortDependencyExclusions = defaultValues.sortDependencyExclusions;

@Parameter
String sortPlugins;
String sortPlugins = defaultValues.sortPlugins;

@Parameter
boolean sortProperties = false;
boolean sortProperties = defaultValues.sortProperties;

@Parameter
boolean sortModules = false;
boolean sortModules = defaultValues.sortModules;

@Parameter
boolean sortExecutions = false;
boolean sortExecutions = defaultValues.sortExecutions;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
return SortPomStep.create(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement, keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation, predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins, sortProperties, sortModules, sortExecutions, stepConfig.getProvisioner());
SortPomCfg cfg = new SortPomCfg();
cfg.encoding = encoding;
cfg.lineSeparator = lineSeparator;
cfg.expandEmptyElements = expandEmptyElements;
cfg.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement;
cfg.keepBlankLines = keepBlankLines;
cfg.nrOfIndentSpace = nrOfIndentSpace;
cfg.indentBlankLines = indentBlankLines;
cfg.indentSchemaLocation = indentSchemaLocation;
cfg.predefinedSortOrder = predefinedSortOrder;
cfg.sortOrderFile = sortOrderFile;
cfg.sortDependencies = sortDependencies;
cfg.sortDependencyExclusions = sortDependencyExclusions;
cfg.sortPlugins = sortPlugins;
cfg.sortProperties = sortProperties;
cfg.sortModules = sortModules;
cfg.sortExecutions = sortExecutions;
return SortPomStep.create(cfg, stepConfig.getProvisioner());
}
}
23 changes: 2 additions & 21 deletions testlib/src/test/java/com/diffplug/spotless/pom/SortPomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,9 @@
public class SortPomTest {
@Test
public void testSortPomWithDefaultConfig() throws Exception {
String encoding = "UTF-8";
String lineSeparator = System.getProperty("line.separator");
boolean expandEmptyElements = true;
boolean spaceBeforeCloseEmptyElement = false;
boolean keepBlankLines = true;
int nrOfIndentSpace = 2;
boolean indentBlankLines = false;
boolean indentSchemaLocation = false;
String predefinedSortOrder = "recommended_2008_06";
String sortOrderFile = null;
String sortDependencies = null;
String sortDependencyExclusions = null;
String sortPlugins = null;
boolean sortProperties = false;
boolean sortModules = false;
boolean sortExecutions = false;
SortPomCfg cfg = new SortPomCfg();
Provisioner provisioner = TestProvisioner.mavenCentral();
StepHarness harness = StepHarness.forStep(SortPomStep.create(encoding, lineSeparator, expandEmptyElements, spaceBeforeCloseEmptyElement,
keepBlankLines, nrOfIndentSpace, indentBlankLines, indentSchemaLocation,
predefinedSortOrder, sortOrderFile, sortDependencies, sortDependencyExclusions, sortPlugins,
sortProperties, sortModules, sortExecutions,
provisioner));
StepHarness harness = StepHarness.forStep(SortPomStep.create(cfg, provisioner));
harness.testResource("pom/pom_dirty.xml", "pom/pom_clean_default.xml");
}
}

0 comments on commit f2c0e4a

Please sign in to comment.