From 7e16ad0388c7fd66cb7e0c106f771c8e99b23769 Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Tue, 27 Sep 2016 19:25:13 -0700 Subject: [PATCH] Adding TargetFrameworks even for single TFM and cleaning up PackageRef. --- .../Rules/MigrateTFMRule.cs | 58 +++++++------------ .../ProjectRootElementExtensions.cs | 2 +- .../GivenThatIWantToMigrateTestApps.cs | 28 ++++++--- 3 files changed, 41 insertions(+), 47 deletions(-) diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateTFMRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateTFMRule.cs index 8b451cc8c..59db57adf 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateTFMRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateTFMRule.cs @@ -16,18 +16,10 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules public class MigrateTFMRule : IMigrationRule { private readonly ITransformApplicator _transformApplicator; - private readonly AddPropertyTransform[] _transforms; public MigrateTFMRule(ITransformApplicator transformApplicator = null) { _transformApplicator = transformApplicator ?? new TransformApplicator(); - - _transforms = new AddPropertyTransform[] - { - OutputPathTransform, - FrameworkIdentifierTransform, - FrameworkVersionTransform - }; } public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs) @@ -37,24 +29,14 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules CleanExistingProperties(csproj); - if (migrationRuleInputs.ProjectContexts.Count() > 1) - { - _transformApplicator.Execute( - FrameworksTransform.Transform(migrationRuleInputs.ProjectContexts.Select(p => p.TargetFramework)), - propertyGroup); - } - - foreach (var transform in _transforms) - { - _transformApplicator.Execute( - transform.Transform(migrationRuleInputs.DefaultProjectContext.TargetFramework), - propertyGroup); - } + _transformApplicator.Execute( + FrameworksTransform.Transform(migrationRuleInputs.ProjectContexts.Select(p => p.TargetFramework)), + propertyGroup); } private void CleanExistingProperties(ProjectRootElement csproj) { - var existingPropertiesToRemove = new string[] { "TargetFrameworkIdentifier", "TargetFrameworkVersion" }; + var existingPropertiesToRemove = new string[] { "TargetFrameworkIdentifier", "TargetFrameworkVersion", "TargetFrameworks" }; var properties = csproj.Properties.Where(p => existingPropertiesToRemove.Contains(p.Name)); foreach (var property in properties) @@ -63,6 +45,23 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules } } + //TODO: this is a hack right now to clean packagerefs. This is not the final resting place for this piece of code + // @brthor will move it to its final location as part of this PackageRef PR: https://github.com/dotnet/cli/pull/4261 + private void CleanExistingPackageReferences(ProjectRootElement outputMSBuildProject) + { + var packageRefs = outputMSBuildProject + .Items + .Where(i => i.ItemType == "PackageReference" && i.Include != ConstantPackageNames.CSdkPackageName) + .ToList(); + + foreach (var packageRef in packageRefs) + { + var parent = packageRef.Parent; + packageRef.Parent.RemoveChild(packageRef); + parent.RemoveIfEmpty(); + } + } + // Taken from private NuGet.Frameworks method // https://github.com/NuGet/NuGet.Client/blob/33b8f85a94b01f805f1e955f9b68992b297fad6e/src/NuGet.Core/NuGet.Frameworks/NuGetFramework.cs#L234 private static string GetDisplayVersion(Version version) @@ -83,21 +82,6 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules return sb.ToString(); } - // TODO: When we have this inferred in the sdk targets, we won't need this - private AddPropertyTransform OutputPathTransform => - new AddPropertyTransform("OutputPath", - f => $"bin/$(Configuration)/{f.GetShortFolderName()}", - f => true); - - private AddPropertyTransform FrameworkIdentifierTransform => - new AddPropertyTransform("TargetFrameworkIdentifier", - f => f.Framework, - f => true); - - private AddPropertyTransform FrameworkVersionTransform => - new AddPropertyTransform("TargetFrameworkVersion", - f => "v" + GetDisplayVersion(f.Version), - f => true); private AddPropertyTransform> FrameworksTransform => new AddPropertyTransform>("TargetFrameworks", frameworks => string.Join(";", frameworks.Select(f => f.GetShortFolderName())), diff --git a/src/dotnet/commands/dotnet-migrate/ProjectRootElementExtensions.cs b/src/dotnet/commands/dotnet-migrate/ProjectRootElementExtensions.cs index 222616b31..262670ad0 100644 --- a/src/dotnet/commands/dotnet-migrate/ProjectRootElementExtensions.cs +++ b/src/dotnet/commands/dotnet-migrate/ProjectRootElementExtensions.cs @@ -11,7 +11,7 @@ namespace Microsoft.DotNet.Tools.Migrate return projectRootElement .Items .Where(i => i.ItemType == "PackageReference") - .First(i=> i.Include == ConstantPackageNames.CSdkPackageName) + .First(i => i.Include == ConstantPackageNames.CSdkPackageName) .GetMetadataWithName("version").Value; } } diff --git a/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs b/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs index 9d02e67e2..3c66d159c 100644 --- a/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs +++ b/test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs @@ -29,7 +29,7 @@ namespace Microsoft.DotNet.Migration.Tests public void It_migrates_apps(string projectName) { var projectDirectory = TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path; - var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory); + var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName); var outputsIdentical = outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs); @@ -79,7 +79,7 @@ namespace Microsoft.DotNet.Migration.Tests { var projectDirectory = TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path; - var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory); + var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName); var outputsIdentical = outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs); @@ -100,7 +100,7 @@ namespace Microsoft.DotNet.Migration.Tests { var projectDirectory = TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path; - var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory); + var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName); var outputsIdentical = outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs); @@ -210,7 +210,8 @@ namespace Microsoft.DotNet.Migration.Tests File.Copy("NuGet.tempaspnetpatch.config", Path.Combine(projectDirectory, "NuGet.Config")); Restore(projectDirectory); - var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory); + var outputComparisonData = + BuildProjectJsonMigrateBuildMSBuild(projectDirectory, Path.GetFileNameWithoutExtension(projectDirectory)); return outputComparisonData; } @@ -227,7 +228,7 @@ namespace Microsoft.DotNet.Migration.Tests } } - private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(string projectDirectory) + private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(string projectDirectory, string projectName) { BuildProjectJson(projectDirectory); var projectJsonBuildOutputs = new HashSet(CollectBuildOutputs(projectDirectory)); @@ -237,8 +238,8 @@ namespace Microsoft.DotNet.Migration.Tests File.Delete(Path.Combine(projectDirectory, "project.lock.json")); MigrateProject(projectDirectory); - Restore(projectDirectory); - BuildMSBuild(projectDirectory); + Restore3(projectDirectory, projectName); + BuildMSBuild(projectDirectory, projectName); var msbuildBuildOutputs = new HashSet(CollectBuildOutputs(projectDirectory)); @@ -297,13 +298,22 @@ namespace Microsoft.DotNet.Migration.Tests .Pass(); } - private string BuildMSBuild(string projectDirectory, string configuration="Debug") + private void Restore3(string projectDirectory, string projectName) + { + new Restore3Command() + .WithWorkingDirectory(projectDirectory) + .Execute($"{projectName}.csproj") + .Should() + .Pass(); + } + + private string BuildMSBuild(string projectDirectory, string projectName, string configuration="Debug") { DeleteXproj(projectDirectory); var result = new Build3Command() .WithWorkingDirectory(projectDirectory) - .ExecuteWithCapturedOutput($"/p:Configuration={configuration}"); + .ExecuteWithCapturedOutput($"{projectName}.csproj /p:Configuration={configuration}"); result .Should()