-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infra changes required for stable builds (#42872)
* address feedback * remove warning
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
tools-local/tasks/installer.tasks/CopyNupkgAndChangeVersion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
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); | ||
} | ||
} | ||
} | ||
} |