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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
using System;
using System.IO;
using System.Threading.Tasks;
namespace QtVsTools.Core.Common
{
using QtVsTools.Common;
public static partial class Utils
{
private static LazyFactory Lazy { get; } = new();
public static string PackageInstallPath => Lazy.Get(() => PackageInstallPath, () =>
{
var uri = new Uri(System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase);
return Path.GetDirectoryName(Uri.UnescapeDataString(uri.AbsolutePath)) + @"\";
});
public static string Unquote(string path)
{
path = path.Trim();
if (!string.IsNullOrEmpty(path) && path.StartsWith("\"") && path.EndsWith("\""))
return path.Substring(1, path.Length - 2);
return path;
}
public static string SafeQuote(string path)
{
if (string.IsNullOrEmpty(path))
return null;
path = Unquote(path);
if (!path.Contains(" "))
return path;
if (path.EndsWith("\\"))
path += Path.DirectorySeparatorChar;
return $"\"{path}\"";
}
/// <summary>
/// Recursively copies the contents of the given directory and all its children to the
/// specified target path. If the target path does not exist, it will be created.
/// </summary>
/// <param name="directory">The directory to copy.</param>
/// <param name="targetPath">The path where the contents will be copied to.</param>
public static void CopyDirectory(string directory, string targetPath)
{
var sourceDir = new DirectoryInfo(directory);
if (!sourceDir.Exists)
return;
try {
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
var files = sourceDir.GetFiles();
foreach (var file in files) {
try {
file.CopyTo(Path.Combine(targetPath, file.Name), true);
} catch (Exception exception) {
exception.Log();
}
}
} catch (Exception exception) {
exception.Log();
}
var subDirs = sourceDir.GetDirectories();
foreach (var subDir in subDirs)
CopyDirectory(subDir.FullName, Path.Combine(targetPath, subDir.Name));
}
/// <summary>
/// Asynchronously reads the contents of a text file and returns the content as a string.
/// </summary>
/// <param name="filePath">The path to the file to read.</param>
/// <returns>A task representing the asynchronous operation. The task result contains the
/// content of the file as a string.</returns>
public static async Task<string> ReadAllTextAsync(string filePath)
{
using var reader = File.OpenText(filePath);
return await reader.ReadToEndAsync();
}
/// <summary>
/// Asynchronously writes the specified string to a file, creating the file if it does
/// not exist, and overwriting the content if it does.
/// </summary>
/// <param name="filePath">The path to the file to write.</param>
/// <param name="text">The string to write to the file.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public static async Task WriteAllTextAsync(string filePath, string text)
{
using var writer = File.CreateText(filePath);
await writer.WriteAsync(text);
}
/// <summary>
/// Deletes a file specified by its path. Does not throw any exceptions.
/// </summary>
/// <param name="file">The path to the file to delete.</param>
public static void DeleteFile(string file)
{
try {
DeleteFile(new FileInfo(file));
} catch (Exception exception) {
exception.Log();
}
}
/// <summary>
/// Deletes a file if it exists. Does not throw any exceptions.
/// </summary>
/// <param name="file">The <see cref="FileInfo"/> object representing the file to delete.</param>
public static void DeleteFile(FileInfo file)
{
if (!file?.Exists ?? true)
return;
try {
file.Delete();
} catch (Exception exception) {
exception.Log();
}
}
/// <summary>
/// Specifies the options for directory deletion.
/// </summary>
public enum Option
{
/// <summary>
/// Only the specified directory itself is deleted, without deleting its contents.
/// </summary>
Shallow,
/// <summary>
/// The specified directory and all its contents, including subdirectories, are deleted.
/// </summary>
Recursive
}
/// <summary>
/// Recursively deletes the given directory and all its contents. Does not throw any
/// exceptions.
/// </summary>
/// <param name="path">The path of the directory to delete.</param>
/// <param name="option">Specifies the options for directory deletion.</param>
public static void DeleteDirectory(string path, Option option = Option.Shallow)
{
try {
DeleteDirectory(new DirectoryInfo(path), option);
} catch (Exception exception) {
exception.Log();
}
}
/// <summary>
/// Recursively deletes the given directory and all its contents. Does not throw any
/// exceptions.
/// </summary>
/// <param name="directory">The directory to delete.</param>
/// <param name="option">Specifies the options for directory deletion.</param>
public static void DeleteDirectory(DirectoryInfo directory, Option option = Option.Shallow)
{
if (!directory?.Exists ?? true)
return;
if (option == Option.Recursive) {
foreach (var file in directory.GetFiles())
DeleteFile(file);
try {
foreach (var subDir in directory.GetDirectories())
DeleteDirectory(subDir, option);
} catch (Exception exception) {
exception.Log();
}
}
try {
directory.Delete();
} catch (Exception exception) {
exception.Log();
}
}
}
}
|