|
| 1 | +/* |
| 2 | + * Copyright 2025 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.cli.steps; |
| 17 | + |
| 18 | +import java.nio.file.Path; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Locale; |
| 21 | +import java.util.function.Supplier; |
| 22 | + |
| 23 | +import org.jetbrains.annotations.NotNull; |
| 24 | +import org.jetbrains.annotations.Nullable; |
| 25 | + |
| 26 | +import com.diffplug.spotless.FormatterStep; |
| 27 | +import com.diffplug.spotless.cli.core.SpotlessActionContext; |
| 28 | +import com.diffplug.spotless.cli.core.TargetFileTypeInferer; |
| 29 | +import com.diffplug.spotless.cli.help.AdditionalInfoLinks; |
| 30 | +import com.diffplug.spotless.cli.help.OptionConstants; |
| 31 | +import com.diffplug.spotless.cli.help.SupportedFileTypes; |
| 32 | +import com.diffplug.spotless.extra.EclipseBasedStepBuilder; |
| 33 | +import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep; |
| 34 | + |
| 35 | +import picocli.CommandLine; |
| 36 | + |
| 37 | +@CommandLine.Command( |
| 38 | + name = "eclipse-wtp", |
| 39 | + description = "Runs Eclipse WTP formatter (" + EclipseWtp.ECLIPSE_WTP_VERSION + ")") |
| 40 | +@SupportedFileTypes({"css", "html", "js", "json", "xml", "xhtml"}) |
| 41 | +@AdditionalInfoLinks({ |
| 42 | + "https://github.com/diffplug/spotless/tree/main/plugin-gradle#eclipse-web-tools-platform", |
| 43 | + "https://projects.eclipse.org/projects/webtools" |
| 44 | +}) |
| 45 | +public class EclipseWtp extends SpotlessFormatterStep { |
| 46 | + |
| 47 | + public static final String ECLIPSE_WTP_VERSION = "4.21.0"; // TODO we need to slurp in the lock file also |
| 48 | + |
| 49 | + @CommandLine.Option( |
| 50 | + names = {"-f", "--config-file"}, |
| 51 | + arity = "0", |
| 52 | + description = "The path to the Eclipse WTP configuration file. " |
| 53 | + + "For supported config file options see spotless documentation (additional info links).") |
| 54 | + List<Path> configFiles; |
| 55 | + |
| 56 | + @CommandLine.Option( |
| 57 | + names = {"-t", "--type"}, |
| 58 | + description = |
| 59 | + "The type of the Eclipse WTP formatter. If not provided, the type will be guessed based on the first few files we find. If that does not work, we fail the formatting run." |
| 60 | + + OptionConstants.VALID_VALUES_SUFFIX) |
| 61 | + Type type; |
| 62 | + |
| 63 | + public enum Type { |
| 64 | + CSS(EclipseWtpFormatterStep.CSS), |
| 65 | + HTML(EclipseWtpFormatterStep.HTML), |
| 66 | + JS(EclipseWtpFormatterStep.JS), |
| 67 | + JSON(EclipseWtpFormatterStep.JSON), |
| 68 | + XML(EclipseWtpFormatterStep.XML), |
| 69 | + XHTML(EclipseWtpFormatterStep.HTML); // XHTML is treated as HTML in Eclipse WTP |
| 70 | + |
| 71 | + private final EclipseWtpFormatterStep backendEclipseWtpType; |
| 72 | + |
| 73 | + Type(EclipseWtpFormatterStep backendEclipseWtpType) { |
| 74 | + this.backendEclipseWtpType = backendEclipseWtpType; |
| 75 | + } |
| 76 | + |
| 77 | + public @NotNull EclipseWtpFormatterStep toEclipseWtpType() { |
| 78 | + return this.backendEclipseWtpType; |
| 79 | + } |
| 80 | + |
| 81 | + public static @Nullable Type fromTargetFileType(@NotNull TargetFileTypeInferer.TargetFileType targetFileType) { |
| 82 | + return switch (targetFileType.fileExtension().toLowerCase(Locale.getDefault())) { |
| 83 | + case "css" -> CSS; |
| 84 | + case "html", "htm" -> HTML; |
| 85 | + case "js" -> JS; |
| 86 | + case "json" -> JSON; |
| 87 | + case "xml" -> XML; |
| 88 | + case "xhtml" -> XHTML; |
| 89 | + default -> null; |
| 90 | + }; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public @NotNull List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) { |
| 96 | + EclipseWtpFormatterStep wtpType = type(context::targetFileType).toEclipseWtpType(); |
| 97 | + EclipseBasedStepBuilder builder = wtpType.createBuilder(context.provisioner()); |
| 98 | + builder.setVersion(ECLIPSE_WTP_VERSION); |
| 99 | + if (configFiles != null && !configFiles.isEmpty()) { |
| 100 | + builder.setPreferences(configFiles.stream() |
| 101 | + .map(context::resolvePath) |
| 102 | + .map(Path::toFile) |
| 103 | + .toList()); |
| 104 | + } |
| 105 | + return List.of(builder.build()); |
| 106 | + } |
| 107 | + |
| 108 | + private Type type(Supplier<TargetFileTypeInferer.TargetFileType> targetFileTypeSupplier) { |
| 109 | + if (type != null) { |
| 110 | + return type; |
| 111 | + } |
| 112 | + // try type inferring |
| 113 | + TargetFileTypeInferer.TargetFileType targetFileType = targetFileTypeSupplier.get(); |
| 114 | + Type inferredType = Type.fromTargetFileType(targetFileType); |
| 115 | + if (inferredType != null) { |
| 116 | + return inferredType; |
| 117 | + } else { |
| 118 | + throw new IllegalArgumentException("Could not infer Eclipse WTP type from target file type: " |
| 119 | + + targetFileType.fileExtension() + " - workaround by specifying the --type option."); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments