-
Notifications
You must be signed in to change notification settings - Fork 373
/
RelativePathExtensions.cs
50 lines (43 loc) · 1.65 KB
/
RelativePathExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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.DotNet.Interactive.CSharpProject;
public static class RelativePathExtensions
{
public static FileInfo Combine(
this DirectoryInfo directory,
RelativeFilePath filePath)
{
var filePart = filePath.Value;
if (filePart.StartsWith("./"))
{
filePart = filePart.Substring(2);
}
return new FileInfo(
Path.Combine(
directory.FullName,
filePart.Replace('/', Path.DirectorySeparatorChar)));
}
public static DirectoryInfo Combine(
this DirectoryInfo directory,
RelativeDirectoryPath directoryPath)
{
return new DirectoryInfo(
Path.Combine(
RelativePath.NormalizeDirectory(directory.FullName),
directoryPath.Value.Replace('/', Path.DirectorySeparatorChar)));
}
public static T Match<T>(this RelativePath path, Func<RelativeDirectoryPath, T> directory, Func<RelativeFilePath, T> file)
{
switch (path)
{
case RelativeDirectoryPath relativeDirectoryPath:
return directory(relativeDirectoryPath);
case RelativeFilePath relativeFilePath:
return file(relativeFilePath);
default:
throw new ArgumentOutOfRangeException($"Unexpected type derived from {nameof(RelativePath)}: {path.GetType().Name}");
}
}
}