예제 #1
0
        //查找
        public static int Find(IWin32Window owner, SearchConfig searchConfig, StringBuilder result)
        {
            if (!Assert(searchConfig, result))
            {
                return(0);
            }

            int           count   = 0;
            List <string> affects = new List <string>();

            using (FrmBatch <FileInfo> dialog = new FrmBatch <FileInfo>(10))
            {
                dialog.Text = "正在执行查找操作";
                //工作
                dialog.Work += delegate(object sender, EventArgs <FileInfo> e)
                {
                    FileInfo fileInfo = e.Argument;
                    if (fileInfo == null)
                    {
                        return;
                    }

                    string           fullName;
                    string           text       = File.ReadAllText(fullName = fileInfo.FullName, Encoding.UTF8);
                    StringComparison comparison = searchConfig.CaseMatch ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; //大小写
                    if (searchConfig.WordMatch
                        ? searchConfig.Find.Keys.Any(word => text.IndexOfWord(word, comparison) >= 0)                                     //全字
                        : searchConfig.Find.Keys.Any(word => text.IndexOf(word, comparison) >= 0))                                        //非全字
                    {
                        lock (affects)
                            affects.Add(fullName);
                        dialog.WriteLog("查找 {0}", fullName);
                    }
                };

                //全部完成,统计
                dialog.AllWorkComplete += delegate(object sender, EventArgs e)
                {
                    count = affects.Count;
                    affects.OrderBy(fullName => fullName).ForEach(fullName => result.AppendLine(fullName));
                };

                //入队
                foreach (FileInfo fileInfo in new DirectoryInfo(searchConfig.Config.WorkDir).VisitFilesByShortPattern(searchConfig.FileUseWildcard ? searchConfig.FileWildcard : null))//通配符
                {
                    dialog.Enqueue(fileInfo);
                }

                //显示
                dialog.ShowDialog(owner);
            }
            return(count);
        }
예제 #2
0
        //替换
        public static int Replace(IWin32Window owner, SearchConfig searchConfig, StringBuilder result)
        {
            if (!Assert(searchConfig, result))
            {
                return(0);
            }

            int           count   = 0;
            List <string> affects = new List <string>();

            using (FrmBatch <FileInfo> dialog = new FrmBatch <FileInfo>(10))
            {
                dialog.Text = "正在执行查找替换操作";
                //工作
                dialog.Work += delegate(object sender, EventArgs <FileInfo> e)
                {
                    FileInfo fileInfo = e.Argument;
                    if (fileInfo == null)
                    {
                        return;
                    }

                    string           fullName;
                    string           oldText    = File.ReadAllText(fullName = fileInfo.FullName, Encoding.UTF8);
                    string           newText    = oldText;
                    StringComparison comparison = searchConfig.CaseMatch ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; //大小写
                    if (searchConfig.WordMatch)                                                                                           //全字
                    {
                        searchConfig.Find.ForEach(find => newText = newText.ReplaceWord(find.Key, find.Value, comparison));
                    }
                    else
                    {
                        searchConfig.Find.ForEach(find => newText = newText.Replace(find.Key, find.Value, comparison));
                    }
                    if (newText != oldText)
                    {
                        searchConfig.Config.CheckOut(fileInfo);
                        fileInfo.IsReadOnly = false;//去掉只读属性
                        File.WriteAllText(fullName, newText, Encoding.UTF8);
                        lock (affects)
                            affects.Add(fullName);
                        dialog.WriteLog("替换 {0}", fullName);
                    }
                };

                //全部完成,统计
                dialog.AllWorkComplete += delegate(object sender, EventArgs e)
                {
                    count = affects.Count;
                    affects.OrderBy(fullName => fullName).ForEach(fullName => result.AppendLine(fullName));
                };

                //入队
                foreach (FileInfo fileInfo in new DirectoryInfo(searchConfig.Config.WorkDir).VisitFilesByShortPattern(searchConfig.FileUseWildcard ? searchConfig.FileWildcard : null))//通配符
                {
                    dialog.Enqueue(fileInfo);
                }

                //显示
                dialog.ShowDialog(owner);
            }
            return(count);
        }
예제 #3
0
        //翻译器-替换
        public static StringBuilder TranslatorReplace(IWin32Window owner, TranslatorConfig config, Translator translator, string comment)
        {
            StringBuilder result = new StringBuilder(1024);

            if (!TranslatorAssert(config, result))
            {
                return(result);
            }

            IEnumerable <string> includefiles  = TranslatorVisitFiles(config, translator.IncludeFile);
            IEnumerable <string> exceludefiles = (translator.ExcludeFile == null || translator.ExcludeFile.Length == 0) ? new string[] { } : TranslatorVisitFiles(config, translator.ExcludeFile);

            int           count   = 0;
            List <string> affects = new List <string>();

            using (FrmBatch <string> dialog = new FrmBatch <string>(10, true))
            {
                dialog.Work += delegate(object sender, EventArgs <string> e)
                {
                    string fullName = e.Argument;

                    string           oldText       = File.ReadAllText(fullName, UTF8_NO_BOM);
                    string           newText       = oldText;
                    StringComparison comparison    = translator.CaseMatch ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; //大小写
                    bool             isWindowsText = oldText.IndexOf("\r\n") >= 0;                                                         //windows换行

                    if (isWindowsText)
                    {
                        if (translator.WordMatch)//全字
                        {
                            newText = newText.ReplaceWord(translator.Find.Replace("\r\n", "\n").Replace("\n", "\r\n"), translator.Replace.Replace("\r\n", "\n").Replace("\n", "\r\n"), translator.BanPrefix, translator.BanSuffix, comparison);
                        }
                        else
                        {
                            newText = newText.Replace(translator.Find.Replace("\r\n", "\n").Replace("\n", "\r\n"), translator.Replace.Replace("\r\n", "\n").Replace("\n", "\r\n"), translator.BanPrefix, translator.BanSuffix, comparison);
                        }
                    }
                    else
                    {
                        if (translator.WordMatch)//全字
                        {
                            newText = newText.ReplaceWord(translator.Find.Replace("\r\n", "\n"), translator.Replace.Replace("\r\n", "\n"), translator.BanPrefix, translator.BanSuffix, comparison);
                        }
                        else
                        {
                            newText = newText.Replace(translator.Find.Replace("\r\n", "\n"), translator.Replace.Replace("\r\n", "\n"), translator.BanPrefix, translator.BanSuffix, comparison);
                        }
                    }

                    if (newText != oldText)
                    {
                        File.WriteAllText(fullName, newText, UTF8_NO_BOM);
                        lock (affects)
                            affects.Add(fullName);
                        dialog.WriteLog("{0} 替换 {1}", translator.Find, fullName);
                    }
                };

                //全部完成,统计
                dialog.AllWorkComplete += delegate(object sender, EventArgs e)
                {
                    count = affects.Count;
                    affects.OrderBy(fullName => fullName).ForEach(fullName => result.AppendLine(fullName));
                    GitHelper.GitCommit(config, comment);
                };

                foreach (string fullname in includefiles.Except(exceludefiles))
                {
                    dialog.Enqueue(fullname);
                }

                dialog.ShowDialog(owner);
            }
            result.Insert(0, string.Format("{0} 替换以下文件,共 {1} 个\r\n", translator.Find, count));
            return(result);
        }