From 9a05c107ec36c19a5c91f1ce047469634cea927a Mon Sep 17 00:00:00 2001 From: NextTurn <45985406+NextTurn@users.noreply.github.com> Date: Fri, 7 Aug 2020 00:00:00 +0800 Subject: [PATCH] Fix path inconsistency --- src/WinSW.Core/Configuration/ServiceConfig.cs | 2 +- .../Configuration/XmlServiceConfig.cs | 44 ------------------ src/WinSW.Tests/Util/CommandLineTestHelper.cs | 8 ++-- src/WinSW/Program.cs | 45 ++++++++++++++----- 4 files changed, 39 insertions(+), 60 deletions(-) diff --git a/src/WinSW.Core/Configuration/ServiceConfig.cs b/src/WinSW.Core/Configuration/ServiceConfig.cs index 9462859..f81c69a 100644 --- a/src/WinSW.Core/Configuration/ServiceConfig.cs +++ b/src/WinSW.Core/Configuration/ServiceConfig.cs @@ -64,7 +64,7 @@ namespace WinSW.Configuration public virtual bool Preshutdown => false; // Logging - public virtual string LogDirectory => Path.GetDirectoryName(this.ExecutablePath)!; + public virtual string LogDirectory => Path.GetDirectoryName(this.FullPath)!; public virtual string LogMode => "append"; diff --git a/src/WinSW.Core/Configuration/XmlServiceConfig.cs b/src/WinSW.Core/Configuration/XmlServiceConfig.cs index 43b8a8f..87793df 100644 --- a/src/WinSW.Core/Configuration/XmlServiceConfig.cs +++ b/src/WinSW.Core/Configuration/XmlServiceConfig.cs @@ -39,45 +39,6 @@ namespace WinSW /// public string BaseName { get; set; } - public XmlServiceConfig() - { - string path = this.ExecutablePath; - string baseName = Path.GetFileNameWithoutExtension(path); - string baseDir = Path.GetDirectoryName(path)!; - - string fullPath = this.FullPath = Path.Combine(baseDir, baseName + ".xml"); - if (!File.Exists(fullPath)) - { - throw new FileNotFoundException("Unable to locate " + baseName + ".xml file within executable directory"); - } - - this.BaseName = baseName; - this.BasePath = Path.Combine(baseDir, baseName); - - try - { - this.dom.Load(fullPath); - } - catch (XmlException e) - { - throw new InvalidDataException(e.Message, e); - } - - // register the base directory as environment variable so that future expansions can refer to this. - Environment.SetEnvironmentVariable("BASE", baseDir); - - // ditto for ID - Environment.SetEnvironmentVariable("SERVICE_ID", this.Name); - - // New name - Environment.SetEnvironmentVariable(WinSWSystem.EnvVarNameExecutablePath, this.ExecutablePath); - - // Also inject system environment variables - Environment.SetEnvironmentVariable(WinSWSystem.EnvVarNameServiceId, this.Name); - - this.environmentVariables = this.LoadEnvironmentVariables(); - } - /// public XmlServiceConfig(string path) { @@ -128,11 +89,6 @@ namespace WinSW this.environmentVariables = this.LoadEnvironmentVariables(); } - internal static XmlServiceConfig Create(string? path) - { - return path != null ? new XmlServiceConfig(path) : TestConfig ?? new XmlServiceConfig(); - } - public static XmlServiceConfig FromXml(string xml) { var dom = new XmlDocument(); diff --git a/src/WinSW.Tests/Util/CommandLineTestHelper.cs b/src/WinSW.Tests/Util/CommandLineTestHelper.cs index 280cf62..f87b552 100644 --- a/src/WinSW.Tests/Util/CommandLineTestHelper.cs +++ b/src/WinSW.Tests/Util/CommandLineTestHelper.cs @@ -43,7 +43,7 @@ $@" Console.SetOut(swOut); Console.SetError(swError); - XmlServiceConfig.TestConfig = config ?? DefaultServiceConfig; + Program.TestConfig = config ?? DefaultServiceConfig; try { _ = Program.Main(arguments); @@ -52,7 +52,7 @@ $@" { Console.SetOut(tmpOut); Console.SetError(tmpError); - XmlServiceConfig.TestConfig = null; + Program.TestConfig = null; } Assert.Equal(string.Empty, swError.ToString()); @@ -77,7 +77,7 @@ $@" Console.SetOut(swOut); Console.SetError(swError); - XmlServiceConfig.TestConfig = config ?? DefaultServiceConfig; + Program.TestConfig = config ?? DefaultServiceConfig; Program.TestExceptionHandler = (e, _) => exception = e; try { @@ -91,7 +91,7 @@ $@" { Console.SetOut(tmpOut); Console.SetError(tmpError); - XmlServiceConfig.TestConfig = null; + Program.TestConfig = null; Program.TestExceptionHandler = null; } diff --git a/src/WinSW/Program.cs b/src/WinSW/Program.cs index bb83083..df45c15 100644 --- a/src/WinSW/Program.cs +++ b/src/WinSW/Program.cs @@ -34,6 +34,7 @@ namespace WinSW private static readonly ILog Log = LogManager.GetLogger(typeof(Program)); internal static Action? TestExceptionHandler; + internal static XmlServiceConfig? TestConfig; internal static string? TestExecutablePath; private static string ExecutablePath @@ -86,7 +87,7 @@ namespace WinSW XmlServiceConfig config = null!; try { - config = XmlServiceConfig.Create(pathToConfig); + config = CreateConfig(pathToConfig); } catch (FileNotFoundException) { @@ -370,7 +371,7 @@ namespace WinSW void Install(string? pathToConfig, bool noElevate, string? username, string? password) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -495,7 +496,7 @@ namespace WinSW void Uninstall(string? pathToConfig, bool noElevate) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -545,7 +546,7 @@ namespace WinSW void Start(string? pathToConfig, bool noElevate, bool noWait, CancellationToken ct) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -589,7 +590,7 @@ namespace WinSW void Stop(string? pathToConfig, bool noElevate, bool noWait, bool force, CancellationToken ct) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -641,7 +642,7 @@ namespace WinSW void Restart(string? pathToConfig, bool noElevate, bool force, CancellationToken ct) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -717,7 +718,7 @@ namespace WinSW void RestartSelf(string? pathToConfig) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -744,7 +745,7 @@ namespace WinSW static int Status(string? pathToConfig) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); using var svc = new ServiceController(config.Name); @@ -777,7 +778,7 @@ namespace WinSW void Test(string? pathToConfig, bool noElevate, int? timeout, bool noBreak) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -823,7 +824,7 @@ namespace WinSW void Refresh(string? pathToConfig, bool noElevate) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -876,7 +877,7 @@ namespace WinSW void DevPs(string? pathToConfig, bool noElevate) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); if (!elevated) { @@ -976,6 +977,28 @@ namespace WinSW } } + /// + private static XmlServiceConfig CreateConfig(string? path) + { + if (path != null) + { + return new XmlServiceConfig(path); + } + + if (TestConfig != null) + { + return TestConfig; + } + + path = Path.ChangeExtension(ExecutablePath, ".xml"); + if (!File.Exists(path)) + { + throw new FileNotFoundException("Unable to locate " + Path.GetFileNameWithoutExtension(path) + ".xml file within executable directory."); + } + + return new XmlServiceConfig(path); + } + private static void InitLoggers(XmlServiceConfig config, bool enableConsoleLogging) { if (XmlServiceConfig.TestConfig != null)