mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
FIle masks option (-m,-mask)
This commit is contained in:
@@ -43,6 +43,7 @@ public class FileSetFormatterTest extends LightPlatformTestCase {
|
||||
MessageOutput messageOutput = new MessageOutput(new PrintWriter(System.out), new PrintWriter(System.err));
|
||||
FileSetFormatter formatter = new FileSetFormatter(messageOutput);
|
||||
formatter.addEntry(fileSpec);
|
||||
formatter.addFileMask("*.java");
|
||||
formatter.setRecursive();
|
||||
formatter.setCodeStyleSettings(settings);
|
||||
formatter.processFiles();
|
||||
|
||||
@@ -18,7 +18,7 @@ package com.intellij.formatting.commandLine;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.util.containers.hash.HashSet;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
@@ -28,7 +28,8 @@ import java.util.Set;
|
||||
public abstract class FileSetProcessor {
|
||||
private static final Logger LOG = Logger.getInstance("#" + FileSetProcessor.class.getName());
|
||||
|
||||
private Set<File> myTopEntries = new HashSet<>();
|
||||
private Set<File> myTopEntries = ContainerUtil.newHashSet();
|
||||
private Set<String> myFileMasks = ContainerUtil.newHashSet();
|
||||
private int myProcessedFiles;
|
||||
private boolean isRecursive;
|
||||
|
||||
@@ -42,6 +43,21 @@ public abstract class FileSetProcessor {
|
||||
isRecursive = true;
|
||||
}
|
||||
|
||||
public void addFileMask(@NotNull String fileMask) {
|
||||
String fileMaskRegexp = fileMaskToRegexp(fileMask);
|
||||
LOG.info("File mask regexp: " + fileMaskRegexp);
|
||||
myFileMasks.add(fileMaskRegexp);
|
||||
}
|
||||
|
||||
private static String fileMaskToRegexp(@NotNull String fileMask) {
|
||||
return
|
||||
fileMask
|
||||
.replace(".", "\\.")
|
||||
.replace("*", ".*")
|
||||
.replace("?", ".")
|
||||
.replace("+", "\\+");
|
||||
}
|
||||
|
||||
public void addEntry(@NotNull String filePath) throws IOException {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
@@ -64,17 +80,28 @@ public abstract class FileSetProcessor {
|
||||
}
|
||||
}
|
||||
else {
|
||||
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(entry);
|
||||
if (virtualFile == null) {
|
||||
throw new IOException("Can not find " + entry.getPath());
|
||||
if (matchesFileMask(entry.getName())) {
|
||||
VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(entry);
|
||||
if (virtualFile == null) {
|
||||
throw new IOException("Can not find " + entry.getPath());
|
||||
}
|
||||
LOG.info("Processing " + virtualFile.getPath());
|
||||
processFile(virtualFile);
|
||||
myProcessedFiles++;
|
||||
}
|
||||
LOG.info("Processing " + virtualFile.getPath());
|
||||
processFile(virtualFile);
|
||||
myProcessedFiles++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean matchesFileMask(@NotNull String name) {
|
||||
for (String fileMask : myFileMasks) {
|
||||
if (name.matches(fileMask)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected abstract void processFile(@NotNull VirtualFile virtualFile);
|
||||
|
||||
public int getProcessedFiles() {
|
||||
|
||||
@@ -75,7 +75,7 @@ public class FormatterStarter extends ApplicationStarterEx {
|
||||
//noinspection AssignmentToForLoopParameter
|
||||
i ++;
|
||||
if (i >= args.length) {
|
||||
fatalError(messageOutput, "Mising settings file path.");
|
||||
fatalError(messageOutput, "Missing settings file path.");
|
||||
}
|
||||
try {
|
||||
CodeStyleSettings settings = readSettings(args[i]);
|
||||
@@ -88,6 +88,18 @@ public class FormatterStarter extends ApplicationStarterEx {
|
||||
else if (checkOption(args[i], "-r", "-R")) {
|
||||
fileSetFormatter.setRecursive();
|
||||
}
|
||||
else if (checkOption(args[i], "-m", "-mask")) {
|
||||
//noinspection AssignmentToForLoopParameter
|
||||
i ++;
|
||||
if (i >= args.length) {
|
||||
fatalError(messageOutput, "Missing file mask(s).");
|
||||
}
|
||||
for (String mask : args[i].split(",")) {
|
||||
if (!mask.isEmpty()) {
|
||||
fileSetFormatter.addFileMask(mask);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
fatalError(messageOutput, "Unknown option " + args[i]);
|
||||
}
|
||||
@@ -143,6 +155,7 @@ public class FormatterStarter extends ApplicationStarterEx {
|
||||
messageOutput.info(" -h|-help Show a help message and exit.");
|
||||
messageOutput.info(" -s|-settings A path to Intellij IDEA code style settings .xml file.\n");
|
||||
messageOutput.info(" -r|-R Scan directories recursively.\n");
|
||||
messageOutput.info(" -m|-mask A comma-separated list of file masks.");
|
||||
messageOutput.info(" path<n> A path to a file or a directory.\n");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user