forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateDepsFile.cs
109 lines (83 loc) · 3.62 KB
/
GenerateDepsFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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.
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Extensions.DependencyModel;
using Newtonsoft.Json;
using NuGet.Frameworks;
using NuGet.ProjectModel;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Generates the $(project).deps.json file.
/// </summary>
public class GenerateDepsFile : TaskBase
{
[Required]
public string ProjectPath { get; set; }
[Required]
public string AssetsFilePath { get; set; }
[Required]
public string DepsFilePath { get; set; }
[Required]
public string TargetFramework { get; set; }
public string RuntimeIdentifier { get; set; }
public string PlatformLibraryName { get; set; }
[Required]
public string AssemblyName { get; set; }
[Required]
public string AssemblyExtension { get; set; }
[Required]
public string AssemblyVersion { get; set; }
[Required]
public ITaskItem[] AssemblySatelliteAssemblies { get; set; }
[Required]
public ITaskItem[] ReferencePaths { get; set; }
[Required]
public ITaskItem[] ReferenceSatellitePaths { get; set; }
public ITaskItem CompilerOptions { get; set; }
public ITaskItem[] PrivateAssetsPackageReferences { get; set; }
List<ITaskItem> _filesWritten = new List<ITaskItem>();
[Output]
public ITaskItem[] FilesWritten
{
get { return _filesWritten.ToArray(); }
}
protected override void ExecuteCore()
{
LockFile lockFile = new LockFileCache(BuildEngine4).GetLockFile(AssetsFilePath);
CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions);
SingleProjectInfo mainProject = SingleProjectInfo.Create(
ProjectPath,
AssemblyName,
AssemblyExtension,
AssemblyVersion,
AssemblySatelliteAssemblies);
IEnumerable<ReferenceInfo> frameworkReferences =
ReferenceInfo.CreateFrameworkReferenceInfos(ReferencePaths);
Dictionary<string, SingleProjectInfo> referenceProjects = SingleProjectInfo.CreateProjectReferenceInfos(
ReferencePaths,
ReferenceSatellitePaths);
IEnumerable<string> privateAssets = PackageReferenceConverter.GetPackageIds(PrivateAssetsPackageReferences);
ProjectContext projectContext = lockFile.CreateProjectContext(
NuGetUtils.ParseFrameworkName(TargetFramework),
RuntimeIdentifier,
PlatformLibraryName);
DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext)
.WithFrameworkReferences(frameworkReferences)
.WithReferenceProjectInfos(referenceProjects)
.WithPrivateAssets(privateAssets)
.WithCompilationOptions(compilationOptions)
.WithReferenceAssembliesPath(FrameworkReferenceResolver.GetDefaultReferenceAssembliesPath())
.Build();
var writer = new DependencyContextWriter();
using (var fileStream = File.Create(DepsFilePath))
{
writer.Write(dependencyContext, fileStream);
}
_filesWritten.Add(new TaskItem(DepsFilePath));
}
}
}