Esempio n. 1
0
 public Transformed(ITextFileInfo source, Func <string, string> trans)
 {
     Debug.Assert(source.Exists && !source.IsDirectory);
     Debug.Assert(trans != null);
     _source = source;
     _trans  = trans;
 }
Esempio n. 2
0
 /// <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);
     }
 }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
 ITextFileInfo GetFile() => _file ?? (_file = FileSystem.GetFileInfo(FilePath).AsTextFileInfo(ignoreExtension: true));
Esempio n. 5
0
 public static ITextFileInfo WithTransformedText(this ITextFileInfo f, Func <string, string> trans)
 {
     return(new Transformed(f, trans));
 }