From e4527b2901ea63d4defe1a89f85d628a05e5c448 Mon Sep 17 00:00:00 2001 From: Schwirg László Date: Fri, 22 Apr 2022 18:35:53 +0200 Subject: [PATCH] v1.11.4 - KeyGroupList osztály bevezetése a WindowsServicegroup helyett - folyamatban: a menuelemek csoportosítása (KeyGroup xml elem) és a csoportokból való előválasztást biztosító mechanizmus beépítése a menükezelőbe --- Vrh.Log4Pro.MaintenanceConsole/Manager - FileCleanerManager.cs | 15 ++++++++++++--- Vrh.Log4Pro.MaintenanceConsole/Manager - WindowsServiceManager.cs | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------- Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs | 4 ++-- 3 files changed, 115 insertions(+), 81 deletions(-) diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - FileCleanerManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - FileCleanerManager.cs index 7d0adcf..46fd9e7 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - FileCleanerManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - FileCleanerManager.cs @@ -184,18 +184,27 @@ namespace Vrh.Log4Pro.MaintenanceConsole.FileCleanerManagerNS #region class FileCleanerManagerCore public static class FileCleanerManagerCore { + #region IsDirectoryEmpty + public static bool IsDirectoryEmpty(DirectoryInfo d) + { + FileInfo[] fis = d.GetFiles("*", SearchOption.TopDirectoryOnly); + DirectoryInfo[] dis = d.GetDirectories(); + return (fis == null || !fis.Any()) && (dis == null || !dis.Any()); + } + #endregion IsDirectoryEmpty + #region public GetDirSize public static long DirSize(DirectoryInfo d, string filenamemask, string filefullpathregex, bool recurse) { long size = 0; // Add file sizes. FileInfo[] fis = d.GetFiles(filenamemask, SearchOption.TopDirectoryOnly); - foreach (FileInfo fi in fis) { if (Regex.Match(fi.FullName, filefullpathregex).Success) { size += fi.Length; } } + if (fis != null) { foreach (FileInfo fi in fis) { if (Regex.Match(fi.FullName, filefullpathregex).Success) { size += fi.Length; } } } // Add subdirectory sizes. if (recurse) { DirectoryInfo[] dis = d.GetDirectories(); - foreach (DirectoryInfo di in dis) { size += DirSize(di, filenamemask, filefullpathregex, recurse); } + if (dis != null) { foreach (DirectoryInfo di in dis) { size += DirSize(di, filenamemask, filefullpathregex, recurse); } } } return size; } @@ -288,7 +297,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.FileCleanerManagerNS { foreach (DirectoryInfo di in d.GetDirectories()) { cleanedsize += CleanFolderFiles(di, ftc, delete); } } - if (ftc.Xml_RemoveEmptyFolder && DirSize(d, ftc.Xml_IncludeMask, ftc.Xml_IncludeFullpathRegexp, ftc.Xml_Recurse) == 0) + if (ftc.Xml_RemoveEmptyFolder && IsDirectoryEmpty(d)) { try { Directory.Delete(d.FullName, ftc.Xml_Recurse); } catch { } } diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - WindowsServiceManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - WindowsServiceManager.cs index 09a7522..e160e65 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - WindowsServiceManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - WindowsServiceManager.cs @@ -37,39 +37,44 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS var config = new WindowsServiceManagerXmlProcessor(XMLCONNECTIONSTRING, "", "hu-HU"); var selectedserviceindexes = CommandLine.GetCommandLineArgument(args, CLP.Module.WindowsServiceManager.Function.CMD_SERVICES); var functionkey = CommandLine.GetCommandLineArgument(args, CLP.CMD_FUNCTION); + bool exitwasrequested = false; while (true) // servicegroup selection { - List selectedServicegroups = null; + KeyGroupList selectedServicegroups = null; List selectedServices = null; string selectedFunctionkey = null; if (!ColorConsole.SilentMode) { var menuservicegroups = DisplayWindowsServiceGroupMenu(config, $"Select the windows service group(s) to manage!"); - menuservicegroups.AddMenuItem(new Menu.ItemSeparator()); - menuservicegroups.AddMenuItem(new Menu.Item(CLP.Module.WindowsServiceManager.Function.Purge.KEY, "Purge")); - menuservicegroups.AddMenuItem(new Menu.ItemSeparator()); + if (menuservicegroups == null) { selectedServicegroups = null; if (exitwasrequested) { break; } } + else { + menuservicegroups.AddMenuItem(new Menu.ItemSeparator()); + menuservicegroups.AddMenuItem(new Menu.Item(CLP.Module.WindowsServiceManager.Function.Purge.KEY, "Purge")); + menuservicegroups.AddMenuItem(new Menu.ItemSeparator()); - menuservicegroups.DisplayTitle(); - menuservicegroups.DisplayItems(1); - Menu.Selection srg = menuservicegroups.Select(selectedserviceindexes); - if (srg.Result == Menu.SelectionResult.Exit) { break; } - else if (srg.Result == Menu.SelectionResult.None) { break; } - else if (srg.Result == Menu.SelectionResult.Error) { break; } - else if (srg.Result == Menu.SelectionResult.Ok) { } - else { } - if (srg.SelectedKeyList.Contains(CLP.Module.WindowsServiceManager.Function.Purge.KEY) && srg.SelectedKeyList.Count()>1) { continue; } - if (srg.SelectedKeyList.Contains(CLP.Module.WindowsServiceManager.Function.Purge.KEY)) { Purge(); continue; } - selectedServicegroups = srg.SelectedParameterList.Select(p => (p.Parameters as WindowsServiceGroup)).ToList(); + menuservicegroups.DisplayTitle(); + menuservicegroups.DisplayItems(1); + Menu.Selection srg = menuservicegroups.Select(selectedserviceindexes); + if (srg.Result == Menu.SelectionResult.Exit) { break; } + else if (srg.Result == Menu.SelectionResult.None) { break; } + else if (srg.Result == Menu.SelectionResult.Error) { break; } + else if (srg.Result == Menu.SelectionResult.Ok) { } + else { } + if (srg.SelectedKeyList.Contains(CLP.Module.WindowsServiceManager.Function.Purge.KEY) && srg.SelectedKeyList.Count() > 1) { continue; } + if (srg.SelectedKeyList.Contains(CLP.Module.WindowsServiceManager.Function.Purge.KEY)) { Purge(); continue; } + var _selectedServicegroups = srg.SelectedParameterList.Select(p => (p.Parameters as KeyGroupList.KeyGroup)).ToList(); + selectedServicegroups = new KeyGroupList(_selectedServicegroups); + } } while (true) //service and function selection { var menuservices = DisplayWindowsServiceMenu(config, $"Select the windows service(es) to manage!", selectedServicegroups); //menuservices.DisplayTitle(); - menuservices.DisplayItems(1, listheader: selectedServicegroups == null ? null : "Services for groups: " + string.Join(",", selectedServicegroups.Select(wsg => $"{wsg.Xml_Description} ({wsg.Xml_Key})"))); + menuservices.DisplayItems(1, listheader: selectedServicegroups == null ? null : "Services for groups: " + string.Join(",", selectedServicegroups.GetOneLineInfoList())); Menu.Selection sr = menuservices.Select(selectedserviceindexes); - if (sr.Result == Menu.SelectionResult.Exit) { break; } + if (sr.Result == Menu.SelectionResult.Exit) { exitwasrequested = true; break; } else if (sr.Result == Menu.SelectionResult.None) { break; } else if (sr.Result == Menu.SelectionResult.Error) { break; } else if (sr.Result == Menu.SelectionResult.Ok) { } @@ -88,7 +93,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS //menufunctions.DisplayTitle(); menufunctions.DisplayItems(1); Menu.Selection srf = menufunctions.Select(functionkey); - if (srf.Result == Menu.SelectionResult.Exit) { break; } + if (srf.Result == Menu.SelectionResult.Exit) { exitwasrequested = true; break; } else if (srf.Result == Menu.SelectionResult.None) { break; } else if (srf.Result == Menu.SelectionResult.Error) { break; } else if (srf.Result == Menu.SelectionResult.Ok) { } @@ -254,7 +259,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS #region DisplayServices private static void ServiceListDisplayer() { DisplayWindowsServiceMenu(); } - public static Menu DisplayWindowsServiceMenu(WindowsServiceManagerXmlProcessor config=null,string prompt = null,List servicegroups=null) + public static Menu DisplayWindowsServiceMenu(WindowsServiceManagerXmlProcessor config=null,string prompt = null, KeyGroupList servicegroups=null) { if (config==null) { config = new WindowsServiceManagerXmlProcessor(XMLCONNECTIONSTRING, "", "hu-HU"); } var menuservices = new Menu("Windows services",prompt) @@ -281,7 +286,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS ; menuservices.ClearMenuItemList(); - List wsgdefList = config.GetWindowsServiceGroupDefinitionList(); + KeyGroupList wsgdefList = config.WinServiceKeyGroupList; + if (wsgdefList == null || !wsgdefList.Any()) return null; foreach (var wsgdef in wsgdefList) { //string menuitemtext = wsgdef.Xml_Description + "(" + string.Join(",", wsgdef.Xml_WindowsServiceKeyList) + ")"; @@ -682,7 +688,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS List _winservicelist; List _winservicelistinstartorder; List _winservicelistinstoporder; - List _winservicegrouplist; + public KeyGroupList WinServiceKeyGroupList; #region constructor public WindowsServiceManagerXmlProcessor(string xmlcs, string basefolder, string lcid) : base(xmlcs, basefolder, lcid, null) { @@ -695,39 +701,19 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS _winservicelistinstartorder = ProduceWindowsServiceDefinitionListInStartOrder(); _winservicelistinstoporder = ProduceWindowsServiceDefinitionListInStartOrder(); _winservicelistinstoporder.Reverse(); - _winservicegrouplist = new List(); - var wsgxmllist = GetAllXElements(nameof(WindowsService.XmlStructure.WindowsServiceGroup)); - if (wsgxmllist != null && wsgxmllist.Any()) - { - foreach (var wsgxml in wsgxmllist) { var wsg = new WindowsServiceGroup(wsgxml); if (wsg.Valid) { _winservicegrouplist.Add(wsg); } } - } + WinServiceKeyGroupList = new KeyGroupList(this.RootElement); } #endregion constructor - #region GetWindowsServiceGroupDefinitionList - public WindowsServiceGroup GetWindowsServiceGroup(string key) { return _winservicegrouplist.FirstOrDefault(x => x.Xml_Key == key); } - public List GetWindowsServiceGroupDefinitionList() { return _winservicegrouplist; } - - - public List GetGroupWindowsServiceKeyList(string groupkey,out string groupdescription) - { - groupdescription = null; - List wsgdefList = GetWindowsServiceGroupDefinitionList().Where(sgr => sgr.Xml_Key == groupkey || groupkey == null).ToList(); - if (groupkey != null && wsgdefList != null && wsgdefList.Any()) { groupdescription = wsgdefList.First().Xml_Description; } - List allwskeys = new List(); - foreach (var wsg in wsgdefList) { allwskeys = allwskeys.Concat(wsg.Xml_WindowsServiceKeyList).ToList(); } - return allwskeys; - } - #endregion GetWindowsServiceGroupDefinitionList #region GetWindowsServiceDefinitionList public WindowsService GetWindowsService(string key) { return _winservicelist.FirstOrDefault(x => x.Xml_Key == key); } - public List GetWindowsServiceDefinitionList(List wsgList=null) + public List GetWindowsServiceDefinitionList(KeyGroupList wsgList =null) { if (wsgList == null) { return _winservicelist; } else { var wsList = new List(); - foreach (var wsg in wsgList) { wsList = wsList.Concat(wsg.Xml_WindowsServiceKeyList.Select(k=> _winservicelist.FirstOrDefault(ws=>ws.Xml_Key==k))).Where(ws=>ws!=null).ToList(); } + foreach (var wsg in wsgList) { wsList = wsList.Concat(wsg.Xml_KeyList.Select(k=> _winservicelist.FirstOrDefault(ws=>ws.Xml_Key==k))).Where(ws=>ws!=null).ToList(); } return wsList; } } @@ -760,6 +746,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS #endregion GetWindowsServiceDefinitionList } #endregion WindowsServiceManagerXmlProcessor class + #region WindowsService class public class WindowsService : XmlLinqBase { @@ -934,15 +921,6 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS #region XmlStructure public static class XmlStructure { - public static class WindowsServiceGroup - { - public static class Attributes - { - public static class Key { public static class Values { public static string DEFAULT = ""; } } - public static class Description { public static class Values { public static string DEFAULT = ""; } } - public static class WindowsServiceKeyList { public static class Values { public static string DEFAULT = ""; } } - } - } public static class WindowsService { public static class Attributes @@ -1030,34 +1008,81 @@ namespace Vrh.Log4Pro.MaintenanceConsole.WindowsServiceManagerNS #endregion public classes,types } #endregion WindowsService class - #region WindowsServiceGroup class - public class WindowsServiceGroup : XmlLinqBase - { - public bool Valid; - public List Xml_WindowsServiceKeyList; - public string Xml_Key; - public string Xml_Description; - public WindowsServiceGroup() { } - public WindowsServiceGroup(WindowsServiceGroup wsg) + + #region KeyGroupList class + public class KeyGroupList : List + { + public class KeyGroup : XmlLinqBase + { + public string GetOneLineInfo() { return $"{this.Xml_Description} ({this.Xml_Key})"; } + public bool Valid; + public List Xml_KeyList; + public string Xml_Key; + public string Xml_Description; + public KeyGroup() { } + public KeyGroup(KeyGroup wsg) + { + Xml_Key = wsg.Xml_Key; + Xml_KeyList = wsg.Xml_KeyList.Select(x => x).ToList(); + Xml_Description = wsg.Xml_Description; + Valid = wsg.Valid; + } + public KeyGroup(XElement keygroupxml) + { + Xml_Key = GetValue(nameof(XmlStructure.KeyGroup.Attributes.Key), keygroupxml, XmlStructure.KeyGroup.Attributes.Key.Values.DEFAULT); + Xml_Description = GetValue(nameof(XmlStructure.KeyGroup.Attributes.Description), keygroupxml, XmlStructure.KeyGroup.Attributes.Description.Values.DEFAULT); + var keycommaliststring = GetValue(nameof(XmlStructure.KeyGroup.Attributes.KeyList), keygroupxml, XmlStructure.KeyGroup.Attributes.KeyList.Values.DEFAULT); + Valid = false; + if (!string.IsNullOrWhiteSpace(keycommaliststring)) + { + Xml_KeyList = new List(keycommaliststring.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)); + if (Xml_KeyList.Contains("*")) { Xml_KeyList = new List() { "*" }; } + Valid = Xml_KeyList.Any(); + } + } + + #region XmlStructure + public static class XmlStructure + { + public static class KeyGroup + { + public static class Attributes + { + public static class Key { public static class Values { public static string DEFAULT = ""; } } + public static class Description { public static class Values { public static string DEFAULT = ""; } } + public static class KeyList { public static class Values { public static string DEFAULT = ""; } } + } + } + } + #endregion XmlStructure + } + public static KeyGroupList Factory(XElement keygrouplistcontainer) { - Xml_Key = wsg.Xml_Key; - Xml_WindowsServiceKeyList = wsg.Xml_WindowsServiceKeyList.Select(x => x).ToList(); - Xml_Description = wsg.Xml_Description; - Valid = wsg.Valid; + return new KeyGroupList(keygrouplistcontainer); } - public WindowsServiceGroup(XElement wsgxml) + public KeyGroupList(XElement keygrouplistcontainer) { - Xml_Key = GetValue(nameof(WindowsService.XmlStructure.WindowsServiceGroup.Attributes.Key), wsgxml, WindowsService.XmlStructure.WindowsServiceGroup.Attributes.Key.Values.DEFAULT); - Xml_Description = GetValue(nameof(WindowsService.XmlStructure.WindowsServiceGroup.Attributes.Description), wsgxml, WindowsService.XmlStructure.WindowsServiceGroup.Attributes.Description.Values.DEFAULT); - var keycommaliststring = GetValue(nameof(WindowsService.XmlStructure.WindowsServiceGroup.Attributes.WindowsServiceKeyList), wsgxml, WindowsService.XmlStructure.WindowsServiceGroup.Attributes.WindowsServiceKeyList.Values.DEFAULT); - Valid = false; - if (!string.IsNullOrWhiteSpace(keycommaliststring)) + var wsgxmllist = keygrouplistcontainer.Elements(XName.Get(nameof(KeyGroup.XmlStructure.KeyGroup))); + if (wsgxmllist != null && wsgxmllist.Any()) { - Xml_WindowsServiceKeyList = new List(keycommaliststring.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries)); - if (Xml_WindowsServiceKeyList.Contains("*")) { Xml_WindowsServiceKeyList = new List() { "*" }; } - Valid = Xml_WindowsServiceKeyList.Any(); + foreach (var wsgxml in wsgxmllist) { var wsg = new KeyGroup(wsgxml); if (wsg.Valid) { this.Add(wsg); } } } } + public KeyGroupList(List keygrouplist) + { + foreach (var kg in keygrouplist) { this.Add(kg); } + } + public List GetOneLineInfoList() { return this.Select(g=>g.GetOneLineInfo()).ToList(); } + public KeyGroup GetGroup(string key) { return this.FirstOrDefault(x => x.Xml_Key == key); } + public List GetGroupKeyList(string groupkey, out string groupdescription) + { + groupdescription = null; + List wsgdefList = this.Where(sgr => sgr.Xml_Key == groupkey || groupkey == null).ToList(); + if (groupkey != null && wsgdefList != null && wsgdefList.Any()) { groupdescription = wsgdefList.First().Xml_Description; } + List allwskeys = new List(); + foreach (var wsg in wsgdefList) { allwskeys = allwskeys.Concat(wsg.Xml_KeyList).ToList(); } + return allwskeys; + } } - #endregion WindowsServiceGroup class + #endregion KeyGroupList class } diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index 82b471e..cd0bffd 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.11.3.0")] -[assembly: AssemblyFileVersion("1.11.3.0")] +[assembly: AssemblyVersion("1.11.4.0")] +[assembly: AssemblyFileVersion("1.11.4.0")] -- libgit2 0.21.2