forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIoExtensions.cs
More file actions
131 lines (106 loc) · 4.21 KB
/
Copy pathIoExtensions.cs
File metadata and controls
131 lines (106 loc) · 4.21 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
// Polyfills for System.IO APIs not available on .NET Framework.
// These are test-only and not optimized for production use.
#if !NET8_0_OR_GREATER
using System.Threading;
using System.Threading.Tasks;
namespace System.IO;
internal static class TestDownlevelPathExtensions
{
extension(Path)
{
public static string Join(string? path1, string? path2)
=> JoinCore(path1, path2);
public static string Join(string? path1, string? path2, string? path3)
=> JoinCore(path1, path2, path3);
public static string Join(string? path1, string? path2, string? path3, string? path4)
=> JoinCore(path1, path2, path3, path4);
public static string Join(params string?[] paths)
=> JoinCore(paths);
}
private static string JoinCore(params string?[] paths)
{
var sb = new System.Text.StringBuilder();
foreach (var path in paths)
{
if (string.IsNullOrEmpty(path))
{
continue;
}
if (sb.Length > 0 && !EndsWithSeparator(sb))
{
sb.Append(Path.DirectorySeparatorChar);
}
sb.Append(path);
}
return sb.ToString();
}
private static bool EndsWithSeparator(System.Text.StringBuilder sb) =>
sb.Length > 0 && (sb[sb.Length - 1] == Path.DirectorySeparatorChar || sb[sb.Length - 1] == Path.AltDirectorySeparatorChar);
}
internal static class TestDownlevelFileExtensions
{
extension(File)
{
public static Task<string> ReadAllTextAsync(string path, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.Run(() => File.ReadAllText(path), cancellationToken);
}
public static Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.Run(() => File.WriteAllText(path, contents), cancellationToken);
}
public static Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.Run(() => File.ReadAllBytes(path), cancellationToken);
}
public static Task WriteAllBytesAsync(string path, byte[] bytes, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.Run(() => File.WriteAllBytes(path, bytes), cancellationToken);
}
public static Task AppendAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
return Task.Run(() => File.AppendAllText(path, contents), cancellationToken);
}
public static void Move(string sourceFileName, string destFileName, bool overwrite)
{
if (overwrite && File.Exists(destFileName))
{
File.Delete(destFileName);
}
File.Move(sourceFileName, destFileName);
}
}
}
internal static class TestDownlevelFileSystemInfoExtensions
{
#pragma warning disable CA1822 // Mark members as static - extension members cannot be static
extension(FileSystemInfo info)
{
public string? LinkTarget => null;
public FileSystemInfo? ResolveLinkTarget(bool returnFinalTarget) => null;
}
#pragma warning restore CA1822
}
internal static class TestDownlevelTextReaderExtensions
{
extension(TextReader reader)
{
public Task<string> ReadToEndAsync(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<string>(cancellationToken);
}
return reader.ReadToEndAsync();
}
}
}
#endif