Merge pull request #5102 from krwq/i1494tools

fix tools when restoring with --packages
This commit is contained in:
Krzysztof Wicher 2016-12-20 18:20:25 -08:00 committed by GitHub
commit a65d857f73
11 changed files with 194 additions and 12 deletions

View file

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<GeneratedPackageId>random-name</GeneratedPackageId>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
<DotNetCliToolReference Include="$(GeneratedPackageId)">
<Version>1.0.0</Version>
</DotNetCliToolReference>
</ItemGroup>
</Project>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="test-packages" value="../pkgs" />
</packageSources>
</configuration>

View file

@ -0,0 +1,8 @@
using System;
class Program
{
static void Main(string[] args)
{
}
}

View file

@ -0,0 +1,9 @@
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World from tool!");
}
}

View file

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<GeneratedPackageId>random-name</GeneratedPackageId>
<PackageId>$(GeneratedPackageId)</PackageId>
<AssemblyName>dotnet-randompackage</AssemblyName>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
<Content Include="$(OutputPath)\$(AssemblyName).runtimeconfig.json">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
</ItemGroup>
</Project>

View file

@ -10,6 +10,8 @@ namespace Microsoft.DotNet.Cli.Utils
{
LockFile GetLockFile();
bool TryGetLockFile(out LockFile lockFile);
IEnumerable<SingleProjectInfo> GetTools();
string DepsJsonPath { get; }

View file

@ -129,6 +129,29 @@ namespace Microsoft.DotNet.Cli.Utils
.Result;
}
public bool TryGetLockFile(out LockFile lockFile)
{
lockFile = null;
var lockFilePath = GetLockFilePathFromProjectLockFileProperty() ??
GetLockFilePathFromIntermediateBaseOutputPath();
if (lockFilePath == null)
{
return false;
}
if (!File.Exists(lockFilePath))
{
return false;
}
lockFile = new LockFileFormat()
.ReadWithLock(lockFilePath)
.Result;
return true;
}
private string GetLockFilePathFromProjectLockFileProperty()
{
return _project

View file

@ -127,16 +127,19 @@ namespace Microsoft.DotNet.Cli.Utils
ProjectToolsCommandResolverName,
toolLibraryRange.Name));
var nuGetPathContext = NuGetPathContext.Create(project.ProjectRoot);
var nugetPackagesRoot = nuGetPathContext.UserPackageFolder;
var possiblePackageRoots = GetPossiblePackageRoots(project).ToList();
Reporter.Verbose.WriteLine(string.Format(
LocalizableStrings.NuGetPackagesRoot,
ProjectToolsCommandResolverName,
nugetPackagesRoot));
string.Join(Environment.NewLine, possiblePackageRoots.Select((p) => $"- {p}"))));
var toolLockFile = GetToolLockFile(toolLibraryRange, nugetPackagesRoot);
string nugetPackagesRoot;
var toolLockFile = GetToolLockFile(toolLibraryRange, possiblePackageRoots, out nugetPackagesRoot);
if (toolLockFile == null)
{
return null;
}
Reporter.Verbose.WriteLine(string.Format(
LocalizableStrings.FoundToolLockFile,
@ -189,19 +192,47 @@ namespace Microsoft.DotNet.Cli.Utils
return commandSpec;
}
private IEnumerable<string> GetPossiblePackageRoots(IProject project)
{
if (project.TryGetLockFile(out LockFile lockFile))
{
return lockFile.PackageFolders.Select((packageFolder) => packageFolder.Path);
}
return Enumerable.Empty<string>();
}
private LockFile GetToolLockFile(
SingleProjectInfo toolLibrary,
string nugetPackagesRoot)
IEnumerable<string> possibleNugetPackagesRoot,
out string nugetPackagesRoot)
{
foreach (var packagesRoot in possibleNugetPackagesRoot)
{
if (TryGetToolLockFile(toolLibrary, packagesRoot, out LockFile lockFile))
{
nugetPackagesRoot = packagesRoot;
return lockFile;
}
}
nugetPackagesRoot = null;
return null;
}
private bool TryGetToolLockFile(
SingleProjectInfo toolLibrary,
string nugetPackagesRoot,
out LockFile lockFile)
{
lockFile = null;
var lockFilePath = GetToolLockFilePath(toolLibrary, nugetPackagesRoot);
if (!File.Exists(lockFilePath))
{
return null;
return false;
}
LockFile lockFile = null;
try
{
lockFile = new LockFileFormat()
@ -213,7 +244,7 @@ namespace Microsoft.DotNet.Cli.Utils
throw ex;
}
return lockFile;
return true;
}
private string GetToolLockFilePath(

View file

@ -0,0 +1 @@
https://github.com/Microsoft/msbuild/issues/927

View file

@ -0,0 +1 @@
https://github.com/Microsoft/msbuild/issues/927

View file

@ -13,6 +13,9 @@ using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.DotNet.InternalAbstractions;
using Xunit;
using Xunit.Abstractions;
using Microsoft.Build.Construction;
using System.Linq;
using Microsoft.Build.Evaluation;
namespace Microsoft.DotNet.Tests
{
@ -135,7 +138,52 @@ namespace Microsoft.DotNet.Tests
.WithWorkingDirectory(testInstance.Root)
.ExecuteWithCapturedOutput("nonexistingtool")
.Should().Fail()
.And.HaveStdErrContaining("Version for package `dotnet-nonexistingtool` could not be resolved.");
.And.HaveStdErrContaining("No executable found matching command \"dotnet-nonexistingtool\"");
}
[Fact]
public void ItRunsToolRestoredToSpecificPackageDir()
{
var testInstance = TestAssets.Get("NonRestoredTestProjects", "ToolWithRandomPackageName")
.CreateInstance()
.WithSourceFiles();
var appWithDepOnToolDir = testInstance.Root.Sub("AppWithDepOnTool");
var toolWithRandPkgNameDir = testInstance.Root.Sub("ToolWithRandomPackageName");
var pkgsDir = testInstance.Root.CreateSubdirectory("pkgs");
// 3ebdd4f1-a194-470a-b01a-4515672791d1
// ^-- index = 24
string randomPackageName = Guid.NewGuid().ToString().Substring(24);
// TODO: This is a workround for https://github.com/dotnet/cli/issues/5020
SetGeneratedPackageName(appWithDepOnToolDir.GetFile("AppWithDepOnTool.csproj"),
randomPackageName);
SetGeneratedPackageName(toolWithRandPkgNameDir.GetFile("ToolWithRandomPackageName.csproj"),
randomPackageName);
new RestoreCommand()
.WithWorkingDirectory(toolWithRandPkgNameDir)
.Execute()
.Should().Pass();
new PackCommand()
.WithWorkingDirectory(toolWithRandPkgNameDir)
.Execute($"-o \"{pkgsDir.FullName}\"")
.Should().Pass();
new RestoreCommand()
.WithWorkingDirectory(appWithDepOnToolDir)
.Execute($"--packages \"{pkgsDir.FullName}\"")
.Should().Pass();
new TestCommand("dotnet")
.WithWorkingDirectory(appWithDepOnToolDir)
.ExecuteWithCapturedOutput("randompackage")
.Should().Pass()
.And.HaveStdOutContaining("Hello World from tool!")
.And.NotHaveStdErr();
}
// need conditional theories so we can skip on non-Windows
@ -294,6 +342,14 @@ namespace Microsoft.DotNet.Tests
stopWatch.ElapsedMilliseconds.Should().BeGreaterThan(1000, "Because dotnet should respect the NuGet lock");
}
private void SetGeneratedPackageName(FileInfo project, string packageName)
{
const string propertyName = "GeneratedPackageId";
var p = ProjectRootElement.Open(project.FullName, new ProjectCollection(), true);
p.AddProperty(propertyName, packageName);
p.Save();
}
class HelloCommand : TestCommand
{
public HelloCommand()