-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtils.cs
97 lines (88 loc) · 3.68 KB
/
Utils.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
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
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace DocFxForUnity
{
public sealed class Utils
{
/// <summary>
/// Client for send HTTP requests and receiving HTTP responses.
/// </summary>
private static readonly HttpClient httpClient = new();
/// <summary>
/// Copy a source file to a destination file. Intermediate folders will be automatically created.
/// </summary>
/// <param name="sourcePath">The path of the source file to copy.</param>
/// <param name="destPath">The destination path of the copied file.</param>
public static void CopyFile(string sourcePath, string destPath)
{
var destDirectoryPath = Path.GetDirectoryName(destPath);
Directory.CreateDirectory(destDirectoryPath);
File.Copy(sourcePath, destPath, overwrite: true);
}
/// <summary>
/// Deletes the specified directories if they exist.
/// </summary>
/// <param name="paths">The path of the directories to delete.</param>
public static void DeleteDirectories(params string[] paths)
{
foreach (var path in paths)
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
}
}
/// <summary>
/// Run a command in a hidden window and returns its output.
/// </summary>
/// <param name="command">The command to run.</param>
/// <param name="arguments">The arguments of the command.</param>
/// <param name="output">The function to call with the output data of the command.</param>
/// <param name="error">The function to call with the error data of the command.</param>
public static void RunCommand(string command, string arguments, Action<string> output, Action<string> error)
{
using var process = new Process();
process.StartInfo = new ProcessStartInfo(command, arguments)
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
process.OutputDataReceived += (sender, args) => output(args.Data);
process.ErrorDataReceived += (sender, args) => error(args.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
/// <summary>
/// Requests the specified URI with <see cref="httpClient"/> and returns if the response status code is in the
/// range 200-299.
/// </summary>
/// <param name="uri">The URI to request.</param>
/// <returns><c>true</c> if the response status code is in the range 200-299.</returns>
public static async Task<bool> TestUriExists(string uri)
{
try
{
var headRequest = new HttpRequestMessage(HttpMethod.Head, uri);
var response = await httpClient.SendAsync(headRequest);
if (!response.IsSuccessStatusCode && response.StatusCode != System.Net.HttpStatusCode.NotFound)
{
Console.Error.WriteLine($"Error: HTTP response code on {uri} is {response.StatusCode}");
}
return response.IsSuccessStatusCode;
}
catch (HttpRequestException e)
{
Console.WriteLine($"Exception on {uri}: {e.Message}");
return false;
}
}
}
}