Esempio n. 1
0
        public static List <ReplaceItem> GetReplaceItems(
            Match match,
            ReplaceOptions replaceOptions,
            CancellationToken cancellationToken = default)
        {
            List <Match> matches = GetMatches(match);

            int offset = 0;
            List <ReplaceItem> replaceItems = ListCache <ReplaceItem> .GetInstance();

            if (replaceItems.Capacity < matches.Count)
            {
                replaceItems.Capacity = matches.Count;
            }

            foreach (Match match2 in matches)
            {
                string value = replaceOptions.Replace(match2);

                replaceItems.Add(new ReplaceItem(match2, value, match2.Index + offset));

                offset += value.Length - match2.Length;

                cancellationToken.ThrowIfCancellationRequested();
            }

            ListCache <Match> .Free(matches);

            return(replaceItems);
Esempio n. 2
0
        protected override void ExecuteMatch(
            FileMatch fileMatch,
            string directoryPath)
        {
            TextWriter?    textWriter = null;
            List <Capture>?captures   = null;

            try
            {
                captures = ListCache <Capture> .GetInstance();

                GetCaptures(
                    fileMatch.ContentMatch !,
                    ContentFilter !.GroupNumber,
                    predicate: ContentFilter.Predicate,
                    captures: captures);

                if (!DryRun)
                {
                    textWriter = new StreamWriter(fileMatch.Path, false, fileMatch.Encoding);

                    WriteMatches(fileMatch.ContentText, captures, ReplaceOptions, textWriter);
                }

                int fileMatchCount       = captures.Count;
                int fileReplacementCount = fileMatchCount;
                Telemetry.MatchCount          += fileMatchCount;
                Telemetry.ProcessedMatchCount += fileReplacementCount;

                if (fileReplacementCount > 0)
                {
                    Telemetry.ProcessedFileCount++;
                }

                Report(fileMatch);
            }
            catch (Exception ex) when(ex is IOException ||
                                      ex is UnauthorizedAccessException)
            {
                Report(fileMatch, ex);
            }
            finally
            {
                textWriter?.Dispose();

                if (captures != null)
                {
                    ListCache <Capture> .Free(captures);
                }
            }
        }
Esempio n. 3
0
        protected override void ExecuteMatch(
            FileMatch fileMatch,
            string directoryPath)
        {
            List <ReplaceItem> replaceItems = ReplaceHelpers.GetReplaceItems(
                fileMatch.NameMatch !,
                RenameOptions,
                NameFilter?.Predicate,
                CancellationToken);

            string path    = fileMatch.Path;
            string newPath = ReplaceHelpers.GetNewPath(fileMatch, replaceItems);

            ListCache <ReplaceItem> .Free(replaceItems);

            if (string.Equals(path, newPath, StringComparison.Ordinal))
            {
                return;
            }

            if (FileSystemHelpers.ContainsInvalidFileNameChars(newPath, FileSystemHelpers.GetFileNameIndex(path)))
            {
                Report(fileMatch, newPath, new IOException("New file name contains invalid characters."));
                return;
            }

            if (File.Exists(newPath))
            {
                if (ConflictResolution == ConflictResolution.Skip)
                {
                    return;
                }
                else if (ConflictResolution == ConflictResolution.Suffix)
                {
                    newPath = FileSystemHelpers.CreateNewFilePath(newPath);
                }
                else if (ConflictResolution == ConflictResolution.Ask)
                {
                    if (!AskToOverwrite(fileMatch, newPath))
                    {
                        return;
                    }
                }
            }

            var renamed = false;

            try
            {
                if (!DryRun)
                {
                    if (fileMatch.IsDirectory)
                    {
                        Directory.Move(path, newPath);
                    }
                    else
                    {
                        File.Move(path, newPath);
                    }

                    renamed = true;
                }

                Report(fileMatch, newPath);

                Telemetry.IncrementProcessedCount(fileMatch.IsDirectory);
            }
            catch (Exception ex) when(ex is IOException ||
                                      ex is UnauthorizedAccessException)
            {
                Report(fileMatch, newPath, ex);
            }

            if (fileMatch.IsDirectory &&
                renamed)
            {
                OnDirectoryChanged(new DirectoryChangedEventArgs(path, newPath));
            }
        }