Skip to content
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 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@

<PropertyGroup>
<SkipLocalsInit Condition="'$(SkipLocalsInit)' == '' and '$(MSBuildProjectExtension)' == '.csproj' and '$(IsNETCoreAppSrc)' == 'true' and '$(TargetFramework)' == '$(NetCoreAppCurrent)'">true</SkipLocalsInit>
<VersionSuffix>$(_PreReleaseLabel)$(_BuildNumberLabels)</VersionSuffix>
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</PropertyGroup>

<!--Instructs compiler not to emit .locals init, using SkipLocalsInitAttribute.-->
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/libraries-packages.proj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
-->
<PkgProjects Include="$(PkgDir)*\*.pkgproj" Exclude="$(MSBuildThisFileDirectory)pkg\*Private*\*.pkgproj" />
<PkgProjects Include="*\pkg\**\*.pkgproj" />
<PkgProjects Remove="Microsoft.Extensions.HostFactoryResolver\pkg\Microsoft.Extensions.HostFactoryResolver.Sources.pkgproj" />
safern marked this conversation as resolved.
Show resolved Hide resolved
<PkgProjects Remove="System.Numerics.Tensors\pkg\System.Numerics.Tensors.pkgproj" />
<PkgProjects Remove="$(PkgDir)Microsoft.Extensions.Internal.Transport\Microsoft.Extensions.Internal.Transport.pkgproj" />
</ItemGroup>

<MSBuild Targets="GetPackageIdentityIfStable"
Expand Down
148 changes: 148 additions & 0 deletions tools-local/tasks/installer.tasks/CopyNupkgAndChangeVersion.cs
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);
}
}
}
}