diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f9ab8b6..c515878 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 diff --git a/shard.yml b/shard.yml index b8221ec..704d2ff 100644 --- a/shard.yml +++ b/shard.yml @@ -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 diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 432029f..90ab958 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -2,6 +2,7 @@ require "spec" require "../src/carbon" require "./support/**" require "lucky_env" +require "lucky_template/spec" LuckyEnv.load?(".env") diff --git a/spec/tasks/email_spec.cr b/spec/tasks/email_spec.cr index 49b83b9..c2c5877 100644 --- a/spec/tasks/email_spec.cr +++ b/spec/tasks/email_spec.cr @@ -1,6 +1,7 @@ require "../spec_helper" include CleanupHelper +include LuckyTemplate::Spec describe Gen::Email do it "generates a new email" do @@ -8,8 +9,10 @@ describe Gen::Email 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 @@ -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 diff --git a/src/carbon/tasks/gen/email.cr b/src/carbon/tasks/gen/email.cr index e1487da..59868e2 100644 --- a/src/carbon/tasks/gen/email.cr +++ b/src/carbon/tasks/gen/email.cr @@ -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 @@ -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 @@ -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 diff --git a/src/carbon/tasks/gen/templates/{{email_filename}}_email.cr.ecr b/src/carbon/tasks/gen/templates/email.cr.ecr similarity index 100% rename from src/carbon/tasks/gen/templates/{{email_filename}}_email.cr.ecr rename to src/carbon/tasks/gen/templates/email.cr.ecr diff --git a/src/carbon/tasks/gen/templates/templates/{{email_filename}}_email/html.ecr.ecr b/src/carbon/tasks/gen/templates/html.ecr.ecr similarity index 100% rename from src/carbon/tasks/gen/templates/templates/{{email_filename}}_email/html.ecr.ecr rename to src/carbon/tasks/gen/templates/html.ecr.ecr diff --git a/src/carbon/tasks/gen/templates/templates/{{email_filename}}_email/text.ecr.ecr b/src/carbon/tasks/gen/templates/text.ecr.ecr similarity index 100% rename from src/carbon/tasks/gen/templates/templates/{{email_filename}}_email/text.ecr.ecr rename to src/carbon/tasks/gen/templates/text.ecr.ecr