generators: don't write to files if content is not changed

This commit is contained in:
Leonid Shalupov
2013-03-28 20:34:37 +04:00
parent 7bac3c5db6
commit 5ee5c841cb
3 changed files with 58 additions and 32 deletions
@@ -19,7 +19,6 @@ package org.jetbrains.jet.di;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -53,21 +52,6 @@ public class DependencyInjectorGenerator {
File file = new File(outputFileName);
// Windows prohibits rename of open files by default.
// http://teamcity.jetbrains.com/viewLog.html?buildId=62376&buildTypeId=bt345&tab=buildLog
boolean useTmpfile = !SystemInfo.isWindows;
File tmpfile = useTmpfile ? new File(outputFileName + ".tmp") : file;
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
if (parentFile.mkdirs()) {
System.out.println("Directory created: " + parentFile.getAbsolutePath());
}
else {
throw new IllegalStateException("Cannot create directory: " + parentFile);
}
}
fields.addAll(dependencies.satisfyDependencies());
@@ -105,15 +89,7 @@ public class DependencyInjectorGenerator {
text.append(imports);
text.append(body);
FileUtil.writeToFile(tmpfile, text.toString());
System.out.println("File written: " + tmpfile.getAbsolutePath());
if (useTmpfile) {
if (!tmpfile.renameTo(file)) {
throw new RuntimeException("failed to rename " + tmpfile + " to " + file);
}
System.out.println("Renamed " + tmpfile + " to " + file);
}
System.out.println();
GeneratorsFileUtil.writeFileIfContentChanged(file, text.toString());
}
private void generatePreamble(String injectorPackageName, Printer p) throws IOException {
@@ -0,0 +1,54 @@
package org.jetbrains.jet.di;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import java.io.File;
import java.io.IOException;
public class GeneratorsFileUtil {
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public static void writeFileIfContentChanged(File file, String newText) throws IOException {
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
if (parentFile.mkdirs()) {
System.out.println("Directory created: " + parentFile.getAbsolutePath());
}
else {
throw new IllegalStateException("Cannot create directory: " + parentFile);
}
}
if (checkFileIgnoringLineSeparators(file, newText)) {
System.out.println("Not changed: " + file.getAbsolutePath());
return;
}
boolean useTmpfile = !SystemInfo.isWindows;
File tmpfile = useTmpfile ? new File(file.getName() + ".tmp") : file;
FileUtil.writeToFile(tmpfile, newText);
System.out.println("File written: " + tmpfile.getAbsolutePath());
if (useTmpfile) {
if (!tmpfile.renameTo(file)) {
throw new RuntimeException("failed to rename " + tmpfile + " to " + file);
}
System.out.println("Renamed " + tmpfile + " to " + file);
}
System.out.println();
}
private static boolean checkFileIgnoringLineSeparators(File file, String content) {
String currentContent;
try {
currentContent = FileUtil.loadFile(file, true);
}
catch (Throwable ignored) {
return false;
}
return StringUtil.convertLineSeparators(content).equals(currentContent);
}
}