• [源代码]


    在前文 [源代码] - C#代码搜索器 中我开发了一个代码搜索器. 我对其做的最后改动是将索引保存到磁盘中, 以备今后使用.

    如今, 我在工作中又接到一项新任务: 有一个大项目, 其中10个负责数据访问的service即将被淘汰, 将会替换成entity framework的方式. 这10个service一共有近130个operation. 项目中所有调用这些operation的地方都必须被替换成新的EF方式. 整个项目有47902个cs文件. 现在要做个分析, 找到所有需要修改的地方.

    稍作思考, 开工! 

    先创建一个console project, 引入WEBUS2.0 SDK. 将10个service和130个operation的信息放到字符串数组中:

    static string[] DS_OP_List = new string[] {
        "xxxxDS.CalculatePilotCloseOut",
        "xxxxDS.CreatexxFirstAcont",
        "xxxxDS.CreatexxUnallocatedULD",
        "xxxxDS.xxCargoGetTare",
        "xxxxDS.xxCargoCreateAbulk",
        "xxxxDS.xxCargoDeleteAbulk",
        "xxxxDS.xxCargoMoveAbulk"
        ...
    }; //共130个, 公司有规定, 所以用xxx替代真实值 :)

    然后打开Index (关于如何创建Index请参见前文:  [源代码] - C#代码搜索器), 再循环对operation进行搜索:

    static void Main(string[] args)
    {
        IIndexer index = new IndexManager(); //构造索引对象
        index.Open(@"C:xxxCodeSearch.Index", IndexOpenMode.Read); //以只读方式打开索引
        ISearcher se = new IndexSearcher(index); //构造搜索对象
        using (CSVFile csv = new CSVFile(@"c:	empxx_DS_Analysis.csv")) //将结果保存在csv文件中
        {
            foreach (var ds_op in DS_OP_List)
            {
                var key = ds_op.Split('.')[1].ToLower(); //从Service.Operation中提取Operation, 比如从xxxxDS.CalculatePilotCloseOut中提取CalculatePilotCloseOut中提取
                var hits = se.Search(string.Format("Code="{0}"", key)); //搜索表达式: Code="CalculatePilotCloseOut"
                Console.WriteLine("{0}/{1}", key, hits.Count);
                foreach (HitDoc hit in hits)
                {
                    csv.Write(ds_op, hit.GetDoc().GetField("FileName").Value.ToString()); //依次输出"服务名", "方法名", "文件名"到csv中
                }
            }
        }
        Console.ReadLine();
    }

    最后上一个生成CSV文件的工具类:

    public class CSVFile : IDisposable
    {
        public string FileName { get; private set; }
        private StreamWriter sw = null;
        private StreamReader sr = null;
        private FileStream fsWrite = null;
        private FileStream fsRead = null;
    
        public CSVFile(string filename)
            : this(filename, Encoding.UTF8, FileMode.OpenOrCreate)
        {
        }
    
        public CSVFile(string filename, FileMode mode)
            : this(filename, Encoding.UTF8, mode)
        {
        }
    
        public CSVFile(string filename, Encoding encoding, FileMode mode)
        {
            this.FileName = filename;
            fsWrite = new FileStream(filename, mode, FileAccess.Write, FileShare.Read);
            fsWrite.Seek(0, SeekOrigin.End);
            fsRead = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            sw = new StreamWriter(fsWrite, encoding);
            sr = new StreamReader(fsRead, encoding);
        }
    
        public void Close()
        {
            sw.Close();
            sr.Close();
        }
    
        public string[] Read()
        {
            string text = sr.ReadLine();
            if (string.IsNullOrEmpty(text))
            {
                return null;
            }
            return text.Split(',');
        }
    
        public string[] FindNext(string startKeyword, StringComparison comparisonType = StringComparison.InvariantCultureIgnoreCase)
        {
            string[] result = null;
            while ((result = this.Read()) != null)
            {
                if (result[0].StartsWith(startKeyword, comparisonType))
                {
                    return result;
                }
                else
                {
                    continue;
                }
            }
            return null;
        }
    
        public void Write(params string[] values)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var v in values)
            {
                sb.Append(v + ",");
            }
            sw.WriteLine(sb.ToString().Substring(0, sb.Length - 1));
        }
    
        public void Dispose()
        {
            this.Close();
        }
    }
    View Code

    ok, 大功告成! 运行:

  • 相关阅读:
    ubuntu 14.4 apache2 django
    github上的版本和本地版本冲突的解决方法
    Javascript能做什么 不能做什么。
    django 取model字段的verbose_name值
    Django在admin模块中显示auto_now_add=True或auto_now=True的时间类型列
    合并多个python list以及合并多个 django QuerySet 的方法
    摘抄
    Python 字符串拼接
    学习HTTP
    Django 自定义模板标签和过滤器
  • 原文地址:https://www.cnblogs.com/iamzyf/p/3270789.html
Copyright © 2020-2023  润新知