Exemple #1
0
        public Project Load(string targetFramework, BuildEnvironment buildEnvironment)
        {
            if (buildEnvironment == null)
            {
                throw new ArgumentNullException(nameof(buildEnvironment));
            }

            // Need a fresh BuildEnvironmentHelper for every load/build
            ResetBuildEnvironmentHelper(buildEnvironment);

            // Some project types can't be built from .NET Core
            if (BuildEnvironment.IsRunningOnCore)
            {
                // Portable projects
                if (ProjectFile.RequiresNetFramework)
                {
                    throw new Exception("This project requires the .NET Framework and can't be built from a .NET Core host");
                }

                // Legacy framework projects with PackageReference
                if (!ProjectFile.UsesSdk && ProjectFile.ContainsPackageReferences)
                {
                    throw new Exception("Can't build legacy projects that contain PackageReference from a .NET Core host");
                }
            }

            // Create a project collection for each project since the toolset might change depending on the type of project
            Dictionary <string, string> effectiveGlobalProperties = GetEffectiveGlobalProperties(buildEnvironment);

            if (!string.IsNullOrEmpty(targetFramework))
            {
                // Setting the TargetFramework MSBuild property tells MSBuild which target framework to use for the outer build
                effectiveGlobalProperties[MsBuildProperties.TargetFramework] = targetFramework;
            }
            ProjectCollection projectCollection = CreateProjectCollection(buildEnvironment, effectiveGlobalProperties);

            // Load the project
            using (new TemporaryEnvironment(GetEffectiveEnvironmentVariables(buildEnvironment)))
            {
                using (XmlReader projectReader = ProjectFile.CreateReader())
                {
                    ProjectRootElement root = ProjectRootElement.Create(projectReader, projectCollection);

                    // When constructing a project from an XmlReader, MSBuild cannot determine the project file path.  Setting the
                    // path explicitly is necessary so that the reserved properties like $(MSBuildProjectDirectory) will work.
                    root.FullPath = ProjectFile.Path;

                    using (new AssemblyResolver(buildEnvironment))
                    {
                        return(new Project(
                                   root,
                                   effectiveGlobalProperties,
                                   ToolLocationHelper.CurrentToolsVersion,
                                   projectCollection,
                                   IgnoreFaultyImports
                                ? ProjectLoadSettings.IgnoreEmptyImports | ProjectLoadSettings.IgnoreInvalidImports | ProjectLoadSettings.IgnoreMissingImports
                                : ProjectLoadSettings.Default));
                    }
                }
            }
        }