GenerateSharedFrameworkDepsFile.cs 4.4 KB

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