commit a26beeb7f8a1b60746d1d9b8f89296bf1d9fb75e
parent 0682da56309ae72b9f643c6902b14d40e5e124b3
Author: Friedel Schön <[email protected]>
Date:   Tue, 11 Oct 2022 11:27:41 +0200
"--inline" doesn't copy original but write to $path.new and moves
Diffstat:
1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/src/main.d b/src/main.d
@@ -5,7 +5,7 @@ module importsort;
 import core.stdc.stdlib : exit;
 import std.algorithm : findSplit, map, sort;
 import std.array : array;
-import std.file : copy, remove;
+import std.file : rename;
 import std.regex : ctRegex, matchFirst;
 import std.stdio : File, stderr, stdin, stdout;
 import std.string : format, indexOf, split, strip, stripLeft;
@@ -163,23 +163,22 @@ void main(string[] args) {
 
 	File infile, outfile;
 	if (inline) {
-		copy(path, path ~ ".bak");
-		infile = File(path ~ ".bak");
-		scope (exit)
-			remove(path ~ ".bak");
+		infile = File(path);
 	} else if (path == "-") {
 		infile = stdin;
 	} else {
 		infile = File(path);
 	}
 
-	if (inline)
-		outfile = File(path, "w");
-	else if (output)
+	if (inline) {
+		outfile = File(path ~ ".new", "w");
+		scope (exit)
+			rename(path ~ ".new", path);
+	} else if (output) {
 		outfile = File(output, "w");
-	else
+	} else {
 		outfile = stdout;
-
+	}
 	string softEnd = null;
 	Import[] matches;