Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using Newtonsoft.Json.Linq;
- namespace ConsoleApp73
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> list = new List<string>();
- using (SqlConnection connection = new SqlConnection(args[0]))
- {
- connection.Open();
- var command = new SqlCommand("SELECT JSON FROM [HTInstaller].[dbo].[VariableSet]", connection);
- using (var sr = command.ExecuteReader())
- {
- while (sr.Read())
- {
- var value = sr.GetString(0);
- list.Add(value);
- }
- }
- }
- var result = ProcessLines(list).Result;
- using (FileStream fs = new FileStream(args[1], FileMode.OpenOrCreate, FileAccess.Write))
- {
- using (StreamWriter sw = new StreamWriter(fs))
- foreach (var item in result)
- {
- sw.WriteLine($"{item.Key}|{item.Value}");
- }
- }
- }
- public static async Task<Dictionary<string, string>> ProcessLines(IEnumerable<string> lines)
- {
- lines = Process(lines);
- var lwd = GetLineWithDependencieses(lines).Where(a=>a!=null && !a.IsEmpty).ToList();
- Dictionary<string, string> result = new Dictionary<string, string>();
- int counter = 0;
- DoEvaluate(lwd, result, ref counter);
- return result;
- }
- private static void DoEvaluate(IEnumerable<LineWithDependencies> lines, Dictionary<string, string> result, ref int counter)
- {
- int prevCounter = counter;
- foreach (var line in lines)
- {
- if (!line.Dependencies.Any())
- {
- if (!result.ContainsKey(line.VariableName))
- {
- result[line.VariableName] = line.Line;
- counter++;
- }
- }
- else
- {
- HashSet<string> foundHashSet = new HashSet<string>();
- foreach (var lineDependency in line.Dependencies)
- {
- if (result.ContainsKey(lineDependency))
- {
- foundHashSet.Add(lineDependency);
- }
- }
- foreach (var item in foundHashSet)
- {
- line.Dependencies.Remove(item);
- line.Line = line.Line.Replace("#{" + item + "}", result[item]);
- }
- }
- }
- if (prevCounter != counter)
- {
- DoEvaluate(lines, result, ref counter);
- }
- }
- public static IEnumerable<LineWithDependencies> GetLineWithDependencieses(IEnumerable<string> lines)
- {
- foreach (var line in lines)
- {
- yield return GetLineWithDependencies(line);
- }
- }
- public static LineWithDependencies GetLineWithDependencies(string line)
- {
- LineWithDependencies result = new LineWithDependencies();
- var parts = line.Split('|');
- if (parts.Length == 0)
- {
- return null;
- }
- if (parts.Length == 1)
- {
- result.VariableName = parts[0];
- result.Line = "";
- }
- if (parts.Length == 2)
- {
- result.VariableName = parts[0];
- result.Line = parts[1];
- }
- if (result.Line!=null)
- {
- var matches = Regex.Matches(result.Line, @"(?<=\#{)(.*?)(?=\})");
- foreach (Match match in matches)
- {
- result.Dependencies.Add(match.Value);
- }
- }
- return result;
- }
- public class LineWithDependencies
- {
- public string VariableName { get; set; }
- public string Line { get; set; }
- public List<string> Dependencies { get; set; } = new List<string>();
- public bool IsEmpty
- {
- get { return VariableName == null; }
- }
- }
- public static IEnumerable<string> Process(IEnumerable<string> lines)
- {
- foreach (var line in lines)
- {
- var jObj = JObject.Parse(line);
- foreach (var variableSet in jObj["Variables"])
- {
- var name = variableSet["Name"];
- var value = variableSet["Value"];
- yield return name + "|" + value;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement