Skip to content

Commit

Permalink
feat: Add WriteLines/AppendLines/Truncate extension methods (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
gigi81 authored Aug 22, 2023
1 parent d169c4d commit fbbc7f2
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 25 deletions.
119 changes: 104 additions & 15 deletions src/System.IO.Abstractions.Extensions/IFileInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,130 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Text;

namespace System.IO.Abstractions
{
public static class IFileInfoExtensions
{
/// <summary>
/// Throws an exception if the file <paramref name="info"/> doesn't exists
/// Throws an exception if the <paramref name="file"/> doesn't exists
/// </summary>
/// <param name="info">File that will be checked for existance</param>
/// <param name="file">File that will be checked for existance</param>
/// <exception cref="FileNotFoundException">Exception thrown if the file is not found</exception>
public static void ThrowIfNotFound(this IFileInfo info)
public static void ThrowIfNotFound(this IFileInfo file)
{
if (!info.Exists)
throw new FileNotFoundException(StringResources.Format("COULD_NOT_FIND_FILE_EXCEPTION", info.FullName));
if (!file.Exists)
throw new FileNotFoundException(StringResources.Format("COULD_NOT_FIND_FILE_EXCEPTION", file.FullName));
}

/// <summary>
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="info"/> file
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="file"/>
/// </summary>
/// <param name="info">File to enumerate content</param>
/// <param name="file">File to enumerate content</param>
/// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns>
public static IEnumerable<string> EnumerateLines(this IFileInfo info)
public static IEnumerable<string> EnumerateLines(this IFileInfo file)
{
return new LineEnumerable(info, null);
return new LineEnumerable(file, null);
}

/// <summary>
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="info"/> file
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the specified <paramref name="file"/>
/// using the specified <paramref name="encoding"/>
/// </summary>
/// <param name="info">File to enumerate content</param>
/// <param name="file">File to enumerate content</param>
/// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns>
public static IEnumerable<string> EnumerateLines(this IFileInfo info, Encoding encoding)
public static IEnumerable<string> EnumerateLines(this IFileInfo file, Encoding encoding)
{
return new LineEnumerable(info, encoding);
return new LineEnumerable(file, encoding);
}

/// <summary>
/// Opens a <see cref="FileSystemStream"/> for the <paramref name="file"/> in the specified <paramref name="mode"/>
/// </summary>
/// <param name="file">File to open stream on</param>
/// <param name="mode">Mode to use when opening the file</param>
/// <returns>A <see cref="FileSystemStream"/> that can read or write data to the specified <paramref name="file"/></returns>
public static FileSystemStream OpenFileStream(this IFileInfo file, FileMode mode)
{
return file.FileSystem.FileStream.New(file.FullName, mode);
}

/// <summary>
/// Creates a new empty <paramref name="file"/>.
/// If the file already exists, the file is truncated.
/// </summary>
/// <param name="file">File to create</param>
/// <returns>The original <see cref="IFileInfo"/> so that methods calls can be chained</returns>
public static IFileInfo Truncate(this IFileInfo file)
{
using(var stream = file.OpenFileStream(FileMode.Create))
{
stream.Dispose();
}

return file;
}

/// <summary>
/// Writes the specified <paramref name="lines"/> to the specified <paramref name="file"/> using the UTF-8 encoding.
/// If the file already exists and the <paramref name="overwrite"/> flag is set to true, the file will be truncated.
/// </summary>
/// <param name="file">File to write to</param>
/// <param name="lines">Lines to write to file as text</param>
/// <param name="overwrite">Flag that specifies if the file can be overwritten if it exists</param>
/// <exception cref="IOException">Exception thrown if the file already exists and the <paramref name="overwrite"/> flag is set to <see cref="false"/></exception>
public static void WriteLines(this IFileInfo file, IEnumerable<string> lines, bool overwrite = false)
{
using (var stream = file.OpenFileStream(GetWriteFileMode(file, overwrite)))
using (var writer = new StreamWriter(stream))
foreach(var line in lines)
{
writer.WriteLine(line);
}
}

/// <summary>
/// Writes the specified <paramref name="lines"/> to the specified <paramref name="file"/>
/// using the specified <paramref name="encoding"/>.
/// If the file already exists and the <paramref name="overwrite"/> flag is set to true, the file will be truncated.
/// </summary>
/// <param name="file">File to write to</param>
/// <param name="lines">Lines to write to file as text</param>
/// <param name="encoding">Encoding to use when writing the <paramref name="lines"/> to the text file</param>
/// <param name="overwrite">Flag that specifies if the file can be overwritten if it exists</param>
/// <exception cref="IOException">Exception thrown if the file already exists and the <paramref name="overwrite"/> flag is set to <see cref="false"/></exception>
public static void WriteLines(this IFileInfo file, IEnumerable<string> lines, Encoding encoding, bool overwrite = false)
{
using (var stream = file.OpenFileStream(GetWriteFileMode(file, overwrite)))
using (var writer = new StreamWriter(stream, encoding))
foreach (var line in lines)
{
writer.WriteLine(line);
}
}

/// <summary>
/// Appends the specified <paramref name="lines"/> to the specified <paramref name="file"/>
/// </summary>
/// <param name="file">File to append to</param>
/// <param name="lines">Lines to append to file as text</param>
public static void AppendLines(this IFileInfo file, IEnumerable<string> lines)
{
using (var writer = file.AppendText())
foreach (var line in lines)
{
writer.WriteLine(line);
}
}

private static FileMode GetWriteFileMode(IFileInfo info, bool overwrite)
{
if (!overwrite && info.Exists)
{
throw new IOException(StringResources.Format("CANNOT_OVERWRITE", info.FullName));
}

//if the file already exists it will be truncated
return FileMode.Create;
}
}
}
11 changes: 10 additions & 1 deletion src/System.IO.Abstractions.Extensions/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/System.IO.Abstractions.Extensions/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="CANNOT_OVERWRITE" xml:space="preserve">
<value>The file '{0}' already exists.</value>
</data>
<data name="COULD_NOT_FIND_FILE_EXCEPTION" xml:space="preserve">
<value>Could not find file '{0}'.</value>
</data>
<data name="COULD_NOT_FIND_PART_OF_PATH_EXCEPTION" xml:space="preserve">
<value>Could not find a part of the path '{0}'.</value>
</data>
<data name="NOT_AN_ANCESTOR" xml:space="preserve">
<value>'{0}' is not an ancestor of '{1}'</value>
<value>'{0}' is not an ancestor of '{1}'.</value>
</data>
</root>
Loading

0 comments on commit fbbc7f2

Please sign in to comment.