From 1b990ab98d23561c4433bcab4175356903b2e5c2 Mon Sep 17 00:00:00 2001 From: Simon Gamma Date: Thu, 26 Jan 2023 16:35:21 +0100 Subject: [PATCH 1/5] 1162: adding tests verifying issue results in 'Couldn't resolve parser "php"' on gradle side and 'Couldn't resolve parser "java"' on maven side --- .../spotless/PrettierIntegrationTest.java | 42 +++++++++++++++ .../maven/MavenIntegrationHarness.java | 15 ++++-- .../prettier/PrettierFormatStepTest.java | 52 +++++++++++++++++++ 3 files changed, 105 insertions(+), 4 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java index 4c458e3ca7..8819d5bc07 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java @@ -183,6 +183,48 @@ void usePhpCommunityPlugin() throws IOException { assertFile("php-example.php").sameAsResource("npm/prettier/plugins/php.clean"); } + /** + * This test is to ensure that we can have multiple prettier instances in one spotless config. + * + * @see Issue #1162 on github + */ + @Test + void usePhpAndJavaCommunityPlugin() throws IOException { + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "def prettierConfigPhp = [:]", + "prettierConfigPhp['tabWidth'] = 3", + "prettierConfigPhp['parser'] = 'php'", + "def prettierPackagesPhp = [:]", + "prettierPackagesPhp['prettier'] = '2.0.5'", + "prettierPackagesPhp['@prettier/plugin-php'] = '0.14.2'", + "def prettierConfigJava = [:]", + "prettierConfigJava['tabWidth'] = 4", + "prettierConfigJava['parser'] = 'java'", + "def prettierPackagesJava = [:]", + "prettierPackagesJava['prettier'] = '2.0.5'", + "prettierPackagesJava['prettier-plugin-java'] = '0.8.0'", + "spotless {", + " format 'php', {", + " target 'php-example.php'", + " prettier(prettierPackagesPhp).config(prettierConfigPhp)", + " }", + " java {", + " target 'JavaTest.java'", + " prettier(prettierPackagesJava).config(prettierConfigJava)", + " }", + "}"); + setFile("php-example.php").toResource("npm/prettier/plugins/php.dirty"); + setFile("JavaTest.java").toResource("npm/prettier/plugins/java-test.dirty"); + final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); + Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL"); + assertFile("php-example.php").sameAsResource("npm/prettier/plugins/php.clean"); + assertFile("JavaTest.java").sameAsResource("npm/prettier/plugins/java-test.clean"); + } + @Test void autodetectNpmrcFileConfig() throws IOException { setFile(".npmrc").toLines( diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java index fdc4a745a1..2891ee3b2c 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java @@ -285,7 +285,7 @@ private static String getSystemProperty(String name) { return value; } - private static String[] groupWithSteps(String group, String[] includes, String... steps) { + protected static String[] groupWithSteps(String group, String[] includes, String... steps) { String[] result = new String[steps.length + includes.length + 2]; result[0] = "<" + group + ">"; System.arraycopy(includes, 0, result, 1, includes.length); @@ -294,15 +294,22 @@ private static String[] groupWithSteps(String group, String[] includes, String.. return result; } - private static String[] groupWithSteps(String group, String... steps) { + protected static String[] groupWithSteps(String group, String... steps) { return groupWithSteps(group, new String[]{}, steps); } - private static String[] including(String... includes) { + protected static String[] including(String... includes) { return groupWithSteps("includes", groupWithSteps("include", includes)); } - private static String[] formats(String... formats) { + protected static String[] formats(String... formats) { return groupWithSteps("formats", formats); } + + protected static String[] formats(String[]... formats) { + String[] formatsArray = Arrays.stream(formats) + .flatMap(Arrays::stream) + .toArray(String[]::new); + return formats(formatsArray); + } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/prettier/PrettierFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/prettier/PrettierFormatStepTest.java index 47dab5d152..78a0fc30ff 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/prettier/PrettierFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/prettier/PrettierFormatStepTest.java @@ -106,6 +106,58 @@ void unique_dependency_config() throws Exception { assertThat(result.stdOutUtf8()).contains(Prettier.ERROR_MESSAGE_ONLY_ONE_CONFIG); } + /** + * This test is to ensure that we can have multiple prettier instances in one spotless config. + * + * @see Issue #1162 on github + */ + @Test + void multiple_prettier_configs() throws Exception { + writePom( + formats( + groupWithSteps("format", including("php-example.php"), + "", + " ", + " ", + " prettier", + " 2.0.5", + " ", + " ", + " @prettier/plugin-php", + " 0.14.2", + " ", + " ", + " ", + " 3", + " php", + " ", + ""), + groupWithSteps("java", including("JavaTest.java"), + "", + " ", + " ", + " prettier", + " 2.0.5", + " ", + " ", + " prettier-plugin-java", + " 0.8.0", + " ", + " ", + " ", + " 4", + " java", + " ", + ""))); + + setFile("php-example.php").toResource("npm/prettier/plugins/php.dirty"); + setFile("JavaTest.java").toResource("npm/prettier/plugins/java-test.dirty"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile("php-example.php").sameAsResource("npm/prettier/plugins/php.clean"); + assertFile("JavaTest.java").sameAsResource("npm/prettier/plugins/java-test.clean"); + + } + @Test void custom_plugin() throws Exception { writePomWithFormatSteps( From fdbbd024158911d65cb567b04340f875d45dd46b Mon Sep 17 00:00:00 2001 From: Simon Gamma Date: Fri, 27 Jan 2023 20:21:18 +0100 Subject: [PATCH 2/5] 1162: add unique suffix reflecting package.json content --- .../diffplug/spotless/npm/NodeServerLayout.java | 4 ++-- .../spotless/npm/NpmFormatterStepStateBase.java | 9 ++++++++- .../spotless/npm/NpmResourceHelper.java | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java b/lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java index 63a6a923da..60c34bcc8c 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java @@ -29,8 +29,8 @@ class NodeServerLayout { private final File serveJsFile; private final File npmrcFile; - NodeServerLayout(File buildDir, String stepName) { - this.nodeModulesDir = new File(buildDir, "spotless-node-modules-" + stepName); + NodeServerLayout(File buildDir, String stepName, String stepSuffix) { + this.nodeModulesDir = new File(buildDir, String.format("spotless-node-modules-%s-%s", stepName, stepSuffix)); this.packageJsonFile = new File(nodeModulesDir, "package.json"); this.serveJsFile = new File(nodeModulesDir, "serve.js"); this.npmrcFile = new File(nodeModulesDir, ".npmrc"); diff --git a/lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java b/lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java index 92ac7df6f2..412a827d9e 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java @@ -56,7 +56,14 @@ protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, NpmFor this.stepName = requireNonNull(stepName); this.npmConfig = requireNonNull(npmConfig); this.locations = locations; - this.nodeServerLayout = new NodeServerLayout(locations.buildDir(), stepName); + String stateSuffix = stateSuffix(); + logger.info("Creating {} with name {} and state suffix {}", this.getClass().getSimpleName(), stepName, stateSuffix); + this.nodeServerLayout = new NodeServerLayout(locations.buildDir(), stepName, stateSuffix); + } + + protected String stateSuffix() { + String packageJsonContent = npmConfig.getPackageJsonContent(); + return NpmResourceHelper.md5(packageJsonContent); } protected void prepareNodeServerLayout() throws IOException { diff --git a/lib/src/main/java/com/diffplug/spotless/npm/NpmResourceHelper.java b/lib/src/main/java/com/diffplug/spotless/npm/NpmResourceHelper.java index 2acf3a180d..aa66c54fcf 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/NpmResourceHelper.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/NpmResourceHelper.java @@ -21,6 +21,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.security.MessageDigest; import java.time.Duration; import java.util.Arrays; import java.util.Objects; @@ -122,4 +123,20 @@ static File copyFileToDirAtSubpath(File file, File targetDir, String relativePat throw ThrowingEx.asRuntime(e); } } + + static String md5(File file) { + return md5(readUtf8StringFromFile(file)); + } + + static String md5(String fileContent) { + MessageDigest md = ThrowingEx.get(() -> MessageDigest.getInstance("MD5")); + md.update(fileContent.getBytes(StandardCharsets.UTF_8)); + byte[] digest = md.digest(); + // convert byte array digest to hex string + StringBuilder sb = new StringBuilder(); + for (byte b : digest) { + sb.append(String.format("%02x", b & 0xff)); + } + return sb.toString(); + } } From e0a290a2b784b073eb13bac62f77dff57b9cee11 Mon Sep 17 00:00:00 2001 From: Simon Gamma Date: Wed, 8 Feb 2023 21:09:13 +0100 Subject: [PATCH 3/5] 1162: separate node dirs per package.json --- .../spotless/npm/NodeServerLayout.java | 40 ++++++++++++++++--- .../npm/NpmFormatterStepStateBase.java | 18 ++++----- .../diffplug/spotless/npm/eslint-package.json | 2 +- .../spotless/npm/prettier-package.json | 2 +- .../diffplug/spotless/npm/tsfmt-package.json | 2 +- 5 files changed, 45 insertions(+), 19 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java b/lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java index 60c34bcc8c..8b39c5e4ab 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java @@ -18,25 +18,44 @@ import java.io.File; import java.nio.file.Files; import java.nio.file.Path; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Stream; import com.diffplug.spotless.ThrowingEx; class NodeServerLayout { + private static final Pattern PACKAGE_JSON_NAME_PATTERN = Pattern.compile("\"name\"\\s*:\\s*\"([^\"]+)\""); + private final File nodeModulesDir; private final File packageJsonFile; + + private final File packageLockJsonFile; + private final File serveJsFile; private final File npmrcFile; - NodeServerLayout(File buildDir, String stepName, String stepSuffix) { - this.nodeModulesDir = new File(buildDir, String.format("spotless-node-modules-%s-%s", stepName, stepSuffix)); + NodeServerLayout(File buildDir, String packageJsonContent) { + this.nodeModulesDir = new File(buildDir, nodeModulesDirName(packageJsonContent)); this.packageJsonFile = new File(nodeModulesDir, "package.json"); + this.packageLockJsonFile = new File(nodeModulesDir, "package-lock.json"); this.serveJsFile = new File(nodeModulesDir, "serve.js"); this.npmrcFile = new File(nodeModulesDir, ".npmrc"); } + private static String nodeModulesDirName(String packageJsonContent) { + String md5Hash = NpmResourceHelper.md5(packageJsonContent); + Matcher matcher = PACKAGE_JSON_NAME_PATTERN.matcher(packageJsonContent); + if (!matcher.find()) { + throw new IllegalArgumentException("package.json must contain a name property"); + } + String packageName = matcher.group(1); + return String.format("%s-node-modules-%s", packageName, md5Hash); + } + File nodeModulesDir() { + return nodeModulesDir; } @@ -52,10 +71,6 @@ public File npmrcFile() { return npmrcFile; } - static File getBuildDirFromNodeModulesDir(File nodeModulesDir) { - return nodeModulesDir.getParentFile(); - } - public boolean isLayoutPrepared() { if (!nodeModulesDir().isDirectory()) { return false; @@ -63,6 +78,9 @@ public boolean isLayoutPrepared() { if (!packageJsonFile().isFile()) { return false; } + if (!packageLockJsonFile.isFile()) { + return false; + } if (!serveJsFile().isFile()) { return false; } @@ -82,4 +100,14 @@ public boolean isNodeModulesPrepared() { } }); } + + @Override + public String toString() { + return String.format( + "NodeServerLayout[nodeModulesDir=%s, packageJsonFile=%s, serveJsFile=%s, npmrcFile=%s]", + this.nodeModulesDir, + this.packageJsonFile, + this.serveJsFile, + this.npmrcFile); + } } diff --git a/lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java b/lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java index 412a827d9e..f3f8a80fe8 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java @@ -56,17 +56,13 @@ protected NpmFormatterStepStateBase(String stepName, NpmConfig npmConfig, NpmFor this.stepName = requireNonNull(stepName); this.npmConfig = requireNonNull(npmConfig); this.locations = locations; - String stateSuffix = stateSuffix(); - logger.info("Creating {} with name {} and state suffix {}", this.getClass().getSimpleName(), stepName, stateSuffix); - this.nodeServerLayout = new NodeServerLayout(locations.buildDir(), stepName, stateSuffix); - } - - protected String stateSuffix() { - String packageJsonContent = npmConfig.getPackageJsonContent(); - return NpmResourceHelper.md5(packageJsonContent); + this.nodeServerLayout = new NodeServerLayout(locations.buildDir(), npmConfig.getPackageJsonContent()); } protected void prepareNodeServerLayout() throws IOException { + final long started = System.currentTimeMillis(); + // maybe introduce trace logger? + logger.info("Preparing {} for npm step {}.", this.nodeServerLayout, getClass().getName()); NpmResourceHelper.assertDirectoryExists(nodeServerLayout.nodeModulesDir()); NpmResourceHelper.writeUtf8StringToFile(nodeServerLayout.packageJsonFile(), this.npmConfig.getPackageJsonContent()); @@ -77,12 +73,14 @@ protected void prepareNodeServerLayout() throws IOException { } else { NpmResourceHelper.deleteFileIfExists(nodeServerLayout.npmrcFile()); } + logger.info("Prepared {} for npm step {} in {} ms.", this.nodeServerLayout, getClass().getName(), System.currentTimeMillis() - started); } protected void prepareNodeServer() throws IOException { - FormattedPrinter.SYSOUT.print("running npm install"); + final long started = System.currentTimeMillis(); + logger.info("running npm install in {} for npm step {}", this.nodeServerLayout.nodeModulesDir(), getClass().getName()); runNpmInstall(nodeServerLayout.nodeModulesDir()); - FormattedPrinter.SYSOUT.print("npm install finished"); + logger.info("npm install finished in {} ms in {} for npm step {}", System.currentTimeMillis() - started, this.nodeServerLayout.nodeModulesDir(), getClass().getName()); } private void runNpmInstall(File npmProjectDir) throws IOException { diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/eslint-package.json b/lib/src/main/resources/com/diffplug/spotless/npm/eslint-package.json index dcd91e729d..0d7ce930f7 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/eslint-package.json +++ b/lib/src/main/resources/com/diffplug/spotless/npm/eslint-package.json @@ -1,5 +1,5 @@ { - "name": "spotless-eslint-formatter-step", + "name": "spotless-eslint", "version": "2.0.0", "description": "Spotless formatter step for running eslint as a rest service.", "repository": "https://github.com/diffplug/spotless", diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/prettier-package.json b/lib/src/main/resources/com/diffplug/spotless/npm/prettier-package.json index 7bda08db8a..395a05da67 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/prettier-package.json +++ b/lib/src/main/resources/com/diffplug/spotless/npm/prettier-package.json @@ -1,5 +1,5 @@ { - "name": "spotless-prettier-formatter-step", + "name": "spotless-prettier", "version": "2.0.0", "description": "Spotless formatter step for running prettier as a rest service.", "repository": "https://github.com/diffplug/spotless", diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-package.json b/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-package.json index 7037bd2ec1..483dd0753d 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-package.json +++ b/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-package.json @@ -1,5 +1,5 @@ { - "name": "spotless-tsfmt-formatter-step", + "name": "spotless-tsfmt", "version": "2.0.0", "description": "Spotless formatter step for running tsfmt as a rest service.", "repository": "https://github.com/diffplug/spotless", From 080f2378af86d3fa8e8db5be207a03415dc47fe8 Mon Sep 17 00:00:00 2001 From: Simon Gamma Date: Thu, 9 Feb 2023 20:44:38 +0100 Subject: [PATCH 4/5] 1162: use slf4j for logging --- .../com/diffplug/spotless/LazyArgLogger.java | 40 ++++++++++++++++++ .../spotless/npm/EslintFormatterStep.java | 9 ++-- .../spotless/npm/FormattedPrinter.java | 42 ------------------- .../spotless/npm/PrettierFormatterStep.java | 7 ++-- .../spotless/PrettierIntegrationTest.java | 4 +- 5 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 lib/src/main/java/com/diffplug/spotless/LazyArgLogger.java delete mode 100644 lib/src/main/java/com/diffplug/spotless/npm/FormattedPrinter.java diff --git a/lib/src/main/java/com/diffplug/spotless/LazyArgLogger.java b/lib/src/main/java/com/diffplug/spotless/LazyArgLogger.java new file mode 100644 index 0000000000..e3456a2317 --- /dev/null +++ b/lib/src/main/java/com/diffplug/spotless/LazyArgLogger.java @@ -0,0 +1,40 @@ +/* + * Copyright 2023 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; + +import java.util.function.Supplier; + +/** + * This is a utility class to allow for lazy evaluation of arguments to be passed to a logger + * and thus avoid unnecessary computation of the arguments if the log level is not enabled. + */ +public final class LazyArgLogger { + + private final Supplier argSupplier; + + private LazyArgLogger(Supplier argSupplier) { + this.argSupplier = argSupplier; + } + + public static LazyArgLogger lazy(Supplier argSupplier) { + return new LazyArgLogger(argSupplier); + } + + @Override + public String toString() { + return String.valueOf(argSupplier.get()); + } +} diff --git a/lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java b/lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java index 221a745ad7..b262bb4b98 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/EslintFormatterStep.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.npm; +import static com.diffplug.spotless.LazyArgLogger.lazy; import static java.util.Objects.requireNonNull; import java.io.File; @@ -115,7 +116,7 @@ protected void prepareNodeServerLayout() throws IOException { // If any config files are provided, we need to make sure they are at the same location as the node modules // as eslint will try to resolve plugin/config names relatively to the config file location and some // eslint configs contain relative paths to additional config files (such as tsconfig.json e.g.) - FormattedPrinter.SYSOUT.print("Copying config file <%s> to <%s> and using the copy", origEslintConfig.getEslintConfigPath(), nodeServerLayout.nodeModulesDir()); + logger.info("Copying config file <{}> to <{}> and using the copy", origEslintConfig.getEslintConfigPath(), nodeServerLayout.nodeModulesDir()); File configFileCopy = NpmResourceHelper.copyFileToDir(origEslintConfig.getEslintConfigPath(), nodeServerLayout.nodeModulesDir()); this.eslintConfigInUse = this.origEslintConfig.withEslintConfigPath(configFileCopy).verify(); } @@ -125,7 +126,7 @@ protected void prepareNodeServerLayout() throws IOException { @Nonnull public FormatterFunc createFormatterFunc() { try { - FormattedPrinter.SYSOUT.print("creating formatter function (starting server)"); + logger.info("Creating formatter function (starting server)"); ServerProcessInfo eslintRestServer = npmRunServer(); EslintRestService restService = new EslintRestService(eslintRestServer.getBaseUrl()); return Closeable.ofDangerous(() -> endServer(restService, eslintRestServer), new EslintFilePathPassingFormatterFunc(locations.projectDir(), nodeServerLayout.nodeModulesDir(), eslintConfigInUse, restService)); @@ -135,7 +136,7 @@ public FormatterFunc createFormatterFunc() { } private void endServer(BaseNpmRestService restService, ServerProcessInfo restServer) throws Exception { - FormattedPrinter.SYSOUT.print("Closing formatting function (ending server)."); + logger.info("Closing formatting function (ending server)."); try { restService.shutdown(); } catch (Throwable t) { @@ -161,7 +162,7 @@ public EslintFilePathPassingFormatterFunc(File projectDir, File nodeModulesDir, @Override public String applyWithFile(String unix, File file) throws Exception { - FormattedPrinter.SYSOUT.print("formatting String '" + unix.substring(0, Math.min(50, unix.length())) + "[...]' in file '" + file + "'"); + logger.info("formatting String '{}[...]' in file '{}'", lazy(() -> unix.substring(0, Math.min(50, unix.length()))), file); Map eslintCallOptions = new HashMap<>(); setConfigToCallOptions(eslintCallOptions); diff --git a/lib/src/main/java/com/diffplug/spotless/npm/FormattedPrinter.java b/lib/src/main/java/com/diffplug/spotless/npm/FormattedPrinter.java deleted file mode 100644 index 97d5cdb4c6..0000000000 --- a/lib/src/main/java/com/diffplug/spotless/npm/FormattedPrinter.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2020 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.npm; - -import static java.util.Objects.requireNonNull; - -import java.io.PrintStream; -import java.time.LocalDateTime; - -enum FormattedPrinter { - SYSOUT(System.out); - - private static final boolean enabled = false; - - private final PrintStream printStream; - - FormattedPrinter(PrintStream printStream) { - this.printStream = requireNonNull(printStream); - } - - public void print(String msg, Object... paramsForStringFormat) { - if (!enabled) { - return; - } - String formatted = String.format(msg, paramsForStringFormat); - String prefixed = String.format("[%s] %s", LocalDateTime.now().toString(), formatted); - this.printStream.println(prefixed); - } -} diff --git a/lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java b/lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java index 22f1a69e7c..05c61f9bdf 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java @@ -15,6 +15,7 @@ */ package com.diffplug.spotless.npm; +import static com.diffplug.spotless.LazyArgLogger.lazy; import static java.util.Objects.requireNonNull; import java.io.File; @@ -86,7 +87,7 @@ private static class State extends NpmFormatterStepStateBase implements Serializ @Nonnull public FormatterFunc createFormatterFunc() { try { - FormattedPrinter.SYSOUT.print("creating formatter function (starting server)"); + logger.info("creating formatter function (starting server)"); ServerProcessInfo prettierRestServer = npmRunServer(); PrettierRestService restService = new PrettierRestService(prettierRestServer.getBaseUrl()); String prettierConfigOptions = restService.resolveConfig(this.prettierConfig.getPrettierConfigPath(), this.prettierConfig.getOptions()); @@ -97,7 +98,7 @@ public FormatterFunc createFormatterFunc() { } private void endServer(PrettierRestService restService, ServerProcessInfo restServer) throws Exception { - FormattedPrinter.SYSOUT.print("Closing formatting function (ending server)."); + logger.info("Closing formatting function (ending server)."); try { restService.shutdown(); } catch (Throwable t) { @@ -119,7 +120,7 @@ public PrettierFilePathPassingFormatterFunc(String prettierConfigOptions, Pretti @Override public String applyWithFile(String unix, File file) throws Exception { - FormattedPrinter.SYSOUT.print("formatting String '" + unix.substring(0, Math.min(50, unix.length())) + "[...]' in file '" + file + "'"); + logger.info("formatting String '{}[...]' in file '{}'", lazy(() -> unix.substring(0, Math.min(50, unix.length()))), file); final String prettierConfigOptionsWithFilepath = assertFilepathInConfigOptions(file); try { diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java index 8819d5bc07..a6b2ea9dc8 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java @@ -219,8 +219,10 @@ void usePhpAndJavaCommunityPlugin() throws IOException { "}"); setFile("php-example.php").toResource("npm/prettier/plugins/php.dirty"); setFile("JavaTest.java").toResource("npm/prettier/plugins/java-test.dirty"); - final BuildResult spotlessApply = gradleRunner().withArguments("--stacktrace", "spotlessApply").build(); + final BuildResult spotlessApply = gradleRunner().forwardOutput().withArguments("--stacktrace", "--info", "spotlessApply").build(); Assertions.assertThat(spotlessApply.getOutput()).contains("BUILD SUCCESSFUL"); + final BuildResult spotlessApply2 = gradleRunner().forwardOutput().withArguments("--stacktrace", "--info", "spotlessApply").build(); + Assertions.assertThat(spotlessApply2.getOutput()).contains("BUILD SUCCESSFUL"); assertFile("php-example.php").sameAsResource("npm/prettier/plugins/php.clean"); assertFile("JavaTest.java").sameAsResource("npm/prettier/plugins/java-test.clean"); } From 92eb869d8a50a86dacc416b0d49052ccd4137ec5 Mon Sep 17 00:00:00 2001 From: Simon Gamma Date: Thu, 9 Feb 2023 20:54:10 +0100 Subject: [PATCH 5/5] 1162: prepare changelog --- CHANGES.md | 5 ++++- plugin-gradle/CHANGES.md | 3 +++ plugin-maven/CHANGES.md | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 5f1a1ffa9f..0e2e994a22 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,7 +10,10 @@ 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] - +### Added +* Introduce `LazyArgLogger` to allow for lazy evaluation of log messages in slf4j logging. ([#1565](https://github.com/diffplug/spotless/pull/1565)) +### Fixed +* Allow multiple instances of the same npm-based formatter to be used by separating their `node_modules` directories. ([#1565](https://github.com/diffplug/spotless/pull/1565)) ## [2.34.1] - 2023-02-05 ### Changes * **POTENTIALLY BREAKING** Bump bytecode from Java 8 to 11 ([#1530](https://github.com/diffplug/spotless/pull/1530) part 2 of [#1337](https://github.com/diffplug/spotless/issues/1337)) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index e3234584b8..ec5ae54c27 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Fixed +* Allow multiple instances of the same npm-based formatter to be used simultaneously. E.g. use prettier for typescript + *and* Java (using the community prettier-plugin-java) without messing up their respective `node_module` dependencies. ([#1565](https://github.com/diffplug/spotless/pull/1565)) ## [6.14.1] - 2023-02-05 ### Fixed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 567d880f4f..cce049488a 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* Allow multiple instances of the same npm-based formatter to be used simultaneously. E.g. use prettier for typescript + *and* Java (using the community prettier-plugin-java) without messing up their respective `node_module` dependencies. ([#1565](https://github.com/diffplug/spotless/pull/1565)) ## [2.32.0] - 2023-02-05 ### Added