RSCG – SyncMethodGenerator

RSCG – SyncMethodGenerator
 
 

name SyncMethodGenerator
nuget https://www.nuget.org/packages/Zomp.SyncMethodGenerator/
link https://github.com/zompinc/sync-method-generator
author Zomp Inc.

Generating Sync method from async

 

This is how you can use SyncMethodGenerator .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Zomp.SyncMethodGenerator" Version="1.0.14" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using Zomp.SyncMethodGeneratorDemo;

Console.WriteLine("Hello, World!");
Writer.Haha("a.txt", "Andrei Ignat");
Writer.Write("a.txt", "andrei ignat");


namespace Zomp.SyncMethodGeneratorDemo;

partial class Writer
{
    [Zomp.SyncMethodGenerator.CreateSyncVersion]
    public static async Task WriteAsync(string file, string contents,
CancellationToken ct)
    {
        await File.WriteAllTextAsync(file, contents, ct).ConfigureAwait(true);
    }
    [Zomp.SyncMethodGenerator.CreateSyncVersion]
    public static async Task HahaAsync(ReadOnlyMemory<byte> buffer, Stream stream,
CancellationToken ct)
    => await stream.WriteAsync(buffer, ct).ConfigureAwait(true);
}


 

The code that is generated is

// <auto-generated/>
namespace Zomp.SyncMethodGenerator
{
    /// <summary>
    /// An attribute that can be used to automatically generate a synchronous version of an async method. Must be used in a partial class.
    /// </summary>
    [System.AttributeUsage(System.AttributeTargets.Method)]
    internal class CreateSyncVersionAttribute : System.Attribute
    {
    }
}
// <auto-generated/>
#nullable enable
namespace Zomp.SyncMethodGeneratorDemo;
partial class Writer
{
    public static void Haha(global::System.ReadOnlySpan<byte> buffer, global::System.IO.Stream stream)
    => stream.Write(buffer);
}

// <auto-generated/>
#nullable enable
namespace Zomp.SyncMethodGeneratorDemo;
partial class Writer
{
    public static void Write(string file, string contents)
    {
        global::System.IO.File.WriteAllText(file, contents);
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/SyncMethodGenerator