Adding TargetFrameworks even for single TFM and cleaning up PackageRef.
This commit is contained in:
parent
715f1f6a10
commit
7e16ad0388
3 changed files with 41 additions and 47 deletions
|
@ -16,18 +16,10 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
public class MigrateTFMRule : IMigrationRule
|
||||
{
|
||||
private readonly ITransformApplicator _transformApplicator;
|
||||
private readonly AddPropertyTransform<NuGetFramework>[] _transforms;
|
||||
|
||||
public MigrateTFMRule(ITransformApplicator transformApplicator = null)
|
||||
{
|
||||
_transformApplicator = transformApplicator ?? new TransformApplicator();
|
||||
|
||||
_transforms = new AddPropertyTransform<NuGetFramework>[]
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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<NuGetFramework> OutputPathTransform =>
|
||||
new AddPropertyTransform<NuGetFramework>("OutputPath",
|
||||
f => $"bin/$(Configuration)/{f.GetShortFolderName()}",
|
||||
f => true);
|
||||
|
||||
private AddPropertyTransform<NuGetFramework> FrameworkIdentifierTransform =>
|
||||
new AddPropertyTransform<NuGetFramework>("TargetFrameworkIdentifier",
|
||||
f => f.Framework,
|
||||
f => true);
|
||||
|
||||
private AddPropertyTransform<NuGetFramework> FrameworkVersionTransform =>
|
||||
new AddPropertyTransform<NuGetFramework>("TargetFrameworkVersion",
|
||||
f => "v" + GetDisplayVersion(f.Version),
|
||||
f => true);
|
||||
private AddPropertyTransform<IEnumerable<NuGetFramework>> FrameworksTransform =>
|
||||
new AddPropertyTransform<IEnumerable<NuGetFramework>>("TargetFrameworks",
|
||||
frameworks => string.Join(";", frameworks.Select(f => f.GetShortFolderName())),
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<string>(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<string>(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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue