Skip to content

Commit

Permalink
Merge pull request #592 from diffplug/feat/cleanup
Browse files Browse the repository at this point in the history
A bunch of backwards-compatible cleanup
  • Loading branch information
nedtwigg authored Jun 3, 2020
2 parents 26c00b7 + 8b44556 commit 57eaf4f
Show file tree
Hide file tree
Showing 91 changed files with 340 additions and 354 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
* We are now dogfooding `ratchetFrom` and `licenseHeader` with a `$YEAR` token to ensure that Spotless copyright headers stay up-to-date without adding noise to file history. ([#595](https://github.com/diffplug/spotless/pull/595))
* Added `LineEnding.nativeIsWin()`, `FileSignature.pathNativeToUnix()`, and `FileSignature.pathUnixToNative()`, along with many API-invisible fixes and cleanup. ([#592](https://github.com/diffplug/spotless/pull/592))

## [1.33.0] - 2020-06-03
### Added
Expand Down
1 change: 1 addition & 0 deletions gradle/java-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ task sourcesJar(type: Jar) {
// Thus, no javadoc warnings.
javadoc {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('Xwerror', '-quiet')
enabled = org.gradle.api.JavaVersion.current() == org.gradle.api.JavaVersion.VERSION_1_8
}

Expand Down
11 changes: 10 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/FileSignature.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -110,4 +110,13 @@ public File getOnlyFile() {
}
}

/** Transforms a native path to a unix one. */
public static String pathNativeToUnix(String pathNative) {
return LineEnding.nativeIsWin() ? pathNative : pathNative.replace('\\', '/');
}

/** Transforms a unix path to a native one. */
public static String pathUnixToNative(String pathUnix) {
return LineEnding.nativeIsWin() ? pathUnix.replace('/', '\\') : pathUnix;
}
}
7 changes: 6 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/LineEnding.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -98,6 +98,11 @@ public String getEndingFor(File file) {
private static final Policy UNIX_POLICY = new ConstantLineEndingPolicy(UNIX.str());
private static final String _platformNative = System.getProperty("line.separator");
private static final Policy _platformNativePolicy = new ConstantLineEndingPolicy(_platformNative);
private static final boolean nativeIsWin = _platformNative.equals(WINDOWS.str());

public static boolean nativeIsWin() {
return nativeIsWin;
}

/** Returns the standard line ending for this policy. */
public String str() {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/main/java/com/diffplug/spotless/PaddedCell.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -235,9 +235,9 @@ public static DirtyState calculateDirtyState(Formatter formatter, File file, byt
/**
* The clean/dirty state of a single file. Intended use:
* - {@link #isClean()} means that the file is is clean, and there's nothing else to say
* - {@link #isConverged()} means that we were able to determine a clean state
* - {@link #didNotConverge()} means that we were unable to determine a clean state
* - once you've tested the above conditions and you know that it's a dirty file with a converged state,
* then you can call {@link #writeCanonicalTo()} to get the canonical form of the given file.
* then you can call {@link #writeCanonicalTo(OutputStream)} to get the canonical form of the given file.
*/
public static class DirtyState {
private final byte[] canonicalBytes;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/com/diffplug/spotless/PaddedCellBulk.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -33,7 +33,7 @@

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/** COMPLETELY DEPRECATED, use {@link PaddedCell#canonicalIfDirty(Formatter, File)} instead. */
/** COMPLETELY DEPRECATED, use {@link PaddedCell#calculateDirtyState(Formatter, File)} instead. */
@Deprecated
public final class PaddedCellBulk {
private static final Logger logger = Logger.getLogger(PaddedCellBulk.class.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -138,7 +138,6 @@ FormatterFunc createFormat() throws Exception {
};
}

@SuppressWarnings({"unchecked", "rawtypes"})
FormatterFunc createRemoveUnusedImportsOnly() throws Exception {
ClassLoader classLoader = jarState.getClassLoader();

Expand All @@ -161,6 +160,7 @@ private static Function<String, String> constructRemoveUnusedFunction(ClassLoade

Function<String, String> removeUnused;
if (removeJavadocOnlyClass != null) {
@SuppressWarnings({"unchecked", "rawtypes"})
Object removeJavadocConstant = Enum.valueOf((Class<Enum>) removeJavadocOnlyClass, REMOVE_UNUSED_IMPORT_JavadocOnlyImports_Keep);
Method removeUnusedMethod = removeUnusedClass.getMethod(REMOVE_UNUSED_METHOD, String.class, removeJavadocOnlyClass);
removeUnused = (x) -> (String) removeUnusedMethod.invoke(null, x, removeJavadocConstant);
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/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 `3.27.0`).

## [Unreleased]
### Fixed
* Improved the warning message for `paddedCell` deprecation, along with many API-invisible fixes and cleanup. ([#592](https://github.com/diffplug/spotless/pull/592))

## [4.2.0] - 2020-06-03
### Added
Expand Down
36 changes: 24 additions & 12 deletions plugin-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ dependencies {
testImplementation "com.diffplug.durian:durian-testlib:${VER_DURIAN}"
}

tasks.eclipse.dependsOn(pluginUnderTestMetadata)

test { testLogging.showStandardStreams = true }

test {
Expand All @@ -46,28 +44,42 @@ task npmTest(type: Test) {
//////////////////////////
// GRADLE PLUGIN PORTAL //
//////////////////////////
gradlePlugin {
plugins {
spotlessPlugin {
id = 'com.diffplug.gradle.spotless'
implementationClass = 'com.diffplug.gradle.spotless.SpotlessPlugin'
displayName = 'Spotless formatting plugin'
description = project.description
}
}
}
if (version.endsWith('-SNAPSHOT')) {
publishPlugins.enabled = false
} else {
pluginBundle {
// These settings are set for the whole plugin bundle
website = "https://github.com/diffplug/spotless"
vcsUrl = "https://github.com/diffplug/spotless"
description = project.description

tags = [
'format',
'style',
'license',
'header',
'google-java-format',
'eclipse',
'ktlint',
'ktfmt',
'tsfmt',
'prettier',
'scalafmt',
'scalafix'
]
plugins {
spotlessPlugin {
id = 'com.diffplug.gradle.spotless'
displayName = 'Spotless formatting plugin'
tags = [
'format',
'style',
'license',
'header'
]
}
}

mavenCoordinates {
groupId = project.group
artifactId = project.artifactIdGradle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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,8 +26,8 @@
public class CppExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "cpp";

public CppExtension(SpotlessExtension rootExtension) {
super(rootExtension);
public CppExtension(SpotlessExtension spotless) {
super(spotless);
}

public EclipseConfig eclipse() {
Expand All @@ -42,7 +42,7 @@ public class EclipseConfig {
private final EclipseBasedStepBuilder builder;

EclipseConfig(String version) {
builder = EclipseCdtFormatterStep.createBuilder(GradleProvisioner.fromProject(getProject()));
builder = EclipseCdtFormatterStep.createBuilder(provisioner());
builder.setVersion(version);
addStep(builder.build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -30,8 +30,8 @@
public class CssExtension extends FormatExtension implements HasBuiltinDelimiterForLicense {
static final String NAME = "css";

public CssExtension(SpotlessExtension rootExtension) {
super(rootExtension);
public CssExtension(SpotlessExtension spotless) {
super(spotless);
}

public EclipseConfig eclipse() {
Expand All @@ -50,7 +50,7 @@ public class EclipseConfig {
private final EclipseBasedStepBuilder builder;

EclipseConfig(String version) {
builder = EclipseWtpFormatterStep.createCssBuilder(GradleProvisioner.fromProject(getProject()));
builder = EclipseWtpFormatterStep.createCssBuilder(provisioner());
builder.setVersion(version);
addStep(builder.build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 DiffPlug
* Copyright 2016-2020 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 @@ -39,6 +39,7 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LazyForwardingEquality;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep;
Expand All @@ -54,14 +55,18 @@

/** Adds a `spotless{Name}Check` and `spotless{Name}Apply` task. */
public class FormatExtension {
final SpotlessExtension root;
final SpotlessExtension spotless;

public FormatExtension(SpotlessExtension root) {
this.root = Objects.requireNonNull(root);
public FormatExtension(SpotlessExtension spotless) {
this.spotless = Objects.requireNonNull(spotless);
}

protected final Provisioner provisioner() {
return spotless.registerDependenciesTask.rootProvisioner;
}

private String formatName() {
for (Map.Entry<String, FormatExtension> entry : root.formats.entrySet()) {
for (Map.Entry<String, FormatExtension> entry : spotless.formats.entrySet()) {
if (entry.getValue() == this) {
return entry.getKey();
}
Expand All @@ -78,14 +83,14 @@ public void paddedCell() {
/** Enables paddedCell mode. @see <a href="https://github.com/diffplug/spotless/blob/master/PADDEDCELL.md">Padded cell</a> */
@Deprecated
public void paddedCell(boolean paddedCell) {
root.project.getLogger().warn("PaddedCell is now always on, and cannot be turned off.");
spotless.project.getLogger().warn("PaddedCell is now always on, and cannot be turned off.");
}

LineEnding lineEndings;

/** Returns the line endings to use (defaults to {@link SpotlessExtension#getLineEndings()}. */
public LineEnding getLineEndings() {
return lineEndings == null ? root.getLineEndings() : lineEndings;
return lineEndings == null ? spotless.getLineEndings() : lineEndings;
}

/** Sets the line endings to use (defaults to {@link SpotlessExtension#getLineEndings()}. */
Expand All @@ -97,7 +102,7 @@ public void setLineEndings(LineEnding lineEndings) {

/** Returns the encoding to use (defaults to {@link SpotlessExtension#getEncoding()}. */
public Charset getEncoding() {
return encoding == null ? root.getEncoding() : encoding;
return encoding == null ? spotless.getEncoding() : encoding;
}

/** Sets the encoding to use (defaults to {@link SpotlessExtension#getEncoding()}. */
Expand Down Expand Up @@ -447,8 +452,8 @@ public LicenseHeaderConfig yearSeparator(String yearSeparator) {
* Will turn `2004` into `2004-2020`, and `2004-2019` into `2004-2020`
* Default value is false, unless {@link SpotlessExtension#ratchetFrom(String)} is used, in which case default value is true.
*/
public LicenseHeaderConfig updateYearWithLatest(boolean overwriteYearLatest) {
this.updateYearWithLatest = overwriteYearLatest;
public LicenseHeaderConfig updateYearWithLatest(boolean updateYearWithLatest) {
this.updateYearWithLatest = updateYearWithLatest;
replaceStep(createStep());
return this;
}
Expand All @@ -458,7 +463,7 @@ public LicenseHeaderConfig updateYearWithLatest(boolean overwriteYearLatest) {
FormatterStep createStep() {
return FormatterStep.createLazy(LicenseHeaderStep.name(), () -> {
// by default, we should update the year if the user is using ratchetFrom
boolean updateYear = updateYearWithLatest == null ? FormatExtension.this.root.getRatchetFrom() != null : updateYearWithLatest;
boolean updateYear = updateYearWithLatest == null ? FormatExtension.this.spotless.getRatchetFrom() != null : updateYearWithLatest;
return new LicenseHeaderStep(licenseHeader(), delimiter, yearSeparator, updateYear);
}, step -> step::format);
}
Expand Down Expand Up @@ -566,7 +571,7 @@ FormatterStep createStep() {
final Project project = getProject();
return PrettierFormatterStep.create(
devDependencies,
GradleProvisioner.fromProject(project),
provisioner(),
project.getBuildDir(),
npmFileOrNull(),
new com.diffplug.spotless.npm.PrettierConfig(
Expand Down Expand Up @@ -596,7 +601,7 @@ public class EclipseWtpConfig {
private final EclipseBasedStepBuilder builder;

EclipseWtpConfig(EclipseWtpFormatterStep type, String version) {
builder = type.createBuilder(GradleProvisioner.fromProject(getProject()));
builder = type.createBuilder(provisioner());
builder.setVersion(version);
addStep(builder.build());
}
Expand Down Expand Up @@ -628,17 +633,18 @@ protected void setupTask(SpotlessTask task) {
}
task.setSteps(steps);
task.setLineEndingsPolicy(getLineEndings().createPolicy(getProject().getProjectDir(), () -> task.target));
if (root.project != root.project.getRootProject()) {
root.registerDependenciesTask.hookSubprojectTask(task);
if (spotless.project != spotless.project.getRootProject()) {
spotless.registerDependenciesTask.hookSubprojectTask(task);
}
if (root.getRatchetFrom() != null) {
task.treeSha = GitRatchet.treeShaOf(root.project, root.getRatchetFrom());
if (spotless.getRatchetFrom() != null) {
task.ratchet = spotless.registerDependenciesTask.gitRatchet;
task.treeSha = task.ratchet.treeShaOf(spotless.project, spotless.getRatchetFrom());
}
}

/** Returns the project that this extension is attached to. */
protected Project getProject() {
return root.project;
return spotless.project;
}

/**
Expand All @@ -654,15 +660,15 @@ protected Project getProject() {
*/
public SpotlessApply createIndependentApplyTask(String taskName) {
// create and setup the task
SpotlessTask spotlessTask = root.project.getTasks().create(taskName + "Helper", SpotlessTask.class);
SpotlessTask spotlessTask = spotless.project.getTasks().create(taskName + "Helper", SpotlessTask.class);
setupTask(spotlessTask);
// enforce the clean ordering
Task clean = root.project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME);
Task clean = spotless.project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME);
spotlessTask.mustRunAfter(clean);
// ignore the filePatterns
spotlessTask.setFilePatterns("");
// create the apply task
SpotlessApply applyTask = root.project.getTasks().create(taskName, SpotlessApply.class);
SpotlessApply applyTask = spotless.project.getTasks().create(taskName, SpotlessApply.class);
applyTask.setSpotlessOutDirectory(spotlessTask.getOutputDirectory());
applyTask.linkSource(spotlessTask);
applyTask.dependsOn(spotlessTask);
Expand Down
Loading

0 comments on commit 57eaf4f

Please sign in to comment.