2015-11-16 11:21:57 -08:00
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
2015-10-13 14:31:29 -07:00
|
|
|
|
2016-02-03 10:57:25 -08:00
|
|
|
using System;
|
2015-10-13 14:31:29 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2015-11-27 16:19:54 -08:00
|
|
|
using Microsoft.DotNet.ProjectModel.Compilation;
|
2016-01-14 11:52:03 -08:00
|
|
|
using Microsoft.DotNet.ProjectModel.Graph;
|
2015-11-27 16:19:54 -08:00
|
|
|
using Microsoft.DotNet.ProjectModel.Resolution;
|
2015-10-13 14:31:29 -07:00
|
|
|
using NuGet.Frameworks;
|
|
|
|
|
2015-11-27 16:19:54 -08:00
|
|
|
namespace Microsoft.DotNet.ProjectModel
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
public class ProjectContext
|
|
|
|
{
|
|
|
|
public GlobalSettings GlobalSettings { get; }
|
|
|
|
|
|
|
|
public ProjectDescription RootProject { get; }
|
|
|
|
|
|
|
|
public NuGetFramework TargetFramework { get; }
|
|
|
|
|
|
|
|
public string RuntimeIdentifier { get; }
|
|
|
|
|
2015-10-15 12:56:07 -07:00
|
|
|
public Project ProjectFile => RootProject.Project;
|
|
|
|
|
2016-01-14 11:52:03 -08:00
|
|
|
public LockFile LockFile { get; }
|
|
|
|
|
2016-02-03 10:57:25 -08:00
|
|
|
public string RootDirectory => GlobalSettings?.DirectoryPath;
|
2015-10-13 14:31:29 -07:00
|
|
|
|
2015-10-15 12:56:07 -07:00
|
|
|
public string ProjectDirectory => ProjectFile.ProjectDirectory;
|
2015-10-13 14:31:29 -07:00
|
|
|
|
|
|
|
public string PackagesDirectory { get; }
|
|
|
|
|
|
|
|
public LibraryManager LibraryManager { get; }
|
|
|
|
|
|
|
|
internal ProjectContext(
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
ProjectDescription rootProject,
|
|
|
|
NuGetFramework targetFramework,
|
|
|
|
string runtimeIdentifier,
|
|
|
|
string packagesDirectory,
|
2016-01-14 11:52:03 -08:00
|
|
|
LibraryManager libraryManager,
|
|
|
|
LockFile lockfile)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
GlobalSettings = globalSettings;
|
|
|
|
RootProject = rootProject;
|
|
|
|
TargetFramework = targetFramework;
|
|
|
|
RuntimeIdentifier = runtimeIdentifier;
|
|
|
|
PackagesDirectory = packagesDirectory;
|
|
|
|
LibraryManager = libraryManager;
|
2016-01-14 11:52:03 -08:00
|
|
|
LockFile = lockfile;
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:57:25 -08:00
|
|
|
public LibraryExporter CreateExporter(string configuration, string buildBasePath = null)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
2016-02-03 10:57:25 -08:00
|
|
|
return new LibraryExporter(RootProject, LibraryManager, configuration, RuntimeIdentifier, buildBasePath, RootDirectory);
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a project context for the project located at <paramref name="projectPath"/>,
|
|
|
|
/// specifically in the context of the framework specified in <paramref name="framework"/>
|
|
|
|
/// </summary>
|
|
|
|
public static ProjectContext Create(string projectPath, NuGetFramework framework)
|
|
|
|
{
|
|
|
|
return Create(projectPath, framework, Enumerable.Empty<string>());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a project context for the project located at <paramref name="projectPath"/>,
|
|
|
|
/// specifically in the context of the framework specified in <paramref name="framework"/>
|
|
|
|
/// and the candidate runtime identifiers specified in <param name="runtimeIdentifiers"/>
|
|
|
|
/// </summary>
|
|
|
|
public static ProjectContext Create(string projectPath, NuGetFramework framework, IEnumerable<string> runtimeIdentifiers)
|
|
|
|
{
|
2015-12-02 23:32:27 -08:00
|
|
|
if (projectPath.EndsWith(Project.FileName))
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
projectPath = Path.GetDirectoryName(projectPath);
|
|
|
|
}
|
|
|
|
return new ProjectContextBuilder()
|
2015-10-29 01:35:23 -07:00
|
|
|
.WithProjectDirectory(projectPath)
|
|
|
|
.WithTargetFramework(framework)
|
|
|
|
.WithRuntimeIdentifiers(runtimeIdentifiers)
|
|
|
|
.Build();
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
2016-02-16 08:42:00 -08:00
|
|
|
|
2016-02-18 01:09:23 -08:00
|
|
|
public static ProjectContextBuilder CreateBuilder(string projectPath, NuGetFramework framework)
|
|
|
|
{
|
|
|
|
if (projectPath.EndsWith(Project.FileName))
|
|
|
|
{
|
|
|
|
projectPath = Path.GetDirectoryName(projectPath);
|
|
|
|
}
|
|
|
|
return new ProjectContextBuilder()
|
|
|
|
.WithProjectDirectory(projectPath)
|
|
|
|
.WithTargetFramework(framework);
|
|
|
|
}
|
|
|
|
|
2015-10-13 14:31:29 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a project context for each framework located in the project at <paramref name="projectPath"/>
|
|
|
|
/// </summary>
|
2016-02-03 10:57:25 -08:00
|
|
|
public static IEnumerable<ProjectContext> CreateContextForEachFramework(string projectPath, ProjectReaderSettings settings = null, IEnumerable<string> runtimeIdentifiers = null)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
2015-12-02 23:32:27 -08:00
|
|
|
if (!projectPath.EndsWith(Project.FileName))
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
projectPath = Path.Combine(projectPath, Project.FileName);
|
|
|
|
}
|
2015-12-04 18:03:57 -08:00
|
|
|
var project = ProjectReader.GetProject(projectPath, settings);
|
2015-10-13 14:31:29 -07:00
|
|
|
|
2015-12-02 23:32:27 -08:00
|
|
|
foreach (var framework in project.GetTargetFrameworks())
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
yield return new ProjectContextBuilder()
|
2015-10-29 01:35:23 -07:00
|
|
|
.WithProject(project)
|
|
|
|
.WithTargetFramework(framework.FrameworkName)
|
2015-12-09 00:47:57 -08:00
|
|
|
.WithReaderSettings(settings)
|
2016-02-03 10:57:25 -08:00
|
|
|
.WithRuntimeIdentifiers(runtimeIdentifiers ?? Enumerable.Empty<string>())
|
2015-10-29 01:35:23 -07:00
|
|
|
.Build();
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
}
|
2015-12-02 23:32:27 -08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a project context for each target located in the project at <paramref name="projectPath"/>
|
|
|
|
/// </summary>
|
|
|
|
public static IEnumerable<ProjectContext> CreateContextForEachTarget(string projectPath)
|
|
|
|
{
|
|
|
|
if (!projectPath.EndsWith(Project.FileName))
|
|
|
|
{
|
|
|
|
projectPath = Path.Combine(projectPath, Project.FileName);
|
|
|
|
}
|
|
|
|
var project = ProjectReader.GetProject(projectPath);
|
|
|
|
|
|
|
|
return new ProjectContextBuilder()
|
|
|
|
.WithProject(project)
|
|
|
|
.BuildAllTargets();
|
|
|
|
}
|
|
|
|
|
2016-02-16 08:42:00 -08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a project context based on existing context but using runtime target
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context"></param>
|
|
|
|
/// <param name="runtimeIdentifiers"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
|
|
public ProjectContext CreateRuntimeContext(IEnumerable<string> runtimeIdentifiers)
|
|
|
|
{
|
2016-02-19 14:43:30 -08:00
|
|
|
var context = Create(ProjectFile.ProjectFilePath, TargetFramework, runtimeIdentifiers);
|
|
|
|
if (context.RuntimeIdentifier == null)
|
|
|
|
{
|
2016-02-24 10:49:00 -08:00
|
|
|
var rids = string.Join(", ", runtimeIdentifiers);
|
|
|
|
throw new InvalidOperationException($"Can not find runtime target for framework '{TargetFramework}' and RID's '{rids}'. " +
|
|
|
|
"Possible causes:" + Environment.NewLine +
|
|
|
|
"1. Project is not restored or restore failed - run `dotnet restore`" + Environment.NewLine +
|
|
|
|
"2. Project is not targeting `runable` framework (`netstandardapp*` or `net*`)"
|
|
|
|
);
|
2016-02-19 14:43:30 -08:00
|
|
|
}
|
|
|
|
return context;
|
2016-02-16 08:42:00 -08:00
|
|
|
}
|
|
|
|
|
2016-02-03 10:57:25 -08:00
|
|
|
public OutputPaths GetOutputPaths(string configuration, string buidBasePath = null, string outputPath = null)
|
2015-11-30 16:24:03 -08:00
|
|
|
{
|
2016-02-03 10:57:25 -08:00
|
|
|
return OutputPathsCalculator.GetOutputPaths(ProjectFile,
|
|
|
|
TargetFramework,
|
|
|
|
RuntimeIdentifier,
|
|
|
|
configuration,
|
|
|
|
RootDirectory,
|
|
|
|
buidBasePath,
|
|
|
|
outputPath);
|
2016-01-20 15:41:46 -08:00
|
|
|
}
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
2015-12-02 23:32:27 -08:00
|
|
|
}
|