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
@@ -30,6 +30,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.di.GeneratorsFileUtil;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
@@ -41,7 +42,6 @@ import org.jetbrains.jet.utils.PathUtil;
import org.jetbrains.jet.utils.Printer;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
@@ -88,12 +88,8 @@ public class GenerateJavaToKotlinMethodMap {
printer.popIndent().println("}");
printer.popIndent().println("}");
//noinspection IOResourceOpenedButNotSafelyClosed
FileWriter out =
new FileWriter("compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaToKotlinMethodMapGenerated.java");
out.write(buf.toString());
out.close();
File file = new File("compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaToKotlinMethodMapGenerated.java");
GeneratorsFileUtil.writeFileIfContentChanged(file, buf.toString());
}
private static class MyMapBuilder extends JavaToKotlinClassMapBuilder {
@@ -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);
}
}