GenerateRestoreSourcesPropsFile.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.Linq;
  5. using System.IO;
  6. using System.Text;
  7. using System.Xml;
  8. using System.Xml.Linq;
  9. using Microsoft.Build.Framework;
  10. using Microsoft.Build.Utilities;
  11. namespace RepoTasks
  12. {
  13. public class GenerateRestoreSourcesPropsFile : Task
  14. {
  15. [Required]
  16. public ITaskItem[] Sources { get; set; }
  17. [Required]
  18. public string OutputPath { get; set; }
  19. public override bool Execute()
  20. {
  21. OutputPath = OutputPath.Replace('\\', '/');
  22. Directory.CreateDirectory(Path.GetDirectoryName(OutputPath));
  23. var sources = new XElement("DotNetRestoreSources");
  24. var propertyGroup = new XElement("PropertyGroup", sources);
  25. var doc = new XDocument(new XElement("Project", propertyGroup));
  26. propertyGroup.Add(new XElement("MSBuildAllProjects", "$(MSBuildAllProjects);$(MSBuildThisFileFullPath)"));
  27. var sb = new StringBuilder();
  28. foreach (var source in Sources)
  29. {
  30. sb.Append(source.ItemSpec).AppendLine(";");
  31. }
  32. sources.SetValue(sb.ToString());
  33. var settings = new XmlWriterSettings
  34. {
  35. OmitXmlDeclaration = true,
  36. };
  37. using (var writer = XmlWriter.Create(OutputPath, settings))
  38. {
  39. Log.LogMessage(MessageImportance.Normal, $"Generate {OutputPath}");
  40. doc.Save(writer);
  41. }
  42. return !Log.HasLoggedErrors;
  43. }
  44. }
  45. }