diff --git a/src/Core/ServiceWrapper/Program.cs b/src/Core/ServiceWrapper/Program.cs index 05152e8..c1fd861 100644 --- a/src/Core/ServiceWrapper/Program.cs +++ b/src/Core/ServiceWrapper/Program.cs @@ -21,6 +21,7 @@ using log4net.Layout; using WinSW.Configuration; using WinSW.Logging; using WinSW.Native; +using WinSW.Util; using WMI; using ServiceType = WMI.ServiceType; @@ -63,8 +64,20 @@ namespace WinSW { bool inConsoleMode = argsArray.Length > 0; + string baseName; + DirectoryInfo d; + + var configType = GetConfigType(out baseName, out d); + // If descriptor is not specified, initialize the new one (and load configs from there) - descriptor ??= new ServiceDescriptor(); + if (configType == ConfigType.XML) + { + descriptor ??= new ServiceDescriptor(baseName, d); + } + else + { + descriptor ??= new ServiceDescriptorYaml(baseName, d).Configurations; + } // Configure the wrapper-internal logging. // STDOUT and STDERR of the child process will be handled independently. @@ -658,6 +671,39 @@ namespace WinSW } } + private static ConfigType GetConfigType(out string baseName, out DirectoryInfo d) + { + var executablePath = new DefaultWinSWSettings().ExecutablePath; + baseName = Path.GetFileNameWithoutExtension(executablePath); + + if (baseName.EndsWith(".vshost")) + { + baseName = baseName.Substring(0, baseName.Length - 7); + } + + d = new DirectoryInfo(Path.GetDirectoryName(executablePath)); + + while (true) + { + if (File.Exists(Path.Combine(d.FullName, baseName + ".xml"))) + { + return ConfigType.XML; + } + + if (File.Exists(Path.Combine(d.FullName, baseName + ".yml"))) + { + return ConfigType.YAML; + } + + if (d.Parent is null) + { + throw new FileNotFoundException("Unable to locate " + baseName + ".xml " + "or " + baseName + ".yml " + "file within executable directory or any parents"); + } + + d = d.Parent; + } + } + private static void PrintHelp() { Console.WriteLine("A wrapper binary that can be used to host executables as Windows services"); diff --git a/src/Core/ServiceWrapper/WrapperService.cs b/src/Core/ServiceWrapper/WrapperService.cs index 5b53bba..04ba60d 100644 --- a/src/Core/ServiceWrapper/WrapperService.cs +++ b/src/Core/ServiceWrapper/WrapperService.cs @@ -73,11 +73,6 @@ namespace WinSW eventLogProvider.Service = this; } - public WrapperService() - : this(new ServiceDescriptor()) - { - } - /// /// Process the file copy instructions, so that we can replace files that are always in use while /// the service runs. diff --git a/src/Core/WinSWCore/ServiceDescriptor.cs b/src/Core/WinSWCore/ServiceDescriptor.cs index 4d31d83..78d8872 100644 --- a/src/Core/WinSWCore/ServiceDescriptor.cs +++ b/src/Core/WinSWCore/ServiceDescriptor.cs @@ -41,34 +41,8 @@ namespace WinSW // Currently there is no opportunity to alter the executable path public virtual string ExecutablePath => Defaults.ExecutablePath; - public ServiceDescriptor() + public ServiceDescriptor(string baseName, DirectoryInfo d) { - // find co-located configuration xml. We search up to the ancestor directories to simplify debugging, - // as well as trimming off ".vshost" suffix (which is used during debugging) - // Get the first parent to go into the recursive loop - string p = this.ExecutablePath; - string baseName = Path.GetFileNameWithoutExtension(p); - if (baseName.EndsWith(".vshost")) - { - baseName = baseName.Substring(0, baseName.Length - 7); - } - - DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(p)); - while (true) - { - if (File.Exists(Path.Combine(d.FullName, baseName + ".xml"))) - { - break; - } - - if (d.Parent is null) - { - throw new FileNotFoundException("Unable to locate " + baseName + ".xml file within executable directory or any parents"); - } - - d = d.Parent; - } - this.BaseName = baseName; this.BasePath = Path.Combine(d.FullName, this.BaseName); diff --git a/src/Core/WinSWCore/ServiceDescriptorYaml.cs b/src/Core/WinSWCore/ServiceDescriptorYaml.cs index 0dec860..332b78b 100644 --- a/src/Core/WinSWCore/ServiceDescriptorYaml.cs +++ b/src/Core/WinSWCore/ServiceDescriptorYaml.cs @@ -7,35 +7,12 @@ namespace WinSW { public class ServiceDescriptorYaml { - public readonly YamlConfiguration Configurations = new YamlConfiguration(); + public readonly YamlConfiguration Configurations; public static DefaultWinSWSettings Defaults { get; } = new DefaultWinSWSettings(); - public ServiceDescriptorYaml() + public ServiceDescriptorYaml(string baseName, DirectoryInfo d) { - string p = Defaults.ExecutablePath; - string baseName = Path.GetFileNameWithoutExtension(p); - if (baseName.EndsWith(".vshost")) - { - baseName = baseName.Substring(0, baseName.Length - 7); - } - - DirectoryInfo d = new DirectoryInfo(Path.GetDirectoryName(p)); - while (true) - { - if (File.Exists(Path.Combine(d.FullName, baseName + ".yml"))) - { - break; - } - - if (d.Parent is null) - { - throw new FileNotFoundException("Unable to locate " + baseName + ".yml file within executable directory or any parents"); - } - - d = d.Parent; - } - var basepath = Path.Combine(d.FullName, baseName); using (var reader = new StreamReader(basepath + ".yml")) diff --git a/src/Core/WinSWCore/Util/ConfigType.cs b/src/Core/WinSWCore/Util/ConfigType.cs new file mode 100644 index 0000000..e3ba976 --- /dev/null +++ b/src/Core/WinSWCore/Util/ConfigType.cs @@ -0,0 +1,8 @@ +namespace WinSW.Util +{ + public enum ConfigType + { + XML, + YAML + } +}