private ICommand CreateCommand(params string[] args) { ICommand command; // Run tests on full framework MSBuild if environment variable is set pointing to it string msbuildPath = Environment.GetEnvironmentVariable("DOTNET_SDK_TEST_MSBUILD_PATH"); if (!string.IsNullOrEmpty(msbuildPath)) { command = Command.Create(msbuildPath, args); } else { var newArgs = args.ToList(); newArgs.Insert(0, $"msbuild"); command = Command.Create(DotNetHostPath, newArgs); } command = RepoInfo.AddTestEnvironmentVariables(command); return(command); }
// This method duplicates a lot of logic from the CLI in order to test generating deps files for tools in the SDK repo private CommandResult GenerateDepsAndRunTool(TestProject toolProject, [CallerMemberName] string callingMethod = "") { DeleteFolder(Path.Combine(RepoInfo.NuGetCachePath, toolProject.Name.ToLowerInvariant())); DeleteFolder(Path.Combine(RepoInfo.NuGetCachePath, ".tools", toolProject.Name.ToLowerInvariant())); var toolProjectInstance = _testAssetsManager.CreateTestProject(toolProject, callingMethod, identifier: toolProject.Name) .Restore(Log, toolProject.Name); var packCommand = new PackCommand(Log, Path.Combine(toolProjectInstance.TestRoot, toolProject.Name)); packCommand.Execute() .Should() .Pass(); string nupkgPath = Path.Combine(packCommand.ProjectRootPath, "bin", "Debug"); TestProject toolReferencer = new TestProject() { Name = "ToolReferencer", IsSdkProject = true, TargetFrameworks = "netcoreapp2.0" }; var toolReferencerInstance = _testAssetsManager.CreateTestProject(toolReferencer, callingMethod, identifier: toolReferencer.Name) .WithProjectChanges(project => { var ns = project.Root.Name.Namespace; var itemGroup = new XElement(ns + "ItemGroup"); project.Root.Add(itemGroup); itemGroup.Add(new XElement(ns + "DotNetCliToolReference", new XAttribute("Include", toolProject.Name), new XAttribute("Version", "1.0.0"))); }); var restoreCommand = toolReferencerInstance.GetRestoreCommand(Log, toolReferencer.Name); restoreCommand.AddSource(nupkgPath); restoreCommand.Execute().Should().Pass(); string toolAssetsFilePath = Path.Combine(RepoInfo.NuGetCachePath, ".tools", toolProject.Name.ToLowerInvariant(), "1.0.0", toolProject.TargetFrameworks, "project.assets.json"); var toolAssetsFile = new LockFileFormat().Read(toolAssetsFilePath); var args = new List <string>(); string generateDepsProjectPath = Path.Combine(RepoInfo.SdksPath, "Microsoft.NET.Sdk", "build", "GenerateDeps", "GenerateDeps.proj"); args.Add($"/p:ProjectAssetsFile=\"{toolAssetsFilePath}\""); args.Add($"/p:ToolName={toolProject.Name}"); string depsFilePath = Path.Combine(Path.GetDirectoryName(toolAssetsFilePath), toolProject.Name + ".deps.json"); args.Add($"/p:ProjectDepsFilePath={depsFilePath}"); var toolTargetFramework = toolAssetsFile.Targets.First().TargetFramework.GetShortFolderName(); args.Add($"/p:TargetFramework={toolProject.TargetFrameworks}"); // Look for the .props file in the Microsoft.NETCore.App package, until NuGet // generates .props and .targets files for tool restores (https://github.com/NuGet/Home/issues/5037) var platformLibrary = toolAssetsFile.Targets .Single() .Libraries .FirstOrDefault(e => e.Name.Equals("Microsoft.NETCore.App", StringComparison.OrdinalIgnoreCase)); if (platformLibrary != null) { string buildRelativePath = platformLibrary.Build.FirstOrDefault()?.Path; var platformLibraryPath = GetPackageDirectory(toolAssetsFile, platformLibrary); if (platformLibraryPath != null && buildRelativePath != null) { // Get rid of "_._" filename buildRelativePath = Path.GetDirectoryName(buildRelativePath); string platformLibraryBuildFolderPath = Path.Combine(platformLibraryPath, buildRelativePath); var platformLibraryPropsFile = Directory.GetFiles(platformLibraryBuildFolderPath, "*.props").FirstOrDefault(); if (platformLibraryPropsFile != null) { args.Add($"/p:AdditionalImport={platformLibraryPropsFile}"); } } } var generateDepsCommand = new MSBuildCommand(Log, "BuildDepsJson", generateDepsProjectPath); generateDepsCommand.Execute(args.ToArray()) .Should() .Pass(); var toolLibrary = toolAssetsFile.Targets .Single() .Libraries.FirstOrDefault( l => StringComparer.OrdinalIgnoreCase.Equals(l.Name, toolProject.Name)); var toolAssembly = toolLibrary?.RuntimeAssemblies .FirstOrDefault(r => Path.GetFileNameWithoutExtension(r.Path) == toolProject.Name); var toolPackageDirectory = GetPackageDirectory(toolAssetsFile, toolLibrary); var toolAssemblyPath = Path.Combine( toolPackageDirectory, toolAssembly.Path); var dotnetArgs = new List <string>(); dotnetArgs.Add("exec"); dotnetArgs.Add("--depsfile"); dotnetArgs.Add(depsFilePath); foreach (var packageFolder in GetNormalizedPackageFolders(toolAssetsFile)) { dotnetArgs.Add("--additionalprobingpath"); dotnetArgs.Add(packageFolder); } dotnetArgs.Add(Path.GetFullPath(toolAssemblyPath)); ICommand toolCommand = Command.Create(RepoInfo.DotNetHostPath, dotnetArgs) .CaptureStdOut(); toolCommand = RepoInfo.AddTestEnvironmentVariables(toolCommand); var toolResult = toolCommand.Execute(); return(toolResult); }