Only rewrite record file if it changed.
This commit is contained in:
@@ -5,17 +5,18 @@
|
|||||||
package com.samskivert.depot.tools;
|
package com.samskivert.depot.tools;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.GenericArrayType;
|
import java.lang.reflect.GenericArrayType;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -28,6 +29,7 @@ import com.google.common.collect.ImmutableMap;
|
|||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
|
import com.google.common.io.Files;
|
||||||
|
|
||||||
import com.samskivert.depot.PersistentRecord;
|
import com.samskivert.depot.PersistentRecord;
|
||||||
import com.samskivert.depot.annotation.GeneratedValue;
|
import com.samskivert.depot.annotation.GeneratedValue;
|
||||||
@@ -120,17 +122,13 @@ public abstract class GenRecord
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// slurp our source file into newline separated strings
|
// read the source file and break it into lines
|
||||||
|
Charset charset = Charset.defaultCharset(); // TODO?
|
||||||
|
String sourceText;
|
||||||
String[] lines = null;
|
String[] lines = null;
|
||||||
try {
|
try {
|
||||||
BufferedReader bin = new BufferedReader(new FileReader(source));
|
sourceText = Files.toString(source, charset);
|
||||||
List<String> llist = Lists.newArrayList();
|
lines = Files.readLines(source, charset).toArray(new String[0]);
|
||||||
String line = null;
|
|
||||||
while ((line = bin.readLine()) != null) {
|
|
||||||
llist.add(line);
|
|
||||||
}
|
|
||||||
lines = llist.toArray(new String[llist.size()]);
|
|
||||||
bin.close();
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
logWarn("Error reading '" + source + "'", ioe);
|
logWarn("Error reading '" + source + "'", ioe);
|
||||||
return;
|
return;
|
||||||
@@ -258,49 +256,53 @@ public abstract class GenRecord
|
|||||||
}
|
}
|
||||||
|
|
||||||
// now bolt everything back together into a class declaration
|
// now bolt everything back together into a class declaration
|
||||||
try {
|
StringWriter out = new StringWriter();
|
||||||
BufferedWriter bout = new BufferedWriter(new FileWriter(source));
|
PrintWriter pout = new PrintWriter(out);
|
||||||
for (int ii = 0; ii < nstart; ii++) {
|
for (int ii = 0; ii < nstart; ii++) {
|
||||||
writeln(bout, lines[ii]);
|
pout.println(lines[ii]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fsection.length() > 0) {
|
if (fsection.length() > 0) {
|
||||||
String prev = get(lines, nstart-1);
|
String prev = get(lines, nstart-1);
|
||||||
if (!StringUtil.isBlank(prev) && !prev.equals("{")) {
|
if (!StringUtil.isBlank(prev) && !prev.equals("{")) {
|
||||||
bout.newLine();
|
pout.println();
|
||||||
}
|
}
|
||||||
writeln(bout, " " + FIELDS_START);
|
pout.println(" " + FIELDS_START);
|
||||||
bout.write(fsection.toString());
|
pout.write(fsection.toString());
|
||||||
writeln(bout, " " + FIELDS_END);
|
pout.println(" " + FIELDS_END);
|
||||||
if (!StringUtil.isBlank(get(lines, nend))) {
|
if (!StringUtil.isBlank(get(lines, nend))) {
|
||||||
bout.newLine();
|
pout.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int ii = nend; ii < mstart; ii++) {
|
for (int ii = nend; ii < mstart; ii++) {
|
||||||
writeln(bout, lines[ii]);
|
pout.println(lines[ii]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msection.length() > 0) {
|
if (msection.length() > 0) {
|
||||||
if (!StringUtil.isBlank(get(lines, mstart-1))) {
|
if (!StringUtil.isBlank(get(lines, mstart-1))) {
|
||||||
bout.newLine();
|
pout.println();
|
||||||
}
|
}
|
||||||
writeln(bout, " " + METHODS_START);
|
pout.println(" " + METHODS_START);
|
||||||
bout.write(msection.toString());
|
pout.write(msection.toString());
|
||||||
writeln(bout, " " + METHODS_END);
|
pout.println(" " + METHODS_END);
|
||||||
String next = get(lines, mend);
|
String next = get(lines, mend);
|
||||||
if (!StringUtil.isBlank(next) && !next.equals("}")) {
|
if (!StringUtil.isBlank(next) && !next.equals("}")) {
|
||||||
bout.newLine();
|
pout.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int ii = mend; ii < lines.length; ii++) {
|
for (int ii = mend; ii < lines.length; ii++) {
|
||||||
writeln(bout, lines[ii]);
|
pout.println(lines[ii]);
|
||||||
}
|
}
|
||||||
|
|
||||||
bout.close();
|
String newSourceText = out.toString();
|
||||||
} catch (IOException ioe) {
|
if (!sourceText.equals(newSourceText)) {
|
||||||
logWarn("Error writing to '" + source + "'", ioe);
|
try {
|
||||||
|
Files.write(newSourceText, source, charset);
|
||||||
|
logInfo("Regenerated '" + source + "'");
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
logWarn("Error writing to '" + source + "'", ioe);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logInfo("Processed '" + source + "'");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -334,14 +336,6 @@ public abstract class GenRecord
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Helper function for writing a string and a newline to a writer. */
|
|
||||||
protected void writeln (BufferedWriter bout, String line)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
bout.write(line);
|
|
||||||
bout.newLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Helper function for generating our boilerplate code. */
|
/** Helper function for generating our boilerplate code. */
|
||||||
protected String mergeTemplate (String tmpl, Map<String, String> subs)
|
protected String mergeTemplate (String tmpl, Map<String, String> subs)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user