LazyFile has no job now, jar

This commit is contained in:
Matthew Kennedy 2023-03-15 11:53:32 -07:00
parent e4c4da2b20
commit 197cf8bb11
2 changed files with 14 additions and 57 deletions

View File

@ -9,77 +9,34 @@ import java.util.regex.Pattern;
* This file would override file content only of content has changed, disregarding the magic tag line. * This file would override file content only of content has changed, disregarding the magic tag line.
*/ */
public class LazyFile implements Output { public class LazyFile implements Output {
public static final String LAZY_FILE_TAG = "was generated automatically by rusEFI tool ";
public static final String LAZY_FILE_TAG_LOWER = LAZY_FILE_TAG.toLowerCase();
public static String TEST = "test_file_name"; public static String TEST = "test_file_name";
private final String filename; private Writer fw = null;
private final StringBuffer content = new StringBuffer();
private final StringBuffer contentWithoutTag = new StringBuffer();
public LazyFile(String filename) { public LazyFile(String filename) {
this.filename = filename; if (!TEST.equals(filename)) {
try {
fw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename), IoUtils.CHARSET));
} catch (IOException e) {
// ignore?
}
}
} }
@Override @Override
public void write(String line) { public void write(String line) {
content.append(line); if (fw != null) {
String[] lines = line.split("\\r?\\n"); try {
for (String subLine : lines) { fw.write(line);
if (!subLine.toLowerCase().contains(LAZY_FILE_TAG_LOWER)) { } catch (IOException e) {
contentWithoutTag.append(subLine);
} }
} }
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (TEST.equals(filename)) if (fw != null) {
return;
String fileContent = unifySpaces(readCurrentContent(filename));
String newContent = unifySpaces(contentWithoutTag.toString());
if (fileContent.equals(newContent)) {
SystemOut.println(getClass().getSimpleName() + ": Not updating " + filename + " since looks to be the same content, new content size=" + contentWithoutTag.length());
return;
}
for (int i = 0; i < Math.min(fileContent.length(), newContent.length()); i++) {
if (fileContent.charAt(i) != newContent.charAt(i)) {
SystemOut.println(getClass().getSimpleName() + " " + filename + ": Not same at " + i);
if (i > 15 && i < fileContent.length() - 6 && i < newContent.length() - 6) {
SystemOut.println("file " + fileContent.substring(i - 15, i + 5));
SystemOut.println("newContent " + newContent.substring(i - 15, i + 5));
}
break;
}
}
Writer fw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename), IoUtils.CHARSET));
fw.write(content.toString());
fw.close(); fw.close();
} }
public static String unifySpaces(String line) {
line = line.replace("\r", "");
return line.replaceAll("\n[\n]*", "");
}
private String readCurrentContent(String filename) throws IOException {
if (TEST.equals(filename))
return "";
if (!new File(filename).exists()) {
SystemOut.println(filename + " does not exist considering empty current content");
return "";
}
Scanner in = new Scanner(Paths.get(filename), IoUtils.CHARSET.name());
Pattern pat = Pattern.compile(".*\\R|.+\\z");
String line;
StringBuilder sb = new StringBuilder();
while ((line = in.findWithinHorizon(pat, 0)) != null) {
if (!line.toLowerCase().contains(LAZY_FILE_TAG_LOWER))
sb.append(line);
}
return sb.toString();
} }
} }

Binary file not shown.