2016-02-24 16:05:55 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2016-09-27 10:51:57 -07:00
|
|
|
|
using Microsoft.DotNet.Tools.Common;
|
2016-02-24 16:05:55 -08:00
|
|
|
|
using NuGet.Packaging;
|
2016-09-23 15:30:53 -07:00
|
|
|
|
using NuGet.ProjectModel;
|
2016-02-24 16:05:55 -08:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils
|
|
|
|
|
{
|
|
|
|
|
public class PackagedCommandSpecFactory : IPackagedCommandSpecFactory
|
|
|
|
|
{
|
2016-03-27 23:54:17 -07:00
|
|
|
|
public CommandSpec CreateCommandSpecFromLibrary(
|
2016-03-27 22:24:20 -07:00
|
|
|
|
LockFileTargetLibrary toolLibrary,
|
2016-02-24 16:05:55 -08:00
|
|
|
|
string commandName,
|
|
|
|
|
IEnumerable<string> commandArguments,
|
|
|
|
|
IEnumerable<string> allowedExtensions,
|
|
|
|
|
string nugetPackagesRoot,
|
|
|
|
|
CommandResolutionStrategy commandResolutionStrategy,
|
2016-04-07 16:20:51 -07:00
|
|
|
|
string depsFilePath,
|
|
|
|
|
string runtimeConfigPath)
|
2016-02-24 16:05:55 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
Reporter.Verbose.WriteLine($"packagedcommandspecfactory: attempting to find command {commandName} in {toolLibrary.Name}");
|
2016-02-24 16:05:55 -08:00
|
|
|
|
|
2016-03-27 22:24:20 -07:00
|
|
|
|
var toolAssembly = toolLibrary?.RuntimeAssemblies
|
|
|
|
|
.FirstOrDefault(r => Path.GetFileNameWithoutExtension(r.Path) == commandName);
|
|
|
|
|
|
|
|
|
|
if (toolAssembly == null)
|
2016-02-24 16:05:55 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
Reporter.Verbose.WriteLine($"packagedcommandspecfactory: failed to find toolAssembly for {commandName}");
|
|
|
|
|
|
2016-02-24 16:05:55 -08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-03-27 22:24:20 -07:00
|
|
|
|
|
|
|
|
|
var commandPath = GetCommandFilePath(nugetPackagesRoot, toolLibrary, toolAssembly);
|
2016-02-24 16:05:55 -08:00
|
|
|
|
|
2016-03-27 22:24:20 -07:00
|
|
|
|
if (!File.Exists(commandPath))
|
2016-02-24 16:05:55 -08:00
|
|
|
|
{
|
2016-10-27 18:46:43 -07:00
|
|
|
|
Reporter.Verbose.WriteLine($"packagedcommandspecfactory: failed to find commandPath {commandPath}");
|
|
|
|
|
|
2016-02-24 16:05:55 -08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 10:50:38 -07:00
|
|
|
|
return CreateCommandSpecWrappingWithMuxerIfDll(
|
2016-02-24 16:05:55 -08:00
|
|
|
|
commandPath,
|
|
|
|
|
commandArguments,
|
|
|
|
|
depsFilePath,
|
2016-03-17 11:45:13 -07:00
|
|
|
|
commandResolutionStrategy,
|
|
|
|
|
nugetPackagesRoot,
|
2016-04-07 16:20:51 -07:00
|
|
|
|
runtimeConfigPath);
|
2016-02-24 16:05:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-27 22:24:20 -07:00
|
|
|
|
private string GetCommandFilePath(string nugetPackagesRoot, LockFileTargetLibrary toolLibrary, LockFileItem runtimeAssembly)
|
2016-02-24 16:05:55 -08:00
|
|
|
|
{
|
|
|
|
|
var packageDirectory = new VersionFolderPathResolver(nugetPackagesRoot)
|
2016-03-27 22:24:20 -07:00
|
|
|
|
.GetInstallPath(toolLibrary.Name, toolLibrary.Version);
|
2016-02-24 16:05:55 -08:00
|
|
|
|
|
2016-09-27 10:51:57 -07:00
|
|
|
|
var filePath = Path.Combine(packageDirectory, PathUtility.GetPathWithDirectorySeparator(runtimeAssembly.Path));
|
2016-02-24 16:05:55 -08:00
|
|
|
|
|
2016-03-27 22:24:20 -07:00
|
|
|
|
return filePath;
|
2016-02-24 16:05:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 10:50:38 -07:00
|
|
|
|
private CommandSpec CreateCommandSpecWrappingWithMuxerIfDll(
|
2016-02-24 16:05:55 -08:00
|
|
|
|
string commandPath,
|
|
|
|
|
IEnumerable<string> commandArguments,
|
|
|
|
|
string depsFilePath,
|
2016-03-17 11:45:13 -07:00
|
|
|
|
CommandResolutionStrategy commandResolutionStrategy,
|
|
|
|
|
string nugetPackagesRoot,
|
2016-04-07 16:20:51 -07:00
|
|
|
|
string runtimeConfigPath)
|
2016-02-24 16:05:55 -08:00
|
|
|
|
{
|
|
|
|
|
var commandExtension = Path.GetExtension(commandPath);
|
|
|
|
|
|
|
|
|
|
if (commandExtension == FileNameSuffixes.DotNet.DynamicLib)
|
|
|
|
|
{
|
2016-06-02 10:50:38 -07:00
|
|
|
|
return CreatePackageCommandSpecUsingMuxer(
|
2016-02-24 16:05:55 -08:00
|
|
|
|
commandPath,
|
|
|
|
|
commandArguments,
|
|
|
|
|
depsFilePath,
|
2016-03-17 11:45:13 -07:00
|
|
|
|
commandResolutionStrategy,
|
|
|
|
|
nugetPackagesRoot,
|
2016-04-07 16:20:51 -07:00
|
|
|
|
runtimeConfigPath);
|
2016-02-24 16:05:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 10:50:38 -07:00
|
|
|
|
private CommandSpec CreatePackageCommandSpecUsingMuxer(
|
2016-02-24 16:05:55 -08:00
|
|
|
|
string commandPath,
|
|
|
|
|
IEnumerable<string> commandArguments,
|
|
|
|
|
string depsFilePath,
|
2016-03-17 11:45:13 -07:00
|
|
|
|
CommandResolutionStrategy commandResolutionStrategy,
|
|
|
|
|
string nugetPackagesRoot,
|
2016-04-07 16:20:51 -07:00
|
|
|
|
string runtimeConfigPath)
|
2016-02-24 16:05:55 -08:00
|
|
|
|
{
|
2016-03-27 22:24:20 -07:00
|
|
|
|
var host = string.Empty;
|
2016-02-24 16:05:55 -08:00
|
|
|
|
var arguments = new List<string>();
|
2016-03-17 11:45:13 -07:00
|
|
|
|
|
2016-06-02 10:50:38 -07:00
|
|
|
|
var muxer = new Muxer();
|
2016-03-17 11:45:13 -07:00
|
|
|
|
|
2016-06-02 10:50:38 -07:00
|
|
|
|
host = muxer.MuxerPath;
|
|
|
|
|
if (host == null)
|
2016-03-17 11:45:13 -07:00
|
|
|
|
{
|
2016-06-02 10:50:38 -07:00
|
|
|
|
throw new Exception("Unable to locate dotnet multiplexer");
|
2016-04-07 18:07:51 -07:00
|
|
|
|
}
|
2016-06-02 10:50:38 -07:00
|
|
|
|
|
|
|
|
|
arguments.Add("exec");
|
|
|
|
|
|
2016-04-07 20:50:44 -07:00
|
|
|
|
if (runtimeConfigPath != null)
|
|
|
|
|
{
|
|
|
|
|
arguments.Add("--runtimeconfig");
|
|
|
|
|
arguments.Add(runtimeConfigPath);
|
|
|
|
|
}
|
2016-06-02 10:50:38 -07:00
|
|
|
|
|
2016-04-07 20:50:44 -07:00
|
|
|
|
if (depsFilePath != null)
|
|
|
|
|
{
|
|
|
|
|
arguments.Add("--depsfile");
|
|
|
|
|
arguments.Add(depsFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arguments.Add("--additionalprobingpath");
|
|
|
|
|
arguments.Add(nugetPackagesRoot);
|
2016-03-17 11:45:13 -07:00
|
|
|
|
|
2016-04-07 20:50:44 -07:00
|
|
|
|
arguments.Add(commandPath);
|
2016-02-24 16:05:55 -08:00
|
|
|
|
arguments.AddRange(commandArguments);
|
|
|
|
|
|
2016-03-17 11:45:13 -07:00
|
|
|
|
return CreateCommandSpec(host, arguments, commandResolutionStrategy);
|
2016-02-24 16:05:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CommandSpec CreateCommandSpec(
|
|
|
|
|
string commandPath,
|
|
|
|
|
IEnumerable<string> commandArguments,
|
|
|
|
|
CommandResolutionStrategy commandResolutionStrategy)
|
|
|
|
|
{
|
|
|
|
|
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(commandArguments);
|
|
|
|
|
|
|
|
|
|
return new CommandSpec(commandPath, escapedArgs, commandResolutionStrategy);
|
2016-03-17 11:45:13 -07:00
|
|
|
|
}
|
2016-02-24 16:05:55 -08:00
|
|
|
|
}
|
|
|
|
|
}
|