public bool Run(IFolderOrFile entity, UpdateOptions o) { UpdateFileOptions options = o as UpdateFileOptions; if (options == null) { throw new NotImplementedException($"Unsupported option '{o.GetType()}'"); } if (options.From == null || options.To == null) { throw new ArgumentException("Set 'From' and 'To' properties for replacing content"); } try { string content = File.ReadAllText(entity.AbsolutePath, Encoding.Default); if (content.Contains(options.From, StringComparison.InvariantCultureIgnoreCase)) { Log.Verbose($"Replacing content in the file '{Path.GetFileName(entity.Name)}' from '{options.From}' to '{options.To}'"); if (!options.Preview) { File.WriteAllText( entity.AbsolutePath, content .ReplaceSmartIgnoreCase( options.From, options.To)); } Log.Verbose("Replaced."); return(true); } Log.Verbose("No changes."); return(false); } catch (Exception e) { Log.Error(e, $"Can't replace content in the file '{entity.Name}'"); return(false); } }
public bool Run(IFolderOrFile entity, UpdateOptions options) { try { Log.Verbose($"Deleting '{entity.AbsolutePath}'"); if (!options.Preview) { Directory.Delete(entity.AbsolutePath, recursive: true); } Log.Verbose("Deleted."); } catch (Exception e) { Log.Error(e, $"Can't delete folder {entity.Name}"); return(false); } return(true); }
public bool Run(IFolderOrFile entity, UpdateOptions o) { if (o is UpdateFolderOptions folderOptions) { if (folderOptions.Recursively) { if (folderOptions.Masks.Length != 1) { throw new NotSupportedException("Please specify a single folder mask for recursive renaming."); } var subDirs = Directory.EnumerateDirectories(entity.AbsolutePath, folderOptions.Masks.Single()); if (subDirs.Any()) { return(false); } string newName = Path.Combine(Path.GetDirectoryName(entity.AbsolutePath), Path.GetFileName(entity.AbsolutePath).ReplaceSmartIgnoreCase(folderOptions.From, folderOptions.To)); if (entity.AbsolutePath.Equals(newName, StringComparison.InvariantCultureIgnoreCase)) { return(false); } Log.Verbose($"Renaming to '{newName}'"); if (!folderOptions.Preview) { Directory.Move(entity.AbsolutePath, newName); } return(true); } throw new NotImplementedException("Folder non-recursive renaming is not implemented yet"); } UpdateFileOptions options = o as UpdateFileOptions; if (options == null) { throw new NotImplementedException($"Unsupported option '{o.GetType()}'"); } if (options.From == null || options.To == null) { throw new ArgumentException("Set 'From' and 'To' properties for renaming"); } if (!entity.Name.Contains(options.From, StringComparison.InvariantCultureIgnoreCase)) { return(false); } try { string sourceFileName = entity.AbsolutePath; string destFileName = Path.Combine(Path.GetDirectoryName(sourceFileName) ?? "", entity.Name.ReplaceSmartIgnoreCase(options.From, options.To)); if (sourceFileName.Equals(destFileName, StringComparison.InvariantCultureIgnoreCase)) { return(false); } Log.Verbose($"Renaming to '{Path.GetFileName(destFileName)}'"); if (!options.Preview) { File.Move(sourceFileName, destFileName); } Log.Verbose("Renamed."); return(true); } catch (Exception e) { Log.Error(e, $"Can't rename '{entity.Name}'"); return(false); } }