Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Corvus.JsonSchema 4 #1081

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ jobs:
- name: Add msbuild to PATH
uses: microsoft/[email protected]

- name: Build schema
run: |
.\scripts\Generate-SourceFromSchema.ps1

- name: Build
run: |
msbuild Whim.sln `
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/docs_commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions .github/workflows/docs_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ jobs:
- name: Add msbuild to PATH
uses: microsoft/[email protected]

- name: Build schema
run: |
.\scripts\Generate-SourceFromSchema.ps1

- name: Build
run: |
msbuild Whim.sln `
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,3 @@ bin/whim-install.exe
# docfx
api
_site

# Whim.Yaml generated files
src/Whim.Yaml/Generated/
21 changes: 11 additions & 10 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Corvus.Json.ExtendedTypes" Version="3.1.1" />
<PackageVersion Include="DotNext" Version="5.11.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Scripting" Version="4.10.0" />
<PackageVersion Include="Microsoft.Win32.SystemEvents" Version="8.0.0" />
<PackageVersion Include="Corvus.Json.ExtendedTypes" Version="4.0.7" />
<PackageVersion Include="Corvus.Json.SourceGenerator" Version="4.0.7" />
<PackageVersion Include="DotNext" Version="5.14.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Scripting" Version="4.11.0" />
<PackageVersion Include="Microsoft.Win32.SystemEvents" Version="9.0.0" />
<PackageVersion Include="Microsoft.Windows.CsWin32" Version="0.3.106" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.5.240627000" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageVersion Include="Octokit" Version="13.0.1" />
<PackageVersion Include="Serilog" Version="4.0.1" />
<PackageVersion Include="Serilog.Sinks.Async" Version="2.0.0" />
<PackageVersion Include="Serilog" Version="4.1.0" />
<PackageVersion Include="Serilog.Sinks.Async" Version="2.1.0" />
<PackageVersion Include="Serilog.Sinks.Debug" Version="3.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="System.Drawing.Common" Version="8.0.7" />
<PackageVersion Include="System.Drawing.Common" Version="9.0.0" />
<PackageVersion Include="Yaml2JsonNode" Version="2.1.1" />
<PackageVersion Include="YamlDotNet" Version="16.1.3" />
<PackageVersion Include="YamlDotNet" Version="16.2.0" />
</ItemGroup>
<!-- Test dependencies -->
<ItemGroup>
Expand Down
49 changes: 0 additions & 49 deletions scripts/Generate-SourceFromSchema.ps1

This file was deleted.

6 changes: 6 additions & 0 deletions src/Whim.Yaml/Schema.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Corvus.Json;

namespace Whim.Yaml;

[JsonSchemaTypeGenerator("./schema.json")]
public readonly partial struct Schema { }
14 changes: 8 additions & 6 deletions src/Whim.Yaml/Whim.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@

<ItemGroup>
<PackageReference Include="Corvus.Json.ExtendedTypes" />
<PackageReference Include="Corvus.Json.SourceGenerator">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Yaml2JsonNode" />
<PackageReference Include="YamlDotNet" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="schema.json" />
</ItemGroup>

<ItemGroup>
<None Remove="ErrorWindow.xaml" />
</ItemGroup>
Expand All @@ -63,10 +71,4 @@
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>

<Target Name="GenerateSchema" BeforeTargets="BeforeCompile">
<Exec Command="powershell.exe -NonInteractive -ExecutionPolicy Unrestricted .\scripts\Generate-SourceFromSchema.ps1" WorkingDirectory="..\..\" ConsoleToMsBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
</Exec>
</Target>
</Project>
71 changes: 36 additions & 35 deletions src/Whim.Yaml/YamlBarPluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ 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())
{
Logger.Debug("Bar plugin is not valid.");
return;
}

if (bar.IsEnabled.AsOptional() is { } isEnabled && !isEnabled)
if (bar.IsEnabled is { } isEnabled && !isEnabled)
{
Logger.Debug("Bar plugin is not enabled.");
return;
Expand All @@ -35,108 +39,105 @@ 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);
}

ctx.PluginManager.AddPlugin(new BarPlugin(ctx, config));
}

private static List<BarComponent> GetBarComponents(IContext ctx, Schema.DefsRequiredEntries componentsWrapper)
private static List<BarComponent> GetBarComponents(IContext ctx, Schema.SchemaRequiredEntries? componentsWrapper)
{
if (componentsWrapper.Entries.AsOptional() is not { } entries)
if (componentsWrapper is not { } components)
{
return [];
}

List<BarComponent> components = [];
List<BarComponent> barComponents = [];

foreach (var entry in entries)
foreach (var entry in components.Entries)
{
entry.Match<object?>(
(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<IWindow, string> getTitle = shortenTitle
? FocusedWindowWidget.GetShortTitle
: FocusedWindowWidget.GetTitle;

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")
Expand Down
Loading
Loading