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()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 09:13:58 +00:00
|
|
|
|
[Required]
|
|
|
|
|
public string SDKVersion { get; set; }
|
|
|
|
|
|
2016-12-27 21:28:24 +00:00
|
|
|
|
[Required]
|
2017-02-01 01:31:37 +00:00
|
|
|
|
public string ToolPath { get; set; }
|
2016-12-27 21:28:24 +00:00
|
|
|
|
|
|
|
|
|
[Output]
|
|
|
|
|
public String Version { get; set; }
|
|
|
|
|
|
2017-02-01 01:31:37 +00:00
|
|
|
|
private static string[][] _templatesAndArgs = new string[][]
|
2016-12-27 21:28:24 +00:00
|
|
|
|
{
|
2017-04-22 18:52:54 +00:00
|
|
|
|
new string[] { "mvc", "-au individual" },
|
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
|
|
|
|
|
2017-02-01 01:31:37 +00:00
|
|
|
|
foreach (var newArgs in _templatesAndArgs)
|
2016-12-27 21:28:24 +00:00
|
|
|
|
{
|
2017-02-01 01:31:37 +00:00
|
|
|
|
var targetDir = Path.GetTempFileName();
|
|
|
|
|
File.Delete(targetDir);
|
|
|
|
|
Directory.CreateDirectory(targetDir);
|
|
|
|
|
var outputDir = Path.Combine(targetDir, newArgs[0]);
|
|
|
|
|
Directory.CreateDirectory(outputDir);
|
|
|
|
|
var newTask = new DotNetNew
|
|
|
|
|
{
|
|
|
|
|
ToolPath = ToolPath,
|
|
|
|
|
TemplateType = newArgs[0],
|
2017-07-10 22:57:30 +00:00
|
|
|
|
TemplateArgs = newArgs[1] + $" --debug:ephemeral-hive -n TempProject -o \"{outputDir}\" --no-restore",
|
2017-02-01 01:31:37 +00:00
|
|
|
|
HostObject = HostObject,
|
|
|
|
|
BuildEngine = BuildEngine
|
|
|
|
|
};
|
|
|
|
|
newTask.Execute();
|
|
|
|
|
var templatePath = Path.Combine(outputDir, "TempProject.csproj");
|
2017-01-06 21:44:01 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-02-09 16:18:39 +00:00
|
|
|
|
var frameworkVersion = rootElement.Properties.LastOrDefault(p => p.Name == "RuntimeFrameworkVersion");
|
|
|
|
|
if (frameworkVersion != null)
|
|
|
|
|
{
|
|
|
|
|
dataToHash += $"RuntimeFrameworkVersion={frameworkVersion.Value}";
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 01:31:37 +00:00
|
|
|
|
Directory.Delete(targetDir, true);
|
2016-12-27 21:28:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-23 09:13:58 +00:00
|
|
|
|
dataToHash += SDKVersion;
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|