Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using Newtonsoft.Json.Linq;
  11.  
  12. namespace ConsoleApp73
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. List<string> list = new List<string>();
  19. using (SqlConnection connection = new SqlConnection(args[0]))
  20. {
  21. connection.Open();
  22. var command = new SqlCommand("SELECT JSON FROM [HTInstaller].[dbo].[VariableSet]", connection);
  23.  
  24. using (var sr = command.ExecuteReader())
  25. {
  26. while (sr.Read())
  27. {
  28. var value = sr.GetString(0);
  29. list.Add(value);
  30. }
  31. }
  32. }
  33.  
  34. var result = ProcessLines(list).Result;
  35. using (FileStream fs = new FileStream(args[1], FileMode.OpenOrCreate, FileAccess.Write))
  36. {
  37. using (StreamWriter sw = new StreamWriter(fs))
  38. foreach (var item in result)
  39. {
  40.  
  41.  
  42.  
  43. sw.WriteLine($"{item.Key}|{item.Value}");
  44.  
  45.  
  46. }
  47. }
  48. }
  49.  
  50. public static async Task<Dictionary<string, string>> ProcessLines(IEnumerable<string> lines)
  51. {
  52. lines = Process(lines);
  53. var lwd = GetLineWithDependencieses(lines).Where(a=>a!=null && !a.IsEmpty).ToList();
  54. Dictionary<string, string> result = new Dictionary<string, string>();
  55. int counter = 0;
  56. DoEvaluate(lwd, result, ref counter);
  57. return result;
  58. }
  59.  
  60. private static void DoEvaluate(IEnumerable<LineWithDependencies> lines, Dictionary<string, string> result, ref int counter)
  61. {
  62. int prevCounter = counter;
  63. foreach (var line in lines)
  64. {
  65. if (!line.Dependencies.Any())
  66. {
  67. if (!result.ContainsKey(line.VariableName))
  68. {
  69. result[line.VariableName] = line.Line;
  70. counter++;
  71. }
  72. }
  73. else
  74. {
  75. HashSet<string> foundHashSet = new HashSet<string>();
  76. foreach (var lineDependency in line.Dependencies)
  77. {
  78. if (result.ContainsKey(lineDependency))
  79. {
  80. foundHashSet.Add(lineDependency);
  81. }
  82. }
  83.  
  84. foreach (var item in foundHashSet)
  85. {
  86. line.Dependencies.Remove(item);
  87. line.Line = line.Line.Replace("#{" + item + "}", result[item]);
  88. }
  89. }
  90. }
  91.  
  92. if (prevCounter != counter)
  93. {
  94. DoEvaluate(lines, result, ref counter);
  95. }
  96. }
  97.  
  98. public static IEnumerable<LineWithDependencies> GetLineWithDependencieses(IEnumerable<string> lines)
  99. {
  100. foreach (var line in lines)
  101. {
  102. yield return GetLineWithDependencies(line);
  103. }
  104. }
  105.  
  106. public static LineWithDependencies GetLineWithDependencies(string line)
  107. {
  108. LineWithDependencies result = new LineWithDependencies();
  109. var parts = line.Split('|');
  110. if (parts.Length == 0)
  111. {
  112. return null;
  113. }
  114.  
  115. if (parts.Length == 1)
  116. {
  117. result.VariableName = parts[0];
  118. result.Line = "";
  119. }
  120. if (parts.Length == 2)
  121. {
  122. result.VariableName = parts[0];
  123. result.Line = parts[1];
  124. }
  125.  
  126. if (result.Line!=null)
  127. {
  128. var matches = Regex.Matches(result.Line, @"(?<=\#{)(.*?)(?=\})");
  129. foreach (Match match in matches)
  130. {
  131. result.Dependencies.Add(match.Value);
  132. }
  133. }
  134. return result;
  135. }
  136.  
  137. public class LineWithDependencies
  138. {
  139. public string VariableName { get; set; }
  140. public string Line { get; set; }
  141. public List<string> Dependencies { get; set; } = new List<string>();
  142.  
  143. public bool IsEmpty
  144. {
  145. get { return VariableName == null; }
  146. }
  147. }
  148.  
  149. public static IEnumerable<string> Process(IEnumerable<string> lines)
  150. {
  151. foreach (var line in lines)
  152. {
  153. var jObj = JObject.Parse(line);
  154. foreach (var variableSet in jObj["Variables"])
  155. {
  156. var name = variableSet["Name"];
  157. var value = variableSet["Value"];
  158. yield return name + "|" + value;
  159. }
  160. }
  161. }
  162.  
  163.  
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement