Refactoring the ProjectTypeDetector to use the Project in the MigrationInputs.
This commit is contained in:
parent
2d27092513
commit
9e88b811a0
3 changed files with 57 additions and 48 deletions
|
@ -2,59 +2,66 @@
|
|||
// Licensed under the MIT license. See LICENSE file in the project root for full license info
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.Internal.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.ProjectJsonMigration
|
||||
{
|
||||
public static class ProjectTypeDetector
|
||||
internal static class ProjectTypeDetector
|
||||
{
|
||||
public static bool TryDetectProjectType(string projectDirectory, out string projectType)
|
||||
public static string TryDetectProjectType(Project project)
|
||||
{
|
||||
string projectJsonFile = Path.Combine(projectDirectory, "project.json");
|
||||
if (!File.Exists(projectJsonFile))
|
||||
string projectType = null;
|
||||
if (IsWebProject(project))
|
||||
{
|
||||
projectType = "web";
|
||||
}
|
||||
|
||||
return projectType;
|
||||
}
|
||||
|
||||
private static bool IsWebProject(Project project)
|
||||
{
|
||||
if(project.IsTestProject)
|
||||
{
|
||||
projectType = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (IsWebProject(projectJsonFile))
|
||||
var isExecutable = project.GetCompilerOptions(null, "Debug").EmitEntryPoint.GetValueOrDefault();
|
||||
if (isExecutable
|
||||
&& HasAnyPackageContainingName(project, ".AspNetCore."))
|
||||
{
|
||||
projectType = "web";
|
||||
return true;
|
||||
}
|
||||
|
||||
projectType = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsWebProject(string projectJsonFile)
|
||||
private static bool HasAnyPackageContainingName(Project project, string nameSegment)
|
||||
{
|
||||
Project project;
|
||||
if (ProjectReader.TryGetProject(projectJsonFile, out project))
|
||||
var containsPackageName = HasAnyPackageContainingName(
|
||||
new ReadOnlyCollection<ProjectLibraryDependency>(project.Dependencies),
|
||||
nameSegment);
|
||||
foreach (var tf in project.GetTargetFrameworks())
|
||||
{
|
||||
if(project.IsTestProject)
|
||||
if(containsPackageName)
|
||||
{
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
foreach (var tf in project.GetTargetFrameworks())
|
||||
{
|
||||
if (tf.CompilerOptions.EmitEntryPoint.GetValueOrDefault()
|
||||
&& HasAnyPackageContainingName(tf, ".AspNetCore."))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
containsPackageName = HasAnyPackageContainingName(tf.Dependencies, nameSegment);
|
||||
}
|
||||
|
||||
return false;
|
||||
return containsPackageName;
|
||||
}
|
||||
|
||||
private static bool HasAnyPackageContainingName(TargetFrameworkInformation tf, string nameSegment)
|
||||
private static bool HasAnyPackageContainingName(
|
||||
IReadOnlyList<ProjectLibraryDependency> dependencies,
|
||||
string nameSegment)
|
||||
{
|
||||
return tf.Dependencies.Any(x => x.Name.IndexOf(nameSegment, StringComparison.OrdinalIgnoreCase) > -1);
|
||||
return dependencies.Any(x => x.Name.IndexOf(nameSegment, StringComparison.OrdinalIgnoreCase) > -1);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -39,8 +39,6 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
|
||||
var noFrameworkPackageReferenceItemGroup = migrationRuleInputs.OutputMSBuildProject.AddItemGroup();
|
||||
|
||||
AddProjectTypeSpecificDependencies(migrationSettings, noFrameworkPackageReferenceItemGroup);
|
||||
|
||||
// Inject Sdk dependency
|
||||
_transformApplicator.Execute(
|
||||
PackageDependencyInfoTransform.Transform(
|
||||
|
@ -50,6 +48,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
Version = migrationSettings.SdkPackageVersion,
|
||||
PrivateAssets = "All"
|
||||
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);
|
||||
|
||||
AddProjectTypeSpecificDependencies(migrationRuleInputs, noFrameworkPackageReferenceItemGroup);
|
||||
|
||||
// Migrate Direct Deps first
|
||||
MigrateDependencies(
|
||||
|
@ -78,24 +78,24 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
MigrateTools(project, migrationRuleInputs.OutputMSBuildProject);
|
||||
}
|
||||
|
||||
private void AddProjectTypeSpecificDependencies(MigrationSettings migrationSettings, ProjectItemGroupElement noFrameworkPackageReferenceItemGroup)
|
||||
private void AddProjectTypeSpecificDependencies(
|
||||
MigrationRuleInputs migrationRuleInputs,
|
||||
ProjectItemGroupElement noFrameworkPackageReferenceItemGroup)
|
||||
{
|
||||
string type;
|
||||
if (ProjectTypeDetector.TryDetectProjectType(migrationSettings.ProjectDirectory, out type))
|
||||
var type = ProjectTypeDetector.TryDetectProjectType(
|
||||
migrationRuleInputs.DefaultProjectContext.ProjectFile);
|
||||
switch (type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case "web":
|
||||
_transformApplicator.Execute(
|
||||
PackageDependencyInfoTransform.Transform(
|
||||
new PackageDependencyInfo
|
||||
{
|
||||
Name = ConstantPackageNames.CWebSdkPackageName,
|
||||
Version = ConstantPackageVersions.WebSdkPackageVersion,
|
||||
PrivateAssets = "All"
|
||||
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);
|
||||
break;
|
||||
}
|
||||
case "web":
|
||||
_transformApplicator.Execute(
|
||||
PackageDependencyInfoTransform.Transform(
|
||||
new PackageDependencyInfo
|
||||
{
|
||||
Name = ConstantPackageNames.CWebSdkPackageName,
|
||||
Version = ConstantPackageVersions.WebSdkPackageVersion,
|
||||
PrivateAssets = "All"
|
||||
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -61,15 +61,17 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
""Microsoft.AspNetCore.Mvc"" : {
|
||||
""version"": ""1.0.0""
|
||||
}
|
||||
},
|
||||
""frameworks"": {
|
||||
""netcoreapp1.0"": {}
|
||||
}
|
||||
}");
|
||||
|
||||
var packageRef = mockProj.Items.FirstOrDefault(i =>
|
||||
i.Include == "Microsoft.NET.Sdk.Web" && i.ItemType == "PackageReference");
|
||||
|
||||
var packageRef = mockProj.Items.First(i => i.Include == "Microsoft.NET.Sdk.Web" && i.ItemType == "PackageReference");
|
||||
|
||||
var privateAssetsMetadata = packageRef.GetMetadataWithName("PrivateAssets");
|
||||
privateAssetsMetadata.Value.Should().NotBeNull();
|
||||
privateAssetsMetadata.Value.Should().Be("All");
|
||||
packageRef.Should().NotBeNull();
|
||||
packageRef.GetMetadataWithName("PrivateAssets").Value.Should().NotBeNull().And.Be("All");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
Loading…
Add table
Reference in a new issue