FileGenerator.Core
7.0.3
dotnet add package FileGenerator.Core --version 7.0.3
NuGet\Install-Package FileGenerator.Core -Version 7.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FileGenerator.Core" Version="7.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FileGenerator.Core --version 7.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: FileGenerator.Core, 7.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FileGenerator.Core as a Cake Addin
#addin nuget:?package=FileGenerator.Core&version=7.0.3
// Install FileGenerator.Core as a Cake Tool
#tool nuget:?package=FileGenerator.Core&version=7.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
FileGenerator.Core
Description
This will help you generate your C# classes to Typescript intefaces, enums and more... You can also create your own custom generator that will make use of the generated interfaces or enums. I build this project for my personal use of generating typescript equivalent of C# codes and now I decided to make it in public use.
Note:
- Currently this generator is useful for typescript checking as it will only generate interfaces
Table of Contents
Installation
- Install using nuget. Click Here
- Add the generator on your startup file
public static async Task Main(string[] args)
{
//...
await FileGeneratorCore.Run("..", options => // In my case i used ".." to watch all inside on my solutions
options
.SetGeneratedRootDirs("Client/Generated") // Where the output folder of generated files
.SetImportPrefix("@generated") // Optional: see the generated "tsconfig.json" on how to resolve import issues
.ReplaceGenerateTs<long,string>() // Optional: Use for replacing the type gnerated
.AddGenerateTs<HttpStatusCode>() // Optional: Use to generate extra type
);
//....
}
Usage
- Use
GenerateTs
attribute
Exmaple:
[GenerateTs]
public enum MyEnum
{
None,
A,
B,
C = 50,
}
[GenerateTs]
public class MyClass
{
public string? Id { get; set; }
public string? Name { get; set; }
public string? TheNullableType { get; set; }
public dynamic? TheNullableTypeDynamic { get; set; }
}
[GenerateTs]
public class MyClassGeneric<TData, TDataExtra> : MyClass where TData : MyClass
{
public TData SubClass { get; set; }
public TDataExtra Extra { get; set; }
public ICollection<MyClass>? ExtendedCollection { get; set; }
public Dictionary<string, MyClass> MyDictionary { get; set; }
public (string Test, int TestInt, MyClass MyClass) Tuple { get; set; }
}
It will generate:
enum MyEnum {
None = 0,
A = 1,
B = 2,
C = 50,
}
export default MyEnum;
export default interface MyClass {
id?: string;
name?: string;
theNullableType?: string;
theNullableTypeDynamic?: unknown;
}
export default interface MyClassGeneric<TData extends MyClass, TDataExtra> extends MyClass {
extendedCollection?: MyClass[];
extra?: TDataExtra;
myDictionary: Record<string,MyClass>;
subClass: TData;
tuple: [string, number, MyClass];
}
How to create a custom generator
- Create a class that will inherit to
BaseFileGenerator
- Implement
Filter
property. - Override
Generate
function. - Note: ⚠️ This custom generator is using Reflection type of generating
- Other functions that can be overridden in
BaseFileGenerator
-GenerateAsync, Generates & GeneratesAsync
- ❗Don't forget to call
GenerateFile
in order to generate file
Example: HubGenerator.cs
using FileGenerator.Core;
using Microsoft.AspNetCore.SignalR;
using System.Reflection;
using System.Text;
using static FileGenerator.Core.Helpers.GeneratorHelper;
namespace CMVirtualize.Web.Generators
{
public sealed class HubGenerator : BaseFileGenerator
{
public override Func<Type, bool> Filter => type =>
type.BaseType != null &&
type.BaseType.IsGenericType &&
!type.IsGenericType &&
type.BaseType.GetGenericTypeDefinition().IsAssignableFrom(typeof(Hub<>));
public override void Generate(Type type)
{
var typeOfReciever = type.BaseType!.GetGenericArguments()[0];
var receiverMethods = typeOfReciever.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)
.Where(method => method.IsPublic);
var senderMethods = type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)
.Where(method => method.IsPublic && !method.GetParameters().Any(parameter => parameter.ParameterType == typeof(CancellationToken)));
var streamMethods = type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)
.Where(method => method.IsPublic && method.GetParameters().Any(parameter => parameter.ParameterType == typeof(CancellationToken)));
var imports = new List<string>();
var content = new StringBuilder();
var name = GetName(type, imports, false);
content.AppendLine($$"""
import { HubConnection, HubConnectionBuilder, LogLevel } from '@microsoft/signalr';
export default class {{name}} {
public connection: HubConnection;
constructor(private url?: string) {
this.connection = new HubConnectionBuilder()
.withUrl(this.url || '/hub/{{name}}')
.configureLogging(process.env.ENV === 'development' ? LogLevel.Information: LogLevel.Error)
.withAutomaticReconnect()
.build();
}
""");
foreach (var method in senderMethods)
{
var parameters = method.GetParameters().Select(s => $"{s.Name}: {GetName(s.ParameterType, imports)}").ToList();
var parameterNames = method.GetParameters().Select(s => s.Name).ToList();
var camelCaseName = method.Name.ToLowerFirst();
content.AppendLine($" public {camelCaseName} = ({string.Join(", ", parameters)}) =>");
content.AppendLine($" this.connection.invoke('{method.Name}', {string.Join(", ", parameterNames)});");
}
foreach (var method in receiverMethods)
{
var parameters = method.GetParameters().Select(s => $"{s.Name}: {GetName(s.ParameterType, imports)}").ToList();
var camelCaseName = method.Name.ToLowerFirst();
content.AppendLine($" public {camelCaseName} = (callback: ({string.Join(", ", parameters)}) => void) =>");
content.AppendLine($" this.connection.on('{method.Name}', callback);");
}
foreach (var method in streamMethods)
{
var parameters = method.GetParameters()
.Where(parameter => parameter.ParameterType != typeof(CancellationToken))
.Select(s => $"{s.Name}: {GetName(s.ParameterType, imports)}").ToList();
var parameterNames = method.GetParameters()
.Where(parameter => (parameter.ParameterType != typeof(CancellationToken)))
.Select(s => s.Name).ToList();
var camelCaseName = method.Name.ToLowerFirst();
var returnType = GetName(method.ReturnType.GetGenericArguments()[0], imports);
content.AppendLine($" public {camelCaseName} = ({string.Join(", ", parameters)}) =>");
content.AppendLine($" this.connection.stream<{returnType}>('{method.Name}', {string.Join(", ", parameterNames)});");
}
content.AppendLine($" public removeEvent = (name: '{string.Join("' | '", receiverMethods.Select(s => s.Name))}', method?: (...args: any[]) => void) =>");
content.AppendLine(" method ? this.connection.off(name, method) : this.connection.off(name);");
content.Append('}');
GenerateFile($"{FileLocation(type)}.ts", CombineImportContent(content, imports));
}
}
}
GeneratorHelper
GetName
- This will be use to get the name of the generated type as well as the import file location but you need to pass avar imports = new List<string>();
types in order to collate the import files needed from generator.FileLocation
- Use to get the location of the file.CombineImportContent
- Use to bombine the imports file and the content
Note:❗Don't forget to call GenerateFile
in order to generate the file you want to generate
Build pipeline generation or CI
Execute the following command to generate on CI
dotnet build "FileGenerator.Core"
License
TBD
How to Contribute
TBD
Not yet added in the Typescript Generator ⚠️
- Generate a class instead of interface.
- Read only properties
- Static and constant value properties
- Include functions in generated types
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
7.0.3 | 171 | 4/13/2023 |
7.0.2 | 159 | 4/12/2023 |
7.0.1 | 168 | 3/31/2023 |
7.0.0 | 165 | 3/30/2023 |
1.0.1-CI-20230330-152644 | 99 | 3/30/2023 |
1.0.1-CI-20230329-223629 | 103 | 3/29/2023 |
1.0.1-CI-20230209-154102 | 102 | 2/9/2023 |
1.0.1-CI-20230131-063749 | 146 | 1/31/2023 |
1.0.1-CI-20230130-075712 | 129 | 1/30/2023 |
1.0.1-CI-20230130-060609 | 110 | 1/30/2023 |
1.0.1-CI-20230129-163119 | 121 | 1/29/2023 |
1.0.1-CI-20230129-134041 | 123 | 1/29/2023 |
1.0.1-CI-20230129-132902 | 127 | 1/29/2023 |
1.0.1-CI-20230128-084947 | 137 | 1/28/2023 |
1.0.1-CI-20230115-153349 | 135 | 1/15/2023 |
1.0.1-CI-20210214-035414 | 192 | 2/14/2021 |
1.0.1-CI-20201125-154434 | 273 | 11/25/2020 |
1.0.1-CI-20200721-153321 | 329 | 7/21/2020 |
1.0.1-CI-20200719-035009 | 353 | 7/19/2020 |
1.0.1-CI-20200410-110552 | 354 | 4/10/2020 |
1.0.1-CI-20200315-072438 | 329 | 3/15/2020 |
1.0.1-CI-20200308-095413 | 315 | 3/8/2020 |
0.1.0-CI-20200315-072037 | 316 | 3/15/2020 |