Skip to content

Commit

Permalink
LuckyTemplate migration (#82)
Browse files Browse the repository at this point in the history
* feat: replace teeplate w/ lucky_template

* chore: add windows to ci
  • Loading branch information
mdwagner authored Jun 10, 2023
1 parent d6808a2 commit b3e27bd
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 51 deletions.
31 changes: 16 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ jobs:
runs-on: ubuntu-latest
continue-on-error: false
steps:
- uses: actions/checkout@v1
- name: Checkout source
uses: actions/checkout@v3
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
with:
crystal: latest
- name: Install shards
- name: Install dependencies
run: shards install
- name: Format
run: crystal tool format --check
Expand All @@ -28,20 +27,22 @@ jobs:
strategy:
fail-fast: false
matrix:
crystal_version:
- 1.4.0
- latest
experimental:
- false
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental }}
os: [ubuntu-latest, windows-latest]
crystal_version: [latest]
include:
- os: ubuntu-latest
crystal_version: 1.4.0
runs-on: ${{ matrix.os }}
continue-on-error: false
steps:
- uses: actions/checkout@v2
- uses: crystal-lang/install-crystal@v1
- name: Checkout source
uses: actions/checkout@v3
- name: Install Crystal
uses: crystal-lang/install-crystal@v1
with:
crystal: ${{matrix.crystal_version}}
crystal: ${{ matrix.crystal_version }}
- name: Install dependencies
run: shards install
run: shards install --skip-postinstall --skip-executables
- name: Create .env file
run: touch .env
- name: Run tests
Expand Down
6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ dependencies:
lucky_task:
github: luckyframework/lucky_task
version: ~> 0.1.1
teeplate:
github: luckyframework/teeplate
version: ~> 0.8.5
lucky_template:
github: luckyframework/lucky_template
version: ~> 0.1.0
wordsmith:
github: luckyframework/wordsmith
version: ~> 0.4.0
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require "spec"
require "../src/carbon"
require "./support/**"
require "lucky_env"
require "lucky_template/spec"

LuckyEnv.load?(".env")

Expand Down
9 changes: 7 additions & 2 deletions spec/tasks/email_spec.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
require "../spec_helper"

include CleanupHelper
include LuckyTemplate::Spec

describe Gen::Email do
it "generates a new email" do
with_cleanup do
generator = Gen::Email.new
generator.output = IO::Memory.new
generator.print_help_or_call ["PasswordReset"]

generator.output.to_s.should contain("src/emails/templates/password_reset_email/html.ecr")

folder = generator.email_template.template_folder
folder.should be_valid_at(Path["."])
end
end

Expand All @@ -18,8 +21,10 @@ describe Gen::Email do
generator = Gen::Email.new
generator.output = IO::Memory.new
generator.print_help_or_call ["WelcomeUserEmail"]

generator.output.to_s.should contain("src/emails/templates/welcome_user_email/html.ecr")

folder = generator.email_template.template_folder
folder.should be_valid_at(Path["."])
end
end
end
70 changes: 39 additions & 31 deletions src/carbon/tasks/gen/email.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
require "colorize"
require "teeplate"
require "lucky_template"
require "wordsmith"
require "file_utils"

class Carbon::EmailTemplate < Teeplate::FileTree
directory "#{__DIR__}/templates"
class Carbon::EmailTemplate
def initialize(@email_filename : String, @email_class_name : String)
end

def render(path : Path)
LuckyTemplate.write!(path, template_folder)
end

def initialize(
@email_filename : String,
@email_class_name : String
)
def template_folder
LuckyTemplate.create_folder do |top_dir|
top_dir.add_folder(Path["src/emails/templates"]) do |templates_dir|
templates_dir.add_file("#{@email_filename}_email.cr") do |io|
ECR.embed("#{__DIR__}/templates/email.cr.ecr", io)
end
templates_dir.add_folder("#{@email_filename}_email") do |email_templates_dir|
email_templates_dir.add_file("html.ecr") do |io|
ECR.embed("#{__DIR__}/templates/html.ecr.ecr", io)
end
email_templates_dir.add_file("text.ecr") do |io|
ECR.embed("#{__DIR__}/templates/text.ecr.ecr", io)
end
end
end
end
end
end

Expand All @@ -31,12 +48,15 @@ class Gen::Email < LuckyTask::Task
end

def call
template = Carbon::EmailTemplate.new(filename, normalized_email_name)
template.render(output_path.to_s)
email_template.render(Path["."])

display_success_messages
end

def email_template
Carbon::EmailTemplate.new(filename, normalized_email_name)
end

private def normalized_email_name : String
email_name.gsub(/email$/i, "")
end
Expand All @@ -45,29 +65,17 @@ class Gen::Email < LuckyTask::Task
Wordsmith::Inflector.underscore(normalized_email_name)
end

private def output_path : Path
Path[".", "src", "emails"]
end

private def display_success_messages
output.puts <<-MESSAGE
Generated email template
#{green_arrow} #{email_class_template_path.colorize.bold}
#{green_arrow} #{email_class_template_file("html").colorize.bold}
#{green_arrow} #{email_class_template_file("text").colorize.bold}
MESSAGE
end

private def email_class_template_filename
"#{filename}_email.cr"
end

private def email_class_template_path
"src/emails/#{email_class_template_filename}"
end
snapshot = LuckyTemplate.snapshot(email_template.template_folder)
paths = snapshot.reject { |_, v| v.folder? }.keys

private def email_class_template_file(file)
"src/emails/templates/#{filename}_email/#{file}.ecr"
output.puts "Generated email template"
output.puts
paths.each_with_index do |path, index|
output.print " #{green_arrow} #{path.colorize.bold}"
unless index == paths.size - 1
output.print '\n'
end
end
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit b3e27bd

Please sign in to comment.