Skip to content

Commit

Permalink
improve nativetoolservice system
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Dec 15, 2023
1 parent 64a5fd4 commit 5ea1ed8
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 57 deletions.
82 changes: 52 additions & 30 deletions src/OneWare.Core/Services/NativeToolService.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,76 @@
using OneWare.SDK.Helpers;
using Avalonia.Media;
using OneWare.SDK.Enums;
using OneWare.SDK.Helpers;
using OneWare.SDK.NativeTools;
using OneWare.SDK.Services;

namespace OneWare.Core.Services;

public class NativeToolService(IHttpService httpService, ISettingsService settingsService, IPaths paths, ILogger logger) : INativeToolService
public class NativeToolService(IHttpService httpService, ISettingsService settingsService, IPaths paths, IApplicationStateService applicationStateService, ILogger logger) : INativeToolService
{
private readonly Dictionary<PlatformId, Dictionary<string, NativeTool>> _nativeTools = new();
private readonly Dictionary<string, NativeToolContainer> _nativeTools = new();

public NativeTool Register(string id, string url, params PlatformId[] supportedPlatforms)
public NativeToolContainer Register(string key)
{
var nativeTool = new NativeTool(id, url, Path.Combine(paths.NativeToolsDirectory, id));
foreach (var platform in supportedPlatforms)
{
_nativeTools.TryAdd(platform, new Dictionary<string, NativeTool>());
_nativeTools[platform].Add(id, nativeTool);
}
return nativeTool;
_nativeTools[key] = new NativeToolContainer(key, Path.Combine(paths.NativeToolsDirectory, key));
return _nativeTools[key];
}

public NativeTool? Get(string key)
public NativeToolContainer? Get(string key)
{
if (_nativeTools.TryGetValue(PlatformHelper.Platform, out var platformTools))
{
if (platformTools.TryGetValue(key, out var nativeTool))
{
return nativeTool;
}
}
return null;
return _nativeTools.GetValueOrDefault(key);
}

public async Task InstallAsync(NativeTool tool)
public async Task<bool> InstallAsync(NativeToolContainer container)
{
logger.Log($"Downloading {tool.Id}...");
var tool = container.GetPlatform();

if (tool == null)
{
logger.Warning($"Tool {container.Id} not supported for {PlatformHelper.Platform.ToString()} yet");
return false;
}

logger.Log($"Downloading {container.Id} for {PlatformHelper.Platform.ToString()}...", ConsoleColor.Gray, true, Brushes.Gray);

var state = applicationStateService.AddState($"Downloading {container.Id}...", AppState.Loading);

var success = false;

await DownloadNativeToolAsync(tool);
try
{
var progress = new Progress<float>(x =>
{
state.StatusMessage = $"Downloading {container.Id} {(int)(x*100)}%";
});
success = await DownloadNativeToolAsync(tool, progress);

foreach (var shortCut in tool.ShortCuts)
foreach (var shortCut in tool.ShortCuts)
{
if(shortCut.Value.SettingKey != null)
settingsService.SetSettingValue(shortCut.Value.SettingKey, Path.Combine(tool.FullPath, shortCut.Value.RelativePath));
}
settingsService.Save(paths.SettingsPath);
}
catch (Exception e)
{
if(shortCut.Value.SettingKey != null)
settingsService.SetSettingValue(shortCut.Value.SettingKey, Path.Combine(tool.FullPath, shortCut.Value.RelativePath));
logger.Error(e.Message, e);
return false;
}
settingsService.Save(paths.SettingsPath);

logger.Log($"Download {tool.Id} finished!");
applicationStateService.RemoveState(state);

if (success)
{
logger.Log($"Download {container.Id} finished!", ConsoleColor.Gray, true, Brushes.Gray);
return true;
}
logger.Warning($"Download {container.Id} failed!");
return false;
}

private async Task DownloadNativeToolAsync(NativeTool tool)
private Task<bool> DownloadNativeToolAsync(NativeTool tool, IProgress<float> progress)
{
await httpService.DownloadAndExtractArchiveAsync(tool.Url, Path.Combine(paths.NativeToolsDirectory, tool.Id));
return httpService.DownloadAndExtractArchiveAsync(tool.Url, tool.FullPath, progress);
}
}
9 changes: 7 additions & 2 deletions src/OneWare.Cpp/CppModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ public void OnInitialized(IContainerProvider containerProvider)
{
var nativeToolService = containerProvider.Resolve<INativeToolService>();

nativeToolService.Register(LspName, "https://github.com/clangd/clangd/releases/download/17.0.3/clangd-windows-17.0.3.zip", PlatformId.WinX64)
var nativeTool = nativeToolService.Register(LspName);

nativeTool.AddPlatform(PlatformId.WinX64, "https://github.com/clangd/clangd/releases/download/17.0.3/clangd-windows-17.0.3.zip")
.WithShortcut("LSP", Path.Combine("clangd_17.0.3", "bin", "clangd.exe"), LspPathSetting);

nativeTool.AddPlatform(PlatformId.OsxX64, "https://github.com/clangd/clangd/releases/download/17.0.3/clangd-mac-17.0.3.zip")
.WithShortcut("LSP", "clangd_17.0.3/bin/clangd", LspPathSetting);

containerProvider.Resolve<ISettingsService>().RegisterTitledPath("Languages", "C++", LspPathSetting, "Clangd Path", "Path for clangd executable",
nativeToolService.Get(LspName)!.GetShorcutPath("LSP")!,
nativeToolService.Get(LspName)!.GetShortcutPathOrEmpty("LSP"),
null, containerProvider.Resolve<IPaths>().PackagesDirectory, File.Exists);

containerProvider.Resolve<IErrorService>().RegisterErrorSource(LspName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace OneWare.SDK.LanguageService;

public abstract class LanguageServiceLspAutoDownload : LanguageServiceLsp
{
private readonly Func<Task> _installTask;
protected LanguageServiceLspAutoDownload(IObservable<string> executablePath, Func<Task> install, string name, string? workspace)
private readonly Func<Task<bool>> _installTask;
protected LanguageServiceLspAutoDownload(IObservable<string> executablePath, Func<Task<bool>> install, string name, string? workspace)
: base(name, workspace)
{
_installTask = install;
Expand All @@ -23,7 +23,7 @@ public override async Task ActivateAsync()
{
if (!File.Exists(ExecutablePath))
{
await _installTask.Invoke();
if(!await _installTask.Invoke()) return;
}
await base.ActivateAsync();
}
Expand Down
7 changes: 3 additions & 4 deletions src/OneWare.SDK/NativeTools/NativeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace OneWare.SDK.NativeTools;

public class NativeTool(string id, string url, string fullPath)
public class NativeTool(string url, string fullPath)
{
public string Id { get; } = id;
public string Url { get; } = url;
public string FullPath { get; } = fullPath;

Expand All @@ -13,7 +12,7 @@ public class NativeTool(string id, string url, string fullPath)

public NativeTool WithShortcut(string shortcutId, string relativePath, string? settingId = null)
{
ShortCuts.Add(shortcutId, new NativeToolShortcut(this, relativePath, settingId));
ShortCuts.Add(shortcutId, new NativeToolShortcut(relativePath, settingId));
return this;
}

Expand All @@ -23,7 +22,7 @@ public NativeToolShortcut GetShortcut(string key)
throw new Exception("Shortcut not registered");
}

public string? GetShorcutPath(string key)
public string? GetShortcutPath(string key)
{
if (ShortCuts.TryGetValue(key, out var shortCut)) return Path.Combine(FullPath,shortCut.RelativePath);
return null;
Expand Down
34 changes: 34 additions & 0 deletions src/OneWare.SDK/NativeTools/NativeToolContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using OneWare.SDK.Helpers;

namespace OneWare.SDK.NativeTools;

public class NativeToolContainer(string id, string fullPath)
{
public string Id { get; } = id;
public string FullPath { get; } = fullPath;
public Dictionary<PlatformId, NativeTool> Platforms { get; } = new();

public NativeTool AddPlatform(PlatformId platform, string url)
{
Platforms[platform] = new NativeTool(url, Path.Combine(FullPath, platform.ToString()));
return Platforms[platform];
}

public NativeTool? GetPlatform(PlatformId? platform = null)
{
var plt = platform ?? PlatformHelper.Platform;

//If OsxArm64 is not available, use X64 to be used with Rosetta
if (plt == PlatformId.OsxArm64 && !Platforms.ContainsKey(PlatformId.OsxArm64) &&
Platforms.ContainsKey(PlatformId.OsxX64))
{
plt = PlatformId.OsxX64;
}
return Platforms.GetValueOrDefault(plt);
}

public string GetShortcutPathOrEmpty(string shortcut, PlatformId? platform = null)
{
return GetPlatform(platform)?.GetShortcutPath(shortcut) ?? string.Empty;
}
}
3 changes: 1 addition & 2 deletions src/OneWare.SDK/NativeTools/NativeToolShortcut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace OneWare.SDK.NativeTools;

public class NativeToolShortcut(NativeTool owner, string relativePath, string? settingKey = null) : ObservableObject
public class NativeToolShortcut(string relativePath, string? settingKey = null) : ObservableObject
{
public NativeTool Owner { get; } = owner;
public string RelativePath { get; } = relativePath;
public string? SettingKey { get; } = settingKey;
}
7 changes: 3 additions & 4 deletions src/OneWare.SDK/Services/INativeToolService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace OneWare.SDK.Services;

public interface INativeToolService
{
public NativeTool Register(string id, string url, params PlatformId[] supportedPlatforms);

public NativeTool? Get(string id);
public Task InstallAsync(NativeTool tool);
public NativeToolContainer Register(string key);
public NativeToolContainer? Get(string key);
public Task<bool> InstallAsync(NativeToolContainer container);
}
26 changes: 16 additions & 10 deletions src/OneWare.Verilog/VerilogModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OneWare.SDK.Helpers;
using OneWare.SDK.NativeTools;
using OneWare.SDK.Services;
using OneWare.UniversalFpgaProjectSystem.Services;
using OneWare.Verilog.Parsing;
Expand All @@ -11,26 +12,31 @@ public class VerilogModule : IModule
{
public const string LspName = "Verible";
public const string LspPathSetting = "VerilogModule_VeriblePath";

public void RegisterTypes(IContainerRegistry containerRegistry)
{

}

public void OnInitialized(IContainerProvider containerProvider)
{
var nativeToolService = containerProvider.Resolve<INativeToolService>();

nativeToolService.Register(LspName, "https://github.com/chipsalliance/verible/releases/download/v0.0-3430-g060bde0f/verible-v0.0-3430-g060bde0f-win64.zip", PlatformId.WinX64)
.WithShortcut("LSP", Path.Combine("verible-v0.0-3430-g060bde0f-win64", "verible-verilog-ls.exe"), LspPathSetting);

containerProvider.Resolve<ISettingsService>().RegisterTitledPath("Languages", "Verilog", LspPathSetting, "Verible Path", "Path for Verible executable",
nativeToolService.Get(LspName)!.GetShorcutPath("LSP")!,
var nativeTool = nativeToolService.Register(LspName);

nativeTool.AddPlatform(PlatformId.WinX64, "https://github.com/chipsalliance/verible/releases/download/v0.0-3430-g060bde0f/verible-v0.0-3430-g060bde0f-win64.zip")
.WithShortcut("LSP", Path.Combine("verible-v0.0-3430-g060bde0f-win64", "verible-verilog-ls.exe"),
LspPathSetting);

containerProvider.Resolve<ISettingsService>().RegisterTitledPath("Languages", "Verilog", LspPathSetting,
"Verible Path", "Path for Verible executable",
nativeToolService.Get(LspName)!.GetShortcutPathOrEmpty("LSP"),
null, containerProvider.Resolve<IPaths>().PackagesDirectory, File.Exists);

containerProvider.Resolve<IErrorService>().RegisterErrorSource(LspName);
containerProvider.Resolve<ILanguageManager>().RegisterTextMateLanguage("verilog", "avares://OneWare.Verilog/Assets/verilog.tmLanguage.json", ".v", ".sv");
containerProvider.Resolve<ILanguageManager>().RegisterService(typeof(LanguageServiceVerilog),true, ".v", ".sv");
containerProvider.Resolve<ILanguageManager>().RegisterTextMateLanguage("verilog",
"avares://OneWare.Verilog/Assets/verilog.tmLanguage.json", ".v", ".sv");
containerProvider.Resolve<ILanguageManager>()
.RegisterService(typeof(LanguageServiceVerilog), true, ".v", ".sv");
containerProvider.Resolve<FpgaService>().RegisterNodeProvider<VerilogNodeProvider>(".v", ".sv");
}
}
6 changes: 4 additions & 2 deletions src/OneWare.Vhdl/VhdlModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public void OnInitialized(IContainerProvider containerProvider)
{
var nativeToolService = containerProvider.Resolve<INativeToolService>();

nativeToolService.Register(LspName, "https://github.com/VHDL-LS/rust_hdl/releases/download/v0.77.0/vhdl_ls-x86_64-pc-windows-msvc.zip", PlatformId.WinX64)
var nativeTool = nativeToolService.Register(LspName);

nativeTool.AddPlatform(PlatformId.WinX64, "https://github.com/VHDL-LS/rust_hdl/releases/download/v0.77.0/vhdl_ls-x86_64-pc-windows-msvc.zip")
.WithShortcut("LSP", Path.Combine("vhdl_ls-x86_64-pc-windows-msvc", "bin" , "vhdl_ls.exe"), LspPathSetting);

containerProvider.Resolve<ISettingsService>().RegisterTitledPath("Languages", "VHDL", LspPathSetting, "RustHDL Path", "Path for RustHDL executable",
nativeToolService.Get(LspName)!.GetShorcutPath("LSP")!,
nativeToolService.Get(LspName)!.GetShortcutPathOrEmpty("LSP"),
null, containerProvider.Resolve<IPaths>().PackagesDirectory, File.Exists);

containerProvider.Resolve<IErrorService>().RegisterErrorSource(LspName);
Expand Down

0 comments on commit 5ea1ed8

Please sign in to comment.