GenerateSharedFrameworkDepsFile.cs 4.4 KB

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