From dd7a76e6f0b4cb948ad1fa946d93eeb35ec63d25 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Wed, 5 Oct 2016 15:00:34 -0700 Subject: [PATCH 1/2] Add migration rule for 'userSecretsId' --- .../DefaultMigrationRuleSet.cs | 1 + .../Rules/MigratePropertiesRule.cs | 52 +++++++++++++++++ .../GivenThatIWantToMigrateProperties.cs | 56 +++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePropertiesRule.cs create mode 100644 test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProperties.cs diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs b/src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs index 6fd2db4f7..7d749040c 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs @@ -19,6 +19,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration new MigratePackageDependenciesAndToolsRule(), new MigrateConfigurationsRule(), new MigrateScriptsRule(), + new MigratePropertiesRule(), new WorkaroundOptionsRule(), new SaveOutputProjectRule() }; diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePropertiesRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePropertiesRule.cs new file mode 100644 index 000000000..21d6ba259 --- /dev/null +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePropertiesRule.cs @@ -0,0 +1,52 @@ +// 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.Collections.Generic; +using System.IO; +using Microsoft.DotNet.ProjectJsonMigration.Transforms; +using Microsoft.Build.Construction; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace Microsoft.DotNet.ProjectJsonMigration.Rules +{ + public class MigratePropertiesRule : IMigrationRule + { + private Dictionary> _propertyMappings + = new Dictionary> + { + ["userSecretsId"] = new AddPropertyTransform("UserSecretsId", + j => j.Value(), + j => !string.IsNullOrEmpty(j.Value())) + }; + + private readonly ITransformApplicator _transformApplicator; + + public MigratePropertiesRule(ITransformApplicator transformApplicator = null) + { + _transformApplicator = transformApplicator ?? new TransformApplicator(); + } + + public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs) + { + var propertyGroup = migrationRuleInputs.CommonPropertyGroup; + + var projectFile = migrationRuleInputs.DefaultProjectContext.ProjectFile.ProjectFilePath; + + using (var stream = new FileStream(projectFile, FileMode.Open)) + using (var streamReader = new StreamReader(stream)) + using (var jsonReader = new JsonTextReader(streamReader)) + { + var rawProject = JObject.Load(jsonReader); + foreach (var prop in _propertyMappings) + { + var token = rawProject.GetValue(prop.Key); + if (token != null) + { + _transformApplicator.Execute(prop.Value.Transform(token), propertyGroup); + } + } + } + } + } +} diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProperties.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProperties.cs new file mode 100644 index 000000000..14ffdecad --- /dev/null +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProperties.cs @@ -0,0 +1,56 @@ +// 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.Linq; +using Microsoft.Build.Construction; +using Microsoft.DotNet.Tools.Test.Utilities; +using FluentAssertions; +using Microsoft.DotNet.ProjectJsonMigration.Rules; +using Xunit; + +namespace Microsoft.DotNet.ProjectJsonMigration.Tests +{ + public class GivenThatIWantToMigrateProperties : TestBase + { + [Fact] + public void It_does_not_migrate_missing_props() + { + var mockProj = RunPropertiesRuleOnPj(@" + {}"); + + mockProj.Properties.Count().Should().Be(0); + } + + [Fact] + public void It_migrates_userSecretsId() + { + var mockProj = RunPropertiesRuleOnPj(@" + { + ""userSecretsId"": ""XYZ"" + }"); + + mockProj.Properties.Count(p => p.Name == "UserSecretsId").Should().Be(1); + mockProj.Properties.First(p => p.Name == "UserSecretsId").Value.Should().Be("XYZ"); + } + + [Fact] + public void It_migrates_empty_userSecretsId() + { + var mockProj = RunPropertiesRuleOnPj(@" + { + ""userSecretsId"": """" + }"); + + mockProj.Properties.Count(p => p.Name == "UserSecretsId").Should().Be(0); + } + + private ProjectRootElement RunPropertiesRuleOnPj(string project, string testDirectory = null) + { + testDirectory = testDirectory ?? Temp.CreateDirectory().Path; + return TemporaryProjectFileRuleRunner.RunRules(new IMigrationRule[] + { + new MigratePropertiesRule() + }, project, testDirectory); + } + } +} \ No newline at end of file From 190f0fc7565f20cd5309095de205d6e4f6412564 Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Wed, 5 Oct 2016 15:15:13 -0700 Subject: [PATCH 2/2] Rename to MigrateJsonPropertiesRule --- .../DefaultMigrationRuleSet.cs | 2 +- ...{MigratePropertiesRule.cs => MigrateJsonPropertiesRule.cs} | 4 ++-- ...Properties.cs => GivenThatIWantToMigrateJsonProperties.cs} | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/Microsoft.DotNet.ProjectJsonMigration/Rules/{MigratePropertiesRule.cs => MigrateJsonPropertiesRule.cs} (92%) rename test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/{GivenThatIWantToMigrateProperties.cs => GivenThatIWantToMigrateJsonProperties.cs} (93%) diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs b/src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs index 7d749040c..537bb68a0 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs @@ -12,6 +12,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration new MigrateRootOptionsRule(), new MigrateTFMRule(), new MigrateBuildOptionsRule(), + new MigrateJsonPropertiesRule(), new MigratePackOptionsRule(), new MigrateRuntimeOptionsRule(), new MigratePublishOptionsRule(), @@ -19,7 +20,6 @@ namespace Microsoft.DotNet.ProjectJsonMigration new MigratePackageDependenciesAndToolsRule(), new MigrateConfigurationsRule(), new MigrateScriptsRule(), - new MigratePropertiesRule(), new WorkaroundOptionsRule(), new SaveOutputProjectRule() }; diff --git a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePropertiesRule.cs b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateJsonPropertiesRule.cs similarity index 92% rename from src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePropertiesRule.cs rename to src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateJsonPropertiesRule.cs index 21d6ba259..e8f771137 100644 --- a/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigratePropertiesRule.cs +++ b/src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateJsonPropertiesRule.cs @@ -10,7 +10,7 @@ using Newtonsoft.Json.Linq; namespace Microsoft.DotNet.ProjectJsonMigration.Rules { - public class MigratePropertiesRule : IMigrationRule + public class MigrateJsonPropertiesRule : IMigrationRule { private Dictionary> _propertyMappings = new Dictionary> @@ -22,7 +22,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules private readonly ITransformApplicator _transformApplicator; - public MigratePropertiesRule(ITransformApplicator transformApplicator = null) + public MigrateJsonPropertiesRule(ITransformApplicator transformApplicator = null) { _transformApplicator = transformApplicator ?? new TransformApplicator(); } diff --git a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProperties.cs b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateJsonProperties.cs similarity index 93% rename from test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProperties.cs rename to test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateJsonProperties.cs index 14ffdecad..b3f109132 100644 --- a/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateProperties.cs +++ b/test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateJsonProperties.cs @@ -10,7 +10,7 @@ using Xunit; namespace Microsoft.DotNet.ProjectJsonMigration.Tests { - public class GivenThatIWantToMigrateProperties : TestBase + public class GivenThatIWantToMigrateJsonProperties : TestBase { [Fact] public void It_does_not_migrate_missing_props() @@ -49,7 +49,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests testDirectory = testDirectory ?? Temp.CreateDirectory().Path; return TemporaryProjectFileRuleRunner.RunRules(new IMigrationRule[] { - new MigratePropertiesRule() + new MigrateJsonPropertiesRule() }, project, testDirectory); } }