Exemplo n.º 1
0
        public static IProjectRootElement ChangeImportsAndAddSdkAttribute(this IProjectRootElement projectRootElement, BaselineProject baselineProject, bool forceRemoveCustomImports)
        {
            foreach (var import in projectRootElement.Imports)
            {
                if (!forceRemoveCustomImports)
                {
                    var fileName = Path.GetFileName(import.Project);
                    if (MSBuildFacts.PropsToRemove.Contains(fileName, StringComparer.OrdinalIgnoreCase) ||
                        MSBuildFacts.TargetsToRemove.Contains(fileName, StringComparer.OrdinalIgnoreCase))
                    {
                        projectRootElement.RemoveChild(import);
                    }
                    else if (!MSBuildFacts.ImportsToKeep.Contains(fileName, StringComparer.OrdinalIgnoreCase))
                    {
                        Console.WriteLine($"This project has an unrecognized custom import which may need reviewed after conversion: {fileName}");
                    }
                }
                else
                {
                    projectRootElement.RemoveChild(import);
                }
            }

            if (baselineProject.ProjectStyle is ProjectStyle.WindowsDesktop && baselineProject.TargetTFM is MSBuildFacts.NetCoreApp31)
            {
                projectRootElement.Sdk = DesktopFacts.WinSDKAttribute;
            }
            else if (MSBuildHelpers.IsAspNetCore(projectRootElement, baselineProject.TargetTFM))
            {
                // Libraries targeting .NET Framework can use the default SDK and still be used by NetFx callers.
                // However, web apps (as opposed to libraries) or libraries that are targeting .NET Core/.NET should use the web SDK.
                projectRootElement.Sdk = WebFacts.WebSDKAttribute;
            }
            else
            {
                projectRootElement.Sdk = MSBuildFacts.DefaultSDKAttribute;
            }

            // Xamarin projects contain the Import line, not needed for .NET MAUI
            if ((baselineProject.ProjectStyle is ProjectStyle.XamarinDroid) || (baselineProject.ProjectStyle is ProjectStyle.XamariniOS))
            {
                foreach (var import in projectRootElement.Imports)
                {
                    if (XamarinFacts.UnnecessaryXamarinImports.Contains(import.Project, StringComparer.OrdinalIgnoreCase))
                    {
                        projectRootElement.RemoveChild(import);
                    }
                }
            }

            return(projectRootElement);
        }
Exemplo n.º 2
0
        private void ChangeImports()
        {
            var projectStyle = _sdkBaselineProject.ProjectStyle;

            if (projectStyle == ProjectStyle.Default)
            {
                foreach (var import in _projectRootElement.Imports)
                {
                    _projectRootElement.RemoveChild(import);
                }
                _projectRootElement.Sdk = "Microsoft.NET.Sdk";
            }
        }
        public static IProjectRootElement RemoveDefaultedProperties(this IProjectRootElement projectRootElement, BaselineProject baselineProject, ImmutableDictionary <string, Differ> differs)
        {
            foreach (var propGroup in projectRootElement.PropertyGroups)
            {
                var configurationName = MSBuildHelpers.GetConfigurationName(propGroup.Condition);
                var propDiff          = differs[configurationName].GetPropertiesDiff();

                foreach (var prop in propGroup.Properties)
                {
                    // These properties were added to the baseline - so don't treat them as defaulted properties.
                    if (baselineProject.GlobalProperties.Contains(prop.Name, StringComparer.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    if (propDiff.DefaultedProperties.Select(p => p.Name).Contains(prop.Name, StringComparer.OrdinalIgnoreCase))
                    {
                        propGroup.RemoveChild(prop);
                    }
                }

                if (propGroup.Properties.Count == 0)
                {
                    projectRootElement.RemoveChild(propGroup);
                }
            }

            return(projectRootElement);
        }
        public static IProjectRootElement ChangeImportsAndAddSdkAttribute(this IProjectRootElement projectRootElement, BaselineProject baselineProject)
        {
            foreach (var import in projectRootElement.Imports)
            {
                var fileName = Path.GetFileName(import.Project);
                if (MSBuildFacts.PropsToRemove.Contains(fileName, StringComparer.OrdinalIgnoreCase) ||
                    MSBuildFacts.TargetsToRemove.Contains(fileName, StringComparer.OrdinalIgnoreCase))
                {
                    projectRootElement.RemoveChild(import);
                }
                else if (!MSBuildFacts.ImportsToKeep.Contains(fileName, StringComparer.OrdinalIgnoreCase))
                {
                    Console.WriteLine($"This project has an unrecognized custom import which may need reviewed after conversion: {fileName}");
                }
            }

            if (baselineProject.ProjectStyle is ProjectStyle.WindowsDesktop && baselineProject.TargetTFM is MSBuildFacts.NetCoreApp31)
            {
                projectRootElement.Sdk = DesktopFacts.WinSDKAttribute;
            }
            else if (MSBuildHelpers.IsWeb(projectRootElement))
            {
                projectRootElement.Sdk = WebFacts.WebSDKAttribute;
            }
            else
            {
                projectRootElement.Sdk = MSBuildFacts.DefaultSDKAttribute;
            }

            return(projectRootElement);
        }
        public static IProjectRootElement RemoveUnnecessaryPropertiesNotInSDKByDefault(this IProjectRootElement projectRootElement, ProjectStyle projectStyle)
        {
            foreach (var propGroup in projectRootElement.PropertyGroups)
            {
                foreach (var prop in propGroup.Properties)
                {
                    if (MSBuildFacts.UnnecessaryProperties.Contains(prop.Name, StringComparer.OrdinalIgnoreCase))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsDefineConstantDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsDebugTypeDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsOutputPathDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsPlatformTargetDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsNameDefault(prop, GetProjectName(projectRootElement.FullPath)))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsDocumentationFileDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsUnnecessaryTestProperty(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsEmptyNuGetPackageImportStamp(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (projectStyle == ProjectStyle.MSTest && ProjectPropertyHelpers.IsOutputTypeNode(prop))
                    {
                        // Old MSTest projects specify library, but this is not valid since tests on .NET Core are netcoreapp projects.
                        propGroup.RemoveChild(prop);
                    }
                }

                if (propGroup.Properties.Count == 0)
                {
                    projectRootElement.RemoveChild(propGroup);
                }
            }

            return(projectRootElement);
        public static IProjectRootElement ChangeImportsAndAddSdkAttribute(this IProjectRootElement projectRootElement, BaselineProject baselineProject)
        {
            foreach (var import in projectRootElement.Imports)
            {
                projectRootElement.RemoveChild(import);
            }

            if (baselineProject.ProjectStyle is ProjectStyle.WindowsDesktop && baselineProject.TargetTFM is MSBuildFacts.NetCoreApp31)
            {
                projectRootElement.Sdk = DesktopFacts.WinSDKAttribute;
            }
            else
            {
                projectRootElement.Sdk = MSBuildFacts.DefaultSDKAttribute;
            }

            return(projectRootElement);
        }
        public static IProjectRootElement RemoveUnnecessaryPropertiesNotInSDKByDefault(this IProjectRootElement projectRootElement)
        {
            foreach (var propGroup in projectRootElement.PropertyGroups)
            {
                foreach (var prop in propGroup.Properties)
                {
                    if (MSBuildFacts.UnnecessaryProperties.Contains(prop.Name, StringComparer.OrdinalIgnoreCase))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsDefineConstantDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsDebugTypeDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsOutputPathDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsPlatformTargetDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsNameDefault(prop, GetProjectName(projectRootElement.FullPath)))
                    {
                        propGroup.RemoveChild(prop);
                    }
                    else if (ProjectPropertyHelpers.IsDocumentationFileDefault(prop))
                    {
                        propGroup.RemoveChild(prop);
                    }
                }

                if (propGroup.Properties.Count == 0)
                {
                    projectRootElement.RemoveChild(propGroup);
                }
            }

            return(projectRootElement);
        public static IProjectRootElement ChangeImports(this IProjectRootElement projectRootElement, BaselineProject baselineProject)
        {
            switch (baselineProject.ProjectStyle)
            {
            case ProjectStyle.Default:
            case ProjectStyle.DefaultSubset:
            case ProjectStyle.WindowsDesktop:
            case ProjectStyle.MSTest:
                foreach (var import in projectRootElement.Imports)
                {
                    projectRootElement.RemoveChild(import);
                }

                projectRootElement.Sdk = MSBuildHelpers.IsWinForms(projectRootElement) || MSBuildHelpers.IsWPF(projectRootElement) || MSBuildHelpers.IsDesktop(projectRootElement)
                        ? DesktopFacts.WinSDKAttribute
                        : MSBuildFacts.DefaultSDKAttribute;
                break;
            }

            return(projectRootElement);
        }
        public static IProjectRootElement ChangeImports(this IProjectRootElement projectRootElement, BaselineProject baselineProject)
        {
            var projectStyle = baselineProject.ProjectStyle;

            if (projectStyle == ProjectStyle.Default || projectStyle == ProjectStyle.DefaultSubset || projectStyle == ProjectStyle.WindowsDesktop)
            {
                foreach (var import in projectRootElement.Imports)
                {
                    projectRootElement.RemoveChild(import);
                }

                if (MSBuildHelpers.IsWinForms(projectRootElement) || MSBuildHelpers.IsWPF(projectRootElement) || MSBuildHelpers.IsDesktop(projectRootElement))
                {
                    projectRootElement.Sdk = DesktopFacts.WinSDKAttribute;
                }
                else
                {
                    projectRootElement.Sdk = MSBuildFacts.DefaultSDKAttribute;
                }
            }

            return(projectRootElement);
        }