This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
UpdateDirectoryBuildTargetsCommand.cs
64 lines (55 loc) · 2.26 KB
/
UpdateDirectoryBuildTargetsCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using NuGet.Configuration;
using NuGet.Versioning;
using NuKeeper.Abstractions.Logging;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Abstractions.RepositoryInspection;
namespace NuKeeper.Update.Process
{
public class UpdateDirectoryBuildTargetsCommand : IUpdateDirectoryBuildTargetsCommand
{
private readonly INuKeeperLogger _logger;
public UpdateDirectoryBuildTargetsCommand(INuKeeperLogger logger)
{
_logger = logger;
}
public Task Invoke(PackageInProject currentPackage, NuGetVersion newVersion, PackageSource packageSource,
NuGetSources allSources)
{
XDocument xml;
using (var xmlInput = File.OpenRead(currentPackage.Path.FullName))
{
xml = XDocument.Load(xmlInput);
}
using (var xmlOutput = File.Open(currentPackage.Path.FullName, FileMode.Truncate))
{
UpdateFile(xmlOutput, newVersion, currentPackage, xml);
}
return Task.CompletedTask;
}
private void UpdateFile(Stream fileContents, NuGetVersion newVersion,
PackageInProject currentPackage, XDocument xml)
{
var packagesNode = xml.Element("Project")?.Element("ItemGroup");
if (packagesNode == null)
{
return;
}
var packageNodeList = packagesNode.Elements()
.Where(x => x.Name == "PackageReference" && (x.Attributes("Include")
.Any(a => a.Value == currentPackage.Id)
|| x.Attributes("Update")
.Any(a => a.Value == currentPackage.Id)));
foreach (var dependencyToUpdate in packageNodeList)
{
_logger.Detailed(
$"Updating directory-level dependencies: {currentPackage.Id} in path {currentPackage.Path.FullName}");
dependencyToUpdate.Attribute("Version").Value = newVersion.ToString();
}
xml.Save(fileContents);
}
}
}