Fix path inconsistency

pull/639/head
NextTurn 2020-08-07 00:00:00 +08:00 committed by Next Turn
parent e49c8c66bc
commit 788a0ff64b
4 changed files with 39 additions and 60 deletions

View File

@ -64,7 +64,7 @@ namespace WinSW.Configuration
public virtual bool Preshutdown => false; public virtual bool Preshutdown => false;
// Logging // Logging
public virtual string LogDirectory => Path.GetDirectoryName(this.ExecutablePath)!; public virtual string LogDirectory => Path.GetDirectoryName(this.FullPath)!;
public virtual string LogMode => "append"; public virtual string LogMode => "append";

View File

@ -39,45 +39,6 @@ namespace WinSW
/// </summary> /// </summary>
public string BaseName { get; set; } 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();
}
/// <exception cref="FileNotFoundException" /> /// <exception cref="FileNotFoundException" />
public XmlServiceConfig(string path) public XmlServiceConfig(string path)
{ {
@ -128,11 +89,6 @@ namespace WinSW
this.environmentVariables = this.LoadEnvironmentVariables(); 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) public static XmlServiceConfig FromXml(string xml)
{ {
var dom = new XmlDocument(); var dom = new XmlDocument();

View File

@ -43,7 +43,7 @@ $@"<service>
Console.SetOut(swOut); Console.SetOut(swOut);
Console.SetError(swError); Console.SetError(swError);
XmlServiceConfig.TestConfig = config ?? DefaultServiceConfig; Program.TestConfig = config ?? DefaultServiceConfig;
try try
{ {
_ = Program.Main(arguments); _ = Program.Main(arguments);
@ -52,7 +52,7 @@ $@"<service>
{ {
Console.SetOut(tmpOut); Console.SetOut(tmpOut);
Console.SetError(tmpError); Console.SetError(tmpError);
XmlServiceConfig.TestConfig = null; Program.TestConfig = null;
} }
Assert.Equal(string.Empty, swError.ToString()); Assert.Equal(string.Empty, swError.ToString());
@ -77,7 +77,7 @@ $@"<service>
Console.SetOut(swOut); Console.SetOut(swOut);
Console.SetError(swError); Console.SetError(swError);
XmlServiceConfig.TestConfig = config ?? DefaultServiceConfig; Program.TestConfig = config ?? DefaultServiceConfig;
Program.TestExceptionHandler = (e, _) => exception = e; Program.TestExceptionHandler = (e, _) => exception = e;
try try
{ {
@ -91,7 +91,7 @@ $@"<service>
{ {
Console.SetOut(tmpOut); Console.SetOut(tmpOut);
Console.SetError(tmpError); Console.SetError(tmpError);
XmlServiceConfig.TestConfig = null; Program.TestConfig = null;
Program.TestExceptionHandler = null; Program.TestExceptionHandler = null;
} }

View File

@ -33,6 +33,7 @@ namespace WinSW
private static readonly ILog Log = LogManager.GetLogger(typeof(Program)); private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
internal static Action<Exception, InvocationContext>? TestExceptionHandler; internal static Action<Exception, InvocationContext>? TestExceptionHandler;
internal static XmlServiceConfig? TestConfig;
internal static string? TestExecutablePath; internal static string? TestExecutablePath;
private static string ExecutablePath private static string ExecutablePath
@ -85,7 +86,7 @@ namespace WinSW
XmlServiceConfig config = null!; XmlServiceConfig config = null!;
try try
{ {
config = XmlServiceConfig.Create(pathToConfig); config = CreateConfig(pathToConfig);
} }
catch (FileNotFoundException) catch (FileNotFoundException)
{ {
@ -355,7 +356,7 @@ namespace WinSW
void Install(string? pathToConfig, bool noElevate, string? username, string? password) void Install(string? pathToConfig, bool noElevate, string? username, string? password)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
InitLoggers(config, enableConsoleLogging: true); InitLoggers(config, enableConsoleLogging: true);
if (!elevated) if (!elevated)
@ -480,7 +481,7 @@ namespace WinSW
void Uninstall(string? pathToConfig, bool noElevate) void Uninstall(string? pathToConfig, bool noElevate)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
InitLoggers(config, enableConsoleLogging: true); InitLoggers(config, enableConsoleLogging: true);
if (!elevated) if (!elevated)
@ -530,7 +531,7 @@ namespace WinSW
void Start(string? pathToConfig, bool noElevate, bool noWait, CancellationToken ct) void Start(string? pathToConfig, bool noElevate, bool noWait, CancellationToken ct)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
InitLoggers(config, enableConsoleLogging: true); InitLoggers(config, enableConsoleLogging: true);
if (!elevated) if (!elevated)
@ -574,7 +575,7 @@ namespace WinSW
void Stop(string? pathToConfig, bool noElevate, bool noWait, bool force, CancellationToken ct) 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); InitLoggers(config, enableConsoleLogging: true);
if (!elevated) if (!elevated)
@ -626,7 +627,7 @@ namespace WinSW
void Restart(string? pathToConfig, bool noElevate, bool force, CancellationToken ct) void Restart(string? pathToConfig, bool noElevate, bool force, CancellationToken ct)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
InitLoggers(config, enableConsoleLogging: true); InitLoggers(config, enableConsoleLogging: true);
if (!elevated) if (!elevated)
@ -702,7 +703,7 @@ namespace WinSW
void RestartSelf(string? pathToConfig) void RestartSelf(string? pathToConfig)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
InitLoggers(config, enableConsoleLogging: true); InitLoggers(config, enableConsoleLogging: true);
if (!elevated) if (!elevated)
@ -729,7 +730,7 @@ namespace WinSW
static int Status(string? pathToConfig) static int Status(string? pathToConfig)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
InitLoggers(config, enableConsoleLogging: true); InitLoggers(config, enableConsoleLogging: true);
using var svc = new ServiceController(config.Name); using var svc = new ServiceController(config.Name);
@ -762,7 +763,7 @@ namespace WinSW
void Test(string? pathToConfig, bool noElevate, bool noBreak) void Test(string? pathToConfig, bool noElevate, bool noBreak)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
InitLoggers(config, enableConsoleLogging: true); InitLoggers(config, enableConsoleLogging: true);
if (!elevated) if (!elevated)
@ -805,7 +806,7 @@ namespace WinSW
void Refresh(string? pathToConfig, bool noElevate) void Refresh(string? pathToConfig, bool noElevate)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
InitLoggers(config, enableConsoleLogging: true); InitLoggers(config, enableConsoleLogging: true);
if (!elevated) if (!elevated)
@ -858,7 +859,7 @@ namespace WinSW
void DevPs(string? pathToConfig, bool noElevate) void DevPs(string? pathToConfig, bool noElevate)
{ {
XmlServiceConfig config = XmlServiceConfig.Create(pathToConfig); XmlServiceConfig config = CreateConfig(pathToConfig);
if (!elevated) if (!elevated)
{ {
@ -958,6 +959,28 @@ namespace WinSW
} }
} }
/// <exception cref="FileNotFoundException" />
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) private static void InitLoggers(XmlServiceConfig config, bool enableConsoleLogging)
{ {
if (XmlServiceConfig.TestConfig != null) if (XmlServiceConfig.TestConfig != null)