-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathProjectIdResolver.cs
167 lines (150 loc) · 5.21 KB
/
ProjectIdResolver.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Tools;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.Tools.Internal;
/// <summary>
/// This API supports infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
internal sealed class ProjectIdResolver
{
private const string DefaultConfig = "Debug";
private readonly IReporter _reporter;
private readonly string _targetsFile;
private readonly string _workingDirectory;
public ProjectIdResolver(IReporter reporter, string workingDirectory)
{
_workingDirectory = workingDirectory;
_reporter = reporter;
_targetsFile = FindTargetsFile();
}
public string Resolve(string project, string configuration)
{
var finder = new MsBuildProjectFinder(_workingDirectory);
string projectFile;
try
{
projectFile = finder.FindMsBuildProject(project);
}
catch (Exception ex)
{
_reporter.Error(ex.Message);
return null;
}
_reporter.Verbose(SecretsHelpersResources.FormatMessage_Project_File_Path(projectFile));
configuration = !string.IsNullOrEmpty(configuration)
? configuration
: DefaultConfig;
var outputFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
try
{
var psi = new ProcessStartInfo
{
FileName = DotNetMuxer.MuxerPathOrDefault(),
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
ArgumentList =
{
"msbuild",
projectFile,
"/nologo",
"/t:_ExtractUserSecretsMetadata", // defined in SecretManager.targets
"/p:_UserSecretsMetadataFile=" + outputFile,
"/p:Configuration=" + configuration,
"/p:CustomAfterMicrosoftCommonTargets=" + _targetsFile,
"/p:CustomAfterMicrosoftCommonCrossTargetingTargets=" + _targetsFile,
"-verbosity:detailed",
}
};
#if DEBUG
_reporter.Verbose($"Invoking '{psi.FileName} {psi.Arguments}'");
#endif
using var process = new Process()
{
StartInfo = psi,
};
var outputBuilder = new StringBuilder();
var errorBuilder = new StringBuilder();
process.OutputDataReceived += (_, d) =>
{
if (!string.IsNullOrEmpty(d.Data))
{
outputBuilder.AppendLine(d.Data);
}
};
process.ErrorDataReceived += (_, d) =>
{
if (!string.IsNullOrEmpty(d.Data))
{
errorBuilder.AppendLine(d.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
if (process.ExitCode != 0)
{
_reporter.Verbose(outputBuilder.ToString());
_reporter.Verbose(errorBuilder.ToString());
_reporter.Error($"Exit code: {process.ExitCode}");
_reporter.Error(SecretsHelpersResources.FormatError_ProjectFailedToLoad(projectFile));
return null;
}
if (!File.Exists(outputFile))
{
_reporter.Error(SecretsHelpersResources.FormatError_ProjectMissingId(projectFile));
return null;
}
var id = File.ReadAllText(outputFile)?.Trim();
if (string.IsNullOrEmpty(id))
{
_reporter.Error(SecretsHelpersResources.FormatError_ProjectMissingId(projectFile));
}
return id;
}
finally
{
TryDelete(outputFile);
}
}
private string FindTargetsFile()
{
var assemblyDir = Path.GetDirectoryName(typeof(ProjectIdResolver).Assembly.Location);
var searchPaths = new[]
{
Path.Combine(AppContext.BaseDirectory, "assets"),
Path.Combine(assemblyDir, "assets"),
AppContext.BaseDirectory,
assemblyDir,
};
var targetPath = searchPaths.Select(p => Path.Combine(p, "SecretManager.targets")).FirstOrDefault(File.Exists);
if (targetPath == null)
{
_reporter.Error("Fatal error: could not find SecretManager.targets");
return null;
}
return targetPath;
}
private static void TryDelete(string file)
{
try
{
if (File.Exists(file))
{
File.Delete(file);
}
}
catch
{
// whatever
}
}
}