Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 8cec61c

Browse files
LivarPiotr Puszkiewicz
Livar
authored and
Piotr Puszkiewicz
committed
Adding MigrateWebSdkRule to the DefaultMigrationRuleSet (#4963)
* Adding MigrateWebSdkRule to the DefaultMigrationRuleSet and adding a E2E test to cover it. * Do not migrate compile and EmbeddedResources for web application, because those are included in the Web Sdk already. * Addressing code review comments
1 parent 5b0801c commit 8cec61c

File tree

4 files changed

+126
-32
lines changed

4 files changed

+126
-32
lines changed

src/Microsoft.DotNet.ProjectJsonMigration/DefaultMigrationRuleSet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ internal class DefaultMigrationRuleSet : IMigrationRule
2525
new MigrateAssemblyInfoRule(),
2626
new RemoveDefaultsFromProjectRule(),
2727
new CleanOutputProjectRule(),
28-
new SaveOutputProjectRule()
28+
new SaveOutputProjectRule(),
29+
new MigrateWebSdkRule()
2930
};
3031

3132
public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)

src/Microsoft.DotNet.ProjectJsonMigration/Rules/MigrateBuildOptionsRule.cs

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migra
196196

197197
var compilerOptions = projectContext.ProjectFile.GetCompilerOptions(null, null);
198198

199+
var project = migrationRuleInputs.DefaultProjectContext.ProjectFile;
200+
var projectType = project.GetProjectType();
201+
199202
// If we're in a configuration, we need to be careful not to overwrite values from BuildOptions
200203
// without a configuration
201204
if (_configurationBuildOptions == null)
@@ -207,7 +210,9 @@ public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migra
207210
propertyGroup,
208211
itemGroup,
209212
_transformApplicator,
210-
migrationSettings.ProjectDirectory);
213+
migrationSettings.ProjectDirectory,
214+
projectType,
215+
csproj);
211216
}
212217
else
213218
{
@@ -217,7 +222,9 @@ public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migra
217222
propertyGroup,
218223
itemGroup,
219224
_transformApplicator,
220-
migrationSettings.ProjectDirectory);
225+
migrationSettings.ProjectDirectory,
226+
projectType,
227+
csproj);
221228
}
222229
}
223230

@@ -227,7 +234,9 @@ private void PerformConfigurationPropertyAndItemMappings(
227234
ProjectPropertyGroupElement propertyGroup,
228235
ProjectItemGroupElement itemGroup,
229236
ITransformApplicator transformApplicator,
230-
string projectDirectory)
237+
string projectDirectory,
238+
ProjectType projectType,
239+
ProjectRootElement csproj)
231240
{
232241
foreach (var transform in _propertyTransforms)
233242
{
@@ -243,7 +252,13 @@ private void PerformConfigurationPropertyAndItemMappings(
243252
foreach (var includeContextTransformExecute in _includeContextTransformExecutes)
244253
{
245254
var nonConfigurationOutput = includeContextTransformExecute(compilerOptions, projectDirectory);
246-
var configurationOutput = includeContextTransformExecute(configurationCompilerOptions, projectDirectory);
255+
var configurationOutput =
256+
includeContextTransformExecute(configurationCompilerOptions, projectDirectory);
257+
258+
configurationOutput = RemoveDefaultCompileAndEmbeddedResourceForWebProjects(
259+
configurationOutput,
260+
projectType,
261+
csproj);
247262

248263
transformApplicator.Execute(configurationOutput, itemGroup, mergeExisting: true);
249264
}
@@ -283,7 +298,9 @@ private void PerformPropertyAndItemMappings(
283298
ProjectPropertyGroupElement propertyGroup,
284299
ProjectItemGroupElement itemGroup,
285300
ITransformApplicator transformApplicator,
286-
string projectDirectory)
301+
string projectDirectory,
302+
ProjectType projectType,
303+
ProjectRootElement csproj)
287304
{
288305
foreach (var transform in _propertyTransforms)
289306
{
@@ -292,13 +309,40 @@ private void PerformPropertyAndItemMappings(
292309

293310
foreach (var includeContextTransformExecute in _includeContextTransformExecutes)
294311
{
312+
var transform = includeContextTransformExecute(compilerOptions, projectDirectory);
313+
314+
transform = RemoveDefaultCompileAndEmbeddedResourceForWebProjects(
315+
transform,
316+
projectType,
317+
csproj);
318+
295319
transformApplicator.Execute(
296-
includeContextTransformExecute(compilerOptions, projectDirectory),
320+
transform,
297321
itemGroup,
298322
mergeExisting: true);
299323
}
300324
}
301325

326+
private IEnumerable<ProjectItemElement> RemoveDefaultCompileAndEmbeddedResourceForWebProjects(
327+
IEnumerable<ProjectItemElement> transform,
328+
ProjectType projectType,
329+
ProjectRootElement csproj)
330+
{
331+
if(projectType == ProjectType.Web)
332+
{
333+
var itemsToRemove = transform.Where(p =>
334+
p != null &&
335+
p.Include.Contains("**\\*") &&
336+
(p.ItemType == "Compile" || p.ItemType == "EmbeddedResource"));
337+
338+
CleanExistingItems(csproj, new [] {"Compile", "EmbeddedResource"});
339+
340+
transform = transform.Where(p => !itemsToRemove.Contains(p));
341+
}
342+
343+
return transform;
344+
}
345+
302346
private void CleanExistingProperties(ProjectRootElement csproj)
303347
{
304348
var existingPropertiesToRemove = new [] {"OutputType", "TargetExt"};
@@ -314,6 +358,19 @@ private void CleanExistingProperties(ProjectRootElement csproj)
314358
}
315359
}
316360

361+
private void CleanExistingItems(ProjectRootElement csproj, IEnumerable<string> itemsToRemove)
362+
{
363+
foreach (var itemName in itemsToRemove)
364+
{
365+
var items = csproj.Items.Where(i => i.ItemType == itemName);
366+
367+
foreach (var item in items)
368+
{
369+
item.Parent.RemoveChild(item);
370+
}
371+
}
372+
}
373+
317374
private IncludeContext GetCompileIncludeContext(CommonCompilerOptions compilerOptions, string projectDirectory)
318375
{
319376
// Defaults from src/Microsoft.DotNet.ProjectModel/ProjectReader.cs #L596

test/Microsoft.DotNet.ProjectJsonMigration.Tests/Rules/GivenThatIWantToMigrateBuildOptions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ public void Migrating_empty_buildOptions_populates_only_Compile_and_EmbeddedReso
6363
mockProj.Items.First(i => i.ItemType == "EmbeddedResource").Exclude.Should().BeEmpty();
6464
}
6565

66+
[Fact]
67+
public void Migrating_web_project_without_custom_sources_or_resources_does_not_emit_compile_and_embeddedResource()
68+
{
69+
var mockProj = RunBuildOptionsRuleOnPj(@"
70+
{
71+
""buildOptions"": {
72+
""emitEntryPoint"": true
73+
},
74+
""dependencies"": {
75+
""Microsoft.AspNetCore.Mvc"" : {
76+
""version"": ""1.0.0""
77+
}
78+
},
79+
""frameworks"": {
80+
""netcoreapp1.0"": {}
81+
}
82+
}");
83+
84+
mockProj.Items.Count().Should().Be(0);
85+
}
86+
6687
[Fact]
6788
public void Migrating_EmitEntryPoint_true_populates_OutputType_field()
6889
{

test/dotnet-migrate.Tests/GivenThatIWantToMigrateTestApps.cs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class GivenThatIWantToMigrateTestApps : TestBase
2222
[InlineData("TestAppWithRuntimeOptions")]
2323
[InlineData("TestAppWithContents")]
2424
[InlineData("AppWithAssemblyInfo")]
25-
public void It_migrates_apps(string projectName)
25+
public void ItMigratesApps(string projectName)
2626
{
2727
var projectDirectory = TestAssetsManager.CreateTestInstance(projectName, identifier: projectName)
2828
.WithLockFiles()
@@ -50,7 +50,7 @@ public void It_migrates_apps(string projectName)
5050
}
5151

5252
[Fact]
53-
public void It_migrates_signed_apps()
53+
public void ItMigratesSignedApps()
5454
{
5555
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppWithSigning").WithLockFiles().Path;
5656

@@ -74,7 +74,7 @@ public void It_migrates_signed_apps()
7474
}
7575

7676
[Fact]
77-
public void It_migrates_dotnet_new_console_with_identical_outputs()
77+
public void ItMigratesDotnetNewConsoleWithIdenticalOutputs()
7878
{
7979
var testInstance = TestAssetsManager
8080
.CreateTestInstance("ProjectJsonConsoleTemplate");
@@ -96,8 +96,8 @@ public void It_migrates_dotnet_new_console_with_identical_outputs()
9696
VerifyAllMSBuildOutputsRunnable(projectDirectory);
9797
}
9898

99-
[Fact(Skip="Final tools version missing.")]
100-
public void It_migrates_old_dotnet_new_web_without_tools_with_outputs_containing_project_json_outputs()
99+
[Fact]
100+
public void ItMigratesOldDotnetNewWebWithoutToolsWithOutputsContainingProjectJsonOutputs()
101101
{
102102
var testInstance = TestAssetsManager
103103
.CreateTestInstance("ProjectJsonWebTemplate")
@@ -108,10 +108,7 @@ public void It_migrates_old_dotnet_new_web_without_tools_with_outputs_containing
108108
var globalDirectory = Path.Combine(projectDirectory, "..");
109109
var projectJsonFile = Path.Combine(projectDirectory, "project.json");
110110

111-
WriteGlobalJson(globalDirectory);
112-
var projectJson = JObject.Parse(File.ReadAllText(projectJsonFile));
113-
projectJson.Remove("tools");
114-
File.WriteAllText(projectJsonFile, projectJson.ToString());
111+
WriteGlobalJson(globalDirectory);
115112

116113
var outputComparisonData = GetComparisonData(projectDirectory);
117114

@@ -126,9 +123,27 @@ public void It_migrates_old_dotnet_new_web_without_tools_with_outputs_containing
126123
outputsIdentical.Should().BeTrue();
127124
}
128125

126+
public void ItAddsMicrosoftNetWebSdkToTheSdkAttributeOfAWebApp()
127+
{
128+
var testInstance = TestAssetsManager
129+
.CreateTestInstance("ProjectJsonWebTemplate")
130+
.WithLockFiles();
131+
132+
var projectDirectory = testInstance.Path;
133+
134+
var globalDirectory = Path.Combine(projectDirectory, "..");
135+
var projectJsonFile = Path.Combine(projectDirectory, "project.json");
136+
137+
MigrateProject(new [] { projectDirectory });
138+
139+
var csProj = Path.Combine(projectDirectory, $"{new DirectoryInfo(projectDirectory).Name}.csproj");
140+
141+
File.ReadAllText(csProj).Should().Contain(@"Sdk=""Microsoft.NET.Sdk.Web""");
142+
}
143+
129144
[Theory]
130145
[InlineData("TestLibraryWithTwoFrameworks")]
131-
public void It_migrates_projects_with_multiple_TFMs(string projectName)
146+
public void ItMigratesProjectsWithMultipleTFMs(string projectName)
132147
{
133148
var projectDirectory =
134149
TestAssetsManager.CreateTestInstance(projectName, identifier: projectName).WithLockFiles().Path;
@@ -150,7 +165,7 @@ public void It_migrates_projects_with_multiple_TFMs(string projectName)
150165
[InlineData("TestAppWithLibrary/TestLibrary")]
151166
[InlineData("TestLibraryWithAnalyzer")]
152167
[InlineData("PJTestLibraryWithConfiguration")]
153-
public void It_migrates_a_library(string projectName)
168+
public void ItMigratesALibrary(string projectName)
154169
{
155170
var projectDirectory =
156171
TestAssetsManager.CreateTestInstance(projectName, identifier: projectName).WithLockFiles().Path;
@@ -174,7 +189,7 @@ public void It_migrates_a_library(string projectName)
174189
[InlineData("ProjectC", "ProjectC,ProjectD,ProjectE")]
175190
[InlineData("ProjectD", "ProjectD")]
176191
[InlineData("ProjectE", "ProjectE")]
177-
public void It_migrates_root_project_and_references(string projectName, string expectedProjects)
192+
public void ItMigratesRootProjectAndReferences(string projectName, string expectedProjects)
178193
{
179194
var projectDirectory =
180195
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"{projectName}.RefsTest").Path;
@@ -192,7 +207,7 @@ public void It_migrates_root_project_and_references(string projectName, string e
192207
[InlineData("ProjectC")]
193208
[InlineData("ProjectD")]
194209
[InlineData("ProjectE")]
195-
public void It_migrates_root_project_and_skips_references(string projectName)
210+
public void ItMigratesRootProjectAndSkipsReferences(string projectName)
196211
{
197212
var projectDirectory =
198213
TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"{projectName}.SkipRefsTest").Path;
@@ -205,7 +220,7 @@ public void It_migrates_root_project_and_skips_references(string projectName)
205220
[Theory]
206221
[InlineData(true)]
207222
[InlineData(false)]
208-
public void It_migrates_all_projects_in_given_directory(bool skipRefs)
223+
public void ItMigratesAllProjectsInGivenDirectory(bool skipRefs)
209224
{
210225
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph", callingMethod: $"MigrateDirectory.SkipRefs.{skipRefs}").Path;
211226

@@ -224,7 +239,7 @@ public void It_migrates_all_projects_in_given_directory(bool skipRefs)
224239
}
225240

226241
[Fact]
227-
public void It_migrates_given_project_json()
242+
public void ItMigratesGivenProjectJson()
228243
{
229244
var projectDirectory = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph").Path;
230245

@@ -239,7 +254,7 @@ public void It_migrates_given_project_json()
239254

240255
[Fact]
241256
// regression test for https://github.com/dotnet/cli/issues/4269
242-
public void It_migrates_and_builds_P2P_references()
257+
public void ItMigratesAndBuildsP2PReferences()
243258
{
244259
var assetsDir = TestAssetsManager.CreateTestInstance("TestAppDependencyGraph").WithLockFiles().Path;
245260

@@ -269,7 +284,7 @@ public void It_migrates_and_builds_P2P_references()
269284
[Theory]
270285
[InlineData("src", "ProjectH")]
271286
[InlineData("src with spaces", "ProjectJ")]
272-
public void It_migrates_and_builds_projects_in_global_json(string path, string projectName)
287+
public void ItMigratesAndBuildsProjectsInGlobalJson(string path, string projectName)
273288
{
274289
var assetsDir = TestAssetsManager.CreateTestInstance(Path.Combine("TestAppDependencyGraph", "ProjectsWithGlobalJson"),
275290
callingMethod: $"ProjectsWithGlobalJson.{projectName}")
@@ -306,7 +321,7 @@ public void It_migrates_and_builds_projects_in_global_json(string path, string p
306321
[Theory]
307322
[InlineData(true)]
308323
[InlineData(false)]
309-
public void Migration_outputs_error_when_no_projects_found(bool useGlobalJson)
324+
public void MigrationOutputsErrorWhenNoProjectsFound(bool useGlobalJson)
310325
{
311326
var projectDirectory = TestAssetsManager.CreateTestDirectory("Migration_outputs_error_when_no_projects_found");
312327

@@ -353,7 +368,7 @@ public void Migration_outputs_error_when_no_projects_found(bool useGlobalJson)
353368
}
354369

355370
[Fact]
356-
public void It_migrates_and_publishes_projects_with_runtimes()
371+
public void ItMigratesAndPublishesProjectsWithRuntimes()
357372
{
358373
var projectName = "PJTestAppSimple";
359374
var projectDirectory = TestAssetsManager
@@ -369,7 +384,7 @@ public void It_migrates_and_publishes_projects_with_runtimes()
369384
[WindowsOnlyTheory]
370385
[InlineData("DesktopTestProjects", "AutoAddDesktopReferencesDuringMigrate", true)]
371386
[InlineData("TestProjects", "PJTestAppSimple", false)]
372-
public void It_auto_add_desktop_references_during_migrate(string testGroup, string projectName, bool isDesktopApp)
387+
public void ItAutoAddDesktopReferencesDuringMigrate(string testGroup, string projectName, bool isDesktopApp)
373388
{
374389
var runtime = DotnetLegacyRuntimeIdentifiers.InferLegacyRestoreRuntimeIdentifier();
375390
var testAssetManager = GetTestGroupTestAssetsManager(testGroup);
@@ -384,7 +399,7 @@ public void It_auto_add_desktop_references_during_migrate(string testGroup, stri
384399
}
385400

386401
[Fact]
387-
public void It_builds_a_migrated_app_with_a_indirect_dependency()
402+
public void ItBuildsAMigratedAppWithAnIndirectDependency()
388403
{
389404
const string projectName = "ProjectA";
390405
var solutionDirectory =
@@ -399,7 +414,7 @@ public void It_builds_a_migrated_app_with_a_indirect_dependency()
399414
}
400415

401416
[Fact]
402-
public void It_migrates_project_with_output_name()
417+
public void ItMigratesProjectWithOutputName()
403418
{
404419
string projectName = "AppWithOutputAssemblyName";
405420
string expectedOutputName = "MyApp";
@@ -426,18 +441,18 @@ public void It_migrates_project_with_output_name()
426441
[Theory]
427442
[InlineData("LibraryWithoutNetStandardLibRef")]
428443
[InlineData("LibraryWithNetStandardLibRef")]
429-
public void It_migrates_and_builds_library(string projectName)
444+
public void ItMigratesAndBuildsLibrary(string projectName)
430445
{
431446
var projectDirectory = TestAssetsManager.CreateTestInstance(projectName,
432-
callingMethod: $"{nameof(It_migrates_and_builds_library)}-projectName").Path;
447+
callingMethod: $"{nameof(ItMigratesAndBuildsLibrary)}-projectName").Path;
433448

434449
MigrateProject(projectDirectory);
435450
Restore(projectDirectory, projectName);
436451
BuildMSBuild(projectDirectory, projectName);
437452
}
438453

439454
[Fact]
440-
public void It_fails_gracefully_when_migrating_app_with_missing_dependency()
455+
public void ItFailsGracefullyWhenMigratingAppWithMissingDependency()
441456
{
442457
string projectName = "MigrateAppWithMissingDep";
443458
var projectDirectory = Path.Combine(GetTestGroupTestAssetsManager("NonRestoredTestProjects").CreateTestInstance(projectName).Path, "MyApp");

0 commit comments

Comments
 (0)