src/MSBuild.Abstractions/MSBuildConversionWorkspace.cs
/// <summary>
/// Clear out the project's construction model and add a simple SDK-based project to get a baseline.
/// We need to use the same name as the original csproj and same path so that all the default that derive
/// from name\path get the right values (there are a lot of them).
/// </summary>
Clear out the project's construction model and add a simple SDK-based project to get a baseline.
We need to use the same name as the original csproj and same path so that all the default that derive
from name\path get the right values (there are a lot of them).
Clear out the project ' s construction model and add a simple SDK - based project to get a baseline . We need to use the same name as the original csproj and same path so that all the default that derive from name \ path get the right values ( there are a lot of them ) .
private bool TryCreateSdkBaselineProject(string projectFilePath, IProject project, IProjectRootElement root, ImmutableDictionary<string, ImmutableDictionary<string, string>> configurations, string tfm, bool keepCurrentTFMs, [NotNullWhen(true)] out BaselineProject? baselineProject)
        {
            var projectStyle = GetProjectStyle(root);
            var outputType = GetProjectOutputType(root);
            var rootElement = ProjectRootElement.Open(projectFilePath);
            rootElement.RemoveAllChildren();
            switch (projectStyle)
            {
                case ProjectStyle.Default:
                case ProjectStyle.DefaultSubset:
                case ProjectStyle.MSTest:
                    rootElement.Sdk = MSBuildFacts.DefaultSDKAttribute;
                    break;
                case ProjectStyle.WindowsDesktop:
                    rootElement.Sdk =
                        tfm.ContainsIgnoreCase(MSBuildFacts.Net5)
                            ? MSBuildFacts.DefaultSDKAttribute
                            : DesktopFacts.WinSDKAttribute; 
                    break;
                default:
                    baselineProject = null;
                    return false;
            }
            var propGroup = rootElement.AddPropertyGroup();
            propGroup.AddProperty(MSBuildFacts.TargetFrameworkNodeName, project.GetTargetFramework());
            propGroup.AddProperty(MSBuildFacts.OutputTypeNodeName,
                project.GetPropertyValue(MSBuildFacts.OutputTypeNodeName) ?? throw new InvalidOperationException($"OutputType is not set! '{projectFilePath}'"));
            if (projectStyle == ProjectStyle.WindowsDesktop)
            {
                if (MSBuildHelpers.IsWinForms(root))
                {
                    MSBuildHelpers.AddUseWinForms(propGroup);
                }
                if (MSBuildHelpers.IsWPF(root))
                {
                    MSBuildHelpers.AddUseWPF(propGroup);
                }
                if (MSBuildHelpers.IsDesktop(root) && !MSBuildHelpers.HasWPFOrWinForms(propGroup))
                {
                    MSBuildHelpers.AddUseWinForms(propGroup);
                }
            }
            using var pc = new ProjectCollection();
            var newProject = new UnconfiguredProject(configurations);
            newProject.LoadProjects(pc, rootElement);
            var propertiesInTheBaseline = ImmutableArray.Create(MSBuildFacts.OutputTypeNodeName);
            if (project.GetProperty(MSBuildFacts.TargetFrameworkNodeName) is { })
            {
                propertiesInTheBaseline = propertiesInTheBaseline.Add(MSBuildFacts.TargetFrameworkNodeName);
            }
            if (project.GetProperty(DesktopFacts.UseWinFormsPropertyName) is { })
            {
                propertiesInTheBaseline = propertiesInTheBaseline.Add(DesktopFacts.UseWinFormsPropertyName);
            }
            if (project.GetProperty(DesktopFacts.UseWPFPropertyName) is { })
            {
                propertiesInTheBaseline = propertiesInTheBaseline.Add(DesktopFacts.UseWPFPropertyName);
            }
            tfm =
                projectStyle == ProjectStyle.WindowsDesktop && tfm.ContainsIgnoreCase(MSBuildFacts.Net5)
                    ? MSBuildFacts.Net5Windows
                    : tfm;
            baselineProject = new BaselineProject(newProject, propertiesInTheBaseline, projectStyle, outputType, tfm, keepCurrentTFMs);
            return true;
        }
private bool TryCreateSdkBaselineProject ( string projectFilePath , IProject project , IProjectRootElement root , ImmutableDictionary < string , ImmutableDictionary < string , string > > configurations , string tfm , bool keepCurrentTFMs , [ NotNullWhen ( true ) ] out BaselineProject ? baselineProject ) { var projectStyle = GetProjectStyle ( root ) ; var outputType = GetProjectOutputType ( root ) ; var rootElement = ProjectRootElement . Open ( projectFilePath ) ; rootElement . RemoveAllChildren ( ) ; switch ( projectStyle ) { case ProjectStyle . Default : case ProjectStyle . DefaultSubset : case ProjectStyle . MSTest : rootElement . Sdk = MSBuildFacts . DefaultSDKAttribute ; break ; case ProjectStyle . WindowsDesktop : rootElement . Sdk = tfm . ContainsIgnoreCase ( MSBuildFacts . Net5 ) ? MSBuildFacts . DefaultSDKAttribute : DesktopFacts . WinSDKAttribute ; break ; default : baselineProject = null ; return false ; } var propGroup = rootElement . AddPropertyGroup ( ) ; propGroup . AddProperty ( MSBuildFacts . TargetFrameworkNodeName , project . GetTargetFramework ( ) ) ; propGroup . AddProperty ( MSBuildFacts . OutputTypeNodeName , project . GetPropertyValue ( MSBuildFacts . OutputTypeNodeName ) ?? throw new InvalidOperationException ( $" OutputType is not set! ' { projectFilePath } ' " ) ) ; if ( projectStyle == ProjectStyle . WindowsDesktop ) { if ( MSBuildHelpers . IsWinForms ( root ) ) { MSBuildHelpers . AddUseWinForms ( propGroup ) ; } if ( MSBuildHelpers . IsWPF ( root ) ) { MSBuildHelpers . AddUseWPF ( propGroup ) ; } if ( MSBuildHelpers . IsDesktop ( root ) && ! MSBuildHelpers . HasWPFOrWinForms ( propGroup ) ) { MSBuildHelpers . AddUseWinForms ( propGroup ) ; } } using var pc = new ProjectCollection ( ) ; var newProject = new UnconfiguredProject ( configurations ) ; newProject . LoadProjects ( pc , rootElement ) ; var propertiesInTheBaseline = ImmutableArray . Create ( MSBuildFacts . OutputTypeNodeName ) ; if ( project . GetProperty ( MSBuildFacts . TargetFrameworkNodeName ) is { } ) { propertiesInTheBaseline = propertiesInTheBaseline . Add ( MSBuildFacts . TargetFrameworkNodeName ) ; } if ( project . GetProperty ( DesktopFacts . UseWinFormsPropertyName ) is { } ) { propertiesInTheBaseline = propertiesInTheBaseline . Add ( DesktopFacts . UseWinFormsPropertyName ) ; } if ( project . GetProperty ( DesktopFacts . UseWPFPropertyName ) is { } ) { propertiesInTheBaseline = propertiesInTheBaseline . Add ( DesktopFacts . UseWPFPropertyName ) ; } tfm = projectStyle == ProjectStyle . WindowsDesktop && tfm . ContainsIgnoreCase ( MSBuildFacts . Net5 ) ? MSBuildFacts . Net5Windows : tfm ; baselineProject = new BaselineProject ( newProject , propertiesInTheBaseline , projectStyle , outputType , tfm , keepCurrentTFMs ) ; return true ; }
Clear out the project's construction model and add a simple SDK-based project to get a baseline.
Clear out the project ' s construction model and add a simple SDK - based project to get a baseline .
// pre-.NET 5 apps need a special SDK attribute. // User is referencing WindowsBase only // Create a new collection because a project with this name has already been loaded into the global collection. // If the original project had the TargetFramework property don't touch it during conversion.
projectFilePath
string
project
IProject
root
IProjectRootElement
configurations
ImmutableDictionary<string, ImmutableDictionary<string, string>>
tfm
string
keepCurrentTFMs
bool
out
[NotNullWhen(true)]