GenerateSharedFrameworkDepsFile.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using Microsoft.Build.Framework;
  11. using Microsoft.Build.Utilities;
  12. using Microsoft.Extensions.DependencyModel;
  13. namespace RepoTasks;
  14. public class GenerateSharedFrameworkDepsFile : Microsoft.Build.Utilities.Task
  15. {
  16. [Required]
  17. public string DepsFilePath { get; set; }
  18. [Required]
  19. public string TargetFramework { get; set; }
  20. [Required]
  21. public string FrameworkName { get; set; }
  22. [Required]
  23. public string FrameworkVersion { get; set; }
  24. [Required]
  25. public ITaskItem[] References { get; set; }
  26. [Required]
  27. public string RuntimeIdentifier { get; set; }
  28. [Required]
  29. public string RuntimePackageName { get; set; }
  30. [Required]
  31. public string PlatformManifestOutputPath { get; set; }
  32. public override bool Execute()
  33. {
  34. ExecuteCore();
  35. return !Log.HasLoggedErrors;
  36. }
  37. private void ExecuteCore()
  38. {
  39. var target = new TargetInfo(TargetFramework, RuntimeIdentifier, string.Empty, isPortable: false);
  40. var runtimeFiles = new List<RuntimeFile>();
  41. var nativeFiles = new List<RuntimeFile>();
  42. var resourceAssemblies = new List<ResourceAssembly>();
  43. var platformManifest = new List<string>();
  44. foreach (var reference in References)
  45. {
  46. var filePath = reference.ItemSpec;
  47. var fileName = Path.GetFileName(filePath);
  48. var fileVersion = FileUtilities.GetFileVersion(filePath)?.ToString() ?? string.Empty;
  49. var assemblyVersion = FileUtilities.GetAssemblyName(filePath)?.Version;
  50. if (assemblyVersion == null)
  51. {
  52. var nativeFile = new RuntimeFile(fileName, null, fileVersion);
  53. nativeFiles.Add(nativeFile);
  54. platformManifest.Add($"{fileName}|{FrameworkName}||{fileVersion}");
  55. }
  56. else
  57. {
  58. var runtimeFile = new RuntimeFile(fileName,
  59. fileVersion: fileVersion,
  60. assemblyVersion: assemblyVersion.ToString());
  61. runtimeFiles.Add(runtimeFile);
  62. platformManifest.Add($"{fileName}|{FrameworkName}|{assemblyVersion}|{fileVersion}");
  63. }
  64. }
  65. var runtimeLibrary = new RuntimeLibrary("package",
  66. RuntimePackageName,
  67. FrameworkVersion,
  68. hash: string.Empty,
  69. runtimeAssemblyGroups: new[] { new RuntimeAssetGroup(string.Empty, runtimeFiles) },
  70. nativeLibraryGroups: new[] { new RuntimeAssetGroup(string.Empty, nativeFiles) },
  71. Enumerable.Empty<ResourceAssembly>(),
  72. Array.Empty<Dependency>(),
  73. hashPath: null,
  74. path: $"{RuntimePackageName.ToLowerInvariant()}/{FrameworkVersion}",
  75. serviceable: true);
  76. var context = new DependencyContext(target,
  77. CompilationOptions.Default,
  78. Enumerable.Empty<CompilationLibrary>(),
  79. new[] { runtimeLibrary },
  80. Enumerable.Empty<RuntimeFallbacks>());
  81. Directory.CreateDirectory(Path.GetDirectoryName(DepsFilePath));
  82. Directory.CreateDirectory(Path.GetDirectoryName(PlatformManifestOutputPath));
  83. File.WriteAllText(
  84. PlatformManifestOutputPath,
  85. string.Join("\n", platformManifest.OrderBy(n => n)),
  86. Encoding.UTF8);
  87. try
  88. {
  89. using (var depsStream = File.Create(DepsFilePath))
  90. {
  91. new DependencyContextWriter().Write(context, depsStream);
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. // If there is a problem, ensure we don't write a partially complete version to disk.
  97. if (File.Exists(DepsFilePath))
  98. {
  99. File.Delete(DepsFilePath);
  100. }
  101. Log.LogErrorFromException(ex);
  102. }
  103. }
  104. }