dotnet-installer/build_projects/dotnet-cli-build/GenerateNuGetPackagesArchiveVersion.cs
Livar Cunha 3a9525b5ac Merge branch 'rel/1.0.1' into merge_rel101_into_master
* rel/1.0.1: (66 commits)
  Update LZMA license with correct text
  Bump to 2.0.0-rc5-61427-04
  Remove duplicate installer suffix
  Add license text to LZMA SDK license notice
  Updating the SDK to 1.0.0-alpha-20170224-6
  Updating both platform abstractions and dependency model to 1.0.3.
  Bump Roslyn to 2.0.0-rc5-61424-02
  Update Stage0 to use the latest build.
  Update README with new distros.
  Back porting #5597 into rel/1.0.0
  Fixing the exclude pattern used by the Migration to exclude WEB SDK globs.
  Remove RID from test package creation
  Disable migrate and publish web app with content because CI does not have NPM
  Adding an E2E test for pack with content during migration.
  Fixing a failing test and adding a few more E2E tests around binplace content for migrated projects.
  Fix debian_config.json on ubuntu16.10
  Updating publish, pack and build of content to use None with Never/false/Never in their metadata for excluded items.
  Intermediate commit to get a WIP PR out. This adds the None Update with CopyToOutputDirectory set to Never.
  Switching the CopyToOutput for build and publish and the file for pack to use None Update instead of include. Also, fixed the exclude patterns for web apps.
  Do not migrate Content that is already included in the Web SDK for web apps.
  ...
2017-03-01 20:50:42 -08:00

104 lines
3.5 KiB
C#

// 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 SDKVersion { get; set; }
[Required]
public string ToolPath { get; set; }
[Output]
public String Version { get; set; }
private static string[][] _templatesAndArgs = new string[][]
{
new string[] { "console", "" },
};
public override bool Execute()
{
var dataToHash = string.Empty;
foreach (var newArgs in _templatesAndArgs)
{
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],
TemplateArgs = newArgs[1] + $" --debug:ephemeral-hive -n TempProject -o \"{outputDir}\"",
HostObject = HostObject,
BuildEngine = BuildEngine
};
newTask.Execute();
var templatePath = Path.Combine(outputDir, "TempProject.csproj");
var rootElement = ProjectRootElement.Open(templatePath);
var packageRefs = rootElement.Items.Where(i => i.ItemType == "PackageReference").ToList();
foreach (var packageRef in packageRefs)
{
dataToHash += $"{packageRef.Include},";
if (packageRef.HasMetadata)
{
foreach (var metadata in packageRef.Metadata)
{
dataToHash += $"{metadata.Name}={metadata.Value};";
}
}
}
var frameworkVersion = rootElement.Properties.LastOrDefault(p => p.Name == "RuntimeFrameworkVersion");
if (frameworkVersion != null)
{
dataToHash += $"RuntimeFrameworkVersion={frameworkVersion.Value}";
}
Directory.Delete(targetDir, true);
}
dataToHash += SDKVersion;
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();
}
}
}