Use CustomAfterMicrosoftCommonTargets in dotnet-user-secrets
This commit is contained in:
parent
3dd029a8b5
commit
f3bb4080b0
|
|
@ -4,6 +4,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.Extensions.CommandLineUtils;
|
using Microsoft.Extensions.CommandLineUtils;
|
||||||
using Microsoft.Extensions.Tools.Internal;
|
using Microsoft.Extensions.Tools.Internal;
|
||||||
|
|
@ -14,19 +15,20 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal
|
||||||
{
|
{
|
||||||
private const string DefaultConfig = "Debug";
|
private const string DefaultConfig = "Debug";
|
||||||
private readonly IReporter _reporter;
|
private readonly IReporter _reporter;
|
||||||
|
private readonly string _targetsFile;
|
||||||
private readonly string _workingDirectory;
|
private readonly string _workingDirectory;
|
||||||
|
|
||||||
public ProjectIdResolver(IReporter reporter, string workingDirectory)
|
public ProjectIdResolver(IReporter reporter, string workingDirectory)
|
||||||
{
|
{
|
||||||
_workingDirectory = workingDirectory;
|
_workingDirectory = workingDirectory;
|
||||||
_reporter = reporter;
|
_reporter = reporter;
|
||||||
|
_targetsFile = FindTargetsFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Resolve(string project, string configuration)
|
public string Resolve(string project, string configuration)
|
||||||
{
|
{
|
||||||
var finder = new MsBuildProjectFinder(_workingDirectory);
|
var finder = new MsBuildProjectFinder(_workingDirectory);
|
||||||
var projectFile = finder.FindMsBuildProject(project);
|
var projectFile = finder.FindMsBuildProject(project);
|
||||||
EnsureProjectExtensionTargetsExist(projectFile);
|
|
||||||
|
|
||||||
_reporter.Verbose(Resources.FormatMessage_Project_File_Path(projectFile));
|
_reporter.Verbose(Resources.FormatMessage_Project_File_Path(projectFile));
|
||||||
|
|
||||||
|
|
@ -42,9 +44,11 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal
|
||||||
"msbuild",
|
"msbuild",
|
||||||
projectFile,
|
projectFile,
|
||||||
"/nologo",
|
"/nologo",
|
||||||
"/t:_ExtractUserSecretsMetadata", // defined in ProjectIdResolverTargets.xml
|
"/t:_ExtractUserSecretsMetadata", // defined in SecretManager.targets
|
||||||
$"/p:_UserSecretsMetadataFile={outputFile}",
|
"/p:_UserSecretsMetadataFile=" + outputFile,
|
||||||
$"/p:Configuration={configuration}"
|
"/p:Configuration=" + configuration,
|
||||||
|
"/p:CustomAfterMicrosoftCommonTargets=" + _targetsFile,
|
||||||
|
"/p:CustomAfterMicrosoftCommonCrossTargetingTargets=" + _targetsFile,
|
||||||
};
|
};
|
||||||
var psi = new ProcessStartInfo
|
var psi = new ProcessStartInfo
|
||||||
{
|
{
|
||||||
|
|
@ -82,25 +86,25 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void EnsureProjectExtensionTargetsExist(string projectFile)
|
private string FindTargetsFile()
|
||||||
{
|
{
|
||||||
// relies on MSBuildProjectExtensionsPath and Microsoft.Common.targets to import this file
|
var assemblyDir = Path.GetDirectoryName(typeof(ProjectIdResolver).Assembly.Location);
|
||||||
// into the target project
|
var searchPaths = new[]
|
||||||
var projectExtensionsPath = Path.Combine(
|
|
||||||
Path.GetDirectoryName(projectFile),
|
|
||||||
"obj",
|
|
||||||
$"{Path.GetFileName(projectFile)}.usersecrets.targets");
|
|
||||||
|
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(projectExtensionsPath));
|
|
||||||
|
|
||||||
// should overwrite the file always. Hypothetically, another version of the user-secrets tool
|
|
||||||
// could have already put a file here. We want to ensure the target file matches the currently
|
|
||||||
// running tool
|
|
||||||
using (var resource = GetType().GetTypeInfo().Assembly.GetManifestResourceStream("ProjectIdResolverTargets.xml"))
|
|
||||||
using (var stream = new FileStream(projectExtensionsPath, FileMode.Create))
|
|
||||||
{
|
{
|
||||||
resource.CopyTo(stream);
|
AppContext.BaseDirectory,
|
||||||
|
assemblyDir,
|
||||||
|
Path.Combine(assemblyDir, "../../toolassets"), // from nuget cache
|
||||||
|
Path.Combine(assemblyDir, "toolassets"), // from local build
|
||||||
|
Path.Combine(AppContext.BaseDirectory, "../../toolassets"), // relative to packaged deps.json
|
||||||
|
};
|
||||||
|
|
||||||
|
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)
|
private static void TryDelete(string file)
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,8 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="..\..\shared\**\*.cs" />
|
<Compile Include="..\..\shared\**\*.cs" />
|
||||||
<EmbeddedResource Include="resources\ProjectIdResolverTargets.xml" LogicalName="ProjectIdResolverTargets.xml" />
|
<Content Include="toolassets\SecretManager.targets" CopyToOutputDirectory="PreserveNewest" PackagePath="%(Identity)"/>
|
||||||
<None Include="prefercliruntime" Pack="true" PackagePath="\prefercliruntime" />
|
<Content Include="prefercliruntime" PackagePath="\prefercliruntime" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
<Project>
|
|
||||||
<Target Name="_ExtractUserSecretsMetadata">
|
|
||||||
<WriteLinesToFile File="$(_UserSecretsMetadataFile)" Lines="$(UserSecretsId)" />
|
|
||||||
</Target>
|
|
||||||
</Project>
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
<Project>
|
||||||
|
<Target Name="_ExtractUserSecretsMetadata">
|
||||||
|
<WriteLinesToFile File="$(_UserSecretsMetadataFile)" Lines="$(UserSecretsId)" />
|
||||||
|
</Target>
|
||||||
|
</Project>
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="..\Shared\**\*.cs" />
|
<Compile Include="..\Shared\**\*.cs" />
|
||||||
|
<Content Include="..\..\src\Microsoft.Extensions.SecretManager.Tools\toolassets\SecretManager.targets" Link="toolassets\SecretManager.targets" CopyToOutputDirectory="PreserveNewest" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ using System.IO;
|
||||||
using Microsoft.Extensions.SecretManager.Tools.Internal;
|
using Microsoft.Extensions.SecretManager.Tools.Internal;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.SecretsManager.Tools.Tests
|
namespace Microsoft.Extensions.SecretManager.Tools.Tests
|
||||||
{
|
{
|
||||||
public class MsBuildProjectFinderTest
|
public class MsBuildProjectFinderTest
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.SecretsManager.Tools.Tests
|
namespace Microsoft.Extensions.SecretManager.Tools.Tests
|
||||||
{
|
{
|
||||||
internal class TemporaryFileProvider : IDisposable
|
internal class TemporaryFileProvider : IDisposable
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue