public virtual int Execute() { // Ignore Ctrl-C for the remainder of the command's execution // Forwarding commands will just spawn the child process and exit Console.CancelKeyPress += (sender, e) => { e.Cancel = true; }; int exitCode; if (_forwardingAppWithoutLogging.ExecuteMSBuildOutOfProc) { ProcessStartInfo startInfo = GetProcessStartInfo(); PerformanceLogEventSource.Log.LogMSBuildStart(startInfo.FileName, startInfo.Arguments); exitCode = startInfo.Execute(); PerformanceLogEventSource.Log.MSBuildStop(exitCode); } else { string[] arguments = _forwardingAppWithoutLogging.GetAllArguments(); if (PerformanceLogEventSource.Log.IsEnabled()) { PerformanceLogEventSource.Log.LogMSBuildStart( _forwardingAppWithoutLogging.MSBuildPath, ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(arguments)); } exitCode = _forwardingAppWithoutLogging.ExecuteInProc(arguments); PerformanceLogEventSource.Log.MSBuildStop(exitCode); } return(exitCode); }
public int Execute() { var processInfo = new ProcessStartInfo { FileName = GetHostExeName(), Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_allArgs), UseShellExecute = false }; if (_environmentVariables != null) { foreach (var entry in _environmentVariables) { processInfo.Environment[entry.Key] = entry.Value; } } var process = new Process { StartInfo = processInfo }; process.Start(); process.WaitForExit(); return(process.ExitCode); }
public static int Main(string[] args) { string packageToolPath = Path.Combine(Path.GetDirectoryName(typeof(Program).GetTypeInfo().Assembly.Location), "tool", "package_tool"); EnsureExecutable(packageToolPath); var processInfo = new ProcessStartInfo() { FileName = packageToolPath, Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args), UseShellExecute = false }; Console.WriteLine($"dotnet-deb-tool: executing - {processInfo.FileName} {processInfo.Arguments}"); var process = new Process() { StartInfo = processInfo }; process.Start(); process.WaitForExit(); return(process.ExitCode); }
private static Command CreateInstallCommand(IEnumerable <string> args) { var path = ""; var finalArgs = ""; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { path = "powershell.exe"; finalArgs = "-ExecutionPolicy Bypass -NoProfile -NoLogo -Command \"" + Path.Combine(RepoDirectoriesProvider.RepoRoot, "scripts", "obtain", "dotnet-install.ps1") + " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args) + "\""; } else { path = Path.Combine(RepoDirectoriesProvider.RepoRoot, "scripts", "obtain", "dotnet-install.sh"); finalArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args); } var psi = new ProcessStartInfo { FileName = path, Arguments = finalArgs, UseShellExecute = false }; var _process = new Process { StartInfo = psi }; return(new Command(_process)); }
public int ExecuteTool(string[] args) { var muxer = new Muxer(); var toolDepsFile = Directory.EnumerateFiles(AppContext.BaseDirectory, "*.deps.json").Single(); var toolRuntimeConfig = Directory.EnumerateFiles(AppContext.BaseDirectory, "*.runtimeconfig.json").Single(); var dotnetArgs = new [] { "exec", "--runtimeconfig", toolRuntimeConfig, "--depsfile", toolDepsFile, _toolAssembly.Location }.Concat(args).ToArray(); var arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(dotnetArgs); var process = Process.Start(new ProcessStartInfo() { Arguments = arguments, FileName = muxer.MuxerPath, UseShellExecute = true, WorkingDirectory = Path.GetDirectoryName(_testProjectPath) }); process.WaitForExit(); return(process.ExitCode); }
private CommandSpec CreateNetCoreCommandSpec(ResolverArguments arguments) { Debug.Assert(!string.IsNullOrEmpty(arguments.RuntimeConfigJson), "RuntimeConfigJson is null or empty."); Debug.Assert(!string.IsNullOrEmpty(arguments.DepsJsonFile), "DepsJsonFile is null or empty."); var args = new List <string>(); args.Add("exec"); args.Add("--runtimeconfig"); args.Add(arguments.RuntimeConfigJson); args.Add("--depsfile"); args.Add(arguments.DepsJsonFile); if (!string.IsNullOrEmpty(arguments.NuGetPackageRoot)) { args.Add("--additionalprobingpath"); args.Add(arguments.NuGetPackageRoot); } var commandPath = Path.Combine(NetCoreToolDir, "ef" + FileNameSuffixes.DotNet.DynamicLib); args.Add(commandPath); args.AddRange(arguments.CommandArguments); var muxer = new Muxer(); var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args.OrEmptyIfNull()); return(new CommandSpec(muxer.MuxerPath, escapedArgs, CommandResolutionStrategy.ProjectToolsPackage)); }
private CommandSpec CreateCommandSpec( string commandPath, IEnumerable <string> commandArguments) { var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(commandArguments); return(new CommandSpec(commandPath, escapedArgs)); }
private CommandSpec CreateCommandSpecFromExecutable( string command, IEnumerable <string> args) { var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args); return(new CommandSpec(command, escapedArgs)); }
private void Restore(FileInfo projectFile) { var restoreArgs = new string[] { "restore", projectFile.FullName }; var commandResult = new DotnetCommand() .Execute(ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(restoreArgs)); commandResult.Should().Pass(); }
private ICommand GetTargetCommand() { var globalProperties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase) { // This property disables default item globbing to improve performance // This should be safe because we are not evaluating items, only properties { Constants.EnableDefaultItems, "false" }, { Constants.MSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } if (!string.IsNullOrWhiteSpace(Runtime)) { globalProperties.Add("RuntimeIdentifier", Runtime); } var project = new ProjectInstance(Project, globalProperties, null); string runProgram = project.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { ThrowUnableToRunError(project); } string runArguments = project.GetPropertyValue("RunArguments"); string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory"); if (Args.Any()) { runArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(Args); } CommandSpec commandSpec = new CommandSpec(runProgram, runArguments); var command = CommandFactoryUsingResolver.Create(commandSpec) .WorkingDirectory(runWorkingDirectory); var rootVariableName = Environment.Is64BitProcess ? "DOTNET_ROOT" : "DOTNET_ROOT(x86)"; if (Environment.GetEnvironmentVariable(rootVariableName) == null) { command.EnvironmentVariable(rootVariableName, Path.GetDirectoryName(new Muxer().MuxerPath)); } return(command); }
public CommandSpec CreateCommandSpec( string commandName, IEnumerable <string> args, string commandPath, IEnvironmentProvider environment) { var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args); return(new CommandSpec(commandPath, escapedArgs)); }
public CommandSpec Resolve(CommandResolverArguments commandResolverArguments) { if (commandResolverArguments.CommandName == Muxer.MuxerName) { var muxer = new Muxer(); var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart( commandResolverArguments.CommandArguments.OrEmptyIfNull()); return(new CommandSpec(muxer.MuxerPath, escapedArgs)); } return(null); }
private CommandSpec CreateDesktopCommandSpec(ResolverArguments arguments) { var exeName = RuntimeInformation.OSArchitecture == Architecture.X86 ? "ef.x86" + FileNameSuffixes.Windows.Exe : "ef" + FileNameSuffixes.Windows.Exe; var path = Path.Combine(DesktopToolDir, exeName); return(new CommandSpec(path, ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(arguments.CommandArguments.OrEmptyIfNull()), CommandResolutionStrategy.ProjectToolsPackage)); }
private async Task Nuget(params string[] args) { var pInfo = new ProcessStartInfo { Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args), FileName = await GetNugetExePath() }; Console.WriteLine("command: ".Bold().Blue() + pInfo.FileName); Console.WriteLine("arguments: ".Bold().Blue() + pInfo.Arguments); Process.Start(pInfo).WaitForExit(); }
private ICommand GetRunCommand() { var globalProperties = new Dictionary <string, string> { { Constants.MSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null); string runProgram = projectInstance.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { string outputType = projectInstance.GetPropertyValue("OutputType"); throw new GracefulException( string.Format( LocalizableStrings.RunCommandExceptionUnableToRun, "dotnet run", "OutputType", outputType)); } string runArguments = projectInstance.GetPropertyValue("RunArguments"); string runWorkingDirectory = projectInstance.GetPropertyValue("RunWorkingDirectory"); string fullArguments = runArguments; if (_args.Any()) { fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args); } CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
private CommandResult RunProcess(string executable, string[] args, StreamForwarder stdOut, StreamForwarder stdErr) { var psi = new ProcessStartInfo { FileName = executable, Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args), RedirectStandardError = true, RedirectStandardOutput = true }; Log("Executing: ".Bold().Blue() + $"{psi.FileName} {psi.Arguments}"); foreach (var item in Environment) { psi.Environment[item.Key] = item.Value; } if (!string.IsNullOrWhiteSpace(WorkingDirectory)) { psi.WorkingDirectory = WorkingDirectory; Log($"Working directory: {WorkingDirectory}"); } var process = new Process { StartInfo = psi, EnableRaisingEvents = true }; using (process) { process.Start(); var threadOut = stdOut.BeginRead(process.StandardOutput); var threadErr = stdErr.BeginRead(process.StandardError); process.WaitForExit(); Task.WaitAll(threadOut, threadErr); var result = new CommandResult( process.StartInfo, process.ExitCode, stdOut.CapturedOutput, stdErr.CapturedOutput); return(result); } }
private ICommand GetTargetCommand() { var globalProperties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase) { // This property disables default item globbing to improve performance // This should be safe because we are not evaluating items, only properties { Constants.EnableDefaultItems, "false" }, { Constants.MSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } if (!string.IsNullOrWhiteSpace(Runtime)) { globalProperties.Add("RuntimeIdentifier", Runtime); } var project = new ProjectInstance(Project, globalProperties, null); string runProgram = project.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { ThrowUnableToRunError(project); } string runArguments = project.GetPropertyValue("RunArguments"); string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory"); if (Args.Any()) { runArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(Args); } CommandSpec commandSpec = new CommandSpec(runProgram, runArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
private ICommand GetRunCommand() { Dictionary <string, string> globalProperties = new Dictionary <string, string>() { { LocalizableStrings.RunCommandMSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add(LocalizableStrings.RunCommandConfiguration, Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add(LocalizableStrings.RunCommandTargetFramework, Framework); } ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null); string runProgram = projectInstance.GetPropertyValue(LocalizableStrings.RunCommandProjectInstance); if (string.IsNullOrEmpty(runProgram)) { string outputType = projectInstance.GetPropertyValue(LocalizableStrings.RunCommandOutputType); throw new GracefulException(string.Join(Environment.NewLine, LocalizableStrings.RunCommandExceptionUnableToRun1, LocalizableStrings.RunCommandExceptionUnableToRun2, $"{LocalizableStrings.RunCommandExceptionUnableToRun3} '{outputType}'.")); } string runArguments = projectInstance.GetPropertyValue(LocalizableStrings.RunCommandRunArguments); string runWorkingDirectory = projectInstance.GetPropertyValue(LocalizableStrings.RunCommandRunWorkingDirectory); string fullArguments = runArguments; if (_args.Any()) { fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args); } CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
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); }
private static Command CreateCommand(string path, IEnumerable <string> args) { var psi = new ProcessStartInfo { FileName = path, Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args), UseShellExecute = false }; var _process = new Process { StartInfo = psi }; return(new Command(_process)); }
private ICommand GetRunCommand() { Dictionary <string, string> globalProperties = new Dictionary <string, string>() { { "MSBuildExtensionsPath", AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null); string runProgram = projectInstance.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { string outputType = projectInstance.GetPropertyValue("OutputType"); throw new GracefulException(string.Join(Environment.NewLine, "Unable to run your project.", "Please ensure you have a runnable project type and ensure 'dotnet run' supports this project.", $"The current OutputType is '{outputType}'.")); } string runArguments = projectInstance.GetPropertyValue("RunArguments"); string runWorkingDirectory = projectInstance.GetPropertyValue("RunWorkingDirectory"); string fullArguments = runArguments; if (_args.Any()) { fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args); } CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
public ProcessStartInfo GetProcessStartInfo() { var processInfo = new ProcessStartInfo { FileName = GetHostExeName(), Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_allArgs), UseShellExecute = false }; if (_environmentVariables != null) { foreach (var entry in _environmentVariables) { processInfo.Environment[entry.Key] = entry.Value; } } return(processInfo); }
private static Command CreateInstallCommand(IEnumerable <string> args) { string path; string finalArgs; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { path = "powershell.exe"; finalArgs = "-ExecutionPolicy Bypass -NoProfile -NoLogo -Command \"" + Path.Combine(GetRepoRoot(), "src", "dotnet-install.ps1") + " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args) + "\""; } else { path = Path.Combine(GetRepoRoot(), "src", "dotnet-install.sh"); finalArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args); } return(Command.Create(new CommandSpec(path, finalArgs, CommandResolutionStrategy.None))); }
private async Task Nuget(params string[] args) { var pInfo = new ProcessStartInfo { Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args), FileName = await GetNugetExePath() }; Console.WriteLine("command: ".Bold().Blue() + pInfo.FileName); Console.WriteLine("arguments: ".Bold().Blue() + pInfo.Arguments); var p = Process.Start(pInfo); p.WaitForExit(); if (p.ExitCode != 0) { throw new InvalidOperationException("nuget.exe command returned non-zero exit code: " + p.ExitCode); } }
private ICommand GetRunCommand() { var globalProperties = new Dictionary <string, string> { { Constants.MSBuildExtensionsPath, AppContext.BaseDirectory } }; if (!string.IsNullOrWhiteSpace(Configuration)) { globalProperties.Add("Configuration", Configuration); } if (!string.IsNullOrWhiteSpace(Framework)) { globalProperties.Add("TargetFramework", Framework); } Project project = new Project(Project, globalProperties, null); string runProgram = project.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { ThrowUnableToRunError(project); } string runArguments = project.GetPropertyValue("RunArguments"); string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory"); string fullArguments = runArguments; if (_args.Any()) { fullArguments += " " + ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(_args); } CommandSpec commandSpec = new CommandSpec(runProgram, fullArguments, CommandResolutionStrategy.None); return(Command.Create(commandSpec) .WorkingDirectory(runWorkingDirectory)); }
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)); }
public CommandSpec Resolve(CommandResolverArguments commandResolverArguments) { if (commandResolverArguments.CommandName == null) { return(null); } if (commandResolverArguments.CommandName.EndsWith(FileNameSuffixes.DotNet.DynamicLib)) { var localPath = Path.Combine(AppContext.BaseDirectory, 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); }
private Process CreateProcess(ProcessSpec processSpec) { var process = new Process { EnableRaisingEvents = true, StartInfo = { FileName = processSpec.Executable, Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(processSpec.Arguments), UseShellExecute = false, WorkingDirectory = processSpec.WorkingDirectory, RedirectStandardOutput = processSpec.IsOutputCaptured || (processSpec.OnOutput != null), RedirectStandardError = processSpec.IsOutputCaptured, } }; foreach (var env in processSpec.EnvironmentVariables) { process.StartInfo.Environment.Add(env.Key, env.Value); } return(process); }
public void EscapesOnlyArgsWithSpecialCharacters() { var args = new[] { "subcommand", "--not-escaped", "1.0.0-prerelease.21165.2", "--with-space", "/mnt/d/Program Files", "--already-escaped", "\"some value\"", "containing-\"-quote", }; string escaped = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args); escaped.Should().Be( "subcommand " + "--not-escaped 1.0.0-prerelease.21165.2 " + "--with-space \"/mnt/d/Program Files\" " + "--already-escaped \"some value\" " + "\"containing-\\\"-quote\""); }
private string EscapeArgs() { // Note: this doesn't handle invoking .cmd files via "cmd /c" on Windows, which probably won't be necessary here // If it is, refer to the code in WindowsExePreferredCommandSpecFactory in Microsoft.DotNet.Cli.Utils return(ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(Arguments)); }