Update migration to use the web template as a base for web projects

This commit is contained in:
Mike Lorbetske 2016-10-28 13:39:07 -07:00 committed by Livar Cunha
parent 22a5a15ba9
commit e847389fc2
5 changed files with 111 additions and 0 deletions

View file

@ -6,5 +6,6 @@ namespace Microsoft.DotNet.ProjectJsonMigration
internal class ConstantPackageNames
{
public const string CSdkPackageName = "Microsoft.NET.Sdk";
public const string CWebSdkPackageName = "Microsoft.NET.Sdk.Web";
}
}

View file

@ -0,0 +1,7 @@
namespace Microsoft.DotNet.ProjectJsonMigration
{
internal class ConstantPackageVersions
{
public const string WebSdkPackageVersion = "1.0.0-alpha-20161027-4-91";
}
}

View file

@ -0,0 +1,60 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license info
using System;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Internal.ProjectModel;
namespace Microsoft.DotNet.ProjectJsonMigration
{
public static class ProjectTypeDetector
{
public static bool TryDetectProjectType(string projectDirectory, out string projectType)
{
string projectJsonFile = Path.Combine(projectDirectory, "project.json");
if (!File.Exists(projectJsonFile))
{
projectType = null;
return false;
}
if (IsWebProject(projectJsonFile))
{
projectType = "web";
return true;
}
projectType = null;
return false;
}
private static bool IsWebProject(string projectJsonFile)
{
Project project;
if (ProjectReader.TryGetProject(projectJsonFile, out project))
{
if(project.IsTestProject)
{
return false;
}
foreach (var tf in project.GetTargetFrameworks())
{
if (tf.CompilerOptions.EmitEntryPoint.GetValueOrDefault()
&& HasAnyPackageContainingName(tf, ".AspNetCore."))
{
return true;
}
}
}
return false;
}
private static bool HasAnyPackageContainingName(TargetFrameworkInformation tf, string nameSegment)
{
return tf.Dependencies.Any(x => x.Name.IndexOf(nameSegment, StringComparison.OrdinalIgnoreCase) > -1);
}
}
}

View file

@ -39,6 +39,8 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
var noFrameworkPackageReferenceItemGroup = migrationRuleInputs.OutputMSBuildProject.AddItemGroup();
AddProjectTypeSpecificDependencies(migrationSettings, noFrameworkPackageReferenceItemGroup);
// Inject Sdk dependency
_transformApplicator.Execute(
PackageDependencyInfoTransform.Transform(
@ -76,6 +78,27 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
MigrateTools(project, migrationRuleInputs.OutputMSBuildProject);
}
private void AddProjectTypeSpecificDependencies(MigrationSettings migrationSettings, ProjectItemGroupElement noFrameworkPackageReferenceItemGroup)
{
string type;
if (ProjectTypeDetector.TryDetectProjectType(migrationSettings.ProjectDirectory, out type))
{
switch (type)
{
case "web":
_transformApplicator.Execute(
PackageDependencyInfoTransform.Transform(
new PackageDependencyInfo
{
Name = ConstantPackageNames.CWebSdkPackageName,
Version = ConstantPackageVersions.WebSdkPackageVersion,
PrivateAssets = "All"
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);
break;
}
}
}
private void MigrateImports(ProjectPropertyGroupElement commonPropertyGroup, TargetFrameworkInformation targetFramework)
{
var transform = ImportsTransformation.Transform(targetFramework);

View file

@ -49,6 +49,26 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
privateAssetsMetadata.Value.Should().Be("All");
}
[Fact]
public void It_migrates_web_projects_to_have_web_sdk_PrivateAssets()
{
var mockProj = RunPackageDependenciesRuleOnPj(@"
{
""dependencies"": {
""Microsoft.AspNetCore.Mvc"" : {
""version"": ""1.0.0""
}
}
}");
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");
}
[Fact]
public void It_migrates_suppress_parent_array_to_PrivateAssets()
{