FileIOProvider.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.IO;
  3. using System.Security;
  4. using System.Text;
  5. using SingularityGroup.HotReload.Editor.Util;
  6. namespace SingularityGroup.HotReload.Editor.ProjectGeneration {
  7. class FileIOProvider : IFileIO
  8. {
  9. public bool Exists(string fileName)
  10. {
  11. return File.Exists(fileName);
  12. }
  13. public string ReadAllText(string fileName)
  14. {
  15. return File.ReadAllText(fileName);
  16. }
  17. public void WriteAllText(string path, string content)
  18. {
  19. File.WriteAllText(path, content, Encoding.UTF8);
  20. }
  21. public string EscapedRelativePathFor(string file, string projectDirectory)
  22. {
  23. var projectDir = Path.GetFullPath(projectDirectory);
  24. // We have to normalize the path, because the PackageManagerRemapper assumes
  25. // dir seperators will be os specific.
  26. var absolutePath = Path.GetFullPath(file.NormalizePath());
  27. var path = SkipPathPrefix(absolutePath, projectDir);
  28. return SecurityElement.Escape(path);
  29. }
  30. private static string SkipPathPrefix(string path, string prefix)
  31. {
  32. return path.StartsWith($@"{prefix}{Path.DirectorySeparatorChar}", StringComparison.Ordinal)
  33. ? path.Substring(prefix.Length + 1)
  34. : path;
  35. }
  36. }
  37. }