static void TryRestorePackage(NuGetRepository repository, NuGetConfig config) { int maxAttempts = 3; bool succeeded = false; int attempt = 1; while (!succeeded && attempt <= maxAttempts) { try { RestorePackage(repository, config); succeeded = true; } catch (Exception ex) { if (ex.IsFatal() || attempt == maxAttempts) { throw; } Thread.Sleep(TimeSpan.FromMilliseconds(200)); } } }
static void RestorePackage(NuGetRepository repository, NuGetConfig config) { CastaneaLogger.WriteDebug( $"Installing packages into directory '{config.OutputDirectory}', defined in '{repository.Path}'"); string args = $"restore \"{repository.Path}\" -PackagesDirectory \"{config.OutputDirectory}\" -NonInteractive"; if (CastaneaLogger.DebugLog != null) { args += " -Verbosity Detailed"; } if (config.DisableParallelProcessing) { args += " -DisableParallelProcessing"; } if (config.NoCache) { args += " -NoCache"; } CastaneaLogger.WriteDebug($"Running exe '{config.NuGetExePath}' with arguments: {args}"); Process process = new Process { StartInfo = new ProcessStartInfo(config.NuGetExePath) { Arguments = args, RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false } }; process.OutputDataReceived += (sender, eventArgs) => { if (!string.IsNullOrWhiteSpace(eventArgs.Data)) { CastaneaLogger.WriteDebug(eventArgs.Data); } }; process.ErrorDataReceived += (sender, eventArgs) => { if (!string.IsNullOrWhiteSpace(eventArgs.Data)) { CastaneaLogger.WriteError(eventArgs.Data); } }; process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); process.WaitForExit(); int exitCode = process.ExitCode; if (exitCode == 0) { CastaneaLogger.WriteDebug($"Successfully installed packages in '{repository.Path}'"); } else { throw new InvalidOperationException( $"Failed to install packages in '{repository.Path}'. The process '{process.StartInfo.FileName}' exited with code {exitCode}"); } }