Skip to content

Commit

Permalink
feat: settings sections (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ authored Dec 20, 2024
1 parent 1d27dd7 commit 6794ceb
Show file tree
Hide file tree
Showing 53 changed files with 2,585 additions and 1,779 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ private void OkButton_OnClick(object sender, RoutedEventArgs e)
var currentList = folderConfigList.Where(x => x.FolderPath != FolderConfig.FolderPath).ToList();
currentList.Add(FolderConfig);

SnykVSPackage.ServiceProvider.Options.FolderConfigs = currentList;

var options = SnykVSPackage.ServiceProvider.Options;
options.FolderConfigs = currentList;
SnykVSPackage.ServiceProvider.SnykOptionsManager.Save(options);
this.CloseDialog();
}

Expand Down
2 changes: 1 addition & 1 deletion Snyk.VisualStudio.Extension.2022/Language/LsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SnykLsInitializationOptions GetInitializationOptions()
},
ScanningMode = options.AutoScan ? "auto" : "manual",
#pragma warning disable VSTHRD104
AdditionalParams = ThreadHelper.JoinableTaskFactory.Run(() => options.GetAdditionalOptionsAsync()),
AdditionalParams = ThreadHelper.JoinableTaskFactory.Run(() => this.serviceProvider.SnykOptionsManager.GetAdditionalOptionsAsync()),
#pragma warning restore VSTHRD104
AuthenticationMethod = options.AuthenticationMethod == AuthenticationType.OAuth ? "oauth" : "token",
CliPath = SnykCli.GetCliFilePath(options.CliCustomPath),
Expand Down
17 changes: 9 additions & 8 deletions Snyk.VisualStudio.Extension.2022/Language/SnykLanguageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ public object GetInitializationOptions()
public async Task<Connection> ActivateAsync(CancellationToken token)
{
await Task.Yield();
if (SnykVSPackage.ServiceProvider?.Options == null)
var serviceProvider = SnykVSPackage.ServiceProvider;
if (serviceProvider?.Options == null)
{
Logger.Error("Could not activate Language Server because ServiceProvider is null. Is the extension initialized?");
return null;
}
var options = SnykVSPackage.ServiceProvider.Options;
var options = serviceProvider.Options;
// ReSharper disable once RedundantAssignment
var lsDebugLevel = await GetLsDebugLevelAsync(options);
var lsDebugLevel = await GetLsDebugLevelAsync(serviceProvider.SnykOptionsManager);
#if DEBUG
lsDebugLevel = "debug";
#endif
Expand Down Expand Up @@ -217,14 +218,17 @@ public Task<InitializationFailureContext> OnServerInitializeFailedAsync(ILanguag

public Task OnServerInitializedAsync()
{
IsReady = true;
FireOnLanguageServerReadyAsyncEvent();
SendPluginInstalledEvent();
Rpc.Disconnected += Rpc_Disconnected;
return Task.CompletedTask;
}

private async Task<string> GetLsDebugLevelAsync(ISnykOptions options)
private async Task<string> GetLsDebugLevelAsync(ISnykOptionsManager optionsManger)
{
var logLevel = "info";
var additionalCliParameters = await options.GetAdditionalOptionsAsync();
var additionalCliParameters = await optionsManger.GetAdditionalOptionsAsync();
if (!string.IsNullOrEmpty(additionalCliParameters) && (additionalCliParameters.Contains("-d") || additionalCliParameters.Contains("--debug")))
{
logLevel = "debug";
Expand All @@ -245,9 +249,6 @@ public async Task AttachForCustomMessageAsync(JsonRpc rpc)
Rpc.AllowModificationWhileListening = true;
Rpc.ActivityTracingStrategy = null;
Rpc.AllowModificationWhileListening = false;
IsReady = true;
FireOnLanguageServerReadyAsyncEvent();
SendPluginInstalledEvent();
}

protected void OnStopping() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.Shell;
using Newtonsoft.Json.Linq;
using Snyk.VisualStudio.Extension.Authentication;
Expand Down Expand Up @@ -109,6 +110,7 @@ public async Task OnFolderConfig(JToken arg)
var folderConfigs = arg.TryParse<FolderConfigsParam>();
if (folderConfigs == null) return;
serviceProvider.Options.FolderConfigs = folderConfigs.FolderConfigs;
serviceProvider.SnykOptionsManager.Save(serviceProvider.Options);
}

[JsonRpcMethod(LsConstants.SnykHasAuthenticated)]
Expand All @@ -121,10 +123,6 @@ public async Task OnHasAuthenticated(JToken arg)
}

var token = arg["token"].ToString();
if (string.IsNullOrEmpty(token))
{
return;
}

var apiUrl = arg["apiUrl"]?.ToString();
if (!string.IsNullOrEmpty(apiUrl))
Expand All @@ -133,10 +131,14 @@ public async Task OnHasAuthenticated(JToken arg)
}

serviceProvider.Options.ApiToken = new AuthenticationToken(serviceProvider.Options.AuthenticationMethod, token);
serviceProvider.SnykOptionsManager.Save(serviceProvider.Options);

await serviceProvider.GeneralOptionsDialogPage.HandleAuthenticationSuccess(token, apiUrl);
serviceProvider.FeatureFlagService.RefreshAsync(SnykVSPackage.Instance.DisposalToken).FireAndForget();

if (!serviceProvider.Options.ApiToken.IsValid())
return;

serviceProvider.FeatureFlagService.RefreshAsync(SnykVSPackage.Instance.DisposalToken).FireAndForget();
if (serviceProvider.Options.AutoScan)
{
await serviceProvider.TasksService.ScanAsync();
Expand All @@ -150,7 +152,7 @@ public async Task OnAddTrustedFolders(JToken arg)
if (trustedFolders == null) return;

serviceProvider.Options.TrustedFolders = new HashSet<string>(trustedFolders.TrustedFolders);
this.serviceProvider.UserStorageSettingsService?.SaveSettings();
this.serviceProvider.SnykOptionsManager.Save(serviceProvider.Options);
await serviceProvider.LanguageClientManager.DidChangeConfigurationAsync(SnykVSPackage.Instance.DisposalToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface ISnykServiceProvider
/// </summary>
ISnykOptions Options { get; }
ISnykGeneralOptionsDialogPage GeneralOptionsDialogPage { get; }

ISnykOptionsManager SnykOptionsManager { get; }
/// <summary>
/// Gets Visual Studio Settiings Manager instance.
/// </summary>
Expand All @@ -58,11 +58,6 @@ public interface ISnykServiceProvider
/// </summary>
SnykVsThemeService VsThemeService { get; }

/// <summary>
/// Gets user storage settings service instance.
/// </summary>
IUserStorageSettingsService UserStorageSettingsService { get; }

/// <summary>
/// Gets <see cref="SnykToolWindowControl"/> instance.
/// </summary>
Expand Down
17 changes: 6 additions & 11 deletions Snyk.VisualStudio.Extension.2022/Service/SnykService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Threading.Tasks;
using EnvDTE;
using EnvDTE80;
using Microsoft;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Settings;
Expand Down Expand Up @@ -36,7 +34,7 @@ public class SnykService : ISnykServiceProvider, ISnykService

private DTE2 dte;

private SnykUserStorageSettingsService userStorageSettingsService;
private SnykOptionsManager snykOptionsManager;
private SnykFeatureFlagService featureFlagService;

private IWorkspaceTrustService workspaceTrustService;
Expand Down Expand Up @@ -101,21 +99,18 @@ public SnykService(IAsyncServiceProvider serviceProvider, string vsVersion = "")
/// </summary>
public SnykVsThemeService VsThemeService => this.vsThemeService;

/// <summary>
/// Gets user storage settings service instance.
/// </summary>
public IUserStorageSettingsService UserStorageSettingsService
public ISnykOptionsManager SnykOptionsManager
{
get
{
if (this.userStorageSettingsService == null)
if (this.snykOptionsManager == null)
{
string settingsFilePath = Path.Combine(SnykExtension.GetExtensionDirectoryPath(), "settings.json");

this.userStorageSettingsService = new SnykUserStorageSettingsService(settingsFilePath, this);
this.snykOptionsManager = new SnykOptionsManager(settingsFilePath, this);
}

return this.userStorageSettingsService;
return this.snykOptionsManager;
}
}

Expand Down Expand Up @@ -167,7 +162,7 @@ public async Task InitializeAsync(CancellationToken cancellationToken)
await SnykSolutionService.Instance.InitializeAsync(this);

this.tasksService = SnykTasksService.Instance;
this.workspaceTrustService = new WorkspaceTrustService(this.UserStorageSettingsService);
this.workspaceTrustService = new WorkspaceTrustService(this);

NotificationService.Initialize(this);
VsStatusBar.Initialize(this);
Expand Down
16 changes: 7 additions & 9 deletions Snyk.VisualStudio.Extension.2022/Service/SnykTasksService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,7 @@ private void CancelTask(CancellationTokenSource tokenSource)

public bool ShouldDownloadCli()
{
var userSettingsStorageService = this.serviceProvider.UserStorageSettingsService;
if (!userSettingsStorageService.BinariesAutoUpdate)
if (!this.serviceProvider.Options.BinariesAutoUpdate)
{
return false;
}
Expand All @@ -751,8 +750,8 @@ public bool ShouldDownloadCli()
private async Task DownloadAsync(CliDownloadFinishedCallback downloadFinishedCallback,
ISnykProgressWorker progressWorker)
{
var userSettingsStorageService = this.serviceProvider.UserStorageSettingsService;
if (!userSettingsStorageService.BinariesAutoUpdate)
var options = this.serviceProvider.Options;
if (!options.BinariesAutoUpdate)
{
Logger.Information("CLI auto-update is disabled, CLI download is skipped.");
this.DownloadCancelled?.Invoke(this, new SnykCliDownloadEventArgs());
Expand All @@ -762,8 +761,7 @@ private async Task DownloadAsync(CliDownloadFinishedCallback downloadFinishedCal
this.isCliDownloading = true;
try
{
var serviceProviderOptions = this.serviceProvider.Options;
var cliDownloader = new SnykCliDownloader(serviceProviderOptions);
var cliDownloader = new SnykCliDownloader(this.serviceProvider.Options);

var downloadFinishedCallbacks = new List<CliDownloadFinishedCallback>();

Expand All @@ -774,12 +772,12 @@ private async Task DownloadAsync(CliDownloadFinishedCallback downloadFinishedCal

downloadFinishedCallbacks.Add(() =>
{
serviceProviderOptions.CurrentCliVersion = cliDownloader.GetLatestReleaseInfo().Name;
userSettingsStorageService.SaveSettings();
this.serviceProvider.Options.CurrentCliVersion = cliDownloader.GetLatestReleaseInfo().Name;
this.serviceProvider.SnykOptionsManager.Save(this.serviceProvider.Options);
DisposeCancellationTokenSource(this.downloadCliTokenSource);
});

var downloadPath = serviceProviderOptions.CliCustomPath;
var downloadPath = this.serviceProvider.Options.CliCustomPath;
await cliDownloader.AutoUpdateCliAsync(
progressWorker,
downloadPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public class WorkspaceTrustService : IWorkspaceTrustService
{
private static readonly ILogger Logger = LogManager.ForContext<WorkspaceTrustService>();

private readonly IUserStorageSettingsService settingsService;
private readonly ISnykServiceProvider serviceProvider;

public WorkspaceTrustService(IUserStorageSettingsService settingsService)
public WorkspaceTrustService(ISnykServiceProvider serviceProvider)
{
this.settingsService = settingsService;
this.serviceProvider = serviceProvider;
}

public void AddFolderToTrusted(string absoluteFolderPath)
Expand All @@ -30,10 +30,10 @@ public void AddFolderToTrusted(string absoluteFolderPath)

try
{
var trustedFolders = this.settingsService.TrustedFolders;
var trustedFolders = this.serviceProvider.Options.TrustedFolders;
trustedFolders.Add(absoluteFolderPath);
this.settingsService.TrustedFolders = trustedFolders;
this.settingsService.SaveSettings();
this.serviceProvider.Options.TrustedFolders = trustedFolders;
this.serviceProvider.SnykOptionsManager.Save(this.serviceProvider.Options);
}
catch (Exception e)
{
Expand All @@ -45,7 +45,7 @@ public bool IsFolderTrusted(string absoluteFolderPath)
{
if (string.IsNullOrEmpty(absoluteFolderPath))
return true;
var trustedFolders = this.settingsService.TrustedFolders;
var trustedFolders = this.serviceProvider.Options.TrustedFolders;

foreach (var trustedFolder in trustedFolders)
{
Expand Down
Loading

0 comments on commit 6794ceb

Please sign in to comment.