private void PrepFile(Stream csFile, Stream tempFile, List <RuleCacheRefresh> linesToRemoveFromFile) { var totalLines = 0; using (var sr = new StreamReader(csFile)) using (var sw = new StreamWriter(tempFile)) { while (sr.ReadLine() != null) { totalLines++; } csFile.Position = 0; sr.BaseStream.Position = 0; string line; var currentLine = 0; int foundStart = 0, foundEnd = 0; while ((line = sr.ReadLine()) != null) { //have to count lines because we want to cut off the footer currentLine++; //Don't read the last X lines as that is definitely the footer and we will add that back in after we add in the updated rule code if (currentLine > totalLines - _codeFooter.Length) { continue; } RuleCacheRefresh toWork = linesToRemoveFromFile.FirstOrDefault(c => c.StartLineNumber == currentLine); if (toWork != null) { foundStart = currentLine; foundEnd = toWork.EndLineNumber; } if (currentLine > foundEnd) { foundStart = 0; foundEnd = 0; } if (foundStart == 0 || foundEnd == 0) { sw.WriteLine(line); } } } }
//private List<RuleCacheRefresh> GetRuleInfoFromFile(string filename, ref List<RuleAppliedBase> rules) private List <RuleCacheRefresh> GetRuleInfoFromFile(string filename) { ////sample //public static //ZirMed.Claims.Objects.Transactions.IClaim Z_1_4_6(ZirMed.Claims.Objects.Transactions.IClaim input) //{ // throw new System.Exception(); //} var refreshResults = new List <RuleCacheRefresh>(); var currentLine = 0; using (TextReader tr = new StreamReader(new FileStream(filename, FileMode.OpenOrCreate))) { var line = tr.ReadLine(); var insideClass = false; var insideMethod = false; var bracketCount = 0; var methodStartLine = 0; while (line != null) { currentLine++; bracketCount += line.Split('{').Length - 1; bracketCount -= line.Split('}').Length - 1; if (bracketCount < 2) { insideClass = false; } if (bracketCount < 3) { if (insideMethod && methodStartLine > 0) { RuleCacheRefresh foundMethodCache = refreshResults.FirstOrDefault(c => c.StartLineNumber == methodStartLine); if (foundMethodCache == null) { throw new NullReferenceException("srsly, why is this null, we just found the method name and marked it a few lines ago"); } foundMethodCache.EndLineNumber = currentLine; } methodStartLine = 0; insideMethod = false; } if (line.Contains("public static class")) { insideClass = true; line = tr.ReadLine(); continue; } if (!insideClass) { line = tr.ReadLine(); continue; } if (line.Contains("public static") && !insideMethod) { insideMethod = true; string[] splitMethodName = line.Split(new[] { ' ' }, StringSplitOptions.None); if (splitMethodName.Any()) { var methodName = splitMethodName[3]; string[] methodNameSplit = methodName.Split(new[] { '_' }, StringSplitOptions.None); methodNameSplit[methodNameSplit.Length - 1] = methodNameSplit[methodNameSplit.Length - 1].Substring(0, methodNameSplit[methodNameSplit.Length - 1].Length - 1); if (methodNameSplit.Any()) { var temp = new RuleCacheRefresh { CodeHash = ulong.Parse(methodNameSplit[1]), CodeId = int.Parse(methodNameSplit[2]) }; if (methodNameSplit.Length > 3 && !string.IsNullOrWhiteSpace(methodNameSplit[3])) { temp.RuleAppliedId = int.Parse(methodNameSplit[3]); } RuleCodeCache tempCode = _rulesDAL.DALCache.RuleCodes.FirstOrDefault(c => c.RuleCodeId == temp.CodeId); if (tempCode != null) { tempCode.ExistsInCache = true; if (tempCode.CodeHash != temp.CodeHash) { temp.StartLineNumber = methodStartLine = currentLine; refreshResults.Add(temp); } else { tempCode.NeedsRefresh = false; } } } } } line = tr.ReadLine(); } } return(refreshResults); }