in cmdline/tool/src/org/netbeans/modules/jackpot30/cmdline/Main.java [139:368]
public static int compile(String... args) throws IOException, ClassNotFoundException {
try {
Class.forName("javax.lang.model.element.ModuleElement");
} catch (ClassNotFoundException ex) {
System.err.println("Error: no suitable javac found, please run on JDK 11+.");
return 1;
}
System.setProperty("netbeans.user", "/tmp/tmp-foo");
System.setProperty("SourcePath.no.source.filter", "true");
OptionParser parser = new OptionParser();
// ArgumentAcceptingOptionSpec<File> projects = parser.accepts("project", "project(s) to refactor").withRequiredArg().withValuesSeparatedBy(File.pathSeparatorChar).ofType(File.class);
GroupOptions globalGroupOptions = setupGroupParser(parser);
ArgumentAcceptingOptionSpec<File> cache = parser.accepts("cache", "a cache directory to store working data").withRequiredArg().ofType(File.class);
ArgumentAcceptingOptionSpec<File> out = parser.accepts("out", "output diff").withRequiredArg().ofType(File.class);
ArgumentAcceptingOptionSpec<File> configFile = parser.accepts("config-file", "configuration file").withRequiredArg().ofType(File.class);
ArgumentAcceptingOptionSpec<String> hint = parser.accepts("hint", "hint name").withRequiredArg().ofType(String.class);
ArgumentAcceptingOptionSpec<String> config = parser.accepts("config", "configurations").withRequiredArg().ofType(String.class);
ArgumentAcceptingOptionSpec<File> hintFile = parser.accepts("hint-file", "file with rules that should be performed").withRequiredArg().ofType(File.class);
ArgumentAcceptingOptionSpec<String> group = parser.accepts("group", "specify roots to process alongside with their classpath").withRequiredArg().ofType(String.class);
ArgumentAcceptingOptionSpec<File> patchFile = parser.accepts("filter-patch", "patch file, which will be used to filter the output").withRequiredArg().ofType(File.class);
parser.accepts("list", "list all known hints");
parser.accepts("progress", "show progress");
parser.accepts("debug", "enable debugging loggers");
parser.accepts("help", "prints this help");
parser.accepts(OPTION_NO_APPLY, "do not apply changes - only print locations were the hint would be applied");
parser.accepts(OPTION_APPLY, "apply changes");
// parser.accepts("show-gui", "show configuration dialog");
parser.accepts(OPTION_FAIL_ON_WARNINGS, "fail when warnings are detected");
parser.accepts(RUN_TESTS, "run tests for declarative rules that were used");
OptionSet parsed;
try {
parsed = parser.parse(inlineParameterFiles(args));
} catch (OptionException ex) {
System.err.println(ex.getLocalizedMessage());
parser.printHelpOn(System.out);
return 1;
}
if (!parsed.has("debug")) {
prepareLoggers();
}
if (parsed.has("help")) {
parser.printHelpOn(System.out);
return 0;
}
List<FileObject> roots = new ArrayList<FileObject>();
List<Folder> rootFolders = new ArrayList<Folder>();
for (String sr : parsed.nonOptionArguments()) {
File r = new File(sr);
FileObject root = FileUtil.toFileObject(r);
if (root != null) {
roots.add(root);
rootFolders.add(new Folder(root));
}
}
final List<RootConfiguration> groups = new ArrayList<>();
groups.add(new RootConfiguration(parsed, globalGroupOptions));
for (String groupValue : parsed.valuesOf(group)) {
OptionParser groupParser = new OptionParser();
GroupOptions groupOptions = setupGroupParser(groupParser);
OptionSet parsedGroup = groupParser.parse(splitGroupArg(groupValue));
groups.add(new RootConfiguration(parsedGroup, groupOptions));
}
if (parsed.has("show-gui")) {
if (parsed.has(configFile)) {
final File settingsFile = parsed.valueOf(configFile);
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override public void run() {
try {
Pair<ClassPath, ClassPath> sourceAndBinaryCP = jointSourceAndBinaryCP(groups);
showGUICustomizer(settingsFile, sourceAndBinaryCP.second(), sourceAndBinaryCP.first());
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} catch (BackingStoreException ex) {
Exceptions.printStackTrace(ex);
}
}
});
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (InvocationTargetException ex) {
Exceptions.printStackTrace(ex);
}
return 0;
} else {
System.err.println("show-gui requires config-file");
return 1;
}
}
File cacheDir = parsed.valueOf(cache);
boolean deleteCacheDir = false;
try {
if (cacheDir == null) {
cacheDir = File.createTempFile("jackpot", "cache");
cacheDir.delete();
if (!(deleteCacheDir = cacheDir.mkdirs())) {
System.err.println("cannot create temporary cache");
return 1;
}
}
if (cacheDir.isFile()) {
System.err.println("cache directory exists and is a file");
return 1;
}
String[] cacheDirContent = cacheDir.list();
if (cacheDirContent != null && cacheDirContent.length > 0 && !new File(cacheDir, "segments").exists()) {
System.err.println("cache directory is not empty, but was not created by this tool");
return 1;
}
cacheDir.mkdirs();
CacheFolder.setCacheFolder(FileUtil.toFileObject(FileUtil.normalizeFile(cacheDir)));
org.netbeans.api.project.ui.OpenProjects.getDefault().getOpenProjects();
RepositoryUpdater.getDefault().start(false);
if (parsed.has("list")) {
Pair<ClassPath, ClassPath> sourceAndBinaryCP = jointSourceAndBinaryCP(groups);
printHints(sourceAndBinaryCP.first(),
sourceAndBinaryCP.second());
return 0;
}
int totalGroups = 0;
for (RootConfiguration groupConfig : groups) {
if (!groupConfig.rootFolders.isEmpty()) totalGroups++;
}
ProgressHandleWrapper progress = parsed.has("progress") ? new ProgressHandleWrapper(new ConsoleProgressHandleAbstraction(), ProgressHandleWrapper.prepareParts(totalGroups)) : new ProgressHandleWrapper(1);
Preferences hintSettingsPreferences;
boolean apply;
boolean runDeclarative;
boolean runDeclarativeTests;
boolean useDefaultEnabledSetting;
if (parsed.has(configFile)) {
ToolPreferences toolPrefs = ToolPreferences.from(parsed.valueOf(configFile).toURI());
hintSettingsPreferences = toolPrefs.getPreferences("hints", "text/x-java");
Preferences toolSettings = toolPrefs.getPreferences("standalone", "text/x-java");
apply = toolSettings.getBoolean("apply", false);
runDeclarative = toolSettings.getBoolean("runDeclarative", true);
runDeclarativeTests = toolSettings.getBoolean("runDeclarativeTests", false);
useDefaultEnabledSetting = true; //TODO: read from the configuration file?
if (parsed.has(hint)) {
System.err.println("cannot specify --hint and --config-file together");
return 1;
} else if (parsed.has(hintFile)) {
System.err.println("cannot specify --hint-file and --config-file together");
return 1;
}
} else {
hintSettingsPreferences = null;
apply = false;
runDeclarative = true;
runDeclarativeTests = parsed.has(RUN_TESTS);
useDefaultEnabledSetting = false;
}
if (parsed.has(config) && !parsed.has(hint)) {
System.err.println("--config cannot specified when no hint is specified");
return 1;
}
if (parsed.has(OPTION_NO_APPLY)) {
apply = false;
} else if (parsed.has(OPTION_APPLY)) {
apply = true;
}
GroupResult result = GroupResult.NOTHING_TO_DO;
try (Writer outS = parsed.has(out) ? new BufferedWriter(new OutputStreamWriter(new FileOutputStream(parsed.valueOf(out)))) : null) {
GlobalConfiguration globalConfig = new GlobalConfiguration(hintSettingsPreferences, apply, runDeclarative, runDeclarativeTests, useDefaultEnabledSetting, parsed.valueOf(hint), parsed.valueOf(hintFile), parsed.valueOf(patchFile), outS, parsed.has(OPTION_FAIL_ON_WARNINGS));
for (RootConfiguration groupConfig : groups) {
result = result.join(handleGroup(groupConfig, progress, globalConfig, parsed.valuesOf(config)));
}
}
progress.finish();
if (result == GroupResult.NOTHING_TO_DO) {
System.err.println("no source roots to work on");
return 1;
}
if (result == GroupResult.NO_HINTS_FOUND) {
System.err.println("no hints specified");
return 1;
}
return result == GroupResult.SUCCESS ? 0 : 1;
} catch (Throwable e) {
e.printStackTrace();
throw new IllegalStateException(e);
} finally {
if (deleteCacheDir) {
FileObject cacheDirFO = FileUtil.toFileObject(cacheDir);
if (cacheDirFO != null) {
//TODO: would be better to do j.i.File.delete():
cacheDirFO.delete();
}
}
}
}