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

Allow specifying custom header and body as an attribute or specification #167

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ public void CustomBase_Invoked_SpecUpdated()
Assert.Equal(isDefaultExport, ((TsCustomBaseAttribute)attribute).IsDefaultExport);
}

[Fact]
public void CustomHeader_Invoked_SpecUpdated()
{
const string header = "header";
var spec = new TypeSpec(new ExportTsInterfaceAttribute());
var builder = new ClassSpecBuilder(spec);

builder.CustomHeader(header);

var attribute = spec.ExportAttribute;
Assert.Equal(header, attribute.CustomHeader);
}

[Fact]
public void CustomBody_Invoked_SpecUpdated()
{
const string body = "body";
var spec = new TypeSpec(new ExportTsInterfaceAttribute());
var builder = new ClassSpecBuilder(spec);

builder.CustomBody(body);

var attribute = spec.ExportAttribute;
Assert.Equal(body, attribute.CustomBody);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -256,4 +282,4 @@ public void Undefined_Invoked_SpecUpdated()
Assert.IsType<TsUndefinedAttribute>(attribute);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ public void CustomBase_Invoked_SpecUpdated()
Assert.Equal(isDefaultExport, ((TsCustomBaseAttribute)attribute).IsDefaultExport);
}

[Fact]
public void CustomHeader_Invoked_SpecUpdated()
{
const string header = "header";
var spec = new TypeSpec(new ExportTsInterfaceAttribute());
var builder = new TypeGen.Core.SpecGeneration.Builders.Generic.InterfaceSpecBuilder<ExportedClass>(spec);

builder.CustomHeader(header);

var attribute = spec.ExportAttribute;
Assert.Equal(header, attribute.CustomHeader);
}

[Fact]
public void CustomBody_Invoked_SpecUpdated()
{
const string body = "body";
var spec = new TypeSpec(new ExportTsInterfaceAttribute());
var builder = new TypeGen.Core.SpecGeneration.Builders.Generic.InterfaceSpecBuilder<ExportedClass>(spec);

builder.CustomBody(body);

var attribute = spec.ExportAttribute;
Assert.Equal(body, attribute.CustomBody);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -271,4 +297,4 @@ public void Undefined_Invoked_SpecUpdated()
Assert.IsType<TsUndefinedAttribute>(attribute);
}
}
}
}
16 changes: 12 additions & 4 deletions src/TypeGen/TypeGen.Core/Generator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,12 @@ private IEnumerable<string> GenerateClass(Type type, ExportTsClassAttribute clas
string tsTypeNameFirstPart = tsTypeName.RemoveTypeGenericComponent();
string filePath = GetFilePath(type, outputDir);
string filePathRelative = GetRelativeFilePath(type, outputDir);
string customHead = _tsContentGenerator.GetCustomHead(filePath);
string customBody = _tsContentGenerator.GetCustomBody(filePath, Options.TabLength);
string customInFileHead = _tsContentGenerator.GetCustomHead(filePath);
string customAttributeHead = classAttribute.CustomHeader;
string customHead = string.Join(Environment.NewLine, new[] { customInFileHead, customAttributeHead }.Where(i => !string.IsNullOrWhiteSpace(i)));
string customInFileBody = _tsContentGenerator.GetCustomBody(filePath, Options.TabLength);
string customAttributeBody = classAttribute.CustomBody;
string customBody = string.Join(Environment.NewLine, new[] { customInFileBody, customAttributeBody }.Where(i => !string.IsNullOrWhiteSpace(i)));
var tsDoc = GetTsDocForType(type);

var content = _typeService.UseDefaultExport(type) ?
Expand Down Expand Up @@ -458,8 +462,12 @@ private IEnumerable<string> GenerateInterface(Type type, ExportTsInterfaceAttrib
string tsTypeNameFirstPart = tsTypeName.RemoveTypeGenericComponent();
string filePath = GetFilePath(type, outputDir);
string filePathRelative = GetRelativeFilePath(type, outputDir);
string customHead = _tsContentGenerator.GetCustomHead(filePath);
string customBody = _tsContentGenerator.GetCustomBody(filePath, Options.TabLength);
string customInFileHead = _tsContentGenerator.GetCustomHead(filePath);
string customAttributeHead = interfaceAttribute.CustomHeader;
string customHead = string.Join(Environment.NewLine, new[] { customInFileHead, customAttributeHead }.Where(i => !string.IsNullOrWhiteSpace(i)));
string customInFileBody = _tsContentGenerator.GetCustomBody(filePath, Options.TabLength);
string customAttributeBody = interfaceAttribute.CustomBody;
string customBody = string.Join(Environment.NewLine, new[] { customInFileBody, customAttributeBody }.Where(i => !string.IsNullOrWhiteSpace(i)));
var tsDoc = GetTsDocForType(type);

var content = _typeService.UseDefaultExport(type) ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace TypeGen.Core.SpecGeneration.Builders
{
public abstract class ClassSpecBuilderBase<TSelf> : SpecBuilderBase,
ICustomBaseTrait<TSelf>,
ICustomHeaderTrait<TSelf>,
ICustomBodyTrait<TSelf>,
IDefaultExportTrait<TSelf>,
IDefaultTypeOutputTrait<TSelf>,
IDefaultValueTrait<TSelf>,
Expand All @@ -26,6 +28,8 @@ public abstract class ClassSpecBuilderBase<TSelf> : SpecBuilderBase,
where TSelf : SpecBuilderBase
{
private readonly CustomBaseTrait<TSelf> _customBaseTrait;
private readonly CustomBodyTrait<TSelf> _customBodyTrait;
private readonly CustomHeaderTrait<TSelf> _customHeaderTrait;
private readonly DefaultExportTrait<TSelf> _defaultExportTrait;
private readonly DefaultTypeOutputTrait<TSelf> _defaultTypeOutputTrait;
private readonly DefaultValueTrait<TSelf> _defaultValueTrait;
Expand All @@ -48,6 +52,8 @@ internal ClassSpecBuilderBase(TypeSpec typeSpec) : base(typeSpec)
{
MemberTrait = new MemberTrait<TSelf>(this as TSelf, typeSpec);
_customBaseTrait = new CustomBaseTrait<TSelf>(this as TSelf, typeSpec);
_customBodyTrait = new CustomBodyTrait<TSelf>(this as TSelf, typeSpec);
_customHeaderTrait = new CustomHeaderTrait<TSelf>(this as TSelf, typeSpec);
_defaultExportTrait = new DefaultExportTrait<TSelf>(this as TSelf, typeSpec);
_defaultTypeOutputTrait = new DefaultTypeOutputTrait<TSelf>(this as TSelf, typeSpec, MemberTrait);
_defaultValueTrait = new DefaultValueTrait<TSelf>(this as TSelf, typeSpec, MemberTrait);
Expand Down Expand Up @@ -75,7 +81,15 @@ public TSelf CustomBase(string @base = null, string importPath = null, string or
public TSelf CustomBase(string @base = null, string importPath = null, string originalTypeName = null, bool isDefaultExport = false,
params ImplementedInterface[] implementedInterfaces)
=> _customBaseTrait.CustomBase(@base, importPath, originalTypeName, isDefaultExport, implementedInterfaces);


/// <inheritdoc />
public TSelf CustomBody(string body)
=> _customBodyTrait.CustomBody(body);

/// <inheritdoc />
public TSelf CustomHeader(string header)
=> _customHeaderTrait.CustomHeader(header);

/// <inheritdoc />
public TSelf DefaultExport(bool enabled = true) => _defaultExportTrait.DefaultExport(enabled);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace TypeGen.Core.SpecGeneration.Builders
/// </summary>
public abstract class InterfaceSpecBuilderBase<TSelf> : SpecBuilderBase,
ICustomBaseTrait<TSelf>,
ICustomHeaderTrait<TSelf>,
ICustomBodyTrait<TSelf>,
IDefaultExportTrait<TSelf>,
IDefaultTypeOutputTrait<TSelf>,
IDefaultValueTrait<TSelf>,
Expand All @@ -28,6 +30,8 @@ public abstract class InterfaceSpecBuilderBase<TSelf> : SpecBuilderBase,
where TSelf : SpecBuilderBase
{
private readonly CustomBaseTrait<TSelf> _customBaseTrait;
private readonly CustomBodyTrait<TSelf> _customBodyTrait;
private readonly CustomHeaderTrait<TSelf> _customHeaderTrait;
private readonly DefaultExportTrait<TSelf> _defaultExportTrait;
private readonly DefaultTypeOutputTrait<TSelf> _defaultTypeOutputTrait;
private readonly DefaultValueTrait<TSelf> _defaultValueTrait;
Expand All @@ -49,6 +53,8 @@ internal InterfaceSpecBuilderBase(TypeSpec typeSpec) : base(typeSpec)
{
MemberTrait = new MemberTrait<TSelf>(this as TSelf, typeSpec);
_customBaseTrait = new CustomBaseTrait<TSelf>(this as TSelf, typeSpec);
_customBodyTrait = new CustomBodyTrait<TSelf>(this as TSelf, typeSpec);
_customHeaderTrait = new CustomHeaderTrait<TSelf>(this as TSelf, typeSpec);
_defaultExportTrait = new DefaultExportTrait<TSelf>(this as TSelf, typeSpec);
_defaultTypeOutputTrait = new DefaultTypeOutputTrait<TSelf>(this as TSelf, typeSpec, MemberTrait);
_defaultValueTrait = new DefaultValueTrait<TSelf>(this as TSelf, typeSpec, MemberTrait);
Expand All @@ -75,6 +81,14 @@ public TSelf CustomBase(string @base = null, string importPath = null, string or
public TSelf CustomBase(string @base = null, string importPath = null, string originalTypeName = null, bool isDefaultExport = false,
params ImplementedInterface[] implementedInterfaces)
=> _customBaseTrait.CustomBase(@base, importPath, originalTypeName, isDefaultExport, implementedInterfaces);

/// <inheritdoc />
public TSelf CustomBody(string body)
=> _customBodyTrait.CustomBody(body);

/// <inheritdoc />
public TSelf CustomHeader(string header)
=> _customHeaderTrait.CustomHeader(header);

/// <inheritdoc />
public TSelf DefaultExport(bool enabled = true) => _defaultExportTrait.DefaultExport(enabled);
Expand Down Expand Up @@ -131,4 +145,4 @@ public TSelf Type(string typeName, string importPath = null, string originalType
/// <inheritdoc />
public TSelf Undefined() => _undefinedTrait.Undefined();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using TypeGen.Core.TypeAnnotations;

namespace TypeGen.Core.SpecGeneration.Builders.Traits;

internal class CustomBodyTrait<TSpecBuilder> : ICustomBodyTrait<TSpecBuilder>
{
private readonly TSpecBuilder _this;
private readonly TypeSpec _typeSpec;

public CustomBodyTrait(TSpecBuilder @this, TypeSpec typeSpec)
{
_this = @this;
_typeSpec = typeSpec;
}

public TSpecBuilder CustomBody(string body)
{
_typeSpec.SetCustomBody(body);
return _this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using TypeGen.Core.TypeAnnotations;

namespace TypeGen.Core.SpecGeneration.Builders.Traits;

internal class CustomHeaderTrait<TSpecBuilder> : ICustomHeaderTrait<TSpecBuilder>
{
private readonly TSpecBuilder _this;
private readonly TypeSpec _typeSpec;

public CustomHeaderTrait(TSpecBuilder @this, TypeSpec typeSpec)
{
_this = @this;
_typeSpec = typeSpec;
}

public TSpecBuilder CustomHeader(string header)
{
_typeSpec.SetCustomHeader(header);
return _this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TypeGen.Core.SpecGeneration.Builders.Traits;

internal interface ICustomBodyTrait<TSpecBuilder>
{
/// <summary>
/// Indicates type has a custom body (equivalent of TsExportAttribute's CustomBody).
/// </summary>
/// <returns>The current instance of <see cref="TSpecBuilder"/>.</returns>
TSpecBuilder CustomBody(string body);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TypeGen.Core.SpecGeneration.Builders.Traits;

internal interface ICustomHeaderTrait<TSpecBuilder>
{
/// <summary>
/// Indicates type has a custom header (equivalent of TsExportAttribute's CustomHeader).
/// </summary>
/// <returns>The current instance of <see cref="TSpecBuilder"/>.</returns>
TSpecBuilder CustomHeader(string header);
}
8 changes: 5 additions & 3 deletions src/TypeGen/TypeGen.Core/SpecGeneration/TypeSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public TypeSpec(ExportAttribute exportAttribute)
_memberAttributes = new Dictionary<string, IList<Attribute>>();
_additionalAttributes = new List<Attribute>();
}

public void AddMember(string memberName) => _memberAttributes[memberName] = new List<Attribute>();

public void AddCustomBaseAttribute(string @base = null, string importPath = null, string originalTypeName = null, bool isDefaultExport = false,
Expand All @@ -32,6 +32,8 @@ public void AddCustomBaseAttribute(string @base = null, string importPath = null
public void AddDefaultValueAttribute(string memberName, string defaultValue) => AddMemberAttribute(memberName, new TsDefaultValueAttribute(defaultValue));
public void AddIgnoreAttribute(string memberName) => AddMemberAttribute(memberName, new TsIgnoreAttribute());
public void AddIgnoreBaseAttribute() => AddAdditionalAttribute(new TsIgnoreBaseAttribute());
public void SetCustomHeader(string header) { ExportAttribute.CustomHeader = header; }
public void SetCustomBody(string body) { ExportAttribute.CustomBody = body; }
public void AddMemberNameAttribute(string memberName, string name) => AddMemberAttribute(memberName, new TsMemberNameAttribute(name));
public void AddNotNullAttribute(string memberName) => AddMemberAttribute(memberName, new TsNotNullAttribute());
public void AddNotReadonlyAttribute(string memberName) => AddMemberAttribute(memberName, new TsNotReadonlyAttribute());
Expand All @@ -48,8 +50,8 @@ public void AddTypeAttribute(string memberName, string typeName, string importPa
public void AddTypeUnionsAttribute(string memberName, IEnumerable<string> typeUnions) => AddMemberAttribute(memberName, new TsTypeUnionsAttribute(typeUnions));
public void AddTypeUnionsAttribute(string memberName, params string[] typeUnions) => AddMemberAttribute(memberName, new TsTypeUnionsAttribute(typeUnions));
public void AddUndefinedAttribute(string memberName) => AddMemberAttribute(memberName, new TsUndefinedAttribute());

private void AddMemberAttribute(string memberName, Attribute attribute) => MemberAttributes[memberName].Add(attribute);
private void AddAdditionalAttribute(Attribute attribute) => _additionalAttributes.Add(attribute);
}
}
}
22 changes: 21 additions & 1 deletion src/TypeGen/TypeGen.Core/TypeAnnotations/ExportAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace TypeGen.Core.TypeAnnotations
public abstract class ExportAttribute : Attribute
{
private string _outputDir;

private string _customHeader;
private string _customBody;

/// <summary>
/// TypeScript file output directory
/// </summary>
Expand All @@ -20,5 +22,23 @@ public string OutputDir
get => _outputDir;
set => _outputDir = FileSystemUtils.AsDirectory(value);
}

/// <summary>
/// TypeScript file custom header
/// </summary>
public string CustomHeader
{
get => _customHeader;
set => _customHeader = value;
}

/// <summary>
/// TypeScript file custom body
/// </summary>
public string CustomBody
{
get => _customBody;
set => _customBody = value;
}
}
}