Skip to content

Commit c3ec06c

Browse files
authored
Merge pull request #52 from pmd/cli-compat
Better expose entry points for pmd-cli integration
2 parents be58f57 + 3936a63 commit c3ec06c

File tree

9 files changed

+109
-65
lines changed

9 files changed

+109
-65
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ buildNumber.properties
9595

9696
# End of https://www.gitignore.io/api/intellij,maven
9797

98+
### Eclipse ###
99+
.project
100+
.settings
101+
.classpath
102+
98103
.idea/*
99104
# Share run configurations for easier setup
100105
!/.idea/runConfigurations/

src/main/java/net/sourceforge/pmd/util/fxdesigner/Designer.java

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@
55
package net.sourceforge.pmd.util.fxdesigner;
66

77
import java.io.IOException;
8-
import java.io.InputStream;
98
import java.util.Arrays;
109
import java.util.List;
1110
import java.util.Objects;
12-
import java.util.Optional;
13-
import java.util.Properties;
1411
import java.util.logging.Level;
1512
import java.util.logging.Logger;
1613
import java.util.stream.Collectors;
1714

18-
import net.sourceforge.pmd.PMDVersion;
1915
import net.sourceforge.pmd.lang.ast.xpath.Attribute;
2016
import net.sourceforge.pmd.util.fxdesigner.app.DesignerParams;
2117
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRoot;
2218
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRootImpl;
2319
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
24-
import net.sourceforge.pmd.util.fxdesigner.util.ResourceUtil;
2520

2621
import javafx.application.Application;
2722
import javafx.application.Platform;
@@ -41,44 +36,6 @@
4136
*/
4237
public class Designer extends Application {
4338

44-
/**
45-
* Constant that contains always the current version of the designer.
46-
*/
47-
private static final String VERSION;
48-
private static final String PMD_CORE_MIN_VERSION;
49-
private static final String UNKNOWN_VERSION = "unknown";
50-
51-
52-
/**
53-
* Determines the version from maven's generated pom.properties file.
54-
*/
55-
static {
56-
VERSION = readProperty("/META-INF/maven/net.sourceforge.pmd/pmd-ui/pom.properties", "version").orElse(UNKNOWN_VERSION);
57-
PMD_CORE_MIN_VERSION = readProperty(ResourceUtil.resolveResource("designer.properties"), "pmd.core.version").orElse(UNKNOWN_VERSION);
58-
}
59-
60-
61-
public static String getCurrentVersion() {
62-
return VERSION;
63-
}
64-
65-
public static String getPmdCoreMinVersion() {
66-
return PMD_CORE_MIN_VERSION;
67-
}
68-
69-
private static Optional<String> readProperty(String resourcePath, String key) {
70-
try (InputStream stream = PMDVersion.class.getResourceAsStream(resourcePath)) {
71-
if (stream != null) {
72-
final Properties properties = new Properties();
73-
properties.load(stream);
74-
return Optional.ofNullable(properties.getProperty(key));
75-
}
76-
} catch (final IOException ignored) {
77-
// fallthrough
78-
}
79-
return Optional.empty();
80-
}
81-
8239
private long initStartTimeMillis;
8340
private DesignerRoot designerRoot;
8441

@@ -101,7 +58,7 @@ public void stop() {
10158
public void start(Stage stage, DesignerRoot owner) throws IOException {
10259
this.designerRoot = owner;
10360

104-
stage.setTitle("PMD Rule Designer (v " + Designer.VERSION + ')');
61+
stage.setTitle("PMD Rule Designer (v " + DesignerVersion.getCurrentVersion() + ')');
10562
setIcons(stage);
10663

10764
Logger.getLogger(Attribute.class.getName()).setLevel(Level.OFF);

src/main/java/net/sourceforge/pmd/util/fxdesigner/DesignerStarter.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import org.apache.commons.lang3.SystemUtils;
1313

14+
import net.sourceforge.pmd.annotation.InternalApi;
15+
1416
import com.beust.jcommander.JCommander;
1517
import com.beust.jcommander.ParameterException;
1618
import javafx.application.Application;
@@ -32,9 +34,6 @@ public final class DesignerStarter {
3234

3335
private static final int MIN_JAVAFX_VERSION_ON_MAC_OSX = 14;
3436

35-
private static final int ERROR_EXIT = 1;
36-
private static final int OK = 0;
37-
3837
private DesignerStarter() {
3938
}
4039

@@ -47,7 +46,7 @@ private static boolean isJavaFxAvailable() {
4746
}
4847
}
4948

50-
49+
@Deprecated
5150
private static MainCliArgs readParameters(String[] argv) {
5251

5352
MainCliArgs argsObj = new MainCliArgs();
@@ -59,7 +58,7 @@ private static MainCliArgs readParameters(String[] argv) {
5958

6059
if (argsObj.help) {
6160
System.out.println(getHelpText(jCommander));
62-
System.exit(OK);
61+
System.exit(ExitStatus.OK.getCode());
6362
}
6463

6564
return argsObj;
@@ -68,19 +67,24 @@ private static MainCliArgs readParameters(String[] argv) {
6867
System.out.println(e.getMessage());
6968
System.out.println();
7069
System.out.println(getHelpText(jCommander));
71-
System.exit(OK);
70+
System.exit(ExitStatus.OK.getCode());
7271
throw new AssertionError();
7372
}
7473

7574

7675
}
7776

78-
77+
/**
78+
* Starting from PMD 7.0.0 this method usage will be limited for development.
79+
* CLI support will be provided by pmd-cli
80+
*/
81+
@InternalApi
7982
public static void main(String[] args) {
8083

8184
readParameters(args);
8285

83-
launchGui(args);
86+
final ExitStatus ret = launchGui(args);
87+
System.exit(ret.getCode());
8488
}
8589

8690
private static void setSystemProperties() {
@@ -118,6 +122,7 @@ private static String getJavaFxVersion() {
118122
return null;
119123
}
120124

125+
@Deprecated
121126
private static String getHelpText(JCommander jCommander) {
122127

123128
StringBuilder sb = new StringBuilder();
@@ -139,7 +144,7 @@ private static String getHelpText(JCommander jCommander) {
139144
}
140145

141146
@SuppressWarnings("PMD.AvoidCatchingThrowable")
142-
private static void launchGui(String[] args) {
147+
public static ExitStatus launchGui(String[] args) {
143148
setSystemProperties();
144149

145150
String message = null;
@@ -152,14 +157,31 @@ private static void launchGui(String[] args) {
152157
if (message != null) {
153158
System.err.println(message);
154159
JOptionPane.showMessageDialog(null, message);
155-
System.exit(ERROR_EXIT);
160+
return ExitStatus.ERROR;
156161
}
157162

158163
try {
159164
Application.launch(Designer.class, args);
160165
} catch (Throwable unrecoverable) {
161166
unrecoverable.printStackTrace();
162-
System.exit(ERROR_EXIT);
167+
return ExitStatus.ERROR;
168+
}
169+
170+
return ExitStatus.OK;
171+
}
172+
173+
public enum ExitStatus {
174+
OK(0),
175+
ERROR(1);
176+
177+
private final int code;
178+
179+
ExitStatus(final int code) {
180+
this.code = code;
181+
}
182+
183+
public int getCode() {
184+
return code;
163185
}
164186
}
165187
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
package net.sourceforge.pmd.util.fxdesigner;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.util.Optional;
10+
import java.util.Properties;
11+
12+
import net.sourceforge.pmd.PMDVersion;
13+
import net.sourceforge.pmd.util.fxdesigner.util.ResourceUtil;
14+
15+
/**
16+
* Stores the current Designer and PMD version and provides utility methods around them.
17+
*/
18+
public final class DesignerVersion {
19+
20+
/**
21+
* Constant that contains always the current version of the designer.
22+
*/
23+
private static final String VERSION;
24+
private static final String PMD_CORE_MIN_VERSION;
25+
private static final String UNKNOWN_VERSION = "unknown";
26+
27+
/**
28+
* Determines the version from maven's generated pom.properties file.
29+
*/
30+
static {
31+
VERSION = readProperty("/META-INF/maven/net.sourceforge.pmd/pmd-ui/pom.properties", "version").orElse(UNKNOWN_VERSION);
32+
PMD_CORE_MIN_VERSION = readProperty(ResourceUtil.resolveResource("designer.properties"), "pmd.core.version").orElse(UNKNOWN_VERSION);
33+
}
34+
35+
private DesignerVersion() {
36+
throw new AssertionError("Can't instantiate a utility class.");
37+
}
38+
39+
public static String getCurrentVersion() {
40+
return VERSION;
41+
}
42+
43+
public static String getPmdCoreMinVersion() {
44+
return PMD_CORE_MIN_VERSION;
45+
}
46+
47+
private static Optional<String> readProperty(String resourcePath, String key) {
48+
try (InputStream stream = PMDVersion.class.getResourceAsStream(resourcePath)) {
49+
if (stream != null) {
50+
final Properties properties = new Properties();
51+
properties.load(stream);
52+
return Optional.ofNullable(properties.getProperty(key));
53+
}
54+
} catch (final IOException ignored) {
55+
// fallthrough
56+
}
57+
return Optional.empty();
58+
}
59+
}

src/main/java/net/sourceforge/pmd/util/fxdesigner/MainCliArgs.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
import com.beust.jcommander.Parameter;
88

9+
@Deprecated
910
final class MainCliArgs {
1011

1112

12-
@Parameter(names = {"--verbose", "-v"},
13+
@Parameter(names = {"--verbose", "-v", "-D", "--debug"},
1314
arity = 0,
1415
description = "Whether to launch the app in verbose mode. "
1516
+ "This enables logging of exception stack traces and "

src/main/java/net/sourceforge/pmd/util/fxdesigner/app/DesignerParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public DesignerParams(String... args) {
4242
public DesignerParams(Parameters params) {
4343
List<String> raw = params.getRaw();
4444
// error output is disabled by default
45-
if (raw.contains("-v") || raw.contains("--verbose")) {
45+
if (raw.contains("-v") || raw.contains("--verbose") || raw.contains("--debug") || raw.contains("-D")) {
4646
isDeveloperMode = true;
4747
}
4848

src/main/java/net/sourceforge/pmd/util/fxdesigner/app/services/GlobalDiskManagerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.List;
1212
import java.util.stream.Collectors;
1313

14-
import net.sourceforge.pmd.util.fxdesigner.Designer;
14+
import net.sourceforge.pmd.util.fxdesigner.DesignerVersion;
1515
import net.sourceforge.pmd.util.fxdesigner.app.ApplicationComponent;
1616
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRoot;
1717

@@ -33,7 +33,7 @@ public GlobalDiskManagerImpl(DesignerRoot root, Path settingsDirectory) {
3333
this.settingsDirectory = settingsDirectory;
3434

3535

36-
Path curVersionStamp = settingsDirectory.resolve("version-" + Designer.getCurrentVersion());
36+
Path curVersionStamp = settingsDirectory.resolve("version-" + DesignerVersion.getCurrentVersion());
3737

3838

3939
List<Path> diskVersionStamps = getDiskVersionStamps();
@@ -62,7 +62,7 @@ private List<Path> getDiskVersionStamps() {
6262
.filter(it -> !Files.isDirectory(it))
6363
.filter(it -> it.getFileName().toString().startsWith(STAMP_PREFIX))
6464
.collect(Collectors.toList());
65-
} catch (IOException e) {
65+
} catch (IOException ignored) {
6666
return Collections.emptyList();
6767
}
6868
}

src/main/java/net/sourceforge/pmd/util/fxdesigner/app/services/OnDiskPersistenceManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.nio.file.Path;
1010
import java.util.concurrent.CompletableFuture;
1111

12-
import net.sourceforge.pmd.util.fxdesigner.Designer;
12+
import net.sourceforge.pmd.util.fxdesigner.DesignerVersion;
1313
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRoot;
1414
import net.sourceforge.pmd.util.fxdesigner.util.beans.SettingsOwner;
1515
import net.sourceforge.pmd.util.fxdesigner.util.beans.SettingsPersistenceUtil;
@@ -85,7 +85,7 @@ private void commitAppState() throws IOException, InterruptedException {
8585
process.start().waitFor();
8686
process.command("git", "add", output.toString());
8787
process.start().waitFor();
88-
process.command("git", "commit", "-m", "\"On version " + Designer.getCurrentVersion() + "\"");
88+
process.command("git", "commit", "-m", "\"On version " + DesignerVersion.getCurrentVersion() + "\"");
8989
process.start().waitFor();
9090

9191
}

src/main/java/net/sourceforge/pmd/util/fxdesigner/popups/SimplePopups.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import net.sourceforge.pmd.PMDVersion;
2323
import net.sourceforge.pmd.lang.Language;
24-
import net.sourceforge.pmd.util.fxdesigner.Designer;
24+
import net.sourceforge.pmd.util.fxdesigner.DesignerVersion;
2525
import net.sourceforge.pmd.util.fxdesigner.app.DesignerRoot;
2626
import net.sourceforge.pmd.util.fxdesigner.util.AuxLanguageRegistry;
2727
import net.sourceforge.pmd.util.fxdesigner.util.DesignerUtil;
@@ -213,8 +213,8 @@ public static void showAboutPopup(DesignerRoot root) {
213213

214214
String sb =
215215
"PMD core version:\t\t\t" + PMDVersion.VERSION + "\n"
216-
+ "Designer version:\t\t\t" + Designer.getCurrentVersion()
217-
+ " (supports PMD core " + Designer.getPmdCoreMinVersion() + ")\n"
216+
+ "Designer version:\t\t\t" + DesignerVersion.getCurrentVersion()
217+
+ " (supports PMD core " + DesignerVersion.getPmdCoreMinVersion() + ")\n"
218218
+ "Designer settings dir:\t\t"
219219
+ root.getService(DesignerRoot.DISK_MANAGER).getSettingsDirectory() + "\n"
220220
+ "Available languages:\t\t"

0 commit comments

Comments
 (0)