From 788a0ff64b6d09de3acc23e1457c78ca68c06a3e 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 158f364..aedb3fd 100644 --- a/src/WinSW/Program.cs +++ b/src/WinSW/Program.cs @@ -33,6 +33,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 @@ -85,7 +86,7 @@ namespace WinSW XmlServiceConfig config = null!; try { - config = XmlServiceConfig.Create(pathToConfig); + config = CreateConfig(pathToConfig); } catch (FileNotFoundException) { @@ -355,7 +356,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) @@ -480,7 +481,7 @@ namespace WinSW void Uninstall(string? pathToConfig, bool noElevate) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -530,7 +531,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) @@ -574,7 +575,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) @@ -626,7 +627,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) @@ -702,7 +703,7 @@ namespace WinSW void RestartSelf(string? pathToConfig) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -729,7 +730,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); @@ -762,7 +763,7 @@ namespace WinSW void Test(string? pathToConfig, bool noElevate, bool noBreak) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -805,7 +806,7 @@ namespace WinSW void Refresh(string? pathToConfig, bool noElevate) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); InitLoggers(config, enableConsoleLogging: true); if (!elevated) @@ -858,7 +859,7 @@ namespace WinSW void DevPs(string? pathToConfig, bool noElevate) { - XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); + XmlServiceConfig config = CreateConfig(pathToConfig); if (!elevated) { @@ -958,6 +959,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)