Skip to content

Commit

Permalink
Fix (code) editor launch on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz authored and kzu committed Dec 21, 2020
1 parent cc3b203 commit 5a5e802
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Config.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Mono.Options;

namespace DotNetConfig
Expand Down Expand Up @@ -301,8 +302,20 @@ static int Run(string[] args)
case ConfigAction.Edit:
if (!Config.Build(path).TryGetString("config", null, "editor", out var editor))
{
var cmd = Environment.OSVersion.Platform == PlatformID.Unix ? "which" : "where";
editor = Process.Start(new ProcessStartInfo(cmd, "code") { RedirectStandardOutput = true })?.StandardOutput.ReadLine() ?? "";
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var cmd = isWindows ? "where" : "which";
var psi = new ProcessStartInfo(cmd)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
};
var extensions = isWindows && Environment.GetEnvironmentVariable("PATHEXT") is { } v
? v.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries)
: Array.Empty<string>();
foreach (var extension in extensions.DefaultIfEmpty(""))
psi.ArgumentList.Add("code" + extension);
using var process = Process.Start(psi)!;
editor = process.StandardOutput.ReadLine() ?? "";
}

var configFile = config.FilePath;
Expand Down

0 comments on commit 5a5e802

Please sign in to comment.