mirror of
https://onedev.site.tesses.net/crosslang/crosslangdevstudio
synced 2026-02-08 09:15:45 +00:00
Get way further
This commit is contained in:
92
ViewModels/AddProjectReferenceViewModel.cs
Normal file
92
ViewModels/AddProjectReferenceViewModel.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
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<CrossLangConfig>(data) ?? new CrossLangConfig();
|
||||
foreach (var item in jsonData.ProjectDependencies)
|
||||
References.Add(new ProjectReferenceEntry(this, item));
|
||||
}
|
||||
public string ProjectDirectory { get; init; }
|
||||
public ObservableCollection<ProjectReferenceEntry> References { get; } = new ObservableCollection<ProjectReferenceEntry>();
|
||||
|
||||
public void Remove(ProjectReferenceEntry projectReferenceEntry)
|
||||
{
|
||||
References.Remove(projectReferenceEntry);
|
||||
var data = File.ReadAllText(Path.Combine(ProjectDirectory, "cross.json"));
|
||||
var jsonData = JsonConvert.DeserializeObject<CrossLangConfig>(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<GetWindowMessage>();
|
||||
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<CrossLangConfig>(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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user