// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Versioning; using Microsoft.Extensions.ProjectModel.Compilation; using Microsoft.Extensions.ProjectModel.Graph; using Microsoft.Extensions.ProjectModel.Resolution; using NuGet.Frameworks; namespace Microsoft.Extensions.ProjectModel { // NOTE(anurse): Copied from ApplicationHostContext in DNX. This name seemed more appropriate for this :) public class ProjectContext { public Project Project { get; } public GlobalSettings GlobalSettings { get; } // TODO: Remove this, it's kinda hacky public ProjectDescription RootProject { get; } public NuGetFramework TargetFramework { get; } public string RuntimeIdentifier { get; } public string RootDirectory => GlobalSettings.DirectoryPath; public string ProjectDirectory => Project.ProjectDirectory; public string PackagesDirectory { get; } public FrameworkReferenceResolver FrameworkResolver { get; } public LibraryManager LibraryManager { get; } internal ProjectContext( Project project, GlobalSettings globalSettings, ProjectDescription rootProject, NuGetFramework targetFramework, string runtimeIdentifier, string packagesDirectory, FrameworkReferenceResolver frameworkResolver, LibraryManager libraryManager) { Project = project; GlobalSettings = globalSettings; RootProject = rootProject; TargetFramework = targetFramework; RuntimeIdentifier = runtimeIdentifier; PackagesDirectory = packagesDirectory; FrameworkResolver = frameworkResolver; LibraryManager = libraryManager; } public LibraryExporter CreateExporter(string configuration) { return new LibraryExporter(RootProject, LibraryManager, configuration); } /// /// Creates a project context for the project located at , /// specifically in the context of the framework specified in /// public static ProjectContext Create(string projectPath, NuGetFramework framework) { return Create(projectPath, framework, Enumerable.Empty()); } /// /// Creates a project context for the project located at , /// specifically in the context of the framework specified in /// and the candidate runtime identifiers specified in /// public static ProjectContext Create(string projectPath, NuGetFramework framework, IEnumerable runtimeIdentifiers) { if(projectPath.EndsWith(Project.FileName)) { projectPath = Path.GetDirectoryName(projectPath); } return new ProjectContextBuilder() { ProjectDirectory = projectPath, TargetFramework = framework, RuntimeIdentifiers = runtimeIdentifiers }.Build(); } /// /// Creates a project context for each framework located in the project at /// public static IEnumerable CreateContextForEachFramework(string projectPath) { if(!projectPath.EndsWith(Project.FileName)) { projectPath = Path.Combine(projectPath, Project.FileName); } var project = ProjectReader.GetProject(projectPath); foreach(var framework in project.GetTargetFrameworks()) { yield return new ProjectContextBuilder() { Project = project, TargetFramework = framework.FrameworkName }.Build(); } } } }