Beispiel #1
0
        private async Task ReplaceContent(ReplaceOptions options, string[] files)
        {
            var(search, replace, ignoreCase, target) =
                (
                    options.Search,
                    options.Replace,
                    options.IgnoreCase,
                    options.Target
                );

            var supportedExtensions = options.ReplaceExtensions;

            foreach (var file in files)
            {
                Console.WriteLine("Processing file '{0}'...", file);

                var ext = Path.GetExtension(file);
                if (supportedExtensions.Contains(ext))
                {
                    var cdet = GetCharsetDetector(file);

                    if (cdet.Charset != null)
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("Charset: {0}, confidence: {1}... ", cdet.Charset, cdet.Confidence);
                        var encoding = Encoding.GetEncoding(cdet.Charset);

                        var original = await File.ReadAllTextAsync(file, encoding);

                        var content = Rename(original, search, replace, ignoreCase);
                        if (content != original)
                        {
                            await File.WriteAllTextAsync(file, content, encoding);

                            Console.Write("Saving content... Done.{0}", Environment.NewLine);
                        }
                        else
                        {
                            Console.Write("Done.{0}", Environment.NewLine);
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Charset detection failed, skipping...");
                    }
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("File extension '{0}' not listed. Done", ext);
                }

                Console.ResetColor();
                Console.WriteLine();
            }
        }
Beispiel #2
0
        private static (string[] Searches, string[] Replaces) GetMultiterms(ReplaceOptions options)
        {
            var searches = options.Search.Split(' ', StringSplitOptions.RemoveEmptyEntries);
            var replaces = options.Replace.Split(' ', StringSplitOptions.RemoveEmptyEntries);

            if (searches.Length != replaces.Length)
            {
                throw new Exception("When multiterm option present, search and replace arrays should have the same number of space-separated terms");
            }

            var len = Math.Min(searches.Length, replaces.Length);

            return(searches.Take(len).ToArray(), replaces.Take(len).ToArray());
        }