godot/modules/mono/editor/GodotTools/GodotTools.Core/StringExtensions.cs
Ignacio Etcheverry 1db0395950 C#: Fix csproj not synced on file move/removal from FS dock
When NormalizePath was called with an absolute
path (with drive letter) on Windows, it would
prepend a file path separator to the path, e.g.:
'\C:\Program Files\'.
Apparently this was still accepted as a valid
path by DotNetGlob and it stopped working when
we switched to MSBuildGlob.
2020-09-13 03:46:27 +02:00

83 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
namespace GodotTools.Core
{
public static class StringExtensions
{
public static string RelativeToPath(this string path, string dir)
{
// Make sure the directory ends with a path separator
dir = Path.Combine(dir, " ").TrimEnd();
if (Path.DirectorySeparatorChar == '\\')
dir = dir.Replace("/", "\\") + "\\";
var fullPath = new Uri(Path.GetFullPath(path), UriKind.Absolute);
var relRoot = new Uri(Path.GetFullPath(dir), UriKind.Absolute);
// MakeRelativeUri converts spaces to %20, hence why we need UnescapeDataString
return Uri.UnescapeDataString(relRoot.MakeRelativeUri(fullPath).ToString());
}
public static string NormalizePath(this string path)
{
if (string.IsNullOrEmpty(path))
return path;
bool rooted = path.IsAbsolutePath();
path = path.Replace('\\', '/');
path = path[path.Length - 1] == '/' ? path.Substring(0, path.Length - 1) : path;
string[] parts = path.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
path = string.Join(Path.DirectorySeparatorChar.ToString(), parts).Trim();
if (!rooted)
return path;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
string maybeDrive = parts[0];
if (maybeDrive.Length == 2 && maybeDrive[1] == ':')
return path; // Already has drive letter
}
return Path.DirectorySeparatorChar + path;
}
private static readonly string DriveRoot = Path.GetPathRoot(Environment.CurrentDirectory);
public static bool IsAbsolutePath(this string path)
{
return path.StartsWith("/", StringComparison.Ordinal) ||
path.StartsWith("\\", StringComparison.Ordinal) ||
path.StartsWith(DriveRoot, StringComparison.Ordinal);
}
public static string ToSafeDirName(this string dirName, bool allowDirSeparator = false)
{
var invalidChars = new List<string> {":", "*", "?", "\"", "<", ">", "|"};
if (allowDirSeparator)
{
// Directory separators are allowed, but disallow ".." to avoid going up the filesystem
invalidChars.Add("..");
}
else
{
invalidChars.Add("/");
}
string safeDirName = dirName.Replace("\\", "/").Trim();
foreach (string invalidChar in invalidChars)
safeDirName = safeDirName.Replace(invalidChar, "-");
return safeDirName;
}
}
}