-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infra changes required for stable builds #42872
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
safern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
[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); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually we set the VersionSuffix in Versions.props. Why here only for libraries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VersionSuffix should be empty when stablizePackageVersion is set. We require this because of this library specific error check. https://github.com/dotnet/arcade/blob/master/src/Microsoft.DotNet.Build.Tasks.Packaging/src/build/Packaging.targets#L706
We have the same thing done in 3.1 as well. cc @ericstj
Not sure why this error check is here, we should probably resolve this for 6.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC this was because of differences with how arcade decided to use VersionSuffix and the way it was already used by our packages. I believe we always want it it to be set and use the index to determine when something is stable vs not. Whereas Arcade decides if something should be stable based solely on properties, then only sets VersionSuffix for non-stable packages.
We could probably change our logic to allow for empty suffix, but then error if we ever tried to build a package that depended on some other package that wasn't represented stable in the index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So should we follow-up on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dotnet/arcade#6305