Merge pull request #4554 from krwq/fix_lib_wo_netstandard

When Library add reference to NETStandardLib if not present
This commit is contained in:
Krzysztof Wicher 2016-11-01 16:50:47 -07:00 committed by GitHub
commit d520160ce3
13 changed files with 134 additions and 39 deletions

View file

@ -0,0 +1 @@
noautobuild

View file

@ -0,0 +1,5 @@
using System;
class Program
{
}

View file

@ -0,0 +1,10 @@
{
"frameworks": {
"netstandard1.3": {
"dependencies": {
"System.AppContext": "4.1.0",
"NETStandard.Library": "1.5.0"
}
}
}
}

View file

@ -0,0 +1 @@
noautobuild

View file

@ -0,0 +1,5 @@
using System;
class Program
{
}

View file

@ -0,0 +1,9 @@
{
"frameworks": {
"netstandard1.3": {
"dependencies": {
"System.AppContext": "4.1.0"
}
}
}
}

View file

@ -19,6 +19,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
public const string TestSdkPackageName = "Microsoft.NET.Test.Sdk"; public const string TestSdkPackageName = "Microsoft.NET.Test.Sdk";
public const string XUnitPackageName = "xunit"; public const string XUnitPackageName = "xunit";
public const string XUnitRunnerPackageName = "xunit.runner.visualstudio"; 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 = public static readonly IDictionary<string, PackageDependencyInfo> ProjectDependencyPackages =
new Dictionary<string, PackageDependencyInfo> { new Dictionary<string, PackageDependencyInfo> {

View file

@ -13,59 +13,59 @@ namespace Microsoft.DotNet.ProjectJsonMigration
{ {
public static ProjectType GetProjectType(this Project project) public static ProjectType GetProjectType(this Project project)
{ {
var projectType = ProjectType.Console; ProjectType projectType = ProjectType.Library;
if (project.IsWebProject()) if (project.IsTestProject)
{
projectType = ProjectType.Test;
}
else if (project.HasEntryPoint())
{
if (project.HasDependency(ContainingName(".AspNetCore.")))
{ {
projectType = ProjectType.Web; projectType = ProjectType.Web;
} }
else if (project.IsTestProject) else
{ {
projectType = ProjectType.Test; projectType = ProjectType.Console;
}
} }
return projectType; return projectType;
} }
private static bool IsWebProject(this Project project) private static bool HasEntryPoint(this Project project)
{ {
if(project.IsTestProject) return project.GetCompilerOptions(null, "Debug").EmitEntryPoint.GetValueOrDefault();
{
return false;
} }
var isExecutable = project.GetCompilerOptions(null, "Debug").EmitEntryPoint.GetValueOrDefault(); private static Func<ProjectLibraryDependency, bool> ContainingName(string nameSegment)
if (isExecutable {
&& project.HasAnyPackageContainingName(".AspNetCore.")) 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; return true;
} }
foreach (var tf in project.GetTargetFrameworks())
{
if(HasAnyDependency(tf.Dependencies, pred))
{
return true;
}
}
return false; 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( return dependencies.Any(pred);
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);
} }
} }
} }

View file

@ -3,7 +3,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration
internal enum ProjectType internal enum ProjectType
{ {
Console, Console,
Web, Library,
Test Test,
Web
} }
} }

View file

@ -136,6 +136,21 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
mergeExisting: false); mergeExisting: false);
} }
break; 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: default:
break; break;
} }

View file

@ -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] [Theory]

View file

@ -347,6 +347,36 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
i.GetMetadataWithName("Version").Value == "2.2.0-beta4-build1188")); 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) private void EmitsPackageReferences(ProjectRootElement mockProj, params Tuple<string, string, string>[] packageSpecs)
{ {
foreach (var packageSpec in packageSpecs) foreach (var packageSpec in packageSpecs)

View file

@ -423,6 +423,19 @@ namespace Microsoft.DotNet.Migration.Tests
.Count().Should().Be(1); .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) private void VerifyAutoInjectedDesktopReferences(string projectDirectory, string projectName, bool shouldBePresent)
{ {
if (projectName != null) if (projectName != null)