dotnet-installer/src/Microsoft.DotNet.InternalAbstractions/DirectoryPath.cs
William Li 2594a6d7ec
Always store absolute full path in directory and file path (#9363)
There is no need to store relative path today. But some part of the system does not accept relative path and there is no indication if it is storing full path or not. This is the root cause of https://github.com/dotnet/cli/issues/9319

“someplace” means different full path for Path class on unix and Windows. And the mock file system uses real Path class. Change tests' setup to use essentially “TEMPATH/someplace” instead of  “someplace”
2018-06-06 11:22:19 -07:00

61 lines
1.7 KiB
C#

// 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; }
/// <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>
public DirectoryPath(string value)
{
if (!Path.IsPathRooted(value))
{
value = Path.GetFullPath(value);
}
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}\"";
}
public string ToXmlEncodeString()
{
return System.Net.WebUtility.HtmlEncode(Value);
}
public override string ToString()
{
return ToQuotedString();
}
public DirectoryPath GetParentPath()
{
return new DirectoryPath(Path.GetDirectoryName(Value));
}
}
}