Add migration rule for 'userSecretsId'
This commit is contained in:
parent
6ad5a185cb
commit
dd7a76e6f0
3 changed files with 109 additions and 0 deletions
|
@ -19,6 +19,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
||||||
new MigratePackageDependenciesAndToolsRule(),
|
new MigratePackageDependenciesAndToolsRule(),
|
||||||
new MigrateConfigurationsRule(),
|
new MigrateConfigurationsRule(),
|
||||||
new MigrateScriptsRule(),
|
new MigrateScriptsRule(),
|
||||||
|
new MigratePropertiesRule(),
|
||||||
new WorkaroundOptionsRule(),
|
new WorkaroundOptionsRule(),
|
||||||
new SaveOutputProjectRule()
|
new SaveOutputProjectRule()
|
||||||
};
|
};
|
||||||
|
|
|
@ -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<string, ConditionalTransform<JToken, ProjectPropertyElement>> _propertyMappings
|
||||||
|
= new Dictionary<string, ConditionalTransform<JToken, ProjectPropertyElement>>
|
||||||
|
{
|
||||||
|
["userSecretsId"] = new AddPropertyTransform<JToken>("UserSecretsId",
|
||||||
|
j => j.Value<string>(),
|
||||||
|
j => !string.IsNullOrEmpty(j.Value<string>()))
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue