Skip to content

Commit

Permalink
Merge pull request #3 from hsndmr/implement-liquid-template
Browse files Browse the repository at this point in the history
Implement liquid template
  • Loading branch information
hsndmr authored Jul 7, 2024
2 parents e7d72a9 + 439e6f7 commit b433f3d
Show file tree
Hide file tree
Showing 28 changed files with 721 additions and 622 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Publish Mailgen Package

on:
push:
branches:
- '*'
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
dotnet-version: [6.0.x, 8.0.x]
include:
- dotnet-version: 6.0.x
- dotnet-version: 8.0.x

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Install dependencies
run: dotnet restore Mailgen/Mailgen.csproj

- name: Build
run: dotnet build Mailgen/Mailgen.csproj --configuration Release --no-restore

- name: Test
run: dotnet test Mailgen/Mailgen.csproj --no-restore --verbosity normal

publish:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.0.x'

- name: Install dependencies
run: dotnet restore Mailgen/Mailgen.csproj

- name: Build
run: dotnet build Mailgen/Mailgen.csproj --configuration Release --no-restore

- name: Pack
run: dotnet pack Mailgen/Mailgen.csproj --configuration Release --no-restore --output nupkg

- name: Publish to NuGet
run: dotnet nuget push nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
2 changes: 0 additions & 2 deletions Example/Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Mailgen\Mailgen.csproj"/>
</ItemGroup>

</Project>
8 changes: 0 additions & 8 deletions Example/Item.cs

This file was deleted.

227 changes: 115 additions & 112 deletions Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,134 +1,137 @@
using Example;
using Mailgen;
using Mailgen.Dtos;
using Mailgen;
using Mailgen.Templates;
using Mailgen.Templates.Models;

var projectPath = Environment.CurrentDirectory;
await TemplateGenerator.GenerateReceiptTemplate();
await TemplateGenerator.GenerateResetTemplate();

public class Item
{
public required string Name { get; set; }
public required string Description { get; set; }
public required string Price { get; set; }
}

var createMailGeneratorDto = new CreateMailGeneratorDto
public static class TemplateGenerator
{
Options = new TemplateOptionsModel
public static async Task GenerateReceiptTemplate()
{
Product = new ProductModel
var product = new ProductModel
{
CopyrightLeft = "© 2024",
CopyrightRight = "All rights reserved.",
Link = "https://github.com/hsndmr",
Name = "Example Product"
}
}
};


// receipt.html
var mailGenerator = new MailGenerator(new HtmlGeneratorFactory(), createMailGeneratorDto);
var generateMailDto = new GenerateMailDto<Item>
{
Body = new TemplateBodyModel<Item>
{
Title = "Hi John Appleseed,",
Intros =
[
"Your order has been processed successfully."
],
Tables = new List<TableModel<Item>>
};
var mailGenerator = new MailGenerator(product, new DefaultTemplate());
var body = new BodyModel<Item>
{
new()
Title = "Hi John Appleseed,",
Intros =
[
"Your order has been processed successfully."
],
Tables = new List<TableModel<Item>>
{
Columns =
[
new TableColumnModel<Item>
{
Header = "Item",
ValueSelector = item => item.Name,
Width = "20%"
},
new TableColumnModel<Item>
{
Header = "Description",
ValueSelector = item => item.Description
},
new TableColumnModel<Item>
{
Header = "Price",
ValueSelector = item => item.Price,
Width = "15%",
Align = "right"
}
],
Rows =
[
new Item
{
Name = "Node.js",
Description = "Event-driven I/O server-side JavaScript environment based on V8.",
Price = "$10.99"
},
new Item
new()
{
Columns =
[
new TableColumnModel<Item>
{
Header = "Item",
ValueSelector = item => item.Name,
Width = "20%"
},
new TableColumnModel<Item>
{
Header = "Description",
ValueSelector = item => item.Description
},
new TableColumnModel<Item>
{
Header = "Price",
ValueSelector = item => item.Price,
Width = "15%",
Align = "right"
}
],
Rows =
[
new Item
{
Name = "Node.js",
Description = "Event-driven I/O server-side JavaScript environment based on V8.",
Price = "$10.99"
},
new Item
{
Name = "Mailgen",
Description = "A .NET library for generating HTML emails.",
Price = "$1.99"
}
]
}
},
Actions =
[
new ActionModel
{
Instructions = "You can check the status of your order and more in your dashboard:",
Button = new ButtonModel
{
Name = "Mailgen",
Description = "A .NET library for generating HTML emails.",
Price = "$1.99"
Color = "#22BC66",
Text = "Go to Dashboard",
Link = "https://github.com/hsndmr"
}
]
}
},
Actions =
[
new ActionModel
{
Instructions = "You can check the status of your order and more in your dashboard:",
Button = new ButtonModel
{
Color = "#22BC66",
Text = "Go to Dashboard",
Link = "https://github.com/hsndmr"
}
}
],
Outro =
[
"We thank you for your purchase."
],
Signature = "Yours truly"
}
};
var receipt = await mailGenerator.GenerateMail(generateMailDto);
var mailPath = Path.Combine(projectPath, "receipt.html");
File.WriteAllText(mailPath, receipt);

],
Outro =
[
"We thank you for your purchase."
],
Signature = "Yours truly"
};

// reset.html
var html = await mailGenerator.GenerateMail(body);
await File.WriteAllTextAsync(@"GeneratedTemplates/receipt.html", html);
}

var generateMailForResetDto = new GenerateMailDto<Item>
{
Body = new TemplateBodyModel<Item>
public static async Task GenerateResetTemplate()
{
Title = "Hi John Appleseed,",
Intros =
[
"You have received this email because a password reset request for your account was received."
],
Actions =
[
new ActionModel
{
Instructions = "Click the button below to reset your password:",
Button = new ButtonModel
var product = new ProductModel
{
Link = "https://github.com/hsndmr",
Name = "Example Product"
};
var mailGenerator = new MailGenerator(product, new DefaultTemplate());
var body = new BodyModel<Item>
{
Title = "Hi John Appleseed,",
Intros =
[
"You have received this email because a password reset request for your account was received."
],
Actions =
[
new ActionModel
{
Color = "#DC4D2F",
Text = "Reset your password",
Link = "https://github.com/hsndmr"
Instructions = "Click the button below to reset your password:",
Button = new ButtonModel
{
Color = "#DC4D2F",
Text = "Reset your password",
Link = "https://github.com/hsndmr"
}
}
}
],
Outro =
[
"If you did not request a password reset, no further action is required on your part."
]
],
Outro =
[
"If you did not request a password reset, no further action is required on your part."
]
};

var html = await mailGenerator.GenerateMail(body);
await File.WriteAllTextAsync(@"GeneratedTemplates/reset.html", html);
}
};
var reset = await mailGenerator.GenerateMail(generateMailForResetDto);
var resetMailPath = Path.Combine(projectPath, "reset.html");
File.WriteAllText(resetMailPath, reset);
}
8 changes: 0 additions & 8 deletions Mailgen/Dtos/CreateMailGeneratorDto.cs

This file was deleted.

8 changes: 0 additions & 8 deletions Mailgen/Dtos/GenerateMailDto.cs

This file was deleted.

20 changes: 0 additions & 20 deletions Mailgen/HtmlGeneratorFactory.cs

This file was deleted.

8 changes: 0 additions & 8 deletions Mailgen/IHtmlGeneratorFactory.cs

This file was deleted.

Loading

0 comments on commit b433f3d

Please sign in to comment.