Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Oct 24, 2017
2 parents 167befa + ca60d0e commit e63dde8
Showing 1 changed file with 47 additions and 43 deletions.
90 changes: 47 additions & 43 deletions src/Paket.VisualStudio/Utils/PaketLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit e63dde8

Please sign in to comment.