Tell dotnet restore to install nuget.props and nuget.targets files using an environment variable, so the SDK .targets are installed into the user's project.

This commit is contained in:
Eric Erhardt 2016-08-09 18:05:28 -05:00
parent 736652fe71
commit 94223a7009
3 changed files with 42 additions and 30 deletions

View file

@ -1,38 +1,34 @@
// 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 Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
namespace Microsoft.DotNet.Cli
{
/// <summary>
/// A class which encapsulates logic needed to forward arguments from the current process to another process
/// invoked with the dotnet.exe host.
/// </summary>
/// <summary>
/// A class which encapsulates logic needed to forward arguments from the current process to another process
/// invoked with the dotnet.exe host.
/// </summary>
public class ForwardingApp
{
private const string s_hostExe = "dotnet";
private readonly string _forwardApplicationPath;
private readonly string[] _argsToForward;
private readonly IEnumerable<string> _argsToForward;
private readonly string _depsFile;
private readonly string _runtimeConfig;
private readonly string _additionalProbingPath;
private readonly Dictionary<string, string> _environmentVariables;
private Dictionary<string, string> _environmentVariables;
private readonly string[] _allArgs;
public ForwardingApp(
string forwardApplicationPath,
string[] argsToForward,
string depsFile = null,
string forwardApplicationPath,
IEnumerable<string> argsToForward,
string depsFile = null,
string runtimeConfig = null,
string additionalProbingPath = null,
Dictionary<string, string> environmentVariables = null)
@ -43,7 +39,7 @@ namespace Microsoft.DotNet.Cli
_runtimeConfig = runtimeConfig;
_additionalProbingPath = additionalProbingPath;
_environmentVariables = environmentVariables;
var allArgs = new List<string>();
allArgs.Add("exec");
@ -99,6 +95,15 @@ namespace Microsoft.DotNet.Cli
return process.ExitCode;
}
public ForwardingApp WithEnvironmentVariable(string name, string value)
{
_environmentVariables = _environmentVariables ?? new Dictionary<string, string>();
_environmentVariables.Add(name, value);
return this;
}
private string GetHostExeName()
{
return $"{s_hostExe}{FileNameSuffixes.CurrentPlatform.Exe}";

View file

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Tools.Restore
{
@ -19,11 +16,18 @@ namespace Microsoft.DotNet.Tools.Restore
}
prefixArgs.Add("restore");
var result = new NuGetForwardingApp(Enumerable.Concat(
prefixArgs,
args).ToArray()).Execute();
var nugetApp = new NuGetForwardingApp(Enumerable.Concat(prefixArgs, args));
return result;
// setting NUGET_XPROJ_WRITE_TARGETS will tell nuget restore to install .props and .targets files
// coming from NuGet packages
const string nugetXProjWriteTargets = "NUGET_XPROJ_WRITE_TARGETS";
bool setXProjWriteTargets = Environment.GetEnvironmentVariable(nugetXProjWriteTargets) == null;
if (setXProjWriteTargets)
{
nugetApp.WithEnvironmentVariable(nugetXProjWriteTargets, "true");
}
return nugetApp.Execute();
}
}
}

View file

@ -1,14 +1,10 @@
// 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 Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.InternalAbstractions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.Cli;
namespace Microsoft.DotNet.Tools.Restore
{
@ -17,7 +13,7 @@ namespace Microsoft.DotNet.Tools.Restore
private const string s_nugetExeName = "NuGet.CommandLine.XPlat.dll";
private readonly ForwardingApp _forwardingApp;
public NuGetForwardingApp(string[] argsToForward)
public NuGetForwardingApp(IEnumerable<string> argsToForward)
{
_forwardingApp = new ForwardingApp(
GetNuGetExePath(),
@ -29,6 +25,13 @@ namespace Microsoft.DotNet.Tools.Restore
return _forwardingApp.Execute();
}
public NuGetForwardingApp WithEnvironmentVariable(string name, string value)
{
_forwardingApp.WithEnvironmentVariable(name, value);
return this;
}
private static string GetNuGetExePath()
{
return Path.Combine(