Merge pull request #4311 from brthor/brthor/keyfile

KeyFile Migration Fixes
This commit is contained in:
Bryan Thornbury 2016-10-05 10:46:27 -07:00 committed by GitHub
commit 3117e4a686
9 changed files with 90 additions and 37 deletions

View file

@ -0,0 +1,12 @@
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

Binary file not shown.

View file

@ -0,0 +1,16 @@
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true,
"keyFile": "./key.snk"
},
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
},
"frameworks": {
"netcoreapp1.0": {}
}
}

View file

@ -19,7 +19,6 @@ namespace Microsoft.DotNet.ProjectJsonMigration
new MigratePackageDependenciesRule(),
new MigrateConfigurationsRule(),
new MigrateScriptsRule(),
new TemporaryMutateProjectJsonRule(),
new WorkaroundOptionsRule(),
new SaveOutputProjectRule()
};

View file

@ -26,12 +26,17 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
private AddPropertyTransform<CommonCompilerOptions>[] KeyFileTransforms
=> new []
{
new AddPropertyTransform<CommonCompilerOptions>("AssemblyOriginatorKeyFile",
new AddPropertyTransform<CommonCompilerOptions>("KeyOriginatorFile",
compilerOptions => compilerOptions.KeyFile,
compilerOptions => !string.IsNullOrEmpty(compilerOptions.KeyFile)),
new AddPropertyTransform<CommonCompilerOptions>("SignAssembly",
"true",
compilerOptions => !string.IsNullOrEmpty(compilerOptions.KeyFile))
.WithMSBuildCondition(" '$(OS)' == 'Windows_NT' "),
new AddPropertyTransform<CommonCompilerOptions>("PublicSign",
"true",
compilerOptions => !string.IsNullOrEmpty(compilerOptions.KeyFile) && (compilerOptions.PublicSign == null))
.WithMSBuildCondition(" '$(OS)' != 'Windows_NT' ")
};
private AddPropertyTransform<CommonCompilerOptions> DefineTransform => new AddPropertyTransform<CommonCompilerOptions>(

View file

@ -1,32 +0,0 @@
// 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 System;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace Microsoft.DotNet.ProjectJsonMigration.Rules
{
/// <summary>
/// This rule is temporary while project.json still exists in the new project system.
/// It renames your existing project.json (if output directory is the current project directory),
/// creates a copy, then mutates that copy.
///
/// Mutations:
/// - inject a dependency on the Microsoft.SDK targets
/// - removing the "runtimes" node.
/// </summary>
public class TemporaryMutateProjectJsonRule : IMigrationRule
{
public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
{
var sourceProjectFile = Path.Combine(migrationSettings.ProjectDirectory, "project.json");
var renamedProjectFile = Path.Combine(migrationSettings.ProjectDirectory, "project.migrated.json");
File.Copy(sourceProjectFile, renamedProjectFile);
sourceProjectFile = renamedProjectFile;
}
}
}

View file

@ -14,6 +14,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
private readonly string _propertyValue;
private readonly Func<T,string> _propertyValueFunc;
private string _msbuildCondition = null;
public AddPropertyTransform(string propertyName, string propertyValue, Func<T,bool> condition)
: base(condition)
{
@ -28,13 +30,24 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Transforms
_propertyValueFunc = propertyValueFunc;
}
public AddPropertyTransform<T> WithMSBuildCondition(string condition)
{
_msbuildCondition = condition;
return this;
}
public override ProjectPropertyElement ConditionallyTransform(T source)
{
string propertyValue = GetPropertyValue(source);
var property = _propertyObjectGenerator.CreatePropertyElement(PropertyName);
property.Value = propertyValue;
if (!string.IsNullOrEmpty(_msbuildCondition))
{
property.Condition = _msbuildCondition;
}
return property;
}

View file

@ -249,11 +249,16 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
}
}");
mockProj.Properties.Count(p => p.Name == "AssemblyOriginatorKeyFile").Should().Be(1);
mockProj.Properties.First(p => p.Name == "AssemblyOriginatorKeyFile").Value.Should().Be("../keyfile.snk");
mockProj.Properties.Count(p => p.Name == "KeyOriginatorFile").Should().Be(1);
mockProj.Properties.First(p => p.Name == "KeyOriginatorFile").Value.Should().Be("../keyfile.snk");
mockProj.Properties.Count(p => p.Name == "SignAssembly").Should().Be(1);
mockProj.Properties.First(p => p.Name == "SignAssembly").Value.Should().Be("true");
mockProj.Properties.First(p => p.Name == "SignAssembly").Condition.Should().Be(" '$(OS)' == 'Windows_NT' ");
mockProj.Properties.Count(p => p.Name == "PublicSign").Should().Be(1);
mockProj.Properties.First(p => p.Name == "PublicSign").Value.Should().Be("true");
mockProj.Properties.First(p => p.Name == "PublicSign").Condition.Should().Be(" '$(OS)' != 'Windows_NT' ");
}
[Fact]

View file

@ -16,6 +16,7 @@ using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Tools.Migrate;
using Build3Command = Microsoft.DotNet.Tools.Test.Utilities.Build3Command;
using BuildCommand = Microsoft.DotNet.Tools.Test.Utilities.BuildCommand;
using System.Runtime.Loader;
namespace Microsoft.DotNet.Migration.Tests
{
@ -44,6 +45,25 @@ namespace Microsoft.DotNet.Migration.Tests
VerifyAllMSBuildOutputsRunnable(projectDirectory);
}
public void It_migrates_signed_apps(string projectName)
{
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppWithSigning", callingMethod: "i").WithLockFiles().Path;
CleanBinObj(projectDirectory);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName);
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
if (!outputsIdentical)
{
OutputDiagnostics(outputComparisonData);
}
outputsIdentical.Should().BeTrue();
VerifyAllMSBuildOutputsRunnable(projectDirectory);
VerifyAllMSBuildOutputsAreSigned(projectDirectory);
}
[Fact]
public void It_migrates_dotnet_new_console_with_identical_outputs()
{
@ -237,6 +257,21 @@ namespace Microsoft.DotNet.Migration.Tests
}
}
private void VerifyAllMSBuildOutputsAreSigned(string projectDirectory)
{
var dllFileName = Path.GetFileName(projectDirectory) + ".dll";
var runnableDlls = Directory.EnumerateFiles(Path.Combine(projectDirectory, "bin"), dllFileName,
SearchOption.AllDirectories);
foreach (var dll in runnableDlls)
{
var assemblyName = AssemblyLoadContext.GetAssemblyName(dll);
var token = assemblyName.GetPublicKeyToken();
token.Should().NotBeNullOrEmpty();
}
}
private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(string projectDirectory, string projectName=null)
{
BuildProjectJson(projectDirectory);