public Transformed(ITextFileInfo source, Func <string, string> trans) { Debug.Assert(source.Exists && !source.IsDirectory); Debug.Assert(trans != null); _source = source; _trans = trans; }
/// <summary> /// Deletes the file if it exists. /// </summary> /// <param name="m">The monitor to use.</param> public void Delete(IActivityMonitor m) { if (GetFile() != null) { FileSystem.Delete(m, FilePath); _file = null; OnDeleted(m); } }
/// <summary> /// Saves (creates or updates), this file with a new content or calls <see cref="Delete"/> /// if content is null. /// If the content is the same as the current <see cref="TextContent"/> the save is skipped. /// </summary> /// <param name="m">The monitor to use.</param> /// <param name="newContent">The content. Null to delete the file.</param> /// <param name="forceSave">True to always save the file.</param> /// <returns>True on success, false on error.</returns> protected bool CreateOrUpdate(IActivityMonitor m, string newContent, bool forceSave = false) { string oldContent = GetFile()?.TextContent; if (oldContent == newContent && !forceSave) { return(true); } if (newContent == null) { Delete(m); return(true); } if (!FileSystem.CopyTo(m, newContent, FilePath, _encoding)) { return(false); } OnSaved(m); _file = null; return(true); }
ITextFileInfo GetFile() => _file ?? (_file = FileSystem.GetFileInfo(FilePath).AsTextFileInfo(ignoreExtension: true));
public static ITextFileInfo WithTransformedText(this ITextFileInfo f, Func <string, string> trans) { return(new Transformed(f, trans)); }