Exemplo n.º 1
0
        private CommandSpec CreateCommandSpecWrappedWithCmd(
            string command,
            IEnumerable <string> args,
            CommandResolutionStrategy resolutionStrategy)
        {
            var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe";

            // Handle the case where ComSpec is already the command
            if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
            {
                command = args.FirstOrDefault();
                args    = args.Skip(1);
            }

            var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);

            if (ArgumentEscaper.ShouldSurroundWithQuotes(command))
            {
                command = $"\"{command}\"";
            }

            var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";

            return(new CommandSpec(comSpec, escapedArgString, resolutionStrategy));
        }
Exemplo n.º 2
0
        private static string ResolveExecutableFromPath(string executable, ref string args, ref CommandResolutionStrategy resolutionStrategy)
        {
            resolutionStrategy = CommandResolutionStrategy.Path;

            foreach (string suffix in Constants.RunnableSuffixes)
            {
                var fullExecutable = Path.GetFullPath(Path.Combine(
                                        AppContext.BaseDirectory, executable + suffix));

                if (File.Exists(fullExecutable))
                {
                    executable = fullExecutable;
                    
                    resolutionStrategy = CommandResolutionStrategy.BaseDirectory;

                    // In priority order we've found the best runnable extension, so break.
                    break;
                }
            }

            // On Windows, we want to avoid using "cmd" if possible (it mangles the colors, and a bunch of other things)
            // So, do a quick path search to see if we can just directly invoke it
            var useCmd = ShouldUseCmd(executable);

            if (useCmd)
            {
                var comSpec = Environment.GetEnvironmentVariable("ComSpec");
                // wrap 'executable' within quotes to deal woth space in its path.
                args = $"/S /C \"\"{executable}\" {args}\"";
                executable = comSpec;
            }

            return executable;
        }
Exemplo n.º 3
0
        private CommandSpec CreatePackageCommandSpecUsingMuxer(
            string commandPath,
            IEnumerable <string> commandArguments,
            CommandResolutionStrategy commandResolutionStrategy)
        {
            var arguments = new List <string>();

            var muxer = new Muxer();

            var host = muxer.MuxerPath;

            if (host == null)
            {
                throw new Exception(LocalizableStrings.UnableToLocateDotnetMultiplexer);
            }

            arguments.Add(commandPath);

            if (commandArguments != null)
            {
                arguments.AddRange(commandArguments);
            }

            return(CreateCommandSpec(host, arguments, commandResolutionStrategy));
        }
Exemplo n.º 4
0
        private CommandSpec CreateCommandSpecWrappingWithCorehostIfDll(
            string commandPath,
            IEnumerable <string> commandArguments,
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy,
            string nugetPackagesRoot,
            bool isPortable,
            string runtimeConfigPath)
        {
            var commandExtension = Path.GetExtension(commandPath);

            if (commandExtension == FileNameSuffixes.DotNet.DynamicLib)
            {
                return(CreatePackageCommandSpecUsingCorehost(
                           commandPath,
                           commandArguments,
                           depsFilePath,
                           commandResolutionStrategy,
                           nugetPackagesRoot,
                           isPortable,
                           runtimeConfigPath));
            }

            return(CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy));
        }
Exemplo n.º 5
0
        private static CommandSpec CreateComSpecCommandSpec(
            string command,
            IEnumerable <string> args,
            CommandResolutionStrategy resolutionStrategy)
        {
            // To prevent Command Not Found, comspec gets passed in as
            // the command already in some cases
            var comSpec = Environment.GetEnvironmentVariable("ComSpec");

            if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
            {
                command = args.FirstOrDefault();
                args    = args.Skip(1);
            }
            var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);

            if (ArgumentEscaper.ShouldSurroundWithQuotes(command))
            {
                command = $"\"{command}\"";
            }

            var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";

            return(new CommandSpec(comSpec, escapedArgString, resolutionStrategy));
        }
        public CommandSpec CreateCommandSpecFromLibrary(
            LockFileTargetLibrary toolLibrary,
            string commandName,
            IEnumerable<string> commandArguments,
            IEnumerable<string> allowedExtensions,
            string nugetPackagesRoot,
            CommandResolutionStrategy commandResolutionStrategy,
            string depsFilePath,
            string runtimeConfigPath)
        {

            var toolAssembly = toolLibrary?.RuntimeAssemblies
                    .FirstOrDefault(r => Path.GetFileNameWithoutExtension(r.Path) == commandName);

            if (toolAssembly == null)
            {
                return null;
            }
            
            var commandPath = GetCommandFilePath(nugetPackagesRoot, toolLibrary, toolAssembly);

            if (!File.Exists(commandPath))
            {
                return null;
            }

            return CreateCommandSpecWrappingWithMuxerIfDll(
                commandPath, 
                commandArguments, 
                depsFilePath, 
                commandResolutionStrategy,
                nugetPackagesRoot,
                runtimeConfigPath);
        }
Exemplo n.º 7
0
        public CommandSpec CreateCommandSpecFromLibrary(
            LockFilePackageLibrary library,
            string commandName,
            IEnumerable <string> commandArguments,
            IEnumerable <string> allowedExtensions,
            string nugetPackagesRoot,
            CommandResolutionStrategy commandResolutionStrategy,
            string depsFilePath)
        {
            var packageDirectory = GetPackageDirectoryFullPath(library, nugetPackagesRoot);

            if (!Directory.Exists(packageDirectory))
            {
                return(null);
            }

            var commandFile = GetCommandFileRelativePath(library, commandName, allowedExtensions);

            if (commandFile == null)
            {
                return(null);
            }

            var commandPath = Path.Combine(packageDirectory, commandFile);

            return(CreateCommandSpecWrappingWithCorehostfDll(
                       commandPath,
                       commandArguments,
                       depsFilePath,
                       commandResolutionStrategy));
        }
Exemplo n.º 8
0
        private static CommandSpec CreateCommandSpecPreferringExe(
            string commandName,
            IEnumerable <string> args,
            string commandPath,
            CommandResolutionStrategy resolutionStrategy,
            bool useComSpec = false)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
                Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
            {
                var preferredCommandPath = Env.GetCommandPath(commandName, ".exe");

                // Use cmd if we can't find an exe
                if (preferredCommandPath == null)
                {
                    useComSpec = true;
                }
                else
                {
                    commandPath = preferredCommandPath;
                }
            }

            if (useComSpec)
            {
                return(CreateComSpecCommandSpec(commandPath, args, resolutionStrategy));
            }
            else
            {
                var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
                return(new CommandSpec(commandPath, escapedArgs, resolutionStrategy));
            }
        }
Exemplo n.º 9
0
        public CommandSpec CreateCommandSpec(
            string commandName,
            IEnumerable <string> args,
            string commandPath,
            CommandResolutionStrategy resolutionStrategy,
            IEnvironmentProvider environment)
        {
            var useCmdWrapper = false;

            if (Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
            {
                var preferredCommandPath = environment.GetCommandPath(commandName, ".exe");

                if (preferredCommandPath == null)
                {
                    useCmdWrapper = true;
                }
                else
                {
                    commandPath = preferredCommandPath;
                }
            }

            return(useCmdWrapper
                ? CreateCommandSpecWrappedWithCmd(commandPath, args, resolutionStrategy)
                : CreateCommandSpecFromExecutable(commandPath, args, resolutionStrategy));
        }
Exemplo n.º 10
0
        public CommandSpec CreateCommandSpecFromLibrary(
            LockFileTargetLibrary toolLibrary,
            string commandName,
            IEnumerable<string> commandArguments,
            IEnumerable<string> allowedExtensions,
            string nugetPackagesRoot,
            CommandResolutionStrategy commandResolutionStrategy,
            string depsFilePath)
        {

            var toolAssembly = toolLibrary?.RuntimeAssemblies
                    .FirstOrDefault(r => Path.GetFileNameWithoutExtension(r.Path) == commandName);

            if (toolAssembly == null)
            {
                return null;
            }
            
            var commandPath = GetCommandFilePath(nugetPackagesRoot, toolLibrary, toolAssembly);

            if (!File.Exists(commandPath))
            {
                return null;
            }

            var isPortable = IsPortableApp(commandPath);

            return CreateCommandSpecWrappingWithCorehostIfDll(
                commandPath, 
                commandArguments, 
                depsFilePath, 
                commandResolutionStrategy,
                nugetPackagesRoot,
                isPortable);
        }
        private CommandSpec CreateCommandSpecWrappedWithCmd(
            string command,
            IEnumerable<string> args,
            CommandResolutionStrategy resolutionStrategy)
        {
            var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe";

            // Handle the case where ComSpec is already the command
            if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
            {
                command = args.FirstOrDefault();
                args = args.Skip(1);
            }

            var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);

            if (ArgumentEscaper.ShouldSurroundWithQuotes(command))
            {
                command = $"\"{command}\"";
            }

            var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";

            return new CommandSpec(comSpec, escapedArgString, resolutionStrategy);
        }
        public CommandSpec CreateCommandSpec(
           string commandName,
           IEnumerable<string> args,
           string commandPath,
           CommandResolutionStrategy resolutionStrategy,
           IEnvironmentProvider environment)
        {
            var useCmdWrapper = false;

            if (Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
            {
                var preferredCommandPath = environment.GetCommandPath(commandName, ".exe");

                if (preferredCommandPath == null)
                {
                    useCmdWrapper = true;
                }
                else
                {
                    commandPath = preferredCommandPath;
                }
            }

            return useCmdWrapper
                ? CreateCommandSpecWrappedWithCmd(commandPath, args, resolutionStrategy)
                : CreateCommandSpecFromExecutable(commandPath, args, resolutionStrategy);
        }
Exemplo n.º 13
0
        public CommandSpec CreateCommandSpecFromLibrary(
            LockFilePackageLibrary library,
            string commandName,
            IEnumerable<string> commandArguments,
            IEnumerable<string> allowedExtensions,
            string nugetPackagesRoot,
            CommandResolutionStrategy commandResolutionStrategy,
            string depsFilePath)
        {
            var packageDirectory = GetPackageDirectoryFullPath(library, nugetPackagesRoot);

            if (!Directory.Exists(packageDirectory))
            {
                return null;
            }

            var commandFile = GetCommandFileRelativePath(library, commandName, allowedExtensions);

            if (commandFile == null)
            {
                return null;
            }

            var commandPath = Path.Combine(packageDirectory, commandFile);

            return CreateCommandSpecWrappingWithCorehostfDll(
                commandPath, 
                commandArguments, 
                depsFilePath, 
                commandResolutionStrategy);
        }
 private CommandSpec CreateCommandSpecFromExecutable(
     string command,
     IEnumerable<string> args,
     CommandResolutionStrategy resolutionStrategy)
 {
     var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
     return new CommandSpec(command, escapedArgs, resolutionStrategy);
 }
Exemplo n.º 15
0
        private CommandSpec CreateCommandSpecFromExecutable(
            string command,
            IEnumerable <string> args,
            CommandResolutionStrategy resolutionStrategy)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);

            return(new CommandSpec(command, escapedArgs, resolutionStrategy));
        }
        private CommandSpec CreateCommandSpec(
            string commandPath,
            IEnumerable <string> commandArguments,
            CommandResolutionStrategy commandResolutionStrategy)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(commandArguments);

            return(new CommandSpec(commandPath, escapedArgs, commandResolutionStrategy));
        }
 public CommandSpec CreateCommandSpec(
    string commandName,
    IEnumerable<string> args,
    string commandPath,
    CommandResolutionStrategy resolutionStrategy,
    IEnvironmentProvider environment)
 {
     var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
     return new CommandSpec(commandPath, escapedArgs, resolutionStrategy);
 }
Exemplo n.º 18
0
 public CommandSpec(
     string path,
     string args,
     CommandResolutionStrategy resolutionStrategy,
     Dictionary <string, string> environmentVariables = null)
 {
     Path = path;
     Args = args;
     ResolutionStrategy   = resolutionStrategy;
     EnvironmentVariables = environmentVariables ?? new Dictionary <string, string>();
 }
Exemplo n.º 19
0
        public CommandSpec CreateCommandSpec(
            string commandName,
            IEnumerable <string> args,
            string commandPath,
            CommandResolutionStrategy resolutionStrategy,
            IEnvironmentProvider environment)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);

            return(new CommandSpec(commandPath, escapedArgs, resolutionStrategy));
        }
 public CommandSpec CreateCommandSpecFromPublishFolder(
     string commandPath,
     IEnumerable <string> commandArguments,
     CommandResolutionStrategy commandResolutionStrategy,
     string depsFilePath,
     string runtimeConfigPath)
 {
     return(CreateCommandSpecWrappingWithMuxerIfDll(
                commandPath,
                commandArguments,
                depsFilePath,
                commandResolutionStrategy,
                runtimeConfigPath));
 }
Exemplo n.º 21
0
        private CommandSpec CreatePackageCommandSpecUsingMuxer(
            string commandPath,
            IEnumerable <string> commandArguments,
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy,
            IEnumerable <string> packageFolders,
            string runtimeConfigPath)
        {
            var host      = string.Empty;
            var arguments = new List <string>();

            var muxer = new Muxer();

            host = muxer.MuxerPath;
            if (host == null)
            {
                throw new Exception(LocalizableStrings.UnableToLocateDotnetMultiplexer);
            }

            arguments.Add("exec");

            if (runtimeConfigPath != null)
            {
                arguments.Add("--runtimeconfig");
                arguments.Add(runtimeConfigPath);
            }

            if (depsFilePath != null)
            {
                arguments.Add("--depsfile");
                arguments.Add(depsFilePath);
            }

            foreach (var packageFolder in packageFolders)
            {
                arguments.Add("--additionalprobingpath");
                arguments.Add(packageFolder);
            }

            if (_addAdditionalArguments != null)
            {
                _addAdditionalArguments(commandPath, arguments);
            }

            arguments.Add(commandPath);
            arguments.AddRange(commandArguments);

            return(CreateCommandSpec(host, arguments, commandResolutionStrategy));
        }
Exemplo n.º 22
0
        public CommandSpec CreateCommandSpecFromLibrary(
            LockFileTargetLibrary toolLibrary,
            string commandName,
            IEnumerable <string> commandArguments,
            IEnumerable <string> allowedExtensions,
            LockFile lockFile,
            CommandResolutionStrategy commandResolutionStrategy,
            string depsFilePath,
            string runtimeConfigPath)
        {
            Reporter.Verbose.WriteLine(string.Format(
                                           LocalizableStrings.AttemptingToFindCommand,
                                           PackagedCommandSpecFactoryName,
                                           commandName,
                                           toolLibrary.Name));

            var toolAssembly = toolLibrary?.RuntimeAssemblies
                               .FirstOrDefault(r => Path.GetFileNameWithoutExtension(r.Path) == commandName);

            if (toolAssembly == null)
            {
                Reporter.Verbose.WriteLine(string.Format(
                                               LocalizableStrings.FailedToFindToolAssembly,
                                               PackagedCommandSpecFactoryName,
                                               commandName));

                return(null);
            }

            var commandPath = GetCommandFilePath(lockFile, toolLibrary, toolAssembly);

            if (!File.Exists(commandPath))
            {
                Reporter.Verbose.WriteLine(string.Format(
                                               LocalizableStrings.FailedToFindCommandPath,
                                               PackagedCommandSpecFactoryName,
                                               commandPath));

                return(null);
            }

            return(CreateCommandSpecWrappingWithMuxerIfDll(
                       commandPath,
                       commandArguments,
                       depsFilePath,
                       commandResolutionStrategy,
                       lockFile.GetNormalizedPackageFolders(),
                       runtimeConfigPath));
        }
Exemplo n.º 23
0
        private CommandSpec CreatePackageCommandSpecUsingCorehost(
            string commandPath,
            IEnumerable <string> commandArguments,
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy,
            string nugetPackagesRoot,
            bool isPortable,
            string runtimeConfigPath)
        {
            var host      = string.Empty;
            var arguments = new List <string>();

            if (isPortable)
            {
                var muxer = new Muxer();

                host = muxer.MuxerPath;
                if (host == null)
                {
                    throw new Exception("Unable to locate dotnet multiplexer");
                }

                arguments.Add("exec");
            }
            else
            {
                host = CoreHost.HostExePath;
            }
            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);
            arguments.AddRange(commandArguments);

            return(CreateCommandSpec(host, arguments, commandResolutionStrategy));
        }
Exemplo n.º 24
0
        private static CommandSpec CreateCommandSpecPreferringExe(string commandName, string args, string commandPath,
                                                                  CommandResolutionStrategy resolutionStrategy)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
                Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
            {
                var preferredCommandPath = Env.GetCommandPath(commandName, ".exe");

                if (preferredCommandPath != null)
                {
                    commandPath = Environment.GetEnvironmentVariable("ComSpec");

                    args = $"/S /C \"\"{preferredCommandPath}\" {args}\"";
                }
            }

            return(new CommandSpec(commandPath, args, resolutionStrategy));
        }
Exemplo n.º 25
0
        private CommandSpec CreateCommandSpecWrappingWithCorehostfDll(
            string commandPath,
            IEnumerable <string> commandArguments,
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy)
        {
            var commandExtension = Path.GetExtension(commandPath);

            if (commandExtension == FileNameSuffixes.DotNet.DynamicLib)
            {
                return(CreatePackageCommandSpecUsingCorehost(
                           commandPath,
                           commandArguments,
                           depsFilePath,
                           commandResolutionStrategy));
            }

            return(CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy));
        }
Exemplo n.º 26
0
        private CommandSpec CreateCommandSpecWrappingWithCorehostfDll(
            string commandPath, 
            IEnumerable<string> commandArguments, 
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy)
        {
            var commandExtension = Path.GetExtension(commandPath);

            if (commandExtension == FileNameSuffixes.DotNet.DynamicLib)
            {
                return CreatePackageCommandSpecUsingCorehost(
                    commandPath, 
                    commandArguments, 
                    depsFilePath, 
                    commandResolutionStrategy);
            }
            
            return CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy);
        }
        private CommandSpec CreatePackageCommandSpecUsingMuxer(
            string commandPath, 
            IEnumerable<string> commandArguments, 
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy,
            string nugetPackagesRoot,
            string runtimeConfigPath)
        {
            var host = string.Empty;
            var arguments = new List<string>();

            var muxer = new Muxer();

            host = muxer.MuxerPath;
            if (host == null)
            {
                throw new Exception("Unable to locate dotnet multiplexer");
            }

            arguments.Add("exec");

            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);
            arguments.AddRange(commandArguments);

            return CreateCommandSpec(host, arguments, commandResolutionStrategy);
        }
Exemplo n.º 28
0
        private Command(string executable, string args, CommandResolutionStrategy resolutionStrategy)
        {
            // Set the things we need
            var psi = new ProcessStartInfo()
            {
                FileName = executable,
                Arguments = args,
                RedirectStandardError = true,
                RedirectStandardOutput = true
            };

            _process = new Process()
            {
                StartInfo = psi
            };

            _stdOut = new StreamForwarder();
            _stdErr = new StreamForwarder();

            ResolutionStrategy = resolutionStrategy;
        }
Exemplo n.º 29
0
        private CommandSpec CreatePackageCommandSpecUsingCorehost(
            string commandPath,
            IEnumerable <string> commandArguments,
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy)
        {
            var corehost = CoreHost.HostExePath;

            var arguments = new List <string>();

            arguments.Add(commandPath);

            if (depsFilePath != null)
            {
                arguments.Add($"--depsfile:{depsFilePath}");
            }

            arguments.AddRange(commandArguments);

            return(CreateCommandSpec(corehost, arguments, commandResolutionStrategy));
        }
Exemplo n.º 30
0
        private Command(string executable, string args, CommandResolutionStrategy resolutionStrategy)
        {
            // Set the things we need
            var psi = new ProcessStartInfo()
            {
                FileName               = executable,
                Arguments              = args,
                RedirectStandardError  = true,
                RedirectStandardOutput = true
            };

            _process = new Process()
            {
                StartInfo = psi
            };

            _stdOut = new StreamForwarder();
            _stdErr = new StreamForwarder();

            ResolutionStrategy = resolutionStrategy;
        }
Exemplo n.º 31
0
 public CommandSpec CreateCommandSpecFromLibrary(
     LockFileTargetLibrary toolLibrary,
     string commandName,
     IEnumerable <string> commandArguments,
     IEnumerable <string> allowedExtensions,
     string nugetPackagesRoot,
     CommandResolutionStrategy commandResolutionStrategy,
     string depsFilePath,
     string runtimeConfigPath)
 {
     return(CreateCommandSpecFromLibrary(
                toolLibrary,
                commandName,
                commandArguments,
                allowedExtensions,
                new List <string> {
         nugetPackagesRoot
     },
                commandResolutionStrategy,
                depsFilePath,
                runtimeConfigPath));
 }
        public CommandSpec CreateCommandSpecFromLibrary(
            LockFileTargetLibrary toolLibrary,
            string commandName,
            IEnumerable <string> commandArguments,
            IEnumerable <string> allowedExtensions,
            string nugetPackagesRoot,
            CommandResolutionStrategy commandResolutionStrategy,
            string depsFilePath,
            string runtimeConfigPath)
        {
            Reporter.Verbose.WriteLine($"packagedcommandspecfactory: attempting to find command {commandName} in {toolLibrary.Name}");

            var toolAssembly = toolLibrary?.RuntimeAssemblies
                               .FirstOrDefault(r => Path.GetFileNameWithoutExtension(r.Path) == commandName);

            if (toolAssembly == null)
            {
                Reporter.Verbose.WriteLine($"packagedcommandspecfactory: failed to find toolAssembly for {commandName}");

                return(null);
            }

            var commandPath = GetCommandFilePath(nugetPackagesRoot, toolLibrary, toolAssembly);

            if (!File.Exists(commandPath))
            {
                Reporter.Verbose.WriteLine($"packagedcommandspecfactory: failed to find commandPath {commandPath}");

                return(null);
            }

            return(CreateCommandSpecWrappingWithMuxerIfDll(
                       commandPath,
                       commandArguments,
                       depsFilePath,
                       commandResolutionStrategy,
                       nugetPackagesRoot,
                       runtimeConfigPath));
        }
Exemplo n.º 33
0
        private CommandSpec CreateCommandSpecUsingMuxerIfPortable(
            string commandPath,
            IEnumerable <string> commandArgs,
            string depsJsonFile,
            CommandResolutionStrategy commandResolutionStrategy,
            string nugetPackagesRoot,
            bool isPortable)
        {
            var depsFileArguments = GetDepsFileArguments(depsJsonFile);
            var additionalProbingPathArguments = GetAdditionalProbingPathArguments();

            var muxerArgs = new List <string>();

            muxerArgs.Add("exec");
            muxerArgs.AddRange(depsFileArguments);
            muxerArgs.AddRange(additionalProbingPathArguments);
            muxerArgs.Add(commandPath);
            muxerArgs.AddRange(commandArgs);

            var escapedArgString = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(muxerArgs);

            return(new CommandSpec(_muxer.MuxerPath, escapedArgString, commandResolutionStrategy));
        }
        private CommandSpec CreateCommandSpecWrappingWithMuxerIfDll(
            string commandPath, 
            IEnumerable<string> commandArguments, 
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy,
            string nugetPackagesRoot,
            string runtimeConfigPath)
        {
            var commandExtension = Path.GetExtension(commandPath);

            if (commandExtension == FileNameSuffixes.DotNet.DynamicLib)
            {
                return CreatePackageCommandSpecUsingMuxer(
                    commandPath, 
                    commandArguments, 
                    depsFilePath, 
                    commandResolutionStrategy,
                    nugetPackagesRoot,
                    runtimeConfigPath);
            }
            
            return CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy);
        }
Exemplo n.º 35
0
        private CommandSpec CreateCommandSpecWrappingWithMuxerIfDll(
            string commandPath,
            IEnumerable <string> commandArguments,
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy,
            IEnumerable <string> packageFolders,
            string runtimeConfigPath)
        {
            var commandExtension = Path.GetExtension(commandPath);

            if (commandExtension == FileNameSuffixes.DotNet.DynamicLib)
            {
                return(CreatePackageCommandSpecUsingMuxer(
                           commandPath,
                           commandArguments,
                           depsFilePath,
                           commandResolutionStrategy,
                           packageFolders,
                           runtimeConfigPath));
            }

            return(CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy));
        }
        private CommandSpec CreatePackageCommandSpecUsingMuxer(
            string commandPath,
            IEnumerable <string> commandArguments,
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy,
            string runtimeConfigPath)
        {
            var arguments = new List <string>();

            var muxer = new Muxer();

            var host = muxer.MuxerPath;

            if (host == null)
            {
                throw new Exception("Unable to locate dotnet multiplexer");
            }

            arguments.Add("exec");

            if (runtimeConfigPath != null)
            {
                arguments.Add("--runtimeconfig");
                arguments.Add(runtimeConfigPath);
            }

            if (depsFilePath != null)
            {
                arguments.Add("--depsfile");
                arguments.Add(depsFilePath);
            }

            arguments.Add(commandPath);
            arguments.AddRange(commandArguments);

            return(CreateCommandSpec(host, arguments, commandResolutionStrategy));
        }
Exemplo n.º 37
0
 private static void ResolveExecutablePath(ref string executable, ref string args, ref CommandResolutionStrategy resolutionStrategy, NuGetFramework framework = null)
 {
     executable =
         ResolveExecutablePathFromProject(executable, framework, ref resolutionStrategy) ??
         ResolveExecutableFromPath(executable, ref args, ref resolutionStrategy);
 }
Exemplo n.º 38
0
        private CommandSpec CreateCommandSpecUsingMuxerIfPortable(
            string commandPath, 
            IEnumerable<string> commandArgs, 
            string depsJsonFile, 
            CommandResolutionStrategy commandResolutionStrategy,
            string nugetPackagesRoot,
            bool isPortable)
        {
            var depsFileArguments = GetDepsFileArguments(depsJsonFile);
            var additionalProbingPathArguments = GetAdditionalProbingPathArguments();

            var muxerArgs = new List<string>();
            muxerArgs.Add("exec");
            muxerArgs.AddRange(depsFileArguments);
            muxerArgs.AddRange(additionalProbingPathArguments);
            muxerArgs.Add(commandPath);
            muxerArgs.AddRange(commandArgs);

            var escapedArgString = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(muxerArgs);

            return new CommandSpec(_muxer.MuxerPath, escapedArgString, commandResolutionStrategy);
        }
Exemplo n.º 39
0
        private static string ResolveExecutablePathFromProject(string executable, NuGetFramework framework, ref CommandResolutionStrategy resolutionStrategy)
        {
            if (framework == null)
            {
                return(null);
            }

            var projectRootPath = Directory.GetCurrentDirectory();

            if (!File.Exists(Path.Combine(projectRootPath, Project.FileName)))
            {
                return(null);
            }

            var commandName = Path.GetFileNameWithoutExtension(executable);

            var projectContext = ProjectContext.Create(projectRootPath, framework);

            var commandPackage = projectContext.LibraryManager.GetLibraries()
                                 .Where(l => l.GetType() == typeof(PackageDescription))
                                 .Select(l => l as PackageDescription)
                                 .FirstOrDefault(p =>
            {
                var fileNames = p.Library.Files
                                .Select(Path.GetFileName)
                                .Where(n => Path.GetFileNameWithoutExtension(n) == commandName)
                                .ToList();

                return(fileNames.Contains(commandName + FileNameSuffixes.DotNet.Exe) &&
                       fileNames.Contains(commandName + FileNameSuffixes.DotNet.DynamicLib) &&
                       fileNames.Contains(commandName + FileNameSuffixes.Deps));
            });

            if (commandPackage == null)
            {
                return(null);
            }

            var commandPath = commandPackage.Library.Files
                              .First(f => Path.GetFileName(f) == commandName + FileNameSuffixes.DotNet.Exe);

            resolutionStrategy = CommandResolutionStrategy.NugetPackage;

            return(Path.Combine(projectContext.PackagesDirectory, commandPackage.Path, commandPath));
        }
Exemplo n.º 40
0
        private static CommandSpec CreateCommandSpecPreferringExe(string commandName, string args, string commandPath,
            CommandResolutionStrategy resolutionStrategy)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
                Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
            {
                var preferredCommandPath = Env.GetCommandPath(commandName, ".exe");

                if (preferredCommandPath != null)
                {
                    commandPath = Environment.GetEnvironmentVariable("ComSpec");

                    args = $"/S /C \"\"{preferredCommandPath}\" {args}\"";
                }
            }

            return new CommandSpec(commandPath, args, resolutionStrategy);
        }
Exemplo n.º 41
0
 public CommandSpec(string path, string args, CommandResolutionStrategy resolutionStrategy)
 {
     Path = path;
     Args = args;
     ResolutionStrategy = resolutionStrategy;
 }
Exemplo n.º 42
0
 public CommandSpec(string path, string args, CommandResolutionStrategy resolutionStrategy)
 {
     Path = path;
     Args = args;
     ResolutionStrategy = resolutionStrategy;
 }
Exemplo n.º 43
0
 private static void ResolveExecutablePath(ref string executable, ref string args, ref CommandResolutionStrategy resolutionStrategy, NuGetFramework framework = null)
 {
     executable = 
         ResolveExecutablePathFromProject(executable, framework, ref resolutionStrategy) ??
         ResolveExecutableFromPath(executable, ref args, ref resolutionStrategy);
 }
Exemplo n.º 44
0
        private static CommandSpec CreateCommandSpecPreferringExe(
            string commandName,
            IEnumerable<string> args,
            string commandPath,
            CommandResolutionStrategy resolutionStrategy)
        {
            var useComSpec = false;
            
            if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows &&
                Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
            {
                var preferredCommandPath = Env.GetCommandPath(commandName, ".exe");

                // Use cmd if we can't find an exe
                if (preferredCommandPath == null)
                {
                    useComSpec = true;
                }
                else
                {
                    commandPath = preferredCommandPath;
                }
            }

            if (useComSpec)
            {
                return CreateCmdCommandSpec(commandPath, args, resolutionStrategy);
            }
            else
            {
                var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
                return new CommandSpec(commandPath, escapedArgs, resolutionStrategy);
            }
        }
Exemplo n.º 45
0
        private static string ResolveExecutablePathFromProject(string executable, NuGetFramework framework, ref CommandResolutionStrategy resolutionStrategy)
        {
            if (framework == null) return null;

            var projectRootPath = Directory.GetCurrentDirectory();

            if (!File.Exists(Path.Combine(projectRootPath, Project.FileName))) return null;

            var commandName = Path.GetFileNameWithoutExtension(executable);

            var projectContext = ProjectContext.Create(projectRootPath, framework);

            var commandPackage = projectContext.LibraryManager.GetLibraries()
                .Where(l => l.GetType() == typeof (PackageDescription))
                .Select(l => l as PackageDescription)
                .FirstOrDefault(p =>
                {
                    var fileNames = p.Library.Files
                        .Select(Path.GetFileName)
                        .Where(n => Path.GetFileNameWithoutExtension(n) == commandName)
                        .ToList();

                    return fileNames.Contains(commandName + FileNameSuffixes.DotNet.Exe) &&
                           fileNames.Contains(commandName + FileNameSuffixes.DotNet.DynamicLib) &&
                           fileNames.Contains(commandName + FileNameSuffixes.Deps);
                });

            if (commandPackage == null) return null;

            var commandPath = commandPackage.Library.Files
                .First(f => Path.GetFileName(f) == commandName + FileNameSuffixes.DotNet.Exe);

            resolutionStrategy = CommandResolutionStrategy.NugetPackage;

            return Path.Combine(projectContext.PackagesDirectory, commandPackage.Path, commandPath);
        }
Exemplo n.º 46
0
        private static string ResolveExecutableFromPath(string executable, ref string args, ref CommandResolutionStrategy resolutionStrategy)
        {
            resolutionStrategy = CommandResolutionStrategy.Path;

            foreach (string suffix in Constants.RunnableSuffixes)
            {
                var fullExecutable = Path.GetFullPath(Path.Combine(
                                                          AppContext.BaseDirectory, executable + suffix));

                if (File.Exists(fullExecutable))
                {
                    executable = fullExecutable;

                    resolutionStrategy = CommandResolutionStrategy.BaseDirectory;

                    // In priority order we've found the best runnable extension, so break.
                    break;
                }
            }

            // On Windows, we want to avoid using "cmd" if possible (it mangles the colors, and a bunch of other things)
            // So, do a quick path search to see if we can just directly invoke it
            var useCmd = ShouldUseCmd(executable);

            if (useCmd)
            {
                var comSpec = Environment.GetEnvironmentVariable("ComSpec");
                // wrap 'executable' within quotes to deal woth space in its path.
                args       = $"/S /C \"\"{executable}\" {args}\"";
                executable = comSpec;
            }

            return(executable);
        }
Exemplo n.º 47
0
        private CommandSpec CreatePackageCommandSpecUsingCorehost(
            string commandPath, 
            IEnumerable<string> commandArguments, 
            string depsFilePath,
            CommandResolutionStrategy commandResolutionStrategy)
        {
            var corehost = CoreHost.HostExePath;

            var arguments = new List<string>();
            arguments.Add(commandPath);

            if (depsFilePath != null)
            {
                arguments.Add($"--depsfile:{depsFilePath}");
            }

            arguments.AddRange(commandArguments);

            return CreateCommandSpec(corehost, arguments, commandResolutionStrategy);
        }
Exemplo n.º 48
0
        private CommandSpec CreateCommandSpec(
            string commandPath, 
            IEnumerable<string> commandArguments,
            CommandResolutionStrategy commandResolutionStrategy)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(commandArguments);

            return new CommandSpec(commandPath, escapedArgs, commandResolutionStrategy);
        }
Exemplo n.º 49
0
        private static CommandSpec CreateComSpecCommandSpec(
            string command, 
            IEnumerable<string> args, 
            CommandResolutionStrategy resolutionStrategy)
        {
            // To prevent Command Not Found, comspec gets passed in as
            // the command already in some cases
            var comSpec = Environment.GetEnvironmentVariable("ComSpec");
            if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
            {
                command = args.FirstOrDefault();
                args = args.Skip(1);
            }
            var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);

            if (ArgumentEscaper.ShouldSurroundWithQuotes(command))
            {
                command = $"\"{command}\"";
            }

            var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";

            return new CommandSpec(comSpec, escapedArgString, resolutionStrategy);
        }