Exemplo n.º 1
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.º 2
0
        private CommandSpec CreateCommandSpec(
            string commandPath,
            IEnumerable <string> commandArguments)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(commandArguments);

            return(new CommandSpec(commandPath, escapedArgs));
        }
Exemplo n.º 3
0
        private CommandSpec CreateCommandSpecFromExecutable(
            string command,
            IEnumerable <string> args)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);

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

            return new CommandSpec(commandPath, escapedArgs, commandResolutionStrategy);
        }
Exemplo n.º 5
0
        public CommandSpec CreateCommandSpec(
            string commandName,
            IEnumerable <string> args,
            string commandPath,
            IEnvironmentProvider environment)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);

            return(new CommandSpec(commandPath, escapedArgs));
        }
Exemplo n.º 6
0
        private static CommandSpec ResolveFromRootedCommand(string commandName, IEnumerable <string> args)
        {
            if (Path.IsPathRooted(commandName))
            {
                var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
                return(new CommandSpec(commandName, escapedArgs, CommandResolutionStrategy.Path));
            }

            return(null);
        }
Exemplo n.º 7
0
        public ProcessStartInfo GetProcessStartInfo()
        {
            var processInfo = new ProcessStartInfo
            {
                FileName        = GetHostExeName(),
                Arguments       = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_allArgs),
                UseShellExecute = false
            };

            foreach (var entry in _environmentVariables)
            {
                processInfo.Environment[entry.Key] = entry.Value;
            }

            return(processInfo);
        }
Exemplo n.º 8
0
        public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
        {
            if (commandResolverArguments.CommandName == null)
            {
                return(null);
            }

            if (Path.IsPathRooted(commandResolverArguments.CommandName))
            {
                var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(
                    commandResolverArguments.CommandArguments.OrEmptyIfNull());

                return(new CommandSpec(commandResolverArguments.CommandName, escapedArgs));
            }

            return(null);
        }
Exemplo n.º 9
0
        private static CommandSpec ConfigureCommandFromPackage(string commandName, IEnumerable <string> args,
                                                               IEnumerable <string> files, string packageDir, string depsPath = null, bool useComSpec = false)
        {
            var fileName = string.Empty;

            var commandPath = files
                              .FirstOrDefault(f => Env.ExecutableExtensions.Contains(Path.GetExtension(f)));

            if (commandPath == null)
            {
                var dllPath = files
                              .Where(f => Path.GetFileName(f) == commandName + FileNameSuffixes.DotNet.DynamicLib)
                              .Select(f => Path.Combine(packageDir, f))
                              .FirstOrDefault();

                fileName = CoreHost.HostExePath;

                var additionalArgs = new List <string>();
                additionalArgs.Add(dllPath);

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

                args = additionalArgs.Concat(args);
            }
            else
            {
                fileName = Path.Combine(packageDir, commandPath);
            }

            if (useComSpec)
            {
                return(CreateComSpecCommandSpec(fileName, args, CommandResolutionStrategy.NugetPackage));
            }
            else
            {
                var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
                return(new CommandSpec(fileName, escapedArgs, CommandResolutionStrategy.NugetPackage));
            }
        }
Exemplo n.º 10
0
        private CommandSpec CreateCommandSpecUsingMuxerIfPortable(
            string commandPath,
            IEnumerable <string> commandArgs,
            string depsJsonFile,
            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));
        }
Exemplo n.º 11
0
 public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
 {
     if (commandResolverArguments.CommandName == null)
     {
         return(null);
     }
     if (commandResolverArguments.CommandName.EndsWith(FileNameSuffixes.DotNet.DynamicLib))
     {
         var localPath = Path.Combine(ApplicationEnvironment.ApplicationBasePath,
                                      commandResolverArguments.CommandName);
         if (File.Exists(localPath))
         {
             var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(
                 new[] { localPath }
                 .Concat(commandResolverArguments.CommandArguments.OrEmptyIfNull()));
             return(new CommandSpec(
                        new Muxer().MuxerPath,
                        escapedArgs));
         }
     }
     return(null);
 }
Exemplo n.º 12
0
        internal void GenerateDepsJsonFile(
            LockFile toolLockFile,
            NuGetFramework framework,
            string depsPath,
            SingleProjectInfo toolLibrary,
            string toolDepsJsonGeneratorProject)
        {
            if (string.IsNullOrEmpty(toolDepsJsonGeneratorProject) ||
                !File.Exists(toolDepsJsonGeneratorProject))
            {
                throw new GracefulException(LocalizableStrings.DepsJsonGeneratorProjectNotSet);
            }

            Reporter.Verbose.WriteLine(string.Format(
                                           LocalizableStrings.GeneratingDepsJson,
                                           depsPath));

            var tempDepsFile = Path.GetTempFileName();

            var args = new List <string>();

            args.Add(toolDepsJsonGeneratorProject);
            args.Add($"-property:ProjectAssetsFile=\"{toolLockFile.Path}\"");
            args.Add($"-property:ToolName={toolLibrary.Name}");
            args.Add($"-property:ProjectDepsFilePath={tempDepsFile}");

            var toolTargetFramework = toolLockFile.Targets.First().TargetFramework.GetShortFolderName();

            args.Add($"-property:TargetFramework={toolTargetFramework}");


            //  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 = toolLockFile.Targets
                                  .FirstOrDefault(t => framework == t.TargetFramework)
                                  ?.GetPlatformLibrary();

            if (platformLibrary != null)
            {
                string buildRelativePath = platformLibrary.Build.FirstOrDefault()?.Path;

                var platformLibraryPath = toolLockFile.GetPackageDirectory(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($"-property:AdditionalImport={platformLibraryPropsFile}");
                    }
                }
            }

            //  Delete temporary file created by Path.GetTempFileName(), otherwise the GenerateBuildDependencyFile target
            //  will think the deps file is up-to-date and skip executing
            File.Delete(tempDepsFile);

            var msBuildExePath = _environment.GetEnvironmentVariable(Constants.MSBUILD_EXE_PATH);

            msBuildExePath = string.IsNullOrEmpty(msBuildExePath) ?
                             Path.Combine(AppContext.BaseDirectory, "MSBuild.dll") :
                             msBuildExePath;

            Reporter.Verbose.WriteLine(string.Format(LocalizableStrings.MSBuildArgs,
                                                     ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args)));

            var result = new MSBuildForwardingAppWithoutLogging(args, msBuildExePath)
                         .GetProcessStartInfo()
                         .ExecuteAndCaptureOutput(out string stdOut, out string stdErr);

            if (result != 0)
            {
                Reporter.Verbose.WriteLine(string.Format(
                                               LocalizableStrings.UnableToGenerateDepsJson,
                                               stdOut + Environment.NewLine + stdErr));

                throw new GracefulException(string.Format(LocalizableStrings.UnableToGenerateDepsJson, toolDepsJsonGeneratorProject));
            }

            try
            {
                File.Move(tempDepsFile, depsPath);
            }
            catch (Exception e)
            {
                Reporter.Verbose.WriteLine(string.Format(
                                               LocalizableStrings.UnableToGenerateDepsJson,
                                               e.Message));

                try
                {
                    File.Delete(tempDepsFile);
                }
                catch (Exception e2)
                {
                    Reporter.Verbose.WriteLine(string.Format(
                                                   LocalizableStrings.UnableToDeleteTemporaryDepsJson,
                                                   e2.Message));
                }
            }
        }