2016-10-20 11:00:41 -07:00
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.DotNet.ProjectJsonMigration.Transforms;
|
|
|
|
using Microsoft.Build.Construction;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Microsoft.DotNet.ProjectJsonMigration.Models;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Reflection;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.ProjectJsonMigration.Rules
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
internal class CleanOutputProjectRule : IMigrationRule
|
2016-10-20 11:00:41 -07:00
|
|
|
{
|
|
|
|
public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
|
|
|
|
{
|
|
|
|
var outputProject = migrationRuleInputs.OutputMSBuildProject;
|
|
|
|
|
|
|
|
CleanEmptyPropertiesAndItems(outputProject);
|
|
|
|
CleanPropertiesThatDontChangeValue(outputProject);
|
|
|
|
CleanEmptyPropertyAndItemGroups(outputProject);
|
2016-12-16 12:23:07 -08:00
|
|
|
CleanEmptyTargets(outputProject);
|
2016-10-20 11:00:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void CleanEmptyPropertyAndItemGroups(ProjectRootElement msbuildProject)
|
|
|
|
{
|
2016-12-16 12:23:07 -08:00
|
|
|
CleanEmptyProjectElementContainers(msbuildProject.PropertyGroups);
|
|
|
|
CleanEmptyProjectElementContainers(msbuildProject.ItemGroups);
|
2016-10-20 11:00:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void CleanEmptyPropertiesAndItems(ProjectRootElement msbuildProject)
|
|
|
|
{
|
|
|
|
foreach (var property in msbuildProject.Properties)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(property.Value))
|
|
|
|
{
|
|
|
|
property.Parent.RemoveChild(property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var item in msbuildProject.Items)
|
|
|
|
{
|
2017-01-28 15:14:17 -10:00
|
|
|
if (string.IsNullOrEmpty(item.Include) &&
|
|
|
|
string.IsNullOrEmpty(item.Remove) &&
|
|
|
|
string.IsNullOrEmpty(item.Update))
|
2016-10-20 11:00:41 -07:00
|
|
|
{
|
|
|
|
item.Parent.RemoveChild(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CleanPropertiesThatDontChangeValue(ProjectRootElement msbuildProject)
|
|
|
|
{
|
|
|
|
foreach (var property in msbuildProject.Properties)
|
|
|
|
{
|
|
|
|
var value = property.Value.Trim();
|
|
|
|
var variableExpectedValue = "$(" + property.Name + ")";
|
|
|
|
|
|
|
|
if (value == variableExpectedValue)
|
|
|
|
{
|
|
|
|
property.Parent.RemoveChild(property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 12:23:07 -08:00
|
|
|
|
|
|
|
private void CleanEmptyTargets(ProjectRootElement msbuildProject)
|
|
|
|
{
|
|
|
|
CleanEmptyProjectElementContainers(msbuildProject.Targets);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CleanEmptyProjectElementContainers(IEnumerable<ProjectElementContainer> containers)
|
|
|
|
{
|
|
|
|
foreach (var container in containers)
|
|
|
|
{
|
|
|
|
container.RemoveIfEmpty();
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 11:00:41 -07:00
|
|
|
}
|
|
|
|
}
|