From ca60d0e63926f57ddd6b2a6b6b0dff4d1cfc5246 Mon Sep 17 00:00:00 2001 From: Chris Blyth Date: Tue, 24 Oct 2017 12:35:44 +0100 Subject: [PATCH] Further fix for #152 Original fix missed the way the paket process was started so results in a file not found exception on process start. --- src/Paket.VisualStudio/Utils/PaketLauncher.cs | 90 ++++++++++--------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/src/Paket.VisualStudio/Utils/PaketLauncher.cs b/src/Paket.VisualStudio/Utils/PaketLauncher.cs index 0130cc2ff..59e8b38e6 100644 --- a/src/Paket.VisualStudio/Utils/PaketLauncher.cs +++ b/src/Paket.VisualStudio/Utils/PaketLauncher.cs @@ -7,59 +7,63 @@ namespace Paket.VisualStudio.Utils public static class PaketLauncher { - const string PAKET_EXE = ".paket\\paket.exe"; - const string PAKET_BOOTSTRAPPER_EXE = ".paket\\paket.bootstrapper.exe"; - const string PAKET_RELEASE_URL = "https://github.com/fsprojects/Paket/releases"; - const string PAKET_GETTING_STARTED_URL = "https://fsprojects.github.io/Paket/getting-started.html"; - - private static int LaunchProcess(string SolutionDirectory, string FileName, string PaketSubCommand, DataReceivedEventHandler PaketDataReceivedHandler) + private static void LaunchProcess(string WorkingDirectory, string ProcessStart, string PaketSubCommand, DataReceivedEventHandler PaketDataReceivedHandler) { - Process process = new Process(); - process.StartInfo.FileName = SolutionDirectory + FileName; - process.StartInfo.Arguments = PaketSubCommand; - process.StartInfo.UseShellExecute = false; - process.StartInfo.WorkingDirectory = SolutionDirectory; - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.RedirectStandardError = true; - process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - process.StartInfo.CreateNoWindow = true; - process.OutputDataReceived += PaketDataReceivedHandler; - process.ErrorDataReceived += PaketDataReceivedHandler; - process.Start(); - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); - process.WaitForExit(); - return process.ExitCode; + try + { + var process = new Process(); + process.StartInfo.FileName = ProcessStart; + process.StartInfo.Arguments = PaketSubCommand; + process.StartInfo.UseShellExecute = false; + process.StartInfo.WorkingDirectory = WorkingDirectory; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.RedirectStandardError = true; + process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + process.StartInfo.CreateNoWindow = true; + process.OutputDataReceived += PaketDataReceivedHandler; + process.ErrorDataReceived += PaketDataReceivedHandler; + process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + process.WaitForExit(); + + if (process.ExitCode != 0) + { + throw new Exception($"Exit code '{process.ExitCode}' returned"); + } + } + catch (Exception e) + { + throw new PaketRuntimeException($"Error running: {ProcessStart} {PaketSubCommand}\n{e.Message}", e); + } + } public static void LaunchPaket(string SolutionDirectory, string PaketSubCommand, DataReceivedEventHandler PaketDataReceivedHandler) { - var paketLocation = Path.Combine(SolutionDirectory, PAKET_EXE); - var paketBootstrapLocation = Path.Combine(SolutionDirectory, PAKET_BOOTSTRAPPER_EXE); + //No error handling, let errors from LaunchProcess bubble! + var PaketLocation = Path.Combine(SolutionDirectory, ".paket", "paket.exe"); + var PaketBootstrapLocation = Path.Combine(SolutionDirectory, ".paket", "paket.bootstrapper.exe"); - if (!File.Exists(paketLocation)) + if (!File.Exists(PaketLocation)) { - //If .paket\paket.exe is not found under the solution dir, try launching paket.bootstrapper.exe - if (File.Exists(paketBootstrapLocation)) + if (!File.Exists(PaketBootstrapLocation)) { - int ExitCode = LaunchProcess(SolutionDirectory, PAKET_BOOTSTRAPPER_EXE, "", PaketDataReceivedHandler); - if (ExitCode != 0) - /* If something went wrong with paket.bootstrapper.exe e.g. couldn't download paket.exe due to proxy auth error - * then we should not execute the command that was originally issued for paket.exe. - */ - throw new PaketRuntimeException("paket.bootstrapper.exe terminated abnormally."); - } - else + //Don't have .paket\paket.exe or paket.bootstrapper.exe throw new FileNotFoundException( - @"Could not locate .paket\paket.exe and .paket\paket.bootstrapper.exe under the solution " + SolutionDirectory - + "\nTo download the binaries, visit " + PAKET_RELEASE_URL - + "\nTo know more about Paket, visit " + PAKET_GETTING_STARTED_URL + "\n"); + $@"Could not locate .paket\paket.exe and .paket\paket.bootstrapper.exe under the folder '{SolutionDirectory}'" + + $"\nTo download the binaries, visit https://github.com/fsprojects/Paket/releases" + + $"\nTo know more about Paket, visit https://fsprojects.github.io/Paket/getting-started.html\n"); + } + + //Try and get the .paket\paket.exe using paket.bootstrapper.exe + //If something went wrong with paket.bootstrapper.exe e.g. couldn't download paket.exe due to proxy auth error + //then we should not execute the command that was originally issued for paket.exe. + LaunchProcess(SolutionDirectory, PaketBootstrapLocation, "", PaketDataReceivedHandler); } - /* At this point, all is well .paket\paket.exe exists or .paket\paket.bootstrapper.exe downloaded it successfully. - * Now issue the original command to .paket\paket.exe - */ - if (LaunchProcess(SolutionDirectory, PAKET_EXE, PaketSubCommand, PaketDataReceivedHandler) != 0) - throw new PaketRuntimeException($"{PAKET_EXE} {PaketSubCommand} failed"); + //At this point, all is well .paket\paket.exe exists or .paket\paket.bootstrapper.exe downloaded it successfully. + //Now issue the original command to .paket\paket.exe + LaunchProcess(SolutionDirectory, PaketLocation, PaketSubCommand, PaketDataReceivedHandler); } }