2016-12-27 21:28:24 +00:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using Microsoft.Build.Construction;
|
|
|
|
|
using Microsoft.Build.Framework;
|
|
|
|
|
using Microsoft.Build.Utilities;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Build
|
|
|
|
|
{
|
|
|
|
|
public class GenerateNuGetPackagesArchiveVersion : Task
|
|
|
|
|
{
|
|
|
|
|
public GenerateNuGetPackagesArchiveVersion()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Required]
|
|
|
|
|
public string RepoRoot { get; set; }
|
|
|
|
|
|
2017-01-27 23:46:55 +00:00
|
|
|
|
[Required]
|
|
|
|
|
public string SharedFrameworkVersion { get; set; }
|
|
|
|
|
|
2016-12-27 21:28:24 +00:00
|
|
|
|
[Output]
|
|
|
|
|
public String Version { get; set; }
|
|
|
|
|
|
2017-01-06 21:44:01 +00:00
|
|
|
|
private static string[] s_TemplatesToArchive = new string[]
|
2016-12-27 21:28:24 +00:00
|
|
|
|
{
|
2017-01-27 23:46:55 +00:00
|
|
|
|
"CSharp_Console",
|
2017-01-06 21:44:01 +00:00
|
|
|
|
};
|
2016-12-27 21:28:24 +00:00
|
|
|
|
|
2017-01-06 21:44:01 +00:00
|
|
|
|
public override bool Execute()
|
|
|
|
|
{
|
2016-12-27 21:28:24 +00:00
|
|
|
|
var dataToHash = string.Empty;
|
2017-01-06 21:44:01 +00:00
|
|
|
|
|
|
|
|
|
foreach (string templateToArchive in s_TemplatesToArchive)
|
2016-12-27 21:28:24 +00:00
|
|
|
|
{
|
2017-01-06 21:44:01 +00:00
|
|
|
|
var templatePath = Path.Combine(
|
|
|
|
|
RepoRoot,
|
|
|
|
|
"src",
|
|
|
|
|
"dotnet",
|
|
|
|
|
"commands",
|
|
|
|
|
"dotnet-new",
|
|
|
|
|
templateToArchive,
|
|
|
|
|
"$projectName$.csproj");
|
|
|
|
|
|
|
|
|
|
var rootElement = ProjectRootElement.Open(templatePath);
|
|
|
|
|
var packageRefs = rootElement.Items.Where(i => i.ItemType == "PackageReference").ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var packageRef in packageRefs)
|
2016-12-27 21:28:24 +00:00
|
|
|
|
{
|
2017-01-06 21:44:01 +00:00
|
|
|
|
dataToHash += $"{packageRef.Include},";
|
|
|
|
|
if (packageRef.HasMetadata)
|
2016-12-27 21:28:24 +00:00
|
|
|
|
{
|
2017-01-06 21:44:01 +00:00
|
|
|
|
foreach (var metadata in packageRef.Metadata)
|
|
|
|
|
{
|
|
|
|
|
dataToHash += $"{metadata.Name}={metadata.Value};";
|
|
|
|
|
}
|
2016-12-27 21:28:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-27 23:46:55 +00:00
|
|
|
|
|
|
|
|
|
dataToHash += SharedFrameworkVersion;
|
2016-12-27 21:28:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.LogMessage($"NuGet Packages Archive Data To Hash: '{dataToHash}'");
|
|
|
|
|
|
|
|
|
|
var sha256 = SHA256.Create();
|
|
|
|
|
var hashBytes = sha256.ComputeHash(Encoding.Unicode.GetBytes(dataToHash));
|
|
|
|
|
Version = GetHashString(hashBytes);
|
|
|
|
|
|
|
|
|
|
Log.LogMessage($"NuGet Packages Archive Version: '{Version}'");
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetHashString(byte[] hashBytes)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder = new StringBuilder(hashBytes.Length * 2);
|
|
|
|
|
foreach (var b in hashBytes)
|
|
|
|
|
{
|
|
|
|
|
builder.AppendFormat("{0:x2}", b);
|
|
|
|
|
}
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|