diff --git a/MAPO.sln b/MAPO.sln index 746fb20f..0361c2d8 100644 --- a/MAPO.sln +++ b/MAPO.sln @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MP-SITE", "MP-SITE\MP-SITE. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapoSDK", "MapoSDK\MapoSDK.csproj", "{D07211B6-CF67-4C7F-8040-5B8C3B12BB4B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebConfigSetter", "WebConfigSetter\WebConfigSetter.csproj", "{58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -183,6 +185,22 @@ Global {D07211B6-CF67-4C7F-8040-5B8C3B12BB4B}.Prod|Any CPU.Build.0 = Release|Any CPU {D07211B6-CF67-4C7F-8040-5B8C3B12BB4B}.Release|Any CPU.ActiveCfg = Release|Any CPU {D07211B6-CF67-4C7F-8040-5B8C3B12BB4B}.Release|Any CPU.Build.0 = Release|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Debug|Any CPU.Build.0 = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Donati|Any CPU.ActiveCfg = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Donati|Any CPU.Build.0 = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.IIS01|Any CPU.ActiveCfg = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.IIS01|Any CPU.Build.0 = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.IIS02|Any CPU.ActiveCfg = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.IIS02|Any CPU.Build.0 = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Jetco|Any CPU.ActiveCfg = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Jetco|Any CPU.Build.0 = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.OVH-Demo|Any CPU.ActiveCfg = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.OVH-Demo|Any CPU.Build.0 = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Prod|Any CPU.ActiveCfg = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Prod|Any CPU.Build.0 = Debug|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Release|Any CPU.ActiveCfg = Release|Any CPU + {58E9A6EB-C1E7-4C0F-B497-C0DE02D7B950}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/WebConfigSetter/CONF/PlaceHolder.file b/WebConfigSetter/CONF/PlaceHolder.file new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/WebConfigSetter/CONF/PlaceHolder.file @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/WebConfigSetter/Objects.cs b/WebConfigSetter/Objects.cs new file mode 100644 index 00000000..c7a3334f --- /dev/null +++ b/WebConfigSetter/Objects.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace WebConfigSetter +{ + public enum operationMode + { + readConf = 0, + writeConf + } + + public class SetCliente + { + #region Public Fields + + public Dictionary appSettRules = new Dictionary(); + public Dictionary connStrRules = new Dictionary(); + + #endregion Public Fields + + #region Public Properties + + public string Cliente { get; set; } = "Steamware"; + + #endregion Public Properties + } +} \ No newline at end of file diff --git a/WebConfigSetter/Program.cs b/WebConfigSetter/Program.cs new file mode 100644 index 00000000..15c66aae --- /dev/null +++ b/WebConfigSetter/Program.cs @@ -0,0 +1,173 @@ +using Newtonsoft.Json; +using System; +using System.IO; +using System.Xml; + +namespace WebConfigSetter +{ + internal class Program + { + #region Protected Fields + + protected const string separatore = "---------------------------------------------------------------------------------------"; + + /// + /// stringa di setup cliente + /// + protected static SetCliente currConfig = new SetCliente(); + + #endregion Protected Fields + + #region Protected Properties + + protected static string baseDir { get; set; } = $"{Environment.CurrentDirectory}"; + protected static string baseWebAppDir { get; set; } = @"\\iis02\c$\inetpub\wwwroot\MP\"; + protected static string confDir { get; set; } = $"{baseDir}\\CONF"; + + protected static string currentSetFile { get; set; } = $"{confDir}\\default.json"; + protected static string globalConfFile { get; set; } = $"{confDir}\\settings.json"; + + /// + /// modalità scrittura (altrimenti legge e carica conf parsando i files) + /// + protected static operationMode opMode { get; set; } = operationMode.readConf; + + protected static string setName { get; set; } = "Steamware"; + + #endregion Protected Properties + + #region Private Methods + + private static void Main(string[] args) + { + //Console.WriteLine($"Base dir: {baseDir}"); + //Console.WriteLine($"Conf dir: {confDir}"); + //Console.WriteLine($"Set file: {setFile}"); + + if (args == null || args.Length == 0) + { + Console.WriteLine(separatore); + Console.WriteLine("Sintassi: WebConfigSetter.exe $1 $2 $3"); + Console.WriteLine("$1: Modalità operativa (0=read, 1=write)"); + Console.WriteLine("$2: WebApp directory di base"); + Console.WriteLine("$3: SetName da applicare"); + Console.WriteLine(separatore); + Console.WriteLine(""); + Console.WriteLine("Nessuna parametro fornito, verranno ora richiesti i parametri"); + Console.WriteLine(separatore); + + // chiedo tutti i dati... + string userInput = ""; + Console.WriteLine($"Modalità operativa 0=read, 1=write [{opMode}]"); + userInput = Console.ReadLine(); + if (!string.IsNullOrEmpty(userInput)) + { + opMode = userInput == "1" ? operationMode.writeConf : operationMode.readConf; + } + Console.WriteLine($"WebApp directory di base [{baseWebAppDir}]"); + userInput = Console.ReadLine(); + if (!string.IsNullOrEmpty(userInput)) + { + baseWebAppDir = userInput; + } + Console.WriteLine($"SetName da applicare [{setName}]"); + userInput = Console.ReadLine(); + if (!string.IsNullOrEmpty(userInput)) + { + baseWebAppDir = userInput; + } + } + else + { + // carico parametri opzionali + + if (args.Length > 0) + { + baseWebAppDir = args[0]; + } + if (args.Length > 1) + { + baseWebAppDir = args[1]; + } + if (args.Length > 2) + { + setName = args[2]; + } + } + + Console.WriteLine(separatore); + Console.WriteLine($"Modalità operativa: {opMode}"); + Console.WriteLine($"baseWebAppDir: {baseWebAppDir}"); + Console.WriteLine($"setName: {setName}"); + + // salvo info... + currConfig.Cliente = setName; + currentSetFile = $"{confDir}\\{setName}.json"; + + // processo richiesta + processRequest(); + + Console.WriteLine(separatore); + Console.Write("Premere un tasto x uscire"); + Console.ReadLine(); + } + + /// + /// Esecuzione richiesta principale + /// + private static void processRequest() + { + // caso 1: acquisisco conf + if (opMode == operationMode.readConf) + { + // cerca i file we.config dalla folder richiesta in giù... + string[] configFiles = Directory.GetFiles(baseWebAppDir, "web.config", SearchOption.AllDirectories); + XmlDocument doc = new XmlDocument(); + + foreach (var item in configFiles) + { + Console.WriteLine($"Found: {item}"); + + doc = new XmlDocument(); + doc.Load(item); + + // cerco in appConfig + XmlNodeList appSettingsList = doc.SelectNodes("configuration/appSettings/*"); + + for (int i = 0; i < appSettingsList.Count; i++) + { + if (appSettingsList[i].Attributes["key"].Value.Contains("ConnectionString")) + { + if (!currConfig.appSettRules.ContainsKey(appSettingsList[i].Attributes["key"].Value)) + { + currConfig.appSettRules.Add(appSettingsList[i].Attributes["key"].Value, appSettingsList[i].Attributes["value"].Value); + } + } + } + // cerco in connectionStrings + XmlNodeList connectionStringsList = doc.SelectNodes("configuration/connectionStrings/*"); + + for (int i = 0; i < connectionStringsList.Count; i++) + { + if (!currConfig.connStrRules.ContainsKey(connectionStringsList[i].Attributes["name"].Value)) + { + currConfig.connStrRules.Add(connectionStringsList[i].Attributes["name"].Value, connectionStringsList[i].Attributes["connectionString"].Value); + } + } + } + + // serializzo! + string rawData = JsonConvert.SerializeObject(currConfig, Newtonsoft.Json.Formatting.Indented); + + // salvo il file di conf! + File.WriteAllText(currentSetFile, rawData); + } + // caso 2: effettuo sostituzioni + else + { + } + } + + #endregion Private Methods + } +} \ No newline at end of file diff --git a/WebConfigSetter/WebConfigSetter.csproj b/WebConfigSetter/WebConfigSetter.csproj new file mode 100644 index 00000000..c7f13013 --- /dev/null +++ b/WebConfigSetter/WebConfigSetter.csproj @@ -0,0 +1,21 @@ + + + + Exe + net5.0 + + + + + + + + + Always + + + + + + + \ No newline at end of file