Skip to content

Commit

Permalink
Use Cake's IFileSystem for read/write operations instead of System.IO…
Browse files Browse the repository at this point in the history
….File (#118)

* Use Cake's IFileSystem for read/write operations instead of System.IO.File

In all file aliases, use Cake's IFileSystem abstraction instead of using System.IO.File.
This enables testing of tasks that use the file aliases by mocking IFileSystem.

The FileTouch() alias continues to use System.IO.File since there is no API in IFileSystem that allows setting the last write time.

* Remove unnecessary nullable annotation

Co-authored-by: C. Augusto Proiete <[email protected]>

* Convert paths to absolute paths before opening file stream

Co-authored-by: C. Augusto Proiete <[email protected]>

---------

Co-authored-by: C. Augusto Proiete <[email protected]>
  • Loading branch information
ap0llo and augustoproiete authored Feb 21, 2023
1 parent 7c43533 commit ead9d86
Showing 1 changed file with 61 additions and 36 deletions.
97 changes: 61 additions & 36 deletions src/Cake.FileHelpers/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public static class FileHelperAliases
[CakeMethodAlias]
public static string FileReadText (this ICakeContext context, FilePath file)
{
var filename = file.MakeAbsolute (context.Environment).FullPath;

return File.ReadAllText (filename);
using var streamReader = CreateStreamReader(context, file);
return streamReader.ReadToEnd();
}

/// <summary>
Expand All @@ -42,9 +41,8 @@ public static string FileReadText (this ICakeContext context, FilePath file)
[CakeMethodAlias]
public static string FileReadText(this ICakeContext context, FilePath file, Encoding encoding)
{
var filename = file.MakeAbsolute(context.Environment).FullPath;

return File.ReadAllText(filename, encoding);
using var streamReader = CreateStreamReader(context, file, encoding);
return streamReader.ReadToEnd();
}

/// <summary>
Expand All @@ -56,9 +54,8 @@ public static string FileReadText(this ICakeContext context, FilePath file, Enco
[CakeMethodAlias]
public static string[] FileReadLines (this ICakeContext context, FilePath file)
{
var filename = file.MakeAbsolute (context.Environment).FullPath;

return File.ReadAllLines (filename);
using var streamReader = CreateStreamReader(context, file);
return ReadLines(streamReader);
}

/// <summary>
Expand All @@ -71,9 +68,8 @@ public static string[] FileReadLines (this ICakeContext context, FilePath file)
[CakeMethodAlias]
public static string[] FileReadLines(this ICakeContext context, FilePath file, Encoding encoding)
{
var filename = file.MakeAbsolute(context.Environment).FullPath;

return File.ReadAllLines(filename, encoding);
using var streamReader = CreateStreamReader(context, file, encoding);
return ReadLines(streamReader);
}

/// <summary>
Expand All @@ -85,9 +81,8 @@ public static string[] FileReadLines(this ICakeContext context, FilePath file, E
[CakeMethodAlias]
public static void FileWriteText (this ICakeContext context, FilePath file, string text)
{
var filename = file.MakeAbsolute (context.Environment).FullPath;

File.WriteAllText (filename, text);
using var streamWriter = CreateStreamWriter(context, file, FileMode.Create);
streamWriter.Write(text);
}

/// <summary>
Expand All @@ -100,9 +95,8 @@ public static void FileWriteText (this ICakeContext context, FilePath file, stri
[CakeMethodAlias]
public static void FileWriteText(this ICakeContext context, FilePath file, Encoding encoding, string text)
{
var filename = file.MakeAbsolute(context.Environment).FullPath;

File.WriteAllText(filename, text, encoding);
using var streamWriter = CreateStreamWriter(context, file, FileMode.Create, encoding);
streamWriter.Write(text);
}

/// <summary>
Expand All @@ -114,9 +108,8 @@ public static void FileWriteText(this ICakeContext context, FilePath file, Encod
[CakeMethodAlias]
public static void FileWriteLines (this ICakeContext context, FilePath file, string[] lines)
{
var filename = file.MakeAbsolute (context.Environment).FullPath;

File.WriteAllLines (filename, lines);
using var streamWriter = CreateStreamWriter(context, file, FileMode.Create);
WriteLines(streamWriter, lines);
}

/// <summary>
Expand All @@ -129,9 +122,8 @@ public static void FileWriteLines (this ICakeContext context, FilePath file, str
[CakeMethodAlias]
public static void FileWriteLines(this ICakeContext context, FilePath file, Encoding encoding, string[] lines)
{
var filename = file.MakeAbsolute(context.Environment).FullPath;

File.WriteAllLines(filename, lines, encoding);
using var streamWriter = CreateStreamWriter(context, file, FileMode.Create, encoding);
WriteLines(streamWriter, lines);
}

/// <summary>
Expand All @@ -143,9 +135,8 @@ public static void FileWriteLines(this ICakeContext context, FilePath file, Enco
[CakeMethodAlias]
public static void FileAppendText (this ICakeContext context, FilePath file, string text)
{
var filename = file.MakeAbsolute (context.Environment).FullPath;

File.AppendAllText (filename, text);
using var streamWriter = CreateStreamWriter(context, file, FileMode.OpenOrCreate);
streamWriter.Write(text);
}

/// <summary>
Expand All @@ -158,9 +149,8 @@ public static void FileAppendText (this ICakeContext context, FilePath file, str
[CakeMethodAlias]
public static void FileAppendText(this ICakeContext context, FilePath file, Encoding encoding, string text)
{
var filename = file.MakeAbsolute(context.Environment).FullPath;

File.AppendAllText(filename, text, encoding);
using var streamWriter = CreateStreamWriter(context, file, FileMode.OpenOrCreate, encoding);
streamWriter.Write(text);
}

/// <summary>
Expand All @@ -172,9 +162,8 @@ public static void FileAppendText(this ICakeContext context, FilePath file, Enco
[CakeMethodAlias]
public static void FileAppendLines (this ICakeContext context, FilePath file, string[] lines)
{
var filename = file.MakeAbsolute (context.Environment).FullPath;

File.AppendAllLines (filename, lines);
using var streamWriter = CreateStreamWriter(context, file, FileMode.OpenOrCreate);
WriteLines(streamWriter, lines);
}

/// <summary>
Expand All @@ -187,9 +176,8 @@ public static void FileAppendLines (this ICakeContext context, FilePath file, st
[CakeMethodAlias]
public static void FileAppendLines(this ICakeContext context, FilePath file, Encoding encoding, string[] lines)
{
var filename = file.MakeAbsolute(context.Environment).FullPath;

File.AppendAllLines(filename, lines, encoding);
using var streamWriter = CreateStreamWriter(context, file, FileMode.OpenOrCreate, encoding);
WriteLines(streamWriter, lines);
}

/// <summary>
Expand Down Expand Up @@ -741,6 +729,43 @@ public static Group FindRegexMatchGroupInFile(this ICakeContext context, FilePat

return null;
}

private static StreamReader CreateStreamReader(ICakeContext context, FilePath file, Encoding encoding = null)
{
var stream = context.FileSystem.GetFile(file.MakeAbsolute(context.Environment)).OpenRead();
return encoding is null
? new StreamReader(stream, leaveOpen: false)
: new StreamReader(stream, encoding, leaveOpen: false);
}

private static StreamWriter CreateStreamWriter(ICakeContext context, FilePath file, FileMode mode, Encoding encoding = null)
{
var stream = context.FileSystem.GetFile(file.MakeAbsolute(context.Environment)).Open(mode);
return encoding is null
? new StreamWriter(stream, leaveOpen: false)
: new StreamWriter(stream, encoding, leaveOpen: false);
}

private static string[] ReadLines(StreamReader streamReader)
{
var lines = new List<string>();

string line;
while ((line = streamReader.ReadLine()) is not null)
{
lines.Add(line);
}

return lines.ToArray();
}

private static void WriteLines(StreamWriter streamWriter, string[] lines)
{
foreach (var line in lines)
{
streamWriter.WriteLine(line);
}
}
}
}

0 comments on commit ead9d86

Please sign in to comment.