forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CopyNupkgAndChangeVersion.cs
149 lines (123 loc) · 4.62 KB
/
CopyNupkgAndChangeVersion.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Build.Framework;
using Newtonsoft.Json.Linq;
using NuGet.Versioning;
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml.Linq;
namespace Microsoft.DotNet.Build.Tasks
{
public class CopyNupkgAndChangeVersion : BuildTask
{
[Required]
public string SourceFile { get; set; }
[Required]
public string TargetFile { get; set; }
[Required]
public string OriginalVersion { get; set; }
[Required]
public string TargetVersion { get; set; }
public string[] DependencyPackageIdsToChange { get; set; }
public override bool Execute()
{
Directory.CreateDirectory(Path.GetDirectoryName(TargetFile));
File.Copy(SourceFile, TargetFile, true);
using (ZipArchive zip = ZipFile.Open(TargetFile, ZipArchiveMode.Update))
{
RewriteNuspec(zip);
RewriteRuntimeJson(zip);
}
return !Log.HasLoggedErrors;
}
private void RewriteNuspec(ZipArchive zip)
{
foreach (var nuspec in zip.Entries.Where(e => e.FullName.EndsWith(".nuspec")))
{
Rewrite(nuspec, s =>
{
XDocument content = XDocument.Parse(s);
RewriteNuspecPackageVersion(content);
RewriteNuspecDependencyVersions(content);
return content.ToString();
});
}
}
private void RewriteRuntimeJson(ZipArchive zip)
{
foreach (var runtimeJson in zip.Entries.Where(e => e.FullName == "runtime.json"))
{
Rewrite(runtimeJson, s =>
{
JObject content = JObject.Parse(s);
RewriteRuntimeJsonVersions(content);
return content.ToString();
});
}
}
private void RewriteNuspecPackageVersion(XDocument content)
{
XElement versionElement = content
.Element(CreateQualifiedName(content, "package"))
.Element(CreateQualifiedName(content, "metadata"))
.Element(CreateQualifiedName(content, "version"));
if (versionElement.Value != OriginalVersion)
{
Log.LogError(
$"Original version is '{versionElement.Value}', " +
$"expected '{OriginalVersion}'");
}
versionElement.Value = TargetVersion;
}
private void RewriteNuspecDependencyVersions(XDocument content)
{
foreach (var dependency in content
.Descendants(CreateQualifiedName(content, "dependency"))
.Where(x =>
x.Attribute("version").Value == OriginalVersion &&
DependencyPackageIdsToChange?.Contains(x.Attribute("id").Value) == true))
{
dependency.Value = TargetVersion;
}
}
private void RewriteRuntimeJsonVersions(JObject content)
{
var versionProperties = content
.Descendants()
.OfType<JProperty>()
.Where(p =>
p.Value is JValue v &&
v.Type == JTokenType.String);
foreach (var p in versionProperties)
{
var range = VersionRange.Parse(p.Value.Value<string>());
if (range.MinVersion.OriginalVersion == OriginalVersion)
{
var newRange = new VersionRange(
NuGetVersion.Parse(TargetVersion),
range.Float);
p.Value = newRange.ToString();
}
}
}
private static XName CreateQualifiedName(XDocument doc, string name)
{
return doc.Root.GetDefaultNamespace().GetName(name);
}
private static void Rewrite(ZipArchiveEntry entry, Func<string, string> rewrite)
{
using (var stream = entry.Open())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
{
var content = rewrite(reader.ReadToEnd());
stream.Position = 0;
stream.SetLength(0);
writer.Write(content);
}
}
}
}