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.

CustomScriptAssemblyMappingFinder.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor.Scripting.ScriptCompilation;
  5. namespace UnityEditor.TestTools.TestRunner.GUI.TestAssets
  6. {
  7. /// <inheritdoc />
  8. internal class CustomScriptAssemblyMappingFinder : ICustomScriptAssemblyMappingFinder
  9. {
  10. /// <inheritdoc />
  11. /// <exception cref="ArgumentNullException">The provided <paramref name="folderPath" /> string argument is null.</exception>
  12. public ICustomScriptAssembly FindCustomScriptAssemblyFromFolderPath(string folderPath)
  13. {
  14. if (folderPath == null)
  15. {
  16. throw new ArgumentNullException(nameof(folderPath));
  17. }
  18. var scriptInFolderPath = Path.Combine(folderPath, "Foo.cs");
  19. var customScriptAssembly = FindCustomScriptAssemblyFromScriptPath(scriptInFolderPath);
  20. return customScriptAssembly;
  21. }
  22. /// <summary>
  23. /// Finds the Custom Script Assembly associated with the provided script asset path.
  24. /// </summary>
  25. /// <param name="scriptPath">The script path to check.</param>
  26. /// <returns>The associated <see cref="ICustomScriptAssembly" />; null if none.</returns>
  27. private static ICustomScriptAssembly FindCustomScriptAssemblyFromScriptPath(string scriptPath)
  28. {
  29. try
  30. {
  31. var customScriptAssembly = EditorCompilationInterface.Instance.FindCustomScriptAssemblyFromScriptPath(scriptPath);
  32. return new CustomScriptAssemblyWrapper(customScriptAssembly);
  33. }
  34. catch (Exception)
  35. {
  36. return null;
  37. }
  38. }
  39. /// <summary>
  40. /// Custom Script Assembly wrapper.
  41. /// </summary>
  42. internal class CustomScriptAssemblyWrapper : ICustomScriptAssembly
  43. {
  44. internal readonly CustomScriptAssembly targetAssembly;
  45. /// <summary>
  46. /// Creates a new instance of the <see cref="CustomScriptAssemblyWrapper" /> class.
  47. /// </summary>
  48. /// <param name="assembly">The <see cref="CustomScriptAssembly" /> to be represented by the wrapper.</param>
  49. /// <exception cref="ArgumentNullException">The provided <paramref name="assembly" /> argument is null.</exception>
  50. internal CustomScriptAssemblyWrapper(CustomScriptAssembly assembly)
  51. {
  52. targetAssembly = assembly
  53. ?? throw new ArgumentNullException(nameof(assembly), "The provided assembly must not be null.");
  54. }
  55. /// <inheritdoc />
  56. public bool HasPrecompiledReference(string libraryFilename)
  57. {
  58. var precompiledReferences = targetAssembly.PrecompiledReferences;
  59. var libraryReferenceExists = precompiledReferences != null
  60. && precompiledReferences.Any(r => Path.GetFileName(r) == libraryFilename);
  61. return libraryReferenceExists;
  62. }
  63. /// <inheritdoc />
  64. public bool HasAssemblyFlag(AssemblyFlags flag)
  65. {
  66. var hasAssemblyFlag = (targetAssembly.AssemblyFlags & flag) == flag;
  67. return hasAssemblyFlag;
  68. }
  69. }
  70. }
  71. }