question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Discussion] [DevTools] [Spec] DevTool Settings Roslyn Source Generator

See original GitHub issue

DevTool Settings Roslyn Source Generator

Hi, I’ve done various tests on how to manage the DevTool configuration. But before I start investing time in development, I’d like to hear from you.

I would like to use Roslyn Source Generator.

I would like to create the following structure:

  • Avalonia.Diagnostics.Settings.Generators
  • Avalonia.Diagnostics.Common
  • Avalonia.Diagnostics.Settings.Generators.Xml
  • Avalonia.Diagnostics.Settings.Xml
  • ec…

Avalonia.Diagnostics.Settings.Generators

It is a code analyzer that generates DevToolsOptions based and a file describing its structure

Avalonia.Diagnostics.Common

dependencies: Avalonia.Diagnostics.Generators(only in build) contents:

  • DevToolsOptions.yaml
  • DevToolsOptions.cs
  • DevToolsOptions.Generated.cs
  • IDevToolOptionsStoreProvider
  • IDevTool

DevToolsOptions.yaml

this is the file that contains the DevToolsOptions structure which will then be expanded into DevToolsOptions.Generated.cs. I thought about YAML because it is easy to read and not very verbose.

example of possible file:

Version: "1.0.0.0" #Type:System.Verison
Window:
  StartupLocation: "CenterScreen" #Type:Avalonia.Controls.WindowStartupLocation
  State: "Normal" #Type:Avalonia.Controls.WindowState
  Position: null #Type:Avalonia.PixelPoint?
Console:
  IsVisible: false #Type:System.Boolean
  Height: NaN #Type:System.Double
ShouldVisualizeMarginPadding: true #Type:System.Boolean
ShouldVisualizeDirtyRects: false #Type:System.Boolean
ShowFpsOverlay: false #Type:System.Boolean
FavoritesProperties:
  - FavoriteProperties:
    Name: "All" #Type:System.String
  - FavoriteProperties:
    Name: "Layout"
    Properties: #Type:System.String[]
      - Item: "Bounds"
      - Item: "Clips"
      - Item: "Height"

Directives:

  • # Type indicates the type of property, if not provided treat it as a new object; example: Console will create a class named ConsoleSettings
  • ? after the type name indicates that it is a null type
  • [] after the type name indicates that it is an array

DevToolsOptions.cs

the partial class is used to not report an error in the intellisense when DevToolsOptions has not yet been generated.

public partial class DevToolsOptions
{
}

IDevToolsOptionsStoreProvider.cs

defines the interface for loading or saving the configuration

public interface IDevToolsOptionsStoreProvider
{
   void Load(DevToolsOptions options);
   Task Save(DevToolsOptions options); // it is asynchronous because saving can take a long time
}

IDevTool.cs

defines the interface returned by the AttachDevTools extension method and allows configuration of additional options

public interface IDevTool
{
   IDevTool With(DevToolsOptions options);
   IDevTool With(IDevToolsOptionsStoreProvider storeProvider);
}

Avalonia.Diagnostics.Settings.Generators.Xml

dependencies: Avalonia.Diagnostics.Common It is a code analyzer that will generate DevToolsOptions.yaml as it persists in xml type configuration without using reflection(‘magic’)

Avalonia.Diagnostics.Settings.Xml

dependencies: DevToolsOptions.yaml, Avalonia.Diagnostics.Common, Avalonia.Diagnostics.Settings.Generators.Xml (only in build). contents:

  • DevToolsOptions.yaml (link)
  • XmlSettingStore (partial class)

Usage examples

In memory (Actual behavior)

public MainWindow()
{
   this.InitializeComponent();
   this.AttachDevTools();
}

With custom settings

public MainWindow()
{
   this.InitializeComponent();
   this.AttachDevTools()
       .With( new DevToolsOptions(){
                ShouldVisualizeDirtyRects = true,
                 Window = new WindowSettings(){
                    State = FullScreen,
                 },
              });
}

XML persistence

public MainWindow()
{
   this.InitializeComponent();
   this.AttachDevTools()
       .With( new DevToolsOptions(){
                ShouldVisualizeDirtyRects = true,
                 Window = new WindowSettings(){
                    State = FullScreen,
                 },
              })
       .With( new XmlSettingStore("Path"){
               Indent = true,
             });

}

Pros and cons

Pro:

  • Low impact on current operation
  • It does not use reflection at runtime
  • Customization of settings
  • Ability to store settings
  • ability to create custom providers for persistence of settings (REST,json,bson,yaml, …)

Cons:

  • Requires Visual Studio 16.8 Preview 3 and .NET 5 Preview

Original message

In these PR #4731 #4762 I need to keep some settings (screenshot path, hot keys, favorite properties).

I would like to create a common way to save all present and future DevTools settings eg: windows location.

I think we could implement a system similar to the .net core or use that.

Do you have any suggestions?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
maxkatz6commented, Oct 5, 2020

Can we use Options pattern here?

this.AttachDevTools(new DevToolsOptions
{
    HotKeys = /* ... */,
    ScreenshotPath = "/screenshots",
    FavoriteProperties = new { /* ... */ } // doesn't overload default ones.
})

If developer want to use local file to store settings that should remember some dynamic props like FavoriteProperties, than we can have optional overload:

this.AttachDevTools(new DevToolsFileOptions(new FileInfo("/devtools-options.json")));
1reaction
kekekekscommented, Oct 1, 2020

Does SpecialFolder.LocalApplicationData exist on the mobile platform?

Yes, special folders are mapped to relevant locations

Before, we should understand if the settings we would like to memorize by machine or by application.

It’s counterproductive to force the user to reconfigure dev tools for each app

We could create providers for example: JsonConfingStore, YamlConfigStore, INIConfigStore, RestConfigStore, XMLConfigStore, etc. and deploy with separate nuget.

I’d rather not overcomplicate devtools that way. The built-in System.Xml should be enough for our needs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

roslyn/docs/features/source-generators.md at main
Generator implementations are defined in external assemblies passed to the compiler using the same -analyzer: option used for diagnostic analyzers.
Read more >
WindowState Enum - Typedescriptor
Defines the minimized/maximized state of a Window. On This Page. Overview; Fields; Assemblies; Issues ...
Read more >
Source Generators
Source Generators is a C# compiler feature that lets C# developers inspect user code as it is being compiled. Source generators create new ......
Read more >
Manual: Roslyn analyzers and source generators
Use Roslyn analyzers, source generators and ruleset files in Unity projects to inspect your code for style, quality, and other issues.
Read more >
Debugging or logging Roslyn source generators in Unity?
I've been following the Unity documentation for setting up a Roslyn source generator in my project. So far, the essential part, generating code, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found