using System; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading.Tasks; using Avalonia; using Avalonia.Media; using Avalonia.Platform.Storage; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using CrossLangDevStudio.Messages; using CrossLangDevStudio.Views; using MsBox.Avalonia; using Newtonsoft.Json; namespace CrossLangDevStudio.ViewModels; public partial class AddProjectReferenceViewModel : ViewModelBase { public AddProjectReferenceViewModel(string projectDir) { ProjectDirectory = projectDir; var data = File.ReadAllText(Path.Combine(projectDir, "cross.json")); var jsonData = JsonConvert.DeserializeObject(data) ?? new CrossLangConfig(); foreach (var item in jsonData.ProjectDependencies) References.Add(new ProjectReferenceEntry(this, item)); } public string ProjectDirectory { get; init; } public ObservableCollection References { get; } = new ObservableCollection(); public void Remove(ProjectReferenceEntry projectReferenceEntry) { References.Remove(projectReferenceEntry); var data = File.ReadAllText(Path.Combine(ProjectDirectory, "cross.json")); var jsonData = JsonConvert.DeserializeObject(data) ?? new CrossLangConfig(); jsonData.ProjectDependencies.Remove(projectReferenceEntry.Path); File.WriteAllText(Path.Combine(ProjectDirectory, "cross.json"), JsonConvert.SerializeObject(jsonData, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore })); } [RelayCommand] private async Task AddProjectAsync() { var window = await WeakReferenceMessenger.Default.Send(); var dir = await window.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions() { Title = "Add Project Dependency" }); if (dir.Count != 0) { string? path = dir[0].TryGetLocalPath(); if (!string.IsNullOrWhiteSpace(path)) { var jsonFile = Path.Combine(path, "cross.json"); if (!File.Exists(jsonFile)) { await MessageBoxManager.GetMessageBoxStandard("Error", $"Could not find cross.json in folder {path}", MsBox.Avalonia.Enums.ButtonEnum.Ok, MsBox.Avalonia.Enums.Icon.Error).ShowAsync(); return; } string projectPath = Path.GetRelativePath(this.ProjectDirectory, path); var abs = Path.GetFullPath(projectPath, ProjectDirectory); foreach (var item in References) { if (Path.GetFullPath(item.Path, ProjectDirectory) == abs) { await MessageBoxManager.GetMessageBoxStandard("Error", $"Project {path} already exists in your project.", MsBox.Avalonia.Enums.ButtonEnum.Ok, MsBox.Avalonia.Enums.Icon.Error).ShowAsync(); return; } } var data = File.ReadAllText(Path.Combine(ProjectDirectory, "cross.json")); var jsonData = JsonConvert.DeserializeObject(data) ?? new CrossLangConfig(); jsonData.ProjectDependencies.Add(projectPath); File.WriteAllText(Path.Combine(ProjectDirectory, "cross.json"), JsonConvert.SerializeObject(jsonData, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore })); References.Add(new ProjectReferenceEntry(this, projectPath)); } } } } public partial class ProjectReferenceEntry(AddProjectReferenceViewModel parent,string path) : ObservableObject { public string Path => path; [RelayCommand] private void Remove() { parent.Remove(this); } }