No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileUtility.cs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Unity Technologies.
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. * Licensed under the MIT License. See License.txt in the project root for license information.
  5. *--------------------------------------------------------------------------------------------*/
  6. using System;
  7. using System.IO;
  8. using UnityEngine;
  9. namespace Microsoft.Unity.VisualStudio.Editor
  10. {
  11. internal static class FileUtility
  12. {
  13. public const char WinSeparator = '\\';
  14. public const char UnixSeparator = '/';
  15. public static string GetPackageAssetFullPath(params string[] components)
  16. {
  17. // Unity has special IO handling of Packages and will resolve those path to the right package location
  18. return Path.GetFullPath(Path.Combine("Packages", "com.unity.ide.visualstudio", Path.Combine(components)));
  19. }
  20. public static string GetAssetFullPath(string asset)
  21. {
  22. var basePath = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
  23. return Path.GetFullPath(Path.Combine(basePath, NormalizePathSeparators(asset)));
  24. }
  25. public static string NormalizePathSeparators(this string path)
  26. {
  27. if (string.IsNullOrEmpty(path))
  28. return path;
  29. if (Path.DirectorySeparatorChar == WinSeparator)
  30. path = path.Replace(UnixSeparator, WinSeparator);
  31. if (Path.DirectorySeparatorChar == UnixSeparator)
  32. path = path.Replace(WinSeparator, UnixSeparator);
  33. return path.Replace(string.Concat(WinSeparator, WinSeparator), WinSeparator.ToString());
  34. }
  35. public static string NormalizeWindowsToUnix(this string path)
  36. {
  37. if (string.IsNullOrEmpty(path))
  38. return path;
  39. return path.Replace(WinSeparator, UnixSeparator);
  40. }
  41. internal static bool IsFileInProjectRootDirectory(string fileName)
  42. {
  43. var relative = MakeRelativeToProjectPath(fileName);
  44. if (string.IsNullOrEmpty(relative))
  45. return false;
  46. return relative == Path.GetFileName(relative);
  47. }
  48. public static string MakeAbsolutePath(this string path)
  49. {
  50. if (string.IsNullOrEmpty(path)) { return string.Empty; }
  51. return Path.IsPathRooted(path) ? path : Path.GetFullPath(path);
  52. }
  53. // returns null if outside of the project scope
  54. internal static string MakeRelativeToProjectPath(string fileName)
  55. {
  56. var basePath = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
  57. fileName = NormalizePathSeparators(fileName);
  58. if (!Path.IsPathRooted(fileName))
  59. fileName = Path.Combine(basePath, fileName);
  60. if (!fileName.StartsWith(basePath, StringComparison.OrdinalIgnoreCase))
  61. return null;
  62. return fileName
  63. .Substring(basePath.Length)
  64. .Trim(Path.DirectorySeparatorChar);
  65. }
  66. }
  67. }