When Library add reference to NETStandardLib if not present
This commit is contained in:
parent
875e275e38
commit
e3bc102b06
13 changed files with 134 additions and 39 deletions
|
@ -0,0 +1 @@
|
|||
noautobuild
|
|
@ -0,0 +1,5 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"frameworks": {
|
||||
"netstandard1.3": {
|
||||
"dependencies": {
|
||||
"System.AppContext": "4.1.0",
|
||||
"NETStandard.Library": "1.5.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
noautobuild
|
|
@ -0,0 +1,5 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"frameworks": {
|
||||
"netstandard1.3": {
|
||||
"dependencies": {
|
||||
"System.AppContext": "4.1.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,6 +19,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
public const string TestSdkPackageName = "Microsoft.NET.Test.Sdk";
|
||||
public const string XUnitPackageName = "xunit";
|
||||
public const string XUnitRunnerPackageName = "xunit.runner.visualstudio";
|
||||
public const string NetStandardPackageName = "NETStandard.Library";
|
||||
public const string NetStandardPackageVersion = "1.6.0";
|
||||
|
||||
public static readonly IDictionary<string, PackageDependencyInfo> ProjectDependencyPackages =
|
||||
new Dictionary<string, PackageDependencyInfo> {
|
||||
|
|
|
@ -13,59 +13,59 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
{
|
||||
public static ProjectType GetProjectType(this Project project)
|
||||
{
|
||||
var projectType = ProjectType.Console;
|
||||
if (project.IsWebProject())
|
||||
ProjectType projectType = ProjectType.Library;
|
||||
if (project.IsTestProject)
|
||||
{
|
||||
projectType = ProjectType.Test;
|
||||
}
|
||||
else if (project.HasEntryPoint())
|
||||
{
|
||||
if (project.HasDependency(ContainingName(".AspNetCore.")))
|
||||
{
|
||||
projectType = ProjectType.Web;
|
||||
}
|
||||
else if (project.IsTestProject)
|
||||
else
|
||||
{
|
||||
projectType = ProjectType.Test;
|
||||
projectType = ProjectType.Console;
|
||||
}
|
||||
}
|
||||
|
||||
return projectType;
|
||||
}
|
||||
|
||||
private static bool IsWebProject(this Project project)
|
||||
private static bool HasEntryPoint(this Project project)
|
||||
{
|
||||
if(project.IsTestProject)
|
||||
{
|
||||
return false;
|
||||
return project.GetCompilerOptions(null, "Debug").EmitEntryPoint.GetValueOrDefault();
|
||||
}
|
||||
|
||||
var isExecutable = project.GetCompilerOptions(null, "Debug").EmitEntryPoint.GetValueOrDefault();
|
||||
if (isExecutable
|
||||
&& project.HasAnyPackageContainingName(".AspNetCore."))
|
||||
private static Func<ProjectLibraryDependency, bool> ContainingName(string nameSegment)
|
||||
{
|
||||
return x => x.Name.IndexOf(nameSegment, StringComparison.OrdinalIgnoreCase) > -1;
|
||||
}
|
||||
|
||||
public static bool HasDependency(this Project project, Func<ProjectLibraryDependency, bool> pred)
|
||||
{
|
||||
if (HasAnyDependency(project.Dependencies, pred))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var tf in project.GetTargetFrameworks())
|
||||
{
|
||||
if(HasAnyDependency(tf.Dependencies, pred))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool HasAnyPackageContainingName(this Project project, string nameSegment)
|
||||
private static bool HasAnyDependency(
|
||||
IEnumerable<ProjectLibraryDependency> dependencies,
|
||||
Func<ProjectLibraryDependency, bool> pred)
|
||||
{
|
||||
var containsPackageName = HasAnyPackageContainingName(
|
||||
new ReadOnlyCollection<ProjectLibraryDependency>(project.Dependencies),
|
||||
nameSegment);
|
||||
foreach (var tf in project.GetTargetFrameworks())
|
||||
{
|
||||
if(containsPackageName)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
containsPackageName = HasAnyPackageContainingName(tf.Dependencies, nameSegment);
|
||||
}
|
||||
|
||||
return containsPackageName;
|
||||
}
|
||||
|
||||
private static bool HasAnyPackageContainingName(
|
||||
IReadOnlyList<ProjectLibraryDependency> dependencies,
|
||||
string nameSegment)
|
||||
{
|
||||
return dependencies.Any(x => x.Name.IndexOf(nameSegment, StringComparison.OrdinalIgnoreCase) > -1);
|
||||
return dependencies.Any(pred);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,7 +3,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
|
|||
internal enum ProjectType
|
||||
{
|
||||
Console,
|
||||
Web,
|
||||
Test
|
||||
Library,
|
||||
Test,
|
||||
Web
|
||||
}
|
||||
}
|
|
@ -136,6 +136,21 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|||
mergeExisting: false);
|
||||
}
|
||||
break;
|
||||
case ProjectType.Library:
|
||||
if (!project.HasDependency(
|
||||
(dep) => dep.Name.Trim().ToLower() == PackageConstants.NetStandardPackageName.ToLower()))
|
||||
{
|
||||
_transformApplicator.Execute(
|
||||
PackageDependencyInfoTransform().Transform(
|
||||
new PackageDependencyInfo
|
||||
{
|
||||
Name = PackageConstants.NetStandardPackageName,
|
||||
Version = PackageConstants.NetStandardPackageVersion
|
||||
}),
|
||||
noFrameworkPackageReferenceItemGroup,
|
||||
mergeExisting: true);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -44,7 +44,10 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
}
|
||||
}");
|
||||
|
||||
var packageRef = mockProj.Items.Where(i => i.Include != "Microsoft.NET.Sdk" && i.ItemType == "PackageReference").Should().BeEmpty();
|
||||
var packageRef = mockProj.Items.Where(i =>
|
||||
i.Include != "Microsoft.NET.Sdk" &&
|
||||
i.Include != "NETStandard.Library" &&
|
||||
i.ItemType == "PackageReference").Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
|
|
@ -347,6 +347,36 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
|
|||
i.GetMetadataWithName("Version").Value == "2.2.0-beta4-build1188"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"
|
||||
{
|
||||
""frameworks"": {
|
||||
""netstandard1.3"": {
|
||||
""dependencies"": {
|
||||
""System.AppContext"": ""4.1.0"",
|
||||
""NETStandard.Library"": ""1.5.0""
|
||||
}
|
||||
}
|
||||
}
|
||||
}")]
|
||||
[InlineData(@"
|
||||
{
|
||||
""frameworks"": {
|
||||
""netstandard1.3"": {
|
||||
""dependencies"": {
|
||||
""System.AppContext"": ""4.1.0""
|
||||
}
|
||||
}
|
||||
}
|
||||
}")]
|
||||
public void It_migrates_library_and_does_not_double_netstandard_ref(string pjContent)
|
||||
{
|
||||
var mockProj = RunPackageDependenciesRuleOnPj(pjContent);
|
||||
|
||||
mockProj.Items.Should().ContainSingle(
|
||||
i => (i.Include == "NETStandard.Library" && i.ItemType == "PackageReference"));
|
||||
}
|
||||
|
||||
private void EmitsPackageReferences(ProjectRootElement mockProj, params Tuple<string, string, string>[] packageSpecs)
|
||||
{
|
||||
foreach (var packageSpec in packageSpecs)
|
||||
|
|
|
@ -423,6 +423,19 @@ namespace Microsoft.DotNet.Migration.Tests
|
|||
.Count().Should().Be(1);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("LibraryWithoutNetStandardLibRef")]
|
||||
[InlineData("LibraryWithNetStandardLibRef")]
|
||||
public void It_migrates_and_builds_library(string projectName)
|
||||
{
|
||||
var projectDirectory = TestAssetsManager.CreateTestInstance(projectName,
|
||||
callingMethod: $"{nameof(It_migrates_and_builds_library)}-projectName").Path;
|
||||
|
||||
MigrateProject(projectDirectory);
|
||||
Restore(projectDirectory, projectName);
|
||||
BuildMSBuild(projectDirectory, projectName);
|
||||
}
|
||||
|
||||
private void VerifyAutoInjectedDesktopReferences(string projectDirectory, string projectName, bool shouldBePresent)
|
||||
{
|
||||
if (projectName != null)
|
||||
|
|
Loading…
Reference in a new issue