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
|
// Licensed under the MIT license. See LICENSE file in the project root for full license info
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.DotNet.Internal.ProjectModel;
|
using Microsoft.DotNet.Internal.ProjectModel;
|
||||||
|
|
||||||
namespace Microsoft.DotNet.ProjectJsonMigration
|
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");
|
string projectType = null;
|
||||||
if (!File.Exists(projectJsonFile))
|
if (IsWebProject(project))
|
||||||
|
{
|
||||||
|
projectType = "web";
|
||||||
|
}
|
||||||
|
|
||||||
|
return projectType;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsWebProject(Project project)
|
||||||
|
{
|
||||||
|
if(project.IsTestProject)
|
||||||
{
|
{
|
||||||
projectType = null;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsWebProject(projectJsonFile))
|
var isExecutable = project.GetCompilerOptions(null, "Debug").EmitEntryPoint.GetValueOrDefault();
|
||||||
|
if (isExecutable
|
||||||
|
&& HasAnyPackageContainingName(project, ".AspNetCore."))
|
||||||
{
|
{
|
||||||
projectType = "web";
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
projectType = null;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsWebProject(string projectJsonFile)
|
private static bool HasAnyPackageContainingName(Project project, string nameSegment)
|
||||||
{
|
{
|
||||||
Project project;
|
var containsPackageName = HasAnyPackageContainingName(
|
||||||
if (ProjectReader.TryGetProject(projectJsonFile, out project))
|
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())
|
containsPackageName = HasAnyPackageContainingName(tf.Dependencies, nameSegment);
|
||||||
{
|
|
||||||
if (tf.CompilerOptions.EmitEntryPoint.GetValueOrDefault()
|
|
||||||
&& HasAnyPackageContainingName(tf, ".AspNetCore."))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
var noFrameworkPackageReferenceItemGroup = migrationRuleInputs.OutputMSBuildProject.AddItemGroup();
|
||||||
|
|
||||||
AddProjectTypeSpecificDependencies(migrationSettings, noFrameworkPackageReferenceItemGroup);
|
|
||||||
|
|
||||||
// Inject Sdk dependency
|
// Inject Sdk dependency
|
||||||
_transformApplicator.Execute(
|
_transformApplicator.Execute(
|
||||||
PackageDependencyInfoTransform.Transform(
|
PackageDependencyInfoTransform.Transform(
|
||||||
|
@ -50,6 +48,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
||||||
Version = migrationSettings.SdkPackageVersion,
|
Version = migrationSettings.SdkPackageVersion,
|
||||||
PrivateAssets = "All"
|
PrivateAssets = "All"
|
||||||
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);
|
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);
|
||||||
|
|
||||||
|
AddProjectTypeSpecificDependencies(migrationRuleInputs, noFrameworkPackageReferenceItemGroup);
|
||||||
|
|
||||||
// Migrate Direct Deps first
|
// Migrate Direct Deps first
|
||||||
MigrateDependencies(
|
MigrateDependencies(
|
||||||
|
@ -78,24 +78,24 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
||||||
MigrateTools(project, migrationRuleInputs.OutputMSBuildProject);
|
MigrateTools(project, migrationRuleInputs.OutputMSBuildProject);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddProjectTypeSpecificDependencies(MigrationSettings migrationSettings, ProjectItemGroupElement noFrameworkPackageReferenceItemGroup)
|
private void AddProjectTypeSpecificDependencies(
|
||||||
|
MigrationRuleInputs migrationRuleInputs,
|
||||||
|
ProjectItemGroupElement noFrameworkPackageReferenceItemGroup)
|
||||||
{
|
{
|
||||||
string type;
|
var type = ProjectTypeDetector.TryDetectProjectType(
|
||||||
if (ProjectTypeDetector.TryDetectProjectType(migrationSettings.ProjectDirectory, out type))
|
migrationRuleInputs.DefaultProjectContext.ProjectFile);
|
||||||
|
switch (type)
|
||||||
{
|
{
|
||||||
switch (type)
|
case "web":
|
||||||
{
|
_transformApplicator.Execute(
|
||||||
case "web":
|
PackageDependencyInfoTransform.Transform(
|
||||||
_transformApplicator.Execute(
|
new PackageDependencyInfo
|
||||||
PackageDependencyInfoTransform.Transform(
|
{
|
||||||
new PackageDependencyInfo
|
Name = ConstantPackageNames.CWebSdkPackageName,
|
||||||
{
|
Version = ConstantPackageVersions.WebSdkPackageVersion,
|
||||||
Name = ConstantPackageNames.CWebSdkPackageName,
|
PrivateAssets = "All"
|
||||||
Version = ConstantPackageVersions.WebSdkPackageVersion,
|
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);
|
||||||
PrivateAssets = "All"
|
break;
|
||||||
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,15 +61,17 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
||||||
""Microsoft.AspNetCore.Mvc"" : {
|
""Microsoft.AspNetCore.Mvc"" : {
|
||||||
""version"": ""1.0.0""
|
""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");
|
packageRef.Should().NotBeNull();
|
||||||
|
packageRef.GetMetadataWithName("PrivateAssets").Value.Should().NotBeNull().And.Be("All");
|
||||||
var privateAssetsMetadata = packageRef.GetMetadataWithName("PrivateAssets");
|
|
||||||
privateAssetsMetadata.Value.Should().NotBeNull();
|
|
||||||
privateAssetsMetadata.Value.Should().Be("All");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue