@@ -192,87 +192,6 @@ void parseString(Visitor)(ubyte[] sourceCode, string fileName, Visitor visitor)
192192 visitor.visit(m);
193193}
194194
195- void parseFile (string fileName, string destFile)
196- {
197- import std.array : uninitializedArray;
198-
199- auto inFile = File (fileName);
200- if (inFile.size == 0 )
201- warningf(" %s is empty" , inFile.name);
202-
203- ubyte [] sourceCode = uninitializedArray! (ubyte [])(to! size_t (inFile.size));
204- if (sourceCode.length == 0 )
205- return ;
206-
207- inFile.rawRead(sourceCode);
208- auto fl = FileLines(fileName, destFile);
209- auto visitor = new TestVisitor! (typeof (fl))(fl);
210- parseString(sourceCode, fileName, visitor);
211- delete visitor;
212- }
213-
214- // Modify a path under oldBase to a new path with the same subpath under newBase.
215- // E.g.: `/foo/bar`.rebasePath(`/foo`, `/quux`) == `/quux/bar`
216- string rebasePath (string path, string oldBase, string newBase)
217- {
218- import std.path : absolutePath, buildPath, relativePath;
219- return buildPath (newBase, path.absolutePath.relativePath(oldBase.absolutePath));
220- }
221-
222- version (unittest ) { void main(){} } else
223- void main (string [] args)
224- {
225- import std.file ;
226- import std.getopt ;
227- import std.path ;
228-
229- string inputDir, outputDir;
230- string [] ignoredFiles;
231-
232- auto helpInfo = getopt(args, config.required,
233- " inputdir|i" , " Folder to start the recursive search for unittest blocks (can be a single file)" , &inputDir,
234- " outputdir|o" , " Alternative folder to use as output (can be a single file)" , &outputDir,
235- " ignore" , " List of files to exclude (partial matching is supported)" , &ignoredFiles);
236-
237- if (helpInfo.helpWanted)
238- {
239- return defaultGetoptPrinter (` assert_writeln_magic
240- Tries to lower EqualExpression in AssertExpressions of Unittest blocks to commented writeln calls.
241- ` , helpInfo.options);
242- }
243-
244- inputDir = inputDir.asNormalizedPath.array;
245-
246- DirEntry [] files;
247-
248- // inputDir as default output directory
249- if (! outputDir.length)
250- outputDir = inputDir;
251-
252- if (inputDir.isFile)
253- {
254- files = [DirEntry (inputDir)];
255- inputDir = " " ;
256- }
257- else
258- {
259- files = dirEntries(inputDir, SpanMode.depth).filter! (
260- a => a.name.endsWith(" .d" ) && ! a.name.canFind(" .git" )).array;
261- }
262-
263- foreach (file; files)
264- {
265- if (! ignoredFiles.any! (x => file.name.canFind(x)))
266- {
267- // single files
268- if (inputDir.length == 0 )
269- parseFile(file.name, outputDir);
270- else
271- parseFile(file.name, file.name.rebasePath(inputDir, outputDir));
272- }
273- }
274- }
275-
276195/**
277196A simple line-based in-memory representation of a file.
278197 - will automatically write all changes when the object is destructed
@@ -284,47 +203,26 @@ struct FileLines
284203
285204 string [] lines;
286205 string destFile;
287- bool overwriteInputFile;
288206 bool hasWrittenChanges;
289207
290- this (string inputFile, string destFile )
208+ this (string inputText )
291209 {
292- stderr.writefln(" %s -> %s" , inputFile, destFile);
293- this .overwriteInputFile = inputFile == destFile;
294- this .destFile = destFile;
295- lines = File (inputFile).byLineCopy.array;
296-
297- destFile.dirName.mkdirRecurse;
298- }
299-
300- // dumps all changes
301- ~this ()
302- {
303- if (overwriteInputFile)
304- {
305- if (hasWrittenChanges)
306- {
307- auto tmpFile = File (destFile ~ " .tmp" , " w" );
308- writeLinesToFile(tmpFile);
309- tmpFile.close;
310- tmpFile.name.rename(destFile);
311- }
312- }
313- else
314- {
315- writeLinesToFile(File (destFile, " w" ));
316- }
210+ lines = inputText.split(" \n " );
317211 }
318212
319213 // writes all changes to a random, temporary file
320- void writeLinesToFile (File outFile) {
214+ auto buildLines () {
215+ auto app = appender! string ;
321216 // dump file
322217 foreach (line; lines)
323- outFile.writeln(line);
218+ {
219+ app ~= line;
220+ app ~= " \n " ;
221+ }
324222 // within the docs we automatically inject std.stdio (hence we need to do the same here)
325223 // writeln needs to be @nogc, @safe, pure and nothrow (we just fake it)
326- outFile.writeln( " // \n private void writeln(T)(T l) { }" ) ;
327- outFile.flush ;
224+ app ~= " // \n private void writeln(T)(T l) { }" ;
225+ return app.data ;
328226 }
329227
330228 string opIndex (size_t i) { return lines[i]; }
0 commit comments