ProcessSharedFrameworkDeps.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // Sourced from https://github.com/dotnet/core-setup/tree/be8d8e3486b2bf598ed69d39b1629a24caaba45e/tools-local/tasks, needs to be kept in sync
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using Microsoft.Build.Framework;
  8. using Microsoft.Build.Utilities;
  9. using Microsoft.Extensions.DependencyModel;
  10. using NuGet.Common;
  11. using NuGet.ProjectModel;
  12. using RepoTasks.Utilities;
  13. namespace RepoTasks
  14. {
  15. public partial class ProcessSharedFrameworkDeps : Task
  16. {
  17. [Required]
  18. public string AssetsFilePath { get; set; }
  19. [Required]
  20. public string DepsFilePath { get; set; }
  21. public string[] PackagesToRemove { get; set; }
  22. [Required]
  23. public string Runtime { get; set; }
  24. public override bool Execute()
  25. {
  26. ExecuteCore();
  27. return true;
  28. }
  29. private void ExecuteCore()
  30. {
  31. DependencyContext context;
  32. using (var depsStream = File.OpenRead(DepsFilePath))
  33. {
  34. context = new DependencyContextJsonReader().Read(depsStream);
  35. }
  36. LockFile lockFile = LockFileUtilities.GetLockFile(AssetsFilePath, NullLogger.Instance);
  37. if (lockFile == null)
  38. {
  39. throw new ArgumentException($"Could not load a LockFile at '{AssetsFilePath}'.", nameof(AssetsFilePath));
  40. }
  41. var manager = new RuntimeGraphManager();
  42. var graph = manager.Collect(lockFile);
  43. var expandedGraph = manager.Expand(graph, Runtime);
  44. var trimmedRuntimeLibraries = context.RuntimeLibraries;
  45. if (PackagesToRemove != null && PackagesToRemove.Any())
  46. {
  47. trimmedRuntimeLibraries = RuntimeReference.RemoveReferences(context.RuntimeLibraries, PackagesToRemove);
  48. }
  49. context = new DependencyContext(
  50. context.Target,
  51. context.CompilationOptions,
  52. context.CompileLibraries,
  53. trimmedRuntimeLibraries,
  54. expandedGraph
  55. );
  56. using (var depsStream = File.Create(DepsFilePath))
  57. {
  58. new DependencyContextWriter().Write(context, depsStream);
  59. }
  60. }
  61. }
  62. }