2016-02-24 16:05:55 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel.Graph;
|
|
|
|
|
using NuGet.Packaging;
|
|
|
|
|
|
|
|
|
|
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-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
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 16:20:51 -07:00
|
|
|
|
var isPortable = IsPortableApp(commandPath, runtimeConfigPath);
|
2016-03-17 11:45:13 -07:00
|
|
|
|
|
2016-03-27 23:54:17 -07:00
|
|
|
|
return CreateCommandSpecWrappingWithCorehostIfDll(
|
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
|
|
|
|
isPortable,
|
|
|
|
|
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-03-27 22:24:20 -07:00
|
|
|
|
var filePath = Path.Combine(packageDirectory, 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-03-27 23:54:17 -07:00
|
|
|
|
private CommandSpec CreateCommandSpecWrappingWithCorehostIfDll(
|
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
|
|
|
|
bool isPortable,
|
|
|
|
|
string runtimeConfigPath)
|
2016-02-24 16:05:55 -08:00
|
|
|
|
{
|
|
|
|
|
var commandExtension = Path.GetExtension(commandPath);
|
|
|
|
|
|
|
|
|
|
if (commandExtension == FileNameSuffixes.DotNet.DynamicLib)
|
|
|
|
|
{
|
|
|
|
|
return CreatePackageCommandSpecUsingCorehost(
|
|
|
|
|
commandPath,
|
|
|
|
|
commandArguments,
|
|
|
|
|
depsFilePath,
|
2016-03-17 11:45:13 -07:00
|
|
|
|
commandResolutionStrategy,
|
|
|
|
|
nugetPackagesRoot,
|
2016-04-07 16:20:51 -07:00
|
|
|
|
isPortable,
|
|
|
|
|
runtimeConfigPath);
|
2016-02-24 16:05:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CommandSpec CreatePackageCommandSpecUsingCorehost(
|
|
|
|
|
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
|
|
|
|
bool isPortable,
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
if (isPortable)
|
|
|
|
|
{
|
|
|
|
|
var muxer = new Muxer();
|
|
|
|
|
|
|
|
|
|
host = muxer.MuxerPath;
|
|
|
|
|
if (host == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Unable to locate dotnet multiplexer");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arguments.Add("exec");
|
2016-04-07 18:07:51 -07:00
|
|
|
|
|
|
|
|
|
if (runtimeConfigPath != null)
|
|
|
|
|
{
|
|
|
|
|
arguments.Add("--runtimeconfig");
|
|
|
|
|
arguments.Add(runtimeConfigPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (depsFilePath != null)
|
|
|
|
|
{
|
|
|
|
|
arguments.Add("--depsfile");
|
|
|
|
|
arguments.Add(depsFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
arguments.Add("--additionalprobingpath");
|
|
|
|
|
arguments.Add(nugetPackagesRoot);
|
|
|
|
|
|
|
|
|
|
arguments.Add(commandPath);
|
2016-03-17 11:45:13 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-03-24 09:36:05 -07:00
|
|
|
|
host = CoreHost.HostExePath;
|
2016-03-17 11:45:13 -07:00
|
|
|
|
|
2016-04-07 18:07:51 -07:00
|
|
|
|
arguments.Add(commandPath);
|
2016-03-17 14:06:15 -07:00
|
|
|
|
|
2016-04-07 18:07:51 -07:00
|
|
|
|
if (runtimeConfigPath != null)
|
|
|
|
|
{
|
|
|
|
|
arguments.Add("--runtimeconfig");
|
|
|
|
|
arguments.Add(runtimeConfigPath);
|
|
|
|
|
}
|
2016-04-07 16:20:51 -07:00
|
|
|
|
|
2016-04-07 18:07:51 -07:00
|
|
|
|
if (depsFilePath != null)
|
|
|
|
|
{
|
|
|
|
|
arguments.Add("--depsfile");
|
|
|
|
|
arguments.Add(depsFilePath);
|
|
|
|
|
}
|
2016-02-24 16:05:55 -08:00
|
|
|
|
|
2016-04-07 18:07:51 -07:00
|
|
|
|
arguments.Add("--additionalprobingpath");
|
|
|
|
|
arguments.Add(nugetPackagesRoot);
|
|
|
|
|
}
|
2016-03-17 11:45:13 -07:00
|
|
|
|
|
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-04-07 16:20:51 -07:00
|
|
|
|
private bool IsPortableApp(string commandPath, string runtimeConfigPath)
|
2016-03-17 11:45:13 -07:00
|
|
|
|
{
|
|
|
|
|
var commandDir = Path.GetDirectoryName(commandPath);
|
|
|
|
|
|
2016-04-07 16:20:51 -07:00
|
|
|
|
runtimeConfigPath = string.IsNullOrEmpty(runtimeConfigPath)
|
|
|
|
|
? Directory.EnumerateFiles(commandDir).FirstOrDefault(x => x.EndsWith("runtimeconfig.json"))
|
|
|
|
|
: runtimeConfigPath;
|
|
|
|
|
|
2016-03-17 16:39:48 -07:00
|
|
|
|
if (runtimeConfigPath == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var runtimeConfig = new RuntimeConfig(runtimeConfigPath);
|
|
|
|
|
|
|
|
|
|
return runtimeConfig.IsPortable;
|
2016-03-17 11:45:13 -07:00
|
|
|
|
}
|
2016-02-24 16:05:55 -08:00
|
|
|
|
}
|
|
|
|
|
}
|