ProjectSimplifier/ProjectLoader.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 BaselineProject CreateSdkBaselineProject(string projectFilePath, 
                                                         IProject project, 
                                                         ImmutableDictionary<string, string> globalProperties, 
                                                         ImmutableDictionary<string, ImmutableDictionary<string, string>> configurations, 
                                                         ImmutableDictionary<string, string> targetProjectProperties)
        {
            var projectStyle = GetProjectStyle(ProjectRootElement);
            var rootElement = Microsoft.Build.Construction.ProjectRootElement.Open(projectFilePath);
            rootElement.RemoveAllChildren();
            switch (projectStyle)
            {
                case ProjectStyle.Default:
                    rootElement.Sdk = "Microsoft.NET.Sdk";
                    break;
                case ProjectStyle.DefaultWithCustomTargets:
                    var imports = ProjectRootElement.Imports;
                    void CopyImport(ProjectImportElement import)
                    {
                        var newImport = rootElement.AddImport(import.Project);
                        newImport.Condition = import.Condition;
                    }
                    CopyImport(imports.First());
                    CopyImport(imports.Last());
                    break;
                default:
                    throw new NotSupportedException("This project has custom imports in a manner that's not supported.");
            }
            var propGroup = rootElement.AddPropertyGroup();
            propGroup.AddProperty("TargetFramework", project.GetTargetFramework());
            propGroup.AddProperty("OutputType", project.GetPropertyValue("OutputType") ?? throw new InvalidOperationException("OutputType is not set!"));
            var newGlobalProperties = globalProperties.AddRange(targetProjectProperties);
            var pc = new ProjectCollection(newGlobalProperties);
            var newProject = new UnconfiguredProject(configurations);
            newProject.LoadProjects(pc, newGlobalProperties, rootElement);
            var propertiesInTheBaseline = ImmutableArray.Create("OutputType");
            if (project.GetProperty("TargetFramework") != null)
            {
                propertiesInTheBaseline = propertiesInTheBaseline.Add("TargetFramework");
            }
            return new BaselineProject(newProject, propertiesInTheBaseline, targetProjectProperties, projectStyle);
        }
private BaselineProject CreateSdkBaselineProject ( string projectFilePath , IProject project , ImmutableDictionary < string , string > globalProperties , ImmutableDictionary < string , ImmutableDictionary < string , string > > configurations , ImmutableDictionary < string , string > targetProjectProperties ) { var projectStyle = GetProjectStyle ( ProjectRootElement ) ; var rootElement = Microsoft . Build . Construction . ProjectRootElement . Open ( projectFilePath ) ; rootElement . RemoveAllChildren ( ) ; switch ( projectStyle ) { case ProjectStyle . Default : rootElement . Sdk = " " ; break ; case ProjectStyle . DefaultWithCustomTargets : var imports = ProjectRootElement . Imports ; void CopyImport ( ProjectImportElement import ) { var newImport = rootElement . AddImport ( import . Project ) ; newImport . Condition = import . Condition ; } CopyImport ( imports . First ( ) ) ; CopyImport ( imports . Last ( ) ) ; break ; default : throw new NotSupportedException ( " " ) ; } var propGroup = rootElement . AddPropertyGroup ( ) ; propGroup . AddProperty ( " " , project . GetTargetFramework ( ) ) ; propGroup . AddProperty ( " " , project . GetPropertyValue ( " " ) ?? throw new InvalidOperationException ( " " ) ) ; var newGlobalProperties = globalProperties . AddRange ( targetProjectProperties ) ; var pc = new ProjectCollection ( newGlobalProperties ) ; var newProject = new UnconfiguredProject ( configurations ) ; newProject . LoadProjects ( pc , newGlobalProperties , rootElement ) ; var propertiesInTheBaseline = ImmutableArray . Create ( " " ) ; if ( project . GetProperty ( " " ) != null ) { propertiesInTheBaseline = propertiesInTheBaseline . Add ( " " ) ; } return new BaselineProject ( newProject , propertiesInTheBaseline , targetProjectProperties , projectStyle ) ; }
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 .
// 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
globalProperties
ImmutableDictionary<string, string>
configurations
ImmutableDictionary<string, ImmutableDictionary<string, string>>
targetProjectProperties
ImmutableDictionary<string, string>