public static string Evaluate(String text, ClientNodeConfig nodeConfig) { Dictionary <String, String> variableLookup = CreateEvaluationLookup(nodeConfig); string newValue = Evaluate(text, variableLookup); if (newValue.StartsWith(".")) { newValue = Path.Combine(nodeConfig.NetworkDirectory, newValue.Substring(1).Trim('\\')); } return(newValue); }
private static Dictionary <String, String> CreateEvaluationLookup(ClientNodeConfig nodeConfig) { Dictionary <String, String> variableLookup = new Dictionary <string, string>(); foreach (PropertyInfo property in typeof(ClientNodeConfig).GetProperties()) { if (property.PropertyType == typeof(String)) { string propertyName = property.Name; string propertyValue = property.GetValue(nodeConfig) as String; variableLookup.Add(propertyName, propertyValue); } } return(variableLookup); }
public ClientConfig ReadConfig() { ClientConfig config = new ClientConfig(); nodeConfigFile = new FileInfo(ConfigFileName); StreamReader configReader = new StreamReader(nodeConfigFile.FullName); string sectionName = string.Empty; int lineNumber = 1; while (!configReader.EndOfStream) { string line = configReader.ReadLine(); if (line.Trim() == String.Empty) { lineNumber++; continue; } if (sectionNameRegex.IsMatch(line)) { sectionName = sectionNameRegex.Match(line).Groups[1].Value; } else if (commentRegEx.IsMatch(line)) { //ignore comments } else if (parameterRegEx.IsMatch(line)) { try { string key = parameterRegEx.Match(line).Groups[1].Value; string value = parameterRegEx.Match(line).Groups[2].Value; if (sectionName == "Global" && key.ToLower() == "deploy") { Resource resource = CreateDeploymentResource("Global", value); config.FileDeploy.Add(resource); } else if (sectionName == "Global" && key.ToLower() != "deploy") { SetProperty(config, key, value); } else { ClientNodeConfig nodeConfig; if (!config.NodeItems.ContainsKey(sectionName)) { nodeConfig = new ClientNodeConfig(sectionName); config.NodeItems.Add(sectionName, nodeConfig); } else { nodeConfig = config.NodeItems[sectionName]; } if (key.ToLower() == "deploy") { Resource resource = CreateDeploymentResource(sectionName, value); config.FileDeploy.Add(resource); } else { SetProperty(nodeConfig, key, value); } } } catch (Exception ex) { throw new ArgumentException($"Error reading node configuration file at line {lineNumber}: {ex.Message}"); } } else { throw new ArgumentException($"Incorrect configuration option at line: {lineNumber} (--> \"{line}\")"); } lineNumber++; } //Enumerate variables to resolve any parameter values foreach (ClientNodeConfig node in config.NodeItems.Values) { Dictionary <String, String> variables = CreateEvaluationLookup(node); foreach (string variableName in variables.Keys) { if (string.IsNullOrEmpty(variables[variableName])) { continue; } string newValue = Evaluate(variables[variableName], variables); if (newValue.StartsWith(".")) { newValue = Path.Combine(node.NetworkDirectory, newValue.Substring(1).Trim('\\')); } if (newValue != variables[variableName]) { SetProperty(node, variableName, newValue); variables[variableName] = newValue; } } var resourceList = config.FileDeploy.Where(d => d.FullNodeName == node.NodeEndpoint.FullNodeName); foreach (Resource resource in resourceList) { resource.ClientPath = Evaluate(resource.ClientPath, variables); resource.AgentPath = Evaluate(resource.AgentPath, variables); if (resource.AgentPath.StartsWith(".")) { resource.AgentPath = Path.Combine(node.NetworkDirectory, resource.AgentPath.Substring(1).Trim('\\')); } } } return(config); }