Skip to content

Commit

Permalink
Merge pull request #809 from filipw/features/global-omnisharp.json
Browse files Browse the repository at this point in the history
Added support for a global omnisharp.json
  • Loading branch information
DustinCampbell authored Mar 31, 2017
2 parents 1aaad81 + 648366e commit 3c3a8e5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IOmniSharpEnvironment
int HostPID { get; }
string Path { get; }
string SolutionFilePath { get; }
string SharedDirectoryPath { get; }
TransportType TransportType { get; }
string[] OtherArgs { get; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.Host/Internal/CommandOptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ internal static T GetValueOrDefault<T>(this CommandOption opt, T defaultValue)
return defaultValue;
}
}
}
}
36 changes: 36 additions & 0 deletions src/OmniSharp.Host/Internal/ConfigurationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using OmniSharp.Services;
using System;
using System.IO;

namespace OmniSharp.Host.Internal
{
internal static class ConfigurationBuilderExtensions
{
internal static void CreateAndAddGlobalOptionsFile(this IConfigurationBuilder configBuilder, IOmniSharpEnvironment env)
{
if (env?.SharedDirectoryPath == null) return;

try
{
if (!Directory.Exists(env.SharedDirectoryPath))
{
Directory.CreateDirectory(env.SharedDirectoryPath);
}

var omnisharpGlobalFilePath = Path.Combine(env.SharedDirectoryPath, Constants.OptionsFile);
configBuilder.AddJsonFile(
new PhysicalFileProvider(env.SharedDirectoryPath),
Constants.OptionsFile,
optional: true,
reloadOnChange: true);
}
catch (Exception e)
{
// at this point we have no ILogger yet
Console.Error.WriteLine($"There was an error when trying to create a global '{Constants.OptionsFile}' file in '{env.SharedDirectoryPath}'. {e.ToString()}");
}
}
}
}
8 changes: 8 additions & 0 deletions src/OmniSharp.Host/Internal/OmniSharpConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OmniSharp.Host.Internal
{
internal static class Constants
{
internal const string ConfigFile = "config.json";
internal const string OptionsFile = "omnisharp.json";
}
}
11 changes: 11 additions & 0 deletions src/OmniSharp.Host/Services/OmniSharpEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class OmniSharpEnvironment : IOmniSharpEnvironment
{
public string Path { get; }
public string SolutionFilePath { get; }
public string SharedDirectoryPath { get; }
public int Port { get; }
public int HostPID { get; }
public LogLevel TraceType { get; }
Expand Down Expand Up @@ -38,6 +39,16 @@ public OmniSharpEnvironment(
TraceType = traceType;
TransportType = transportType;
OtherArgs = otherArgs;

// On Windows: %APPDATA%\.omnisharp\omnisharp.json
// On Mac/Linux: ~/.omnisharp/omnisharp.json
var root = Environment.GetEnvironmentVariable("USERPROFILE") ??
Environment.GetEnvironmentVariable("HOME");

if (root != null)
{
SharedDirectoryPath = System.IO.Path.Combine(root, ".omnisharp");
}
}
}
}
8 changes: 6 additions & 2 deletions src/OmniSharp.Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OmniSharp.Host.Internal;
using OmniSharp.Mef;
using OmniSharp.Middleware;
using OmniSharp.Options;
Expand All @@ -36,18 +37,21 @@ public Startup(IOmniSharpEnvironment env)

var configBuilder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("config.json", optional: true)
.AddJsonFile(Constants.ConfigFile, optional: true)
.AddEnvironmentVariables();

if (env.OtherArgs?.Length > 0)
{
configBuilder.AddCommandLine(env.OtherArgs);
}

// Use the global omnisharp config if there's any in the shared path
configBuilder.CreateAndAddGlobalOptionsFile(env);

// Use the local omnisharp config if there's any in the root path
configBuilder.AddJsonFile(
new PhysicalFileProvider(env.Path),
"omnisharp.json",
Constants.OptionsFile,
optional: true,
reloadOnChange: true);

Expand Down

0 comments on commit 3c3a8e5

Please sign in to comment.