2017-11-21 20:10:06 -08:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Extensions.EnvironmentAbstractions
|
|
|
|
|
{
|
|
|
|
|
public struct DirectoryPath
|
|
|
|
|
{
|
|
|
|
|
public string Value { get; }
|
|
|
|
|
|
2018-06-06 11:22:19 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create DirectoryPath to repesent a absolute directory path. Note it may not exist.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">If the value is not rooted. Path.GetFullPath will be called during the consturctor.</param>
|
2017-11-21 20:10:06 -08:00
|
|
|
|
public DirectoryPath(string value)
|
|
|
|
|
{
|
2018-06-06 11:22:19 -07:00
|
|
|
|
if (!Path.IsPathRooted(value))
|
|
|
|
|
{
|
|
|
|
|
value = Path.GetFullPath(value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 20:10:06 -08:00
|
|
|
|
Value = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DirectoryPath WithSubDirectories(params string[] paths)
|
|
|
|
|
{
|
|
|
|
|
string[] insertValueInFront = new string[paths.Length + 1];
|
|
|
|
|
insertValueInFront[0] = Value;
|
|
|
|
|
Array.Copy(paths, 0, insertValueInFront, 1, paths.Length);
|
|
|
|
|
|
|
|
|
|
return new DirectoryPath(Path.Combine(insertValueInFront));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FilePath WithFile(string fileName)
|
|
|
|
|
{
|
|
|
|
|
return new FilePath(Path.Combine(Value, fileName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ToQuotedString()
|
|
|
|
|
{
|
|
|
|
|
return $"\"{Value}\"";
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 15:58:05 -08:00
|
|
|
|
public string ToXmlEncodeString()
|
|
|
|
|
{
|
|
|
|
|
return System.Net.WebUtility.HtmlEncode(Value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 20:10:06 -08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return ToQuotedString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DirectoryPath GetParentPath()
|
|
|
|
|
{
|
2018-06-06 11:22:19 -07:00
|
|
|
|
return new DirectoryPath(Path.GetDirectoryName(Value));
|
2017-11-21 20:10:06 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|