Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Cody.Core/Cody.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
<Compile Include="Agent\Protocol\ResolveWebviewViewParams.cs" />
<Compile Include="Agent\SetHtmlEvent.cs" />
<Compile Include="Agent\WebviewMessageHandler.cs" />
<Compile Include="Common\Configuration.cs" />
<Compile Include="Common\Configuration.Setup.cs" />
<Compile Include="DocumentSync\DocumentSyncCallback.cs" />
<Compile Include="DocumentSync\IDocumentSyncActions.cs" />
<Compile Include="Infrastructure\ConfigurationService.cs" />
Expand Down
60 changes: 60 additions & 0 deletions src/Cody.Core/Common/Configuration.Setup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Cody.Core.Common
{
public static partial class Configuration
{
private static Dictionary<string, object> config = new Dictionary<string, object>();

public static void AddFromJsonFile(string path)
{
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
try
{
var json = File.ReadAllText(path);
var configuration = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
AddFromDictionary(configuration);
}
catch { }
}
}

public static void AddFromEnviromentVariableJsonFile(string variable)
{
var path = Environment.GetEnvironmentVariable(variable);
AddFromJsonFile(path);
}

public static void AddFromDictionary(Dictionary<string, object> dictionary)
{
if (dictionary == null) return;

foreach (var pair in dictionary)
{
if (config.ContainsKey(pair.Key)) config[pair.Key] = pair.Value;
else config.Add(pair.Key, pair.Value);
}
}

private static T Get<T>(T defaultValue, [CallerMemberName] string key = null) => (T)(config.TryGetValue(key, out object value) ? value : defaultValue);


public static bool IsDebug
{
get
{
#if DEBUG
return true;
#else
return false;
#endif
}
}
}
}
31 changes: 31 additions & 0 deletions src/Cody.Core/Common/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;

namespace Cody.Core.Common
{
public static partial class Configuration
{

public static bool AgentDebug => Get(false);

public static bool AgentVerboseDebug => Get(false);

public static bool AllowNodeDebug => Get(false);

public static string AgentDirectory => Get((string)null);

public static int? RemoteAgentPort => Get((int?)null);

public static bool Trace => Get(false);

public static string TraceFile => Get((string)null);

public static string TraceLogioHostname => Get((string)null);

public static int? TraceLogioPort => Get((int?)null);

public static bool ShowCodyAgentOutput => Get(false);

public static bool ShowCodyNotificationsOutput => Get(false);
}
}
5 changes: 3 additions & 2 deletions src/Cody.Core/Infrastructure/ConfigurationService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cody.Core.Agent.Protocol;
using Cody.Core.Common;
using Cody.Core.Ide;
using Cody.Core.Inf;
using Cody.Core.Logging;
Expand Down Expand Up @@ -71,8 +72,8 @@ public ExtensionConfiguration GetConfiguration()
Proxy = null,
AccessToken = _userSettingsService.AccessToken,
AutocompleteAdvancedProvider = null,
Debug = true,
VerboseDebug = true,
Debug = Configuration.AgentDebug,
VerboseDebug = Configuration.AgentVerboseDebug,
CustomConfiguration = GetCustomConfiguration()
};

Expand Down
31 changes: 17 additions & 14 deletions src/Cody.Core/Logging/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Cody.Core.Common;
using System;
using System.IO;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -27,24 +28,26 @@ public void Info(string message, [CallerMemberName] string callerName = "")

public void Debug(string message, [CallerMemberName] string callerName = "", [CallerFilePath] string callerFilePath = null)
{
#if DEBUG
var callerTypeName = Path.GetFileNameWithoutExtension(callerFilePath);
callerName = $"{callerTypeName}.{callerName}";
var customMessage = FormatCallerName(message, callerName);

// TODO: _fileLogger.Debug(customMessage);
DebugWrite(customMessage);
_outputWindowPane?.Debug(message, callerName);
_testLogger?.WriteLog(message, "DEBUG", callerName);
#endif
if (Configuration.IsDebug)
{
var callerTypeName = Path.GetFileNameWithoutExtension(callerFilePath);
callerName = $"{callerTypeName}.{callerName}";
var customMessage = FormatCallerName(message, callerName);

// TODO: _fileLogger.Debug(customMessage);
DebugWrite(customMessage);
_outputWindowPane?.Debug(message, callerName);
_testLogger?.WriteLog(message, "DEBUG", callerName);
}
}

public void Debug(string message, Exception ex, [CallerMemberName] string callerName = "", [CallerFilePath] string callerFilePath = null)
{
#if DEBUG
Debug(message, callerName, callerFilePath);
Error(message, ex, callerName);
#endif
if (Configuration.IsDebug)
{
Debug(message, callerName, callerFilePath);
Error(message, ex, callerName);
}
}

public void Warn(string message, [CallerMemberName] string callerName = "")
Expand Down
5 changes: 2 additions & 3 deletions src/Cody.Core/Logging/SentryLog.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Cody.Core.Common;
using Sentry;
using System;
using System.Diagnostics;
Expand All @@ -19,8 +20,7 @@ public void Error(string message)

public static void Initialize()
{
#if !DEBUG
if (!Debugger.IsAttached)
if (!Configuration.IsDebug && !Debugger.IsAttached)
{
var version = Assembly.GetExecutingAssembly().GetName().Version;
string env = "dev";
Expand All @@ -34,7 +34,6 @@ public static void Initialize()
options.Release = "cody-vs@" + version.ToString();
});
}
#endif
}
}
}
15 changes: 6 additions & 9 deletions src/Cody.UI/Controls/WebView2Dev.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Windows.Controls;
using System.Windows.Input;
using Cody.Core.Logging;
using Cody.Core.Common;

namespace Cody.UI.Controls
{
Expand Down Expand Up @@ -84,18 +85,14 @@ private async Task<CoreWebView2Environment> CreateWebView2Environment()
try
{
Logger?.Debug("Initializing ...");
var codyDir = "Cody";
#if DEBUG
codyDir = "Cody\\Debug";
#endif
var codyDir = Configuration.IsDebug ? "Cody\\Debug" : "Cody";

var appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), codyDir);
var options = new CoreWebView2EnvironmentOptions
{
#if DEBUG
AdditionalBrowserArguments =
"--remote-debugging-port=9222 --disable-web-security --allow-file-access-from-files",
AllowSingleSignOnUsingOSPrimaryAccount = true,
#endif
AdditionalBrowserArguments = Configuration.IsDebug ?
"--remote-debugging-port=9222 --disable-web-security --allow-file-access-from-files" : null,
AllowSingleSignOnUsingOSPrimaryAccount = Configuration.IsDebug,
};

var webView2 = await CoreWebView2Environment.CreateAsync(null, appData, options);
Expand Down
9 changes: 3 additions & 6 deletions src/Cody.UI/Controls/WebviewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows;
using System.Windows.Input;
using Cody.Core.Logging;
using Cody.Core.Common;

namespace Cody.UI.Controls
{
Expand Down Expand Up @@ -47,13 +48,9 @@ private void ConfigureWebView()
_webview.Settings.AreBrowserAcceleratorKeysEnabled = true;
_webview.Settings.IsGeneralAutofillEnabled = true;
// Enable below settings only in DEBUG mode.
_webview.Settings.AreDefaultContextMenusEnabled = false;
_webview.Settings.AreDevToolsEnabled = false;
_webview.Settings.AreDefaultContextMenusEnabled = Configuration.IsDebug;
_webview.Settings.AreDevToolsEnabled = Configuration.IsDebug;
_webview.Settings.IsStatusBarEnabled = false;
#if DEBUG
_webview.Settings.AreDefaultContextMenusEnabled = true;
_webview.Settings.AreDevToolsEnabled = true;
#endif
}

private void SetupEventHandlers()
Expand Down
6 changes: 5 additions & 1 deletion src/Cody.VisualStudio/Cody.VisualStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<None Include="..\CodyDevConfig.json">
<Link>CodyDevConfig.json</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="publishManifest.json" />
<None Include="..\..\README.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -99,7 +103,7 @@
<IncludeInVSIX>true</IncludeInVSIX>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Remove="Agent\index.js.map" Condition="'$(Configuration)' == 'Release'"/>
<Content Remove="Agent\index.js.map" Condition="'$(Configuration)' == 'Release'" />
<Content Include="Agent\webviews\*.*">
<IncludeInVSIX>true</IncludeInVSIX>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
49 changes: 28 additions & 21 deletions src/Cody.VisualStudio/CodyPackage.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Cody.Core.Agent;
using Cody.Core.Agent.Protocol;
using Cody.Core.Common;
using Cody.Core.DocumentSync;
using Cody.Core.Ide;
using Cody.Core.Inf;
using Cody.Core.Infrastructure;
using Cody.Core.Logging;
using Cody.Core.Settings;
using Cody.Core.Trace;
using Cody.Core.Workspace;
using Cody.UI.Controls;
using Cody.UI.ViewModels;
Expand All @@ -14,8 +16,6 @@
using Cody.VisualStudio.Inf;
using Cody.VisualStudio.Options;
using Cody.VisualStudio.Services;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Editor;
Expand All @@ -32,9 +32,7 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using SolutionEvents = Microsoft.VisualStudio.Shell.Events.SolutionEvents;
using Task = System.Threading.Tasks.Task;

Expand Down Expand Up @@ -86,11 +84,12 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
try
{
InitializeErrorHandling();
Configuration.AddFromJsonFile("CodyDevConfig.json");
Configuration.AddFromEnviromentVariableJsonFile("CODY_VS_DEV_CONFIG");

// When initialized asynchronously, the current thread may be a background thread at this point.
// Do any initialization that requires the UI thread after switching to the UI thread.
await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

InitializeTrace();
InitializeServices();
await InitOleMenu();

Expand All @@ -105,13 +104,10 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke

private void InitializeServices()
{
//TraceManager.Listeners.Add(new FileTraceListener(@"c:\tmp\cody.log"));
//TraceManager.Enabled = true;

var loggerFactory = new LoggerFactory();
AgentLogger = loggerFactory.Create(WindowPaneLogger.CodyAgent);
AgentNotificationsLogger = loggerFactory.Create(WindowPaneLogger.CodyNotifications);
Logger = loggerFactory.Create();
AgentLogger = loggerFactory.Create(Configuration.ShowCodyAgentOutput ? WindowPaneLogger.CodyAgent : null);
AgentNotificationsLogger = loggerFactory.Create(Configuration.ShowCodyNotificationsOutput ? WindowPaneLogger.CodyNotifications : null);
Logger = loggerFactory.Create(WindowPaneLogger.DefaultCody);

var vsSolution = this.GetService<SVsSolution, IVsSolution>();
SolutionService = new SolutionService(vsSolution, Logger);
Expand Down Expand Up @@ -149,6 +145,20 @@ private void InitializeServices()
Logger.Info($"Visual Studio version: {VsVersionService.DisplayVersion} ({VsVersionService.EditionName})");
}

private static void InitializeTrace()
{
if (Configuration.Trace)
{
if (!string.IsNullOrEmpty(Configuration.TraceFile))
TraceManager.Listeners.Add(new FileTraceListener(Configuration.TraceFile));

if (!string.IsNullOrEmpty(Configuration.TraceLogioHostname) && Configuration.TraceLogioPort.HasValue)
TraceManager.Listeners.Add(new LogioTraceListener(Configuration.TraceLogioHostname, Configuration.TraceLogioPort.Value));

TraceManager.Enabled = true;
}
}

private void HandleOnOptionsPageShowRequest(object sender, EventArgs e)
{
try
Expand Down Expand Up @@ -249,23 +259,20 @@ private void PrepareAgentConfiguration()
{
try
{
var agentDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Agent");

var devPort = Environment.GetEnvironmentVariable("CODY_VS_DEV_PORT", EnvironmentVariableTarget.User);
var portNumber = int.TryParse(devPort, out int port) ? port : 3113;
var agentDir = Configuration.AgentDirectory ?? Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Agent");

if (devPort != null)
Logger.Debug($"Connecting to the agent using port:{devPort}");
if (Configuration.RemoteAgentPort.HasValue)
Logger.Debug($"Connecting to the agent using port:{Configuration.RemoteAgentPort}");

var options = new AgentClientOptions
{
CallbackHandlers = new List<object> { NotificationHandlers, ProgressNotificationHandlers },
AgentDirectory = agentDir,
RestartAgentOnFailure = true,
ConnectToRemoteAgent = devPort != null,
RemoteAgentPort = portNumber,
ConnectToRemoteAgent = Configuration.RemoteAgentPort.HasValue,
RemoteAgentPort = Configuration.RemoteAgentPort ?? 0,
AcceptNonTrustedCertificates = UserSettingsService.AcceptNonTrustedCert,
Debug = true
Debug = Configuration.AllowNodeDebug
};

AgentClient = new AgentClient(options, Logger, AgentLogger);
Expand Down
Loading