From bba3c745f516c5a40da228ad01280c0fc5d1ecbb Mon Sep 17 00:00:00 2001 From: Isaac Daly Date: Sun, 17 Nov 2024 16:17:02 +1100 Subject: [PATCH 1/5] Upgrade to Corvus.JsonSchema 4 with a source generator --- .github/workflows/commit.yml | 4 - .github/workflows/docs_commit.yml | 4 - .github/workflows/docs_deploy.yml | 4 - .github/workflows/release.yml | 4 - .gitignore | 3 - Directory.Packages.props | 21 ++-- scripts/Generate-SourceFromSchema.ps1 | 49 --------- src/Whim.Yaml/Schema.cs | 6 ++ src/Whim.Yaml/Whim.Yaml.csproj | 14 +-- src/Whim.Yaml/YamlBarPluginLoader.cs | 71 ++++++------ src/Whim.Yaml/YamlLayoutEngineLoader.cs | 40 +++---- src/Whim.Yaml/YamlLoader.cs | 32 +++--- src/Whim.Yaml/YamlLoaderUtils.cs | 5 +- src/Whim.Yaml/YamlPluginLoader.cs | 138 ++++++++++++++++-------- 14 files changed, 188 insertions(+), 207 deletions(-) delete mode 100644 scripts/Generate-SourceFromSchema.ps1 create mode 100644 src/Whim.Yaml/Schema.cs diff --git a/.github/workflows/commit.yml b/.github/workflows/commit.yml index 0b0eeef09..0fc1800dc 100644 --- a/.github/workflows/commit.yml +++ b/.github/workflows/commit.yml @@ -92,10 +92,6 @@ jobs: - name: Add msbuild to PATH uses: microsoft/setup-msbuild@v2.0.0 - - name: Build schema - run: | - .\scripts\Generate-SourceFromSchema.ps1 - - name: Build run: | msbuild Whim.sln ` diff --git a/.github/workflows/docs_commit.yml b/.github/workflows/docs_commit.yml index 07b8c65d5..3b3300a46 100644 --- a/.github/workflows/docs_commit.yml +++ b/.github/workflows/docs_commit.yml @@ -57,9 +57,5 @@ jobs: run: | dotnet tool restore - - name: Build schema - run: | - .\scripts\Generate-SourceFromSchema.ps1 - - name: Run docfx run: dotnet docfx .\docs\docfx.json diff --git a/.github/workflows/docs_deploy.yml b/.github/workflows/docs_deploy.yml index 088b4f555..8db84333a 100644 --- a/.github/workflows/docs_deploy.yml +++ b/.github/workflows/docs_deploy.yml @@ -55,10 +55,6 @@ jobs: run: | dotnet tool restore - - name: Build schema - run: | - .\scripts\Generate-SourceFromSchema.ps1 - - run: dotnet tool update -g docfx - run: docfx docs/docfx.json diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dad2879aa..227ea4806 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -100,10 +100,6 @@ jobs: - name: Add msbuild to PATH uses: microsoft/setup-msbuild@v2.0.0 - - name: Build schema - run: | - .\scripts\Generate-SourceFromSchema.ps1 - - name: Build run: | msbuild Whim.sln ` diff --git a/.gitignore b/.gitignore index 2286cd15e..d8f12c1f2 100644 --- a/.gitignore +++ b/.gitignore @@ -361,6 +361,3 @@ bin/whim-install.exe # docfx api _site - -# Whim.Yaml generated files -src/Whim.Yaml/Generated/ diff --git a/Directory.Packages.props b/Directory.Packages.props index 70aada601..775aea4e3 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,21 +3,22 @@ true - - - - + + + + + - - + + - - + + - + - + diff --git a/scripts/Generate-SourceFromSchema.ps1 b/scripts/Generate-SourceFromSchema.ps1 deleted file mode 100644 index 003d839d6..000000000 --- a/scripts/Generate-SourceFromSchema.ps1 +++ /dev/null @@ -1,49 +0,0 @@ -Write-Host "Generating source from schema..." - -$schemaPath = ".\src\Whim.Yaml\schema.json" -$outputPath = ".\src\Whim.Yaml\Generated" -$metadataPath = "$outputPath\metadata.json" - -$schemaHash = (Get-FileHash $schemaPath -Algorithm SHA256).Hash - -function Test-Regenerate { - if (!(Test-Path $outputPath)) { - Write-Host "Output directory does not exist, generating..." - New-Item $outputPath -ItemType Directory - return $true - } - - Write-Host "Output directory exists..." - - if ((Test-Path $schemaPath) -eq $false) { - Write-Host "Schema file does not exist, skipping..." - return $false - } - - $metadata = Get-Content $metadataPath | ConvertFrom-Json - if ($metadata.schemaHash -ne $schemaHash) { - Write-Host "Schema has changed since last generation, regenerating..." - Write-Host "Old hash: $($metadata.schemaHash)" - Write-Host "New hash: $schemaHash" - return $true - } - - return $false -} - -if (!(Test-Regenerate -schemaPath $schemaPath -outputPath $outputPath)) { - Write-Host "Skipping generation..." - return -} - -dotnet tool run generatejsonschematypes ` - $schemaPath ` - --rootNamespace Whim.Yaml ` - --useSchema Draft7 ` - --outputPath $outputPath - -# If not in CI, write metadata file -if ($LASTEXITCODE -eq 0 -and $null -eq $env:CI) { - Write-Host "Writing metadata file..." - @{ schemaHash = $schemaHash } | ConvertTo-Json | Set-Content $metadataPath -} diff --git a/src/Whim.Yaml/Schema.cs b/src/Whim.Yaml/Schema.cs new file mode 100644 index 000000000..eb4043f21 --- /dev/null +++ b/src/Whim.Yaml/Schema.cs @@ -0,0 +1,6 @@ +using Corvus.Json; + +namespace Whim.Yaml; + +[JsonSchemaTypeGenerator("./schema.json")] +public readonly partial struct Schema { } diff --git a/src/Whim.Yaml/Whim.Yaml.csproj b/src/Whim.Yaml/Whim.Yaml.csproj index 368a29391..05b59d1bc 100644 --- a/src/Whim.Yaml/Whim.Yaml.csproj +++ b/src/Whim.Yaml/Whim.Yaml.csproj @@ -50,10 +50,18 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + @@ -63,10 +71,4 @@ MSBuild:Compile - - - - - - \ No newline at end of file diff --git a/src/Whim.Yaml/YamlBarPluginLoader.cs b/src/Whim.Yaml/YamlBarPluginLoader.cs index a7227a585..c73edbba6 100644 --- a/src/Whim.Yaml/YamlBarPluginLoader.cs +++ b/src/Whim.Yaml/YamlBarPluginLoader.cs @@ -13,9 +13,13 @@ internal static class YamlBarPluginLoader "CA2000:Dispose objects before losing scope", Justification = "Items will be disposed by the context where appropriate." )] - public static void LoadBarPlugin(IContext ctx, Schema schema) + public static void LoadBarPlugin(IContext ctx, Schema.PluginsEntity plugins) { - var bar = schema.Plugins.Bar; + if (plugins.Bar is not { } bar) + { + Logger.Debug("Bar plugin configuration not found."); + return; + } if (!bar.IsValid()) { @@ -23,7 +27,7 @@ public static void LoadBarPlugin(IContext ctx, Schema schema) return; } - if (bar.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (bar.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("Bar plugin is not enabled."); return; @@ -35,12 +39,12 @@ public static void LoadBarPlugin(IContext ctx, Schema schema) BarConfig config = new(leftComponents, centerComponents, rightComponents); - if (bar.Height.AsOptional() is { } height) + if (bar.Height is { } height) { config.Height = (int)height; } - if (bar.Backdrop.AsOptional() is { } backdrop) + if (bar.Backdrop is { } backdrop) { config.Backdrop = YamlLoaderUtils.ParseWindowBackdropConfig(backdrop); } @@ -48,82 +52,79 @@ public static void LoadBarPlugin(IContext ctx, Schema schema) ctx.PluginManager.AddPlugin(new BarPlugin(ctx, config)); } - private static List GetBarComponents(IContext ctx, Schema.DefsRequiredEntries componentsWrapper) + private static List GetBarComponents(IContext ctx, Schema.SchemaRequiredEntries? componentsWrapper) { - if (componentsWrapper.Entries.AsOptional() is not { } entries) + if (componentsWrapper is not { } components) { return []; } - List components = []; + List barComponents = []; - foreach (var entry in entries) + foreach (var entry in components.Entries) { entry.Match( - (in Schema.AWidgetToDisplayTheActiveLayout widget) => + (in Schema.SchemaRequiredType4 widget) => { - components.Add(CreateActiveLayoutBarWidget(ctx, widget)); + barComponents.Add(CreateActiveLayoutBarWidget(ctx, widget)); return null; }, - (in Schema.AWidgetToDisplayTheBatteryStatus widget) => + (in Schema.SchemaRequiredType5 widget) => { - components.Add(CreateBatteryBarWidget(ctx, widget)); + barComponents.Add(CreateBatteryBarWidget(ctx, widget)); return null; }, - (in Schema.AWidgetToDisplayTheDateAndTime widget) => + (in Schema.SchemaRequiredType6 widget) => { - components.Add(CreateDateTimeBarWidget(ctx, widget)); + barComponents.Add(CreateDateTimeBarWidget(ctx, widget)); return null; }, - (in Schema.AWidgetToDisplayTheFocusedWindow widget) => + (in Schema.SchemaRequiredType7 widget) => { - components.Add(CreateFocusedWindowBarWidget(ctx, widget)); + barComponents.Add(CreateFocusedWindowBarWidget(ctx, widget)); return null; }, - (in Schema.AWidgetToDisplayTheWorkspace widget) => + (in Schema.SchemaRequiredType9 widget) => { - components.Add(CreateWorkspaceBarWidget(ctx, widget)); + barComponents.Add(CreateWorkspaceBarWidget(ctx, widget)); return null; }, - (in Schema.DefsRequiredType3 widget) => + (in Schema.SchemaRequiredType8 widget) => { - components.Add(CreateTreeLayoutEngineBarWidget(ctx, widget)); + barComponents.Add(CreateTreeLayoutEngineBarWidget(ctx, widget)); return null; }, - (in Schema.DefsRequiredType2 _) => + (in Schema.SchemaRequiredType3 _) => { return null; } ); } - return components; + return barComponents; } - private static BarComponent CreateActiveLayoutBarWidget(IContext ctx, Schema.AWidgetToDisplayTheActiveLayout widget) + private static BarComponent CreateActiveLayoutBarWidget(IContext ctx, Schema.SchemaRequiredType4 widget) { return ActiveLayoutWidget.CreateComponent(); } - private static BarComponent CreateBatteryBarWidget(IContext ctx, Schema.AWidgetToDisplayTheBatteryStatus widget) + private static BarComponent CreateBatteryBarWidget(IContext ctx, Schema.SchemaRequiredType5 widget) { return BatteryWidget.CreateComponent(); } - private static BarComponent CreateDateTimeBarWidget(IContext ctx, Schema.AWidgetToDisplayTheDateAndTime widget) + private static BarComponent CreateDateTimeBarWidget(IContext ctx, Schema.SchemaRequiredType6 widget) { - int interval = (int?)widget.Interval.AsOptional() ?? 1000; - string format = (string?)widget.Format.AsOptional() ?? "yyyy-MM-dd HH:mm:ss"; + int interval = widget.Interval is { } i ? (int)i : 1000; + string format = widget.Format is { } f ? (string)f : "yyyy-MM-dd HH:mm:ss"; return DateTimeWidget.CreateComponent(interval, format); } - private static BarComponent CreateFocusedWindowBarWidget( - IContext ctx, - Schema.AWidgetToDisplayTheFocusedWindow widget - ) + private static BarComponent CreateFocusedWindowBarWidget(IContext ctx, Schema.SchemaRequiredType7 widget) { - bool shortenTitle = widget.ShortenTitle.AsOptional() ?? false; + bool shortenTitle = widget.ShortenTitle is { } st && st; Func getTitle = shortenTitle ? FocusedWindowWidget.GetShortTitle : FocusedWindowWidget.GetTitle; @@ -131,12 +132,12 @@ Schema.AWidgetToDisplayTheFocusedWindow widget return FocusedWindowWidget.CreateComponent(getTitle); } - private static BarComponent CreateWorkspaceBarWidget(IContext ctx, Schema.AWidgetToDisplayTheWorkspace widget) + private static BarComponent CreateWorkspaceBarWidget(IContext ctx, Schema.SchemaRequiredType9 widget) { return WorkspaceWidget.CreateComponent(); } - private static BarComponent CreateTreeLayoutEngineBarWidget(IContext ctx, Schema.DefsRequiredType3 widget) + private static BarComponent CreateTreeLayoutEngineBarWidget(IContext ctx, Schema.SchemaRequiredType8 widget) { if ( ctx.PluginManager.LoadedPlugins.FirstOrDefault(p => p.Name == "whim.tree_layout") diff --git a/src/Whim.Yaml/YamlLayoutEngineLoader.cs b/src/Whim.Yaml/YamlLayoutEngineLoader.cs index 1bfa92e64..a1545b77c 100644 --- a/src/Whim.Yaml/YamlLayoutEngineLoader.cs +++ b/src/Whim.Yaml/YamlLayoutEngineLoader.cs @@ -1,4 +1,3 @@ -using Corvus.Json; using Whim.FloatingWindow; using Whim.SliceLayout; using Whim.TreeLayout; @@ -9,13 +8,13 @@ internal static class YamlLayoutEngineLoader { public static void UpdateLayoutEngines(IContext ctx, Schema schema) { - if (schema.LayoutEngines.AsOptional() is not { } layoutEngines) + if (schema.LayoutEngines is not { } layoutEngines) { Logger.Debug("No layout engines found."); return; } - CreateLeafLayoutEngine[]? engineCreators = GetCreateLeafLayoutEngines(ctx, [.. layoutEngines.Entries]); + CreateLeafLayoutEngine[]? engineCreators = GetCreateLeafLayoutEngines(ctx, layoutEngines.Entries); if (engineCreators is not null) { @@ -25,25 +24,20 @@ public static void UpdateLayoutEngines(IContext ctx, Schema schema) public static CreateLeafLayoutEngine[]? GetCreateLeafLayoutEngines( IContext ctx, - Schema.RequiredType[]? layoutEngines + Schema.RequiredEntries.RequiredTypeArray layoutEngines ) { - if (layoutEngines is null) - { - return null; - } - List leafLayoutEngineCreators = []; foreach (var engine in layoutEngines) { engine.Match( - (in Schema.DefsRequiredType floatingWindow) => + (in Schema.SchemaRequiredType floatingWindow) => { CreateFloatingLayoutEngineCreator(ctx, leafLayoutEngineCreators); return null; }, - (in Schema.ALayoutEngineThatDisplaysOneWindowAtATime focusLayoutEngine) => + (in Schema.SchemaRequiredType1 focusLayoutEngine) => { CreateFocusLayoutEngineCreator(ctx, leafLayoutEngineCreators, focusLayoutEngine); return null; @@ -53,7 +47,7 @@ public static void UpdateLayoutEngines(IContext ctx, Schema schema) CreateSliceLayoutEngineCreator(ctx, leafLayoutEngineCreators, sliceLayoutEngine); return null; }, - (in Schema.ALayoutEngineThatArrangesWindowsInATreeStructure treeLayoutEngine) => + (in Schema.SchemaRequiredType2 treeLayoutEngine) => { CreateTreeLayoutEngineCreator(ctx, leafLayoutEngineCreators, treeLayoutEngine); return null; @@ -77,10 +71,10 @@ List leafLayoutEngineCreators private static void CreateFocusLayoutEngineCreator( IContext ctx, List leafLayoutEngineCreators, - Schema.ALayoutEngineThatDisplaysOneWindowAtATime focusLayoutEngine + Schema.SchemaRequiredType1 focusLayoutEngine ) { - bool maximize = focusLayoutEngine.Maximize.AsOptional() ?? false; + bool maximize = focusLayoutEngine.Maximize is { } m && m; leafLayoutEngineCreators.Add((id) => new FocusLayoutEngine(id, maximize)); } @@ -100,22 +94,22 @@ is not SliceLayoutPlugin plugin } sliceLayoutEngine.Variant.Match( - (in Schema.RequiredTypeAndVariant.VariantEntity.CreatesAColumnLayoutWhereWindowsAreStackedVertically _) => + (in Schema.RequiredTypeAndVariant.VariantEntity.RequiredType _) => { leafLayoutEngineCreators.Add((id) => SliceLayouts.CreateColumnLayout(ctx, plugin, id)); return null; }, - (in Schema.RequiredTypeAndVariant.VariantEntity.CreatesARowLayoutWhereWindowsAreStackedHorizontally _) => + (in Schema.RequiredTypeAndVariant.VariantEntity.VariantEntityRequiredType _) => { leafLayoutEngineCreators.Add((id) => SliceLayouts.CreateRowLayout(ctx, plugin, id)); return null; }, - (in Schema.RequiredTypeAndVariant.VariantEntity.RequiredType _) => + (in Schema.RequiredTypeAndVariant.VariantEntity.VariantEntityRequiredType1 _) => { leafLayoutEngineCreators.Add((id) => SliceLayouts.CreatePrimaryStackLayout(ctx, plugin, id)); return null; }, - (in Schema.RequiredTypeAndVariant.VariantEntity.RequiredTypeAndColumns multiColumnLayout) => + (in Schema.RequiredTypeAndVariant.VariantEntity.RequiredColumnsAndType multiColumnLayout) => { var columns = multiColumnLayout.Columns; uint[] unsignedColumns = columns.Select(c => (uint)c).ToArray(); @@ -124,10 +118,10 @@ is not SliceLayoutPlugin plugin ); return null; }, - (in Schema.RequiredTypeAndVariant.VariantEntity.AnyOfRequiredType secondaryPrimaryStack) => + (in Schema.RequiredTypeAndVariant.VariantEntity.VariantEntityRequiredType2 secondaryPrimaryStack) => { - var primaryCapacity = (uint?)secondaryPrimaryStack.PrimaryCapacity.AsOptional() ?? 1; - var secondaryCapacity = (uint?)secondaryPrimaryStack.SecondaryCapacity.AsOptional() ?? 2; + uint primaryCapacity = secondaryPrimaryStack.PrimaryCapacity is { } pc ? (uint)pc : 1; + uint secondaryCapacity = secondaryPrimaryStack.SecondaryCapacity is { } sc ? (uint)sc : 2; leafLayoutEngineCreators.Add( (id) => @@ -142,7 +136,7 @@ is not SliceLayoutPlugin plugin private static void CreateTreeLayoutEngineCreator( IContext ctx, List leafLayoutEngineCreators, - Schema.ALayoutEngineThatArrangesWindowsInATreeStructure treeLayoutEngine + Schema.SchemaRequiredType2 treeLayoutEngine ) { if ( @@ -155,7 +149,7 @@ is not TreeLayoutPlugin plugin } Direction defaultAddNodeDirection = Direction.Right; - if (treeLayoutEngine.InitialDirection.TryGetString(out string? initialDirection)) + if (((string?)treeLayoutEngine.InitialDirection) is { } initialDirection) { defaultAddNodeDirection = initialDirection switch { diff --git a/src/Whim.Yaml/YamlLoader.cs b/src/Whim.Yaml/YamlLoader.cs index d9145db28..192695396 100644 --- a/src/Whim.Yaml/YamlLoader.cs +++ b/src/Whim.Yaml/YamlLoader.cs @@ -126,13 +126,13 @@ private static void ShowError(IContext ctx, string errors) private static void UpdateWorkspaces(IContext ctx, Schema schema) { - if (!schema.Workspaces.IsValid()) + if (schema.Workspaces is not { } workspaces || !workspaces.IsValid()) { Logger.Debug("Workspaces config is not valid."); return; } - if (schema.Workspaces.Entries.AsOptional() is not { } entries) + if (workspaces.Entries is not { } entries) { Logger.Debug("No workspaces found."); return; @@ -145,12 +145,12 @@ private static void UpdateWorkspaces(IContext ctx, Schema schema) CreateLeafLayoutEngine[]? engines = null; List? monitorIndices = null; - if (workspace.LayoutEngines.Entries.AsOptional() is Schema.RequiredEntries.RequiredTypeArray definedEngines) + if (workspace.LayoutEngines?.Entries is { } definedEngines) { engines = YamlLayoutEngineLoader.GetCreateLeafLayoutEngines(ctx, [.. definedEngines]); } - if (workspace.Monitors.AsOptional() is Schema.RequiredName.MonitorsEntityArray definedMonitorIndices) + if (workspace.Monitors is { } definedMonitorIndices) { monitorIndices = []; foreach (var monitorIndex in definedMonitorIndices) @@ -165,18 +165,18 @@ private static void UpdateWorkspaces(IContext ctx, Schema schema) private static void UpdateKeybinds(IContext ctx, Schema schema) { - if (!schema.Keybinds.IsValid()) + if (schema.Keybinds is not { } keybinds || !keybinds.IsValid()) { Logger.Debug("Keybinds config is not valid."); return; } - if (schema.Keybinds.UnifyKeyModifiers.TryGetBoolean(out bool unifyKeyModifiers)) + if (keybinds.UnifyKeyModifiers?.TryGetBoolean(out bool unifyKeyModifiers) == true) { ctx.KeybindManager.UnifyKeyModifiers = unifyKeyModifiers; } - if (schema.Keybinds.Entries.AsOptional() is not { } entries) + if (keybinds.Entries is not { } entries) { Logger.Debug("No keybinds found."); return; @@ -196,13 +196,13 @@ private static void UpdateKeybinds(IContext ctx, Schema schema) private static void UpdateFilters(IContext ctx, Schema schema) { - if (!schema.Filters.IsValid()) + if (schema.Filters is not { } filters || !filters.IsValid()) { Logger.Debug("Filters config is not valid."); return; } - if (schema.Filters.Entries.AsOptional() is not { } entries) + if (filters.Entries is not { } entries) { Logger.Debug("No filters found."); return; @@ -235,22 +235,24 @@ private static void UpdateFilters(IContext ctx, Schema schema) private static void UpdateRouters(IContext ctx, Schema schema) { - if (!schema.Routers.IsValid()) + if (schema.Routers is not { } routers || !routers.IsValid()) { Logger.Debug("Routers cohfig is not valid."); return; } if ( - schema.Routers.RoutingBehavior.TryGetString(out string? routingBehavior) - && Enum.TryParse(routingBehavior?.SnakeToPascal(), out RouterOptions routerOptions) + routers.RoutingBehavior?.TryGetString(out string? routingBehavior) == true + && routingBehavior != null + && Enum.TryParse(routingBehavior.SnakeToPascal(), out RouterOptions routerOptions) ) { ctx.RouterManager.RouterOptions = routerOptions; } - if (schema.Routers.Entries.AsOptional() is not { } entries) + if (routers.Entries is not { } entries) { + Logger.Debug("No routers found."); return; } @@ -282,13 +284,13 @@ private static void UpdateRouters(IContext ctx, Schema schema) private static void UpdateStyles(IContext ctx, Schema schema, bool showErrorWindow) { - if (!schema.Styles.IsValid()) + if (schema.Styles is not { } styles || !styles.IsValid()) { Logger.Debug("Styles config is not valid."); return; } - if (schema.Styles.UserDictionaries.AsOptional() is not { } userDictionaries) + if (styles.UserDictionaries is not { } userDictionaries) { Logger.Debug("No styles found."); return; diff --git a/src/Whim.Yaml/YamlLoaderUtils.cs b/src/Whim.Yaml/YamlLoaderUtils.cs index 00454ecf6..d9142b02a 100644 --- a/src/Whim.Yaml/YamlLoaderUtils.cs +++ b/src/Whim.Yaml/YamlLoaderUtils.cs @@ -1,7 +1,6 @@ using System.Drawing; using System.Runtime.CompilerServices; using System.Text; -using Corvus.Json; using static Whim.Yaml.Schema; namespace Whim.Yaml; @@ -40,12 +39,12 @@ internal static WindowBackdropConfig ParseWindowBackdropConfig(WindowBackdropCon BackdropType backdropType = BackdropType.Mica; bool alwaysShowBackdrop = true; - if (entity.Type.AsOptional() is { } backdropTypeStr) + if (entity.Type is { } backdropTypeStr) { backdropType = ((string)backdropTypeStr).ParseBackdropType(); } - if (entity.AlwaysShowBackdrop.AsOptional() is { } alwaysShowBackdropValue) + if (entity.AlwaysShowBackdrop is { } alwaysShowBackdropValue) { alwaysShowBackdrop = alwaysShowBackdropValue; } diff --git a/src/Whim.Yaml/YamlPluginLoader.cs b/src/Whim.Yaml/YamlPluginLoader.cs index e64d62671..ff64d83f9 100644 --- a/src/Whim.Yaml/YamlPluginLoader.cs +++ b/src/Whim.Yaml/YamlPluginLoader.cs @@ -26,28 +26,38 @@ internal static class YamlPluginLoader { public static void LoadPlugins(IContext ctx, Schema schema) { - LoadFocusIndicatorPlugin(ctx, schema); - LoadLayoutPreviewPlugin(ctx, schema); - LoadUpdaterPlugin(ctx, schema); - LoadSliceLayoutPlugin(ctx, schema); - LoadTreeLayoutPlugin(ctx, schema); + if (schema.Plugins is not { } plugins) + { + Logger.Debug("No plugins configuration found."); + return; + } + + LoadFocusIndicatorPlugin(ctx, plugins); + LoadLayoutPreviewPlugin(ctx, plugins); + LoadUpdaterPlugin(ctx, plugins); + LoadSliceLayoutPlugin(ctx, plugins); + LoadTreeLayoutPlugin(ctx, plugins); // Load the command palette after the TreeLayoutPlugin, as it has dependencies on the TreeLayout plugin. - LoadCommandPalettePlugin(ctx, schema); + LoadCommandPalettePlugin(ctx, plugins); // Load the bar plugin after the TreeLayoutPlugin, as it has dependencies on the TreeLayout plugin. - YamlBarPluginLoader.LoadBarPlugin(ctx, schema); + YamlBarPluginLoader.LoadBarPlugin(ctx, plugins); // It's important for FloatingWindowPlugin to immediately precede GapsPlugin in the plugin loading order. // This ensures that the GapsLayoutEngine will immediately contain the ProxyFloatingLayoutEngine, which // is required for preventing gaps for floating windows. - LoadFloatingWindowPlugin(ctx, schema); - LoadGapsPlugin(ctx, schema); + LoadFloatingWindowPlugin(ctx, plugins); + LoadGapsPlugin(ctx, plugins); } - private static void LoadGapsPlugin(IContext ctx, Schema schema) + private static void LoadGapsPlugin(IContext ctx, Schema.PluginsEntity plugins) { - var gaps = schema.Plugins.Gaps; + if (plugins.Gaps is not { } gaps) + { + Logger.Debug("Gaps plugin configuration not found."); + return; + } if (!gaps.IsValid()) { @@ -55,7 +65,7 @@ private static void LoadGapsPlugin(IContext ctx, Schema schema) return; } - if (gaps.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (gaps.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("Gaps plugin is not enabled."); return; @@ -63,22 +73,22 @@ private static void LoadGapsPlugin(IContext ctx, Schema schema) GapsConfig config = new(); - if (gaps.OuterGap.AsOptional() is { } outerGap) + if (gaps.OuterGap is { } outerGap) { config.OuterGap = (int)outerGap; } - if (gaps.InnerGap.AsOptional() is { } innerGap) + if (gaps.InnerGap is { } innerGap) { config.InnerGap = (int)innerGap; } - if (gaps.DefaultOuterDelta.AsOptional() is { } defaultOuterDelta) + if (gaps.DefaultOuterDelta is { } defaultOuterDelta) { config.DefaultOuterDelta = (int)defaultOuterDelta; } - if (gaps.DefaultInnerDelta.AsOptional() is { } defaultInnerDelta) + if (gaps.DefaultInnerDelta is { } defaultInnerDelta) { config.DefaultInnerDelta = (int)defaultInnerDelta; } @@ -86,9 +96,13 @@ private static void LoadGapsPlugin(IContext ctx, Schema schema) ctx.PluginManager.AddPlugin(new GapsPlugin(ctx, config)); } - private static void LoadCommandPalettePlugin(IContext ctx, Schema schema) + private static void LoadCommandPalettePlugin(IContext ctx, Schema.PluginsEntity plugins) { - var commandPalette = schema.Plugins.CommandPalette; + if (plugins.CommandPalette is not { } commandPalette) + { + Logger.Debug("CommandPalette plugin configuration not found."); + return; + } if (!commandPalette.IsValid()) { @@ -96,7 +110,7 @@ private static void LoadCommandPalettePlugin(IContext ctx, Schema schema) return; } - if (commandPalette.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (commandPalette.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("CommandPalette plugin is not enabled."); return; @@ -104,22 +118,22 @@ private static void LoadCommandPalettePlugin(IContext ctx, Schema schema) CommandPaletteConfig config = new(ctx); - if (commandPalette.MaxHeightPercent.AsOptional() is { } maxHeightPercent) + if (commandPalette.MaxHeightPercent is { } maxHeightPercent) { config.MaxHeightPercent = (int)maxHeightPercent; } - if (commandPalette.MaxWidthPixels.AsOptional() is { } maxWidthPixels) + if (commandPalette.MaxWidthPixels is { } maxWidthPixels) { config.MaxWidthPixels = (int)maxWidthPixels; } - if (commandPalette.YPositionPercent.AsOptional() is { } yPositionPercent) + if (commandPalette.YPositionPercent is { } yPositionPercent) { config.YPositionPercent = (int)yPositionPercent; } - if (commandPalette.Backdrop.AsOptional() is { } backdrop) + if (commandPalette.Backdrop is { } backdrop) { config.Backdrop = YamlLoaderUtils.ParseWindowBackdropConfig(backdrop); } @@ -141,15 +155,21 @@ is null } } - private static void LoadFloatingWindowPlugin(IContext ctx, Schema schema) + private static void LoadFloatingWindowPlugin(IContext ctx, Schema.PluginsEntity plugins) { - if (!schema.Plugins.FloatingWindow.IsValid()) + if (plugins.FloatingWindow is not { } floatingWindow) + { + Logger.Debug("FloatingWindow plugin configuration not found."); + return; + } + + if (!floatingWindow.IsValid()) { Logger.Debug("FloatingWindow plugin is not valid."); return; } - if (schema.Plugins.FloatingWindow.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (floatingWindow.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("FloatingWindow plugin is not enabled."); return; @@ -158,9 +178,13 @@ private static void LoadFloatingWindowPlugin(IContext ctx, Schema schema) ctx.PluginManager.AddPlugin(new FloatingWindowPlugin(ctx)); } - private static void LoadFocusIndicatorPlugin(IContext ctx, Schema schema) + private static void LoadFocusIndicatorPlugin(IContext ctx, Schema.PluginsEntity plugins) { - var focusIndicator = schema.Plugins.FocusIndicator; + if (plugins.FocusIndicator is not { } focusIndicator) + { + Logger.Debug("FocusIndicator plugin configuration not found."); + return; + } if (!focusIndicator.IsValid()) { @@ -168,7 +192,7 @@ private static void LoadFocusIndicatorPlugin(IContext ctx, Schema schema) return; } - if (focusIndicator.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (focusIndicator.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("FocusIndicator plugin is not enabled."); return; @@ -176,7 +200,7 @@ private static void LoadFocusIndicatorPlugin(IContext ctx, Schema schema) FocusIndicatorConfig config = new(); - if (focusIndicator.Color.AsOptional() is { } color) + if (focusIndicator.Color is { } color) { string colorStr = (string)color; var rawColor = colorStr.ParseColor(); @@ -185,17 +209,17 @@ private static void LoadFocusIndicatorPlugin(IContext ctx, Schema schema) config.Color = new SolidColorBrush(winUiColor); } - if (focusIndicator.BorderSize.AsOptional() is { } borderSize) + if (focusIndicator.BorderSize is { } borderSize) { config.BorderSize = (int)borderSize; } - if (focusIndicator.IsFadeEnabled.AsOptional() is { } isFadeEnabled) + if (focusIndicator.IsFadeEnabled is { } isFadeEnabled) { config.FadeEnabled = isFadeEnabled; } - if (focusIndicator.FadeTimeout.AsOptional() is { } fadeTimeout) + if (focusIndicator.FadeTimeout is { } fadeTimeout) { config.FadeTimeout = TimeSpan.FromSeconds((int)fadeTimeout); } @@ -203,9 +227,13 @@ private static void LoadFocusIndicatorPlugin(IContext ctx, Schema schema) ctx.PluginManager.AddPlugin(new FocusIndicatorPlugin(ctx, config)); } - private static void LoadLayoutPreviewPlugin(IContext ctx, Schema schema) + private static void LoadLayoutPreviewPlugin(IContext ctx, Schema.PluginsEntity plugins) { - var layoutPreview = schema.Plugins.LayoutPreview; + if (plugins.LayoutPreview is not { } layoutPreview) + { + Logger.Debug("LayoutPreview plugin configuration not found."); + return; + } if (!layoutPreview.IsValid()) { @@ -213,7 +241,7 @@ private static void LoadLayoutPreviewPlugin(IContext ctx, Schema schema) return; } - if (layoutPreview.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (layoutPreview.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("LayoutPreview plugin is not enabled."); return; @@ -222,9 +250,13 @@ private static void LoadLayoutPreviewPlugin(IContext ctx, Schema schema) ctx.PluginManager.AddPlugin(new LayoutPreviewPlugin(ctx)); } - private static void LoadUpdaterPlugin(IContext ctx, Schema schema) + private static void LoadUpdaterPlugin(IContext ctx, Schema.PluginsEntity plugins) { - var updater = schema.Plugins.Updater; + if (plugins.Updater is not { } updater) + { + Logger.Debug("Updater plugin configuration not found."); + return; + } if (!updater.IsValid()) { @@ -232,7 +264,7 @@ private static void LoadUpdaterPlugin(IContext ctx, Schema schema) return; } - if (updater.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (updater.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("Updater plugin is not enabled."); return; @@ -240,7 +272,7 @@ private static void LoadUpdaterPlugin(IContext ctx, Schema schema) UpdaterConfig config = new(); - if (updater.UpdateFrequency.AsOptional() is { } updateFrequency) + if (updater.UpdateFrequency is { } updateFrequency) { string updateFrequencyStr = ((string)updateFrequency).Capitalize(); config.UpdateFrequency = Enum.TryParse(updateFrequencyStr, out var frequency) @@ -248,7 +280,7 @@ private static void LoadUpdaterPlugin(IContext ctx, Schema schema) : UpdateFrequency.Never; } - if (updater.ReleaseChannel.AsOptional() is { } releaseChannel) + if (updater.ReleaseChannel is { } releaseChannel) { string releaseChannelStr = ((string)releaseChannel).Capitalize(); config.ReleaseChannel = Enum.TryParse(releaseChannelStr, out var channel) @@ -259,15 +291,21 @@ private static void LoadUpdaterPlugin(IContext ctx, Schema schema) ctx.PluginManager.AddPlugin(new UpdaterPlugin(ctx, config)); } - private static void LoadSliceLayoutPlugin(IContext ctx, Schema schema) + private static void LoadSliceLayoutPlugin(IContext ctx, Schema.PluginsEntity plugins) { - if (!schema.Plugins.SliceLayout.IsValid()) + if (plugins.SliceLayout is not { } sliceLayout) + { + Logger.Debug("SliceLayout plugin configuration not found."); + return; + } + + if (!sliceLayout.IsValid()) { Logger.Debug("SliceLayout plugin is not valid."); return; } - if (schema.Plugins.SliceLayout.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (sliceLayout.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("SliceLayout plugin is not enabled."); return; @@ -276,15 +314,21 @@ private static void LoadSliceLayoutPlugin(IContext ctx, Schema schema) ctx.PluginManager.AddPlugin(new SliceLayoutPlugin(ctx)); } - private static void LoadTreeLayoutPlugin(IContext ctx, Schema schema) + private static void LoadTreeLayoutPlugin(IContext ctx, Schema.PluginsEntity plugins) { - if (!schema.Plugins.TreeLayout.IsValid()) + if (plugins.TreeLayout is not { } treeLayout) + { + Logger.Debug("TreeLayout plugin configuration not found."); + return; + } + + if (!treeLayout.IsValid()) { Logger.Debug("TreeLayout plugin is not valid."); return; } - if (schema.Plugins.TreeLayout.IsEnabled.AsOptional() is { } isEnabled && !isEnabled) + if (treeLayout.IsEnabled is { } isEnabled && !isEnabled) { Logger.Debug("TreeLayout plugin is not enabled."); return; From 7bdce63e37491b72a59bc4dc01b901628babec32 Mon Sep 17 00:00:00 2001 From: Isaac Daly Date: Sun, 17 Nov 2024 17:00:35 +1100 Subject: [PATCH 2/5] Build before running docfx --- .github/workflows/docs_commit.yml | 11 +++++++++++ src/Whim.Yaml/Whim.Yaml.csproj | 1 - 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.github/workflows/docs_commit.yml b/.github/workflows/docs_commit.yml index 3b3300a46..f99b0aeee 100644 --- a/.github/workflows/docs_commit.yml +++ b/.github/workflows/docs_commit.yml @@ -57,5 +57,16 @@ jobs: run: | dotnet tool restore + - name: Add msbuild to PATH + uses: microsoft/setup-msbuild@v2.0.0 + + - name: Build + run: | + msbuild Whim.sln ` + -p:Configuration=$env:Configuration ` + -p:Platform=$env:Platform ` + -p:BuildInParallel=true ` + -maxCpuCount + - name: Run docfx run: dotnet docfx .\docs\docfx.json diff --git a/src/Whim.Yaml/Whim.Yaml.csproj b/src/Whim.Yaml/Whim.Yaml.csproj index 05b59d1bc..dfee11c6a 100644 --- a/src/Whim.Yaml/Whim.Yaml.csproj +++ b/src/Whim.Yaml/Whim.Yaml.csproj @@ -52,7 +52,6 @@ all - runtime; build; native; contentfiles; analyzers; buildtransitive From 1d29425645d95f02651af560c078e6e6dbb6e673 Mon Sep 17 00:00:00 2001 From: Isaac Daly Date: Sun, 17 Nov 2024 20:17:39 +1100 Subject: [PATCH 3/5] Run metadata --- .github/workflows/docs_commit.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/docs_commit.yml b/.github/workflows/docs_commit.yml index f99b0aeee..765cd4dfc 100644 --- a/.github/workflows/docs_commit.yml +++ b/.github/workflows/docs_commit.yml @@ -68,5 +68,8 @@ jobs: -p:BuildInParallel=true ` -maxCpuCount + - name: Run docfx metadata + run: dotnet docfx metadata .\docs\docfx.json + - name: Run docfx run: dotnet docfx .\docs\docfx.json From e7d1208c5c73a97b8b4d4bd5fb6657e5ea08082c Mon Sep 17 00:00:00 2001 From: Isaac Daly Date: Tue, 19 Nov 2024 07:19:18 +1100 Subject: [PATCH 4/5] Bump versions --- .config/dotnet-tools.json | 4 ++-- Directory.Packages.props | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index aaf8e5c71..a214c9009 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -13,12 +13,12 @@ "rollForward": false }, "docfx": { - "version": "2.77.0", + "version": "2.78.0", "commands": ["docfx"], "rollForward": false }, "corvus.json.jsonschema.typegeneratortool": { - "version": "3.1.1", + "version": "4.0.9", "commands": ["generatejsonschematypes"], "rollForward": false } diff --git a/Directory.Packages.props b/Directory.Packages.props index 775aea4e3..d9bcae2d5 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,8 +3,8 @@ true - - + + From 32cc08b0e32bd30857c05586571f6e59ce92bb2c Mon Sep 17 00:00:00 2001 From: Isaac Daly Date: Tue, 19 Nov 2024 19:13:15 +1100 Subject: [PATCH 5/5] Coverage --- ...amlLoader_LoadFloatingWindowPluginTests.cs | 22 +++++++++++++++++++ ...amlLoader_LoadFocusIndicatorPluginTests.cs | 22 +++++++++++++++++++ ...YamlLoader_LoadLayoutPreviewPluginTests.cs | 22 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadFloatingWindowPluginTests.cs b/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadFloatingWindowPluginTests.cs index 44ec50078..91e33bd33 100644 --- a/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadFloatingWindowPluginTests.cs +++ b/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadFloatingWindowPluginTests.cs @@ -95,6 +95,28 @@ public void LoadFloatingWindowPlugin(string config, bool isYaml, IContext ctx) """, false }, + // YAML, invalid + { + """ + plugins: + floating_window: + bob: true + """, + true + }, + // JSON, invalid + { + """ + { + "plugins": { + "floating_window": { + "bob": true + } + } + } + """, + false + }, }; [Theory, MemberAutoSubstituteData(nameof(DisabledFloatingWindowConfig))] diff --git a/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadFocusIndicatorPluginTests.cs b/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadFocusIndicatorPluginTests.cs index dbd88d6ab..089c373c5 100644 --- a/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadFocusIndicatorPluginTests.cs +++ b/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadFocusIndicatorPluginTests.cs @@ -212,6 +212,28 @@ IContext ctx """, false }, + // YAML, invalid + { + """ + plugins: + focus_indicator: + bob: true + """, + true + }, + // JSON, invalid + { + """ + { + "plugins": { + "focus_indicator": { + "bob": true + } + } + } + """, + false + }, }; [Theory, MemberAutoSubstituteData(nameof(DisabledFocusIndicatorConfig))] diff --git a/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadLayoutPreviewPluginTests.cs b/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadLayoutPreviewPluginTests.cs index f9fd56599..45f8dc88d 100644 --- a/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadLayoutPreviewPluginTests.cs +++ b/src/Whim.Yaml.Tests/Plugins/YamlLoader_LoadLayoutPreviewPluginTests.cs @@ -96,6 +96,28 @@ public void LoadLayoutPreviewPlugin(string yaml, bool isYaml, bool isEnabled, IC """, false }, + // YAML, invalid + { + """ + plugins: + layout_preview: + bob: true + """, + true + }, + // JSON, invalid + { + """ + { + "plugins": { + "layout_preview": { + "bob": true + } + } + } + """, + false + }, }; [Theory, MemberAutoSubstituteData(nameof(DisabledLayoutPreviewConfig))]