|
| 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 | + */ |
1 | 16 | package com.diffplug.spotless.cli.steps; |
2 | 17 |
|
| 18 | +import java.nio.file.Path; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.jetbrains.annotations.NotNull; |
3 | 22 |
|
4 | 23 | import com.diffplug.spotless.FormatterStep; |
5 | | -import com.diffplug.spotless.ThrowingEx; |
6 | 24 | import com.diffplug.spotless.cli.core.SpotlessActionContext; |
7 | 25 | import com.diffplug.spotless.cli.help.OptionConstants; |
8 | 26 | import com.diffplug.spotless.extra.EclipseBasedStepBuilder; |
9 | 27 | import com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep; |
10 | | -import org.jetbrains.annotations.NotNull; |
11 | | -import picocli.CommandLine; |
12 | 28 |
|
13 | | -import java.lang.reflect.Constructor; |
14 | | -import java.nio.file.Path; |
15 | | -import java.util.List; |
16 | | -import java.util.Properties; |
| 29 | +import picocli.CommandLine; |
17 | 30 |
|
18 | | -@CommandLine.Command(name = "eclipse-wtp", description = "Runs Eclipse WTP formatter (" + EclipseWtp.ECLIPSE_WTP_VERSION+")") |
| 31 | +@CommandLine.Command( |
| 32 | + name = "eclipse-wtp", |
| 33 | + description = "Runs Eclipse WTP formatter (" + EclipseWtp.ECLIPSE_WTP_VERSION + ")") |
19 | 34 | public class EclipseWtp extends SpotlessFormatterStep { |
20 | 35 |
|
21 | 36 | public static final String ECLIPSE_WTP_VERSION = "4.21.0"; // TODO we need to slurp in the lock file also |
22 | 37 |
|
23 | 38 | @CommandLine.Option( |
24 | 39 | names = {"-f", "--config-file"}, |
25 | 40 | arity = "0", |
26 | | - description = "The path to the Eclipse WTP configuration file.\n" + |
27 | | - "For supported config file options see <https://github.com/diffplug/spotless/tree/main/plugin-gradle#eclipse-web-tools-platform>" |
28 | | - ) |
| 41 | + description = |
| 42 | + "The path to the Eclipse WTP configuration file.\n" |
| 43 | + + "For supported config file options see <https://github.com/diffplug/spotless/tree/main/plugin-gradle#eclipse-web-tools-platform>") |
29 | 44 | List<Path> configFiles; |
30 | 45 |
|
31 | 46 | @CommandLine.Option( |
32 | | - names = { "-t", "--type" }, |
| 47 | + names = {"-t", "--type"}, |
33 | 48 | description = "The type of the Eclipse WTP formatter." + OptionConstants.VALID_VALUES_SUFFIX, |
34 | | - required = true |
35 | | - ) |
36 | | - Type type; |
| 49 | + required = true) |
| 50 | + Type type; // TODO: might trying inferring type |
37 | 51 |
|
38 | 52 | public enum Type { |
39 | | - CSS, |
40 | | - HTML, |
41 | | - JS, |
42 | | - JSON, |
43 | | - XML, |
44 | | - XHTML; |
| 53 | + CSS(EclipseWtpFormatterStep.CSS), |
| 54 | + HTML(EclipseWtpFormatterStep.HTML), |
| 55 | + JS(EclipseWtpFormatterStep.JS), |
| 56 | + JSON(EclipseWtpFormatterStep.JSON), |
| 57 | + XML(EclipseWtpFormatterStep.XML), |
| 58 | + XHTML(EclipseWtpFormatterStep.HTML); // XHTML is treated as HTML in Eclipse WTP |
45 | 59 |
|
46 | | - public void initCorrespondingEclipseWtpCorePlugin() { |
47 | | - // due to internals of eclipse wtp, invoking various variants of the formatter in the same java process |
48 | | - // requires some static initialization to take place |
| 60 | + private final EclipseWtpFormatterStep backendEclipseWtpType; |
49 | 61 |
|
| 62 | + Type(EclipseWtpFormatterStep backendEclipseWtpType) { |
| 63 | + this.backendEclipseWtpType = backendEclipseWtpType; |
50 | 64 | } |
51 | 65 |
|
52 | 66 | public @NotNull EclipseWtpFormatterStep toEclipseWtpType() { |
53 | | - EclipseWtpFormatterStep step = switch (this) { |
54 | | - case CSS -> EclipseWtpFormatterStep.CSS; |
55 | | - case HTML, XHTML -> EclipseWtpFormatterStep.HTML; // XHTML is treated as HTML in Eclipse WTP |
56 | | - case JS -> EclipseWtpFormatterStep.JS; |
57 | | - case JSON -> EclipseWtpFormatterStep.JSON; |
58 | | - case XML -> EclipseWtpFormatterStep.XML; |
59 | | - }; |
60 | | - |
61 | | - @SuppressWarnings("unused") Class<?> reverseLookup = lookupClass(step); |
62 | | - return step; |
63 | | - } |
64 | | - |
65 | | - static Class<?> lookupClass(EclipseWtpFormatterStep step) { |
66 | | - /* this statement is only here to |
67 | | - 1) get compile errors in case a new type is added in EclipseWtpFormatterStep |
68 | | - 2) allow graal to collect reflective Metadata*/ |
69 | | - @SuppressWarnings("unused") Class<?> reverseLookup = switch(step) { |
70 | | - case CSS -> EclipseWtpMetadata.cssClass(); |
71 | | - case HTML -> EclipseWtpMetadata.htmlClass(); |
72 | | - case JS -> EclipseWtpMetadata.jsClass(); |
73 | | - case JSON -> EclipseWtpMetadata.jsonClass(); |
74 | | - case XML -> EclipseWtpMetadata.xmlClass(); |
75 | | - }; |
76 | | - return reverseLookup; |
| 67 | + return this.backendEclipseWtpType; |
77 | 68 | } |
78 | 69 | } |
79 | 70 |
|
80 | | - |
81 | 71 | @Override |
82 | 72 | public @NotNull List<FormatterStep> prepareFormatterSteps(SpotlessActionContext context) { |
83 | 73 | EclipseWtpFormatterStep wtpType = type.toEclipseWtpType(); |
84 | 74 | EclipseBasedStepBuilder builder = wtpType.createBuilder(context.provisioner()); |
85 | 75 | builder.setVersion(ECLIPSE_WTP_VERSION); |
86 | 76 | if (configFiles != null && !configFiles.isEmpty()) { |
87 | | - builder.setPreferences( |
88 | | - configFiles.stream() |
89 | | - .map(context::resolvePath) |
90 | | - .map(Path::toFile) |
91 | | - .toList()); |
| 77 | + builder.setPreferences(configFiles.stream() |
| 78 | + .map(context::resolvePath) |
| 79 | + .map(Path::toFile) |
| 80 | + .toList()); |
92 | 81 | } |
93 | 82 | return List.of(builder.build()); |
94 | 83 | } |
95 | | - |
96 | | - |
97 | | - static class EclipseWtpMetadata { |
98 | | - |
99 | | - private static Class<?> cssClass() { |
100 | | - return ThrowingEx.get(() -> Class.forName("com.diffplug.spotless.extra.eclipse.wtp.EclipseCssFormatterStepImpl")); |
101 | | - } |
102 | | - |
103 | | - private static Class<?> htmlClass() { |
104 | | - return ThrowingEx.get(() -> { |
105 | | - Class<?> result = Class.forName("com.diffplug.spotless.extra.eclipse.wtp.EclipseHtmlFormatterStepImpl"); |
106 | | - Constructor<?> declaredConstructor = result.getDeclaredConstructor(Properties.class); |
107 | | - return result; |
108 | | - }); |
109 | | - } |
110 | | - |
111 | | - private static Class<?> jsClass() { |
112 | | - return ThrowingEx.get(() -> Class.forName("com.diffplug.spotless.extra.eclipse.wtp.EclipseJsFormatterStepImpl")); |
113 | | - } |
114 | | - |
115 | | - private static Class<?> jsonClass() { |
116 | | - return ThrowingEx.get(() -> Class.forName("com.diffplug.spotless.extra.eclipse.wtp.EclipseJsonFormatterStepImpl")); |
117 | | - } |
118 | | - |
119 | | - private static Class<?> xmlClass() { |
120 | | - return ThrowingEx.get(() -> Class.forName("com.diffplug.spotless.extra.eclipse.wtp.EclipseXmlFormatterStepImpl")); |
121 | | - } |
122 | | - } |
123 | 84 | } |
0 commit comments