local folder workspace

This commit is contained in:
2025-11-17 16:35:02 +01:00
parent 5df8019cfb
commit e04e5f0b13
17 changed files with 347 additions and 151 deletions

View File

@@ -6,19 +6,14 @@ namespace ModpackUpdater.Apps.Manager.Features.Workspaces.GitLabRepo;
internal class GitLabRepoWorkspace(GitLabRepoWorkspaceConfig config) : IWorkspace
{
private string? rawInstallInfos = null;
private string? rawUpdateInfos = null;
private string? rawInstallInfos;
private string? rawUpdateInfos;
public WorkspaceConfig Config => ConfigX;
public GitLabRepoWorkspaceConfig ConfigX { get; } = config;
public IGitLabClient Gitlab { get; } = new GitLabClient(config.InstanceUrl, config.ApiToken);
public ModpackConfig? ModpackConfig { get; private set; }
public InstallInfos? InstallInfos { get; private set; }
public UpdateInfos? UpdateInfos { get; private set; }
public async Task<bool> Load()
@@ -39,13 +34,17 @@ internal class GitLabRepoWorkspace(GitLabRepoWorkspaceConfig config) : IWorkspac
public async Task<bool> Save()
{
var failed = true;
if (InstallInfos != null)
{
var newInstallInfos = InstallInfos.ToString();
if (newInstallInfos != rawInstallInfos)
{
await SaveContent(ConfigX.FileLocationInstallJson, newInstallInfos);
rawInstallInfos = newInstallInfos;
if (await SaveContent(ConfigX.FileLocationInstallJson, newInstallInfos))
rawInstallInfos = newInstallInfos;
else
failed = true;
}
}
@@ -54,12 +53,14 @@ internal class GitLabRepoWorkspace(GitLabRepoWorkspaceConfig config) : IWorkspac
var newUpdateInfos = UpdateInfos.ToString();
if (newUpdateInfos != rawUpdateInfos)
{
await SaveContent(ConfigX.FileLocationUpdateJson, newUpdateInfos);
rawUpdateInfos = newUpdateInfos;
if (await SaveContent(ConfigX.FileLocationUpdateJson, newUpdateInfos))
rawUpdateInfos = newUpdateInfos;
else
failed = true;
}
}
return true;
return failed;
}
private async Task<string> GetContent(string path)

View File

@@ -10,27 +10,28 @@
d:DesignWidth="800"
d:DesignHeight="450"
Width="300"
x:Class="ModpackUpdater.Apps.Manager.Features.Workspaces.GitLabRepo.GitLabRepoWorkspaceConfigEditorView">
x:Class="ModpackUpdater.Apps.Manager.Features.Workspaces.GitLabRepo.GitLabRepoWorkspaceConfigEditorView"
x:DataType="gitLabRepo:GitLabRepoWorkspaceConfig">
<dialogs:AvaloniaFlyoutBase.MainContent>
<StackPanel Spacing="6">
<TextBlock Text="{x:Static langRes:GeneralLangRes.GitLabInstanceUrl}"/>
<TextBox Text="{Binding InstanceUrl, DataType=gitLabRepo:GitLabRepoWorkspaceConfig}"/>
<Label Target="TextBoxInstanceUrl" Content="{x:Static langRes:GeneralLangRes.GitLabInstanceUrl}"/>
<TextBox Name="TextBoxInstanceUrl" Text="{Binding InstanceUrl}"/>
<TextBlock Text="{x:Static langRes:GeneralLangRes.GitLabApiToken}"/>
<TextBox Text="{Binding ApiToken, DataType=gitLabRepo:GitLabRepoWorkspaceConfig}"/>
<Label Target="TextBoxApiToken" Content="{x:Static langRes:GeneralLangRes.GitLabApiToken}"/>
<TextBox Name="TextBoxApiToken" Text="{Binding ApiToken}"/>
<TextBlock Text="{x:Static langRes:GeneralLangRes.RepositoryId}"/>
<NumericUpDown Value="{Binding RepoId, DataType=gitLabRepo:GitLabRepoWorkspaceConfig}"/>
<Label Target="NumericUpDownRepoId" Content="{x:Static langRes:GeneralLangRes.RepositoryId}"/>
<NumericUpDown Name="NumericUpDownRepoId" Value="{Binding RepoId}"/>
<TextBlock Text="{x:Static langRes:GeneralLangRes.FileLocationOfInstallJson}"/>
<TextBox Text="{Binding FileLocationInstallJson, DataType=gitLabRepo:GitLabRepoWorkspaceConfig}"/>
<Label Target="TextBoxInstallJson" Content="{x:Static langRes:GeneralLangRes.FileLocationOfInstallJson}"/>
<TextBox Name="TextBoxInstallJson" Text="{Binding FileLocationInstallJson}"/>
<TextBlock Text="{x:Static langRes:GeneralLangRes.FileLocationOfUpdateJson}"/>
<TextBox Text="{Binding FileLocationUpdateJson, DataType=gitLabRepo:GitLabRepoWorkspaceConfig}"/>
<Label Target="TextBoxUpdateJson" Content="{x:Static langRes:GeneralLangRes.FileLocationOfUpdateJson}"/>
<TextBox Name="TextBoxUpdateJson" Text="{Binding FileLocationUpdateJson}"/>
<TextBlock Text="{x:Static langRes:GeneralLangRes.ModpackConfigUrl}"/>
<TextBox Text="{Binding ModpackConfigUrl, DataType=gitLabRepo:GitLabRepoWorkspaceConfig}"/>
<Label Target="TextBoxModpackConfigUrl" Content="{x:Static langRes:GeneralLangRes.ModpackConfigUrl}"/>
<TextBox Name="TextBoxModpackConfigUrl" Text="{Binding ModpackConfigUrl}"/>
</StackPanel>
</dialogs:AvaloniaFlyoutBase.MainContent>
</dialogs:AvaloniaFlyoutBase>

View File

@@ -31,7 +31,7 @@ internal class GitLabRepoWorkspaceFeature() : WorkspaceFeature("origin.gitlab",
protected override void OnCreate(out IWorkspace workspace, WorkspaceConfig config)
{
if (config is not GitLabRepoWorkspaceConfig gitlabConfig)
throw new NotImplementedException($"Only configs of type {nameof(GitLabRepoWorkspaceConfig)} are allowed.");
throw new NotSupportedException($"Only configs of type {nameof(GitLabRepoWorkspaceConfig)} are allowed.");
workspace = new GitLabRepoWorkspace(gitlabConfig);
}
}

View File

@@ -0,0 +1,84 @@
using ModpackUpdater.Apps.Manager.Api.Model;
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.LocalFolder;
internal class LocalFolderWorkspace(LocalFolderWorkspaceConfig config) : IWorkspace
{
private string? rawInstallInfos;
private string? rawUpdateInfos;
public WorkspaceConfig Config => ConfigX;
public LocalFolderWorkspaceConfig ConfigX { get; } = config;
public ModpackConfig? ModpackConfig { get; private set; }
public InstallInfos? InstallInfos { get; private set; }
public UpdateInfos? UpdateInfos { get; private set; }
public async Task<bool> Load()
{
if (!string.IsNullOrWhiteSpace(Config.ModpackConfigUrl))
ModpackConfig = ModpackConfig.LoadFromUrl(Config.ModpackConfigUrl);
rawInstallInfos = await GetContent(ConfigX.FileLocationInstallJson);
InstallInfos = InstallInfos.Parse(rawInstallInfos);
rawUpdateInfos = await GetContent(ConfigX.FileLocationUpdateJson);
UpdateInfos = UpdateInfos.Parse(rawUpdateInfos);
ConfigX.FolderName = Path.GetFileName(ConfigX.RootFolderPath);
return InstallInfos != null && UpdateInfos != null;
}
public async Task<bool> Save()
{
var failed = false;
if (InstallInfos != null)
{
var newInstallInfos = InstallInfos.ToString();
if (newInstallInfos != rawInstallInfos)
{
if (await SaveContent(ConfigX.FileLocationInstallJson, newInstallInfos))
rawInstallInfos = newInstallInfos;
else
failed = true;
}
}
if (UpdateInfos != null)
{
var newUpdateInfos = UpdateInfos.ToString();
if (newUpdateInfos != rawUpdateInfos)
{
if (await SaveContent(ConfigX.FileLocationUpdateJson, newUpdateInfos))
rawUpdateInfos = newUpdateInfos;
else
failed = true;
}
}
return !failed;
}
private async Task<string?> GetContent(string path)
{
if (ConfigX.RootFolderPath == null)
return null;
var filePath = Path.Combine(ConfigX.RootFolderPath!, path);
return File.Exists(filePath) ? await File.ReadAllTextAsync(filePath) : null;
}
private async Task<bool> SaveContent(string path, string content)
{
if (ConfigX.RootFolderPath == null)
return false;
var filePath = Path.Combine(ConfigX.RootFolderPath!, path);
if (!File.Exists(filePath))
return false;
await File.WriteAllTextAsync(filePath, content);
return true;
}
}

View File

@@ -0,0 +1,17 @@
using ModpackUpdater.Apps.Manager.Api.Model;
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.LocalFolder;
public class LocalFolderWorkspaceConfig : WorkspaceConfig, ICloneable
{
public override string DisplayText => FolderName ?? string.Empty;
public string? FolderName { get; set; }
public string? RootFolderPath { get; set; }
public string FileLocationInstallJson { get; set; } = "install.json";
public string FileLocationUpdateJson { get; set; } = "updates.json";
public object Clone()
{
return MemberwiseClone();
}
}

View File

@@ -0,0 +1,43 @@
<dialogs:AvaloniaFlyoutBase
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:dialogs="https://git.pilzinsel64.de/pilz-framework/pilz"
xmlns:langRes="clr-namespace:ModpackUpdater.Apps.Manager.LangRes"
xmlns:localFolder="clr-namespace:ModpackUpdater.Apps.Manager.Features.Workspaces.LocalFolder"
mc:Ignorable="d"
d:DesignWidth="800"
d:DesignHeight="450"
Width="300"
x:Class="ModpackUpdater.Apps.Manager.Features.Workspaces.LocalFolder.LocalFolderWorkspaceConfigEditorView"
x:DataType="localFolder:LocalFolderWorkspaceConfig">
<dialogs:AvaloniaFlyoutBase.MainContent>
<StackPanel Spacing="6">
<Label Target="TextBoxRootFolderPath" Content="{x:Static langRes:GeneralLangRes.RootFolderPath}"/>
<StackPanel
Orientation="Horizontal"
Spacing="6">
<TextBox x:Name="TextBoxRootFolderPath" Text="{Binding RootFolderPath}"/>
<dialogs:ImageButton
x:Name="ButtonSearch"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Text="{x:Static langRes:GeneralLangRes.Select}"
Click="ButtonSearch_OnClick"/>
</StackPanel>
<Label Target="TextBoxInstallJson" Content="{x:Static langRes:GeneralLangRes.FileLocationOfInstallJson}"/>
<TextBox Name="TextBoxInstallJson" Text="{Binding FileLocationInstallJson}"/>
<Label Target="TextBoxUpdateJson" Content="{x:Static langRes:GeneralLangRes.FileLocationOfUpdateJson}"/>
<TextBox Name="TextBoxUpdateJson" Text="{Binding FileLocationUpdateJson}"/>
<Label Target="TextBoxModpackConfigUrl" Content="{x:Static langRes:GeneralLangRes.ModpackConfigUrl}"/>
<TextBox Name="TextBoxModpackConfigUrl" Text="{Binding ModpackConfigUrl}"/>
</StackPanel>
</dialogs:AvaloniaFlyoutBase.MainContent>
</dialogs:AvaloniaFlyoutBase>

View File

@@ -0,0 +1,35 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using ModpackUpdater.Apps.Manager.LangRes;
using Pilz.UI.AvaloniaUI.Dialogs;
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.LocalFolder;
public partial class LocalFolderWorkspaceConfigEditorView : AvaloniaFlyoutBase
{
public LocalFolderWorkspaceConfig Settings { get; }
public LocalFolderWorkspaceConfigEditorView(LocalFolderWorkspaceConfig settings)
{
Settings = settings;
DataContext = settings;
InitializeComponent();
ButtonSearch.ImageSource = AppGlobals.Symbols.GetImageSource(AppSymbols.opened_folder);
}
private async void ButtonSearch_OnClick(object? sender, RoutedEventArgs e)
{
if (TopLevel.GetTopLevel(this) is not { } topLevel)
return;
var filePaths = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = GeneralLangRes.SelectRootFolder,
AllowMultiple = false,
});
if (filePaths.Count >= 1)
TextBoxRootFolderPath.Text = filePaths[0].Path.AbsolutePath;
}
}

View File

@@ -0,0 +1,37 @@
using ModpackUpdater.Apps.Manager.Api.Model;
using ModpackUpdater.Apps.Manager.Api.Plugins.Features;
using ModpackUpdater.Apps.Manager.LangRes;
using Pilz.Features;
using Pilz.UI.AvaloniaUI.Dialogs;
using Pilz.UI.Symbols;
namespace ModpackUpdater.Apps.Manager.Features.Workspaces.LocalFolder;
internal class LocalFolderWorkspaceFeature() : WorkspaceFeature("origin.localfolder", FeatureNamesLangRes.LocalFolderWorkspace), IPluginFeatureProvider<LocalFolderWorkspaceFeature>
{
public static LocalFolderWorkspaceFeature Instance { get; } = new();
public override object? Icon => AppGlobals.Symbols.GetImage(AppSymbols.opened_folder, SymbolSize.Small);
protected override async Task OnConfigure(WorkspaceContext context)
{
var settings = context.Workspace?.Config as LocalFolderWorkspaceConfig ?? new();
if ((await AvaloniaFlyoutBase.Show(
new LocalFolderWorkspaceConfigEditorView((LocalFolderWorkspaceConfig)settings.Clone()),
context.MainApi.MainWindow,
TitlesLangRes.LocalFolderWorkspaceEditor,
AppGlobals.Symbols.GetImageSource(AppSymbols.gitlab)))
.Result is LocalFolderWorkspaceConfig settingsNew)
context.Workspace = new LocalFolderWorkspace(settingsNew);
else
context.Canceled = true;
}
protected override void OnCreate(out IWorkspace workspace, WorkspaceConfig config)
{
if (config is not LocalFolderWorkspaceConfig gitlabConfig)
throw new NotSupportedException($"Only configs of type {nameof(LocalFolderWorkspaceConfig)} are allowed.");
workspace = new LocalFolderWorkspace(gitlabConfig);
}
}