Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmazza committed May 2, 2022
0 parents commit 79611a3
Show file tree
Hide file tree
Showing 53 changed files with 1,674 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
swoosh_gallery-*.tar

# Temporary files, for example, from tests.
/tmp/
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
erlang 24.2
elixir 1.13.3-otp-24
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Remote

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Swoosh Gallery

**A gallery of previews for your emails.**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `swoosh_gallery` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:swoosh_gallery, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/swoosh_gallery>.

## Contributing

1. Download the project.
2. Run `mix do deps.get, tailwind.install`
3. Make some changes.
4. If you need add new tailwind styles, run `mix tailwind default`.
3 changes: 3 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
14 changes: 14 additions & 0 deletions assets/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// See the Tailwind configuration guide for advanced usage
// https://tailwindcss.com/docs/configuration
module.exports = {
content: [
'../lib/**/*.eex',
'../lib/**/*.ex'
],
theme: {
extend: {},
},
plugins: [
require('@tailwindcss/forms')
]
}
13 changes: 13 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Config

config :tailwind,
version: "3.0.12",
default: [
args: ~w(
--config=tailwind.config.js
--input=css/app.css
--output=../lib/swoosh/gallery/app.css
--minify
),
cd: Path.expand("../assets", __DIR__)
]
148 changes: 148 additions & 0 deletions lib/mix/tasks/swoosh.gallery.html.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
defmodule Mix.Tasks.Swoosh.Gallery.Html do
@moduledoc """
Serialize the given Swoosh.Gallery module and outputs HTML files to a folder.
"""
use Mix.Task
require Mix.Generator

alias Swoosh.Gallery
alias Swoosh.Gallery.Layout

defmodule Options do
@moduledoc false

defstruct gallery: nil, path: nil
end

@impl Mix.Task
def run(argv) do
Mix.Task.run("app.start")
opts = parse_options(argv)
generate_html(opts)
end

defp parse_options(argv) do
parse_options = [strict: [gallery: :string, path: :string]]
{opts, _args, _} = OptionParser.parse(argv, parse_options)

%Options{
gallery: ensure_gallery!(opts),
path: ensure_path!(opts)
}
end

defp ensure_gallery!(opts) do
if gallery = Keyword.get(opts, :gallery) do
gallery
|> List.wrap()
|> Module.concat()
|> Code.ensure_compiled!()
|> tap(fn mod ->
unless function_exported?(mod, :previews, 0) do
Mix.raise("""
The module #{inspect(mod)} is not a valid gallery. Make sure it uses Swoosh.Gallery:
defmodule #{inspect(mod)} do
use Swoosh.Gallery
// preview(...)
end
""")
end
end)
|> tap(&ensure_required_functions!(&1.previews))
else
Mix.raise("No gallery available. Please pass a gallery with the --gallery option")
end
end

defp ensure_required_functions!(previews) when is_list(previews) do
Enum.each(previews, fn %{email_mfa: {module, _fun, _args}} ->
ensure_required_functions!(module)
end)
end

defp ensure_required_functions!(module) do
Code.ensure_compiled!(module)

unless function_exported?(module, :preview, 0) do
raise """
The preview/3 function expected #{inspect(module)} to declare a function `preview/0`, but it is missing.
Make sure it is implemented:
def preview do
// return Swoosh.Email.t
end
"""
end

unless function_exported?(module, :preview_details, 0) do
raise """
The preview/3 function expected expected #{inspect(module)} to declare a function `preview_details/0`, but it is missing.
Make sure it is implemented:
def preview_details do
[title: "My Email"]
end
"""
end
end

defp ensure_path!(opts) do
if path = Keyword.get(opts, :path) do
Mix.Generator.create_directory(path)
path
else
Mix.raise("No path available. Please pass a path with the --path option")
end
end

defp generate_html(%{path: destination, gallery: gallery}) do
generate_root(gallery, destination)

for preview <- gallery.previews do
generate_preview(gallery, preview, destination)
end
end

defp generate_root(gallery, destination) do
root = Layout.render(gallery, base_path: "./", preview: nil, format: ".html")
Mix.Generator.create_file(Path.join([destination, "index.html"]), root, force: true)
end

defp generate_preview(gallery, preview, destination) do
preview = Gallery.eval_preview(preview)
preview_path = Path.join([destination, preview.path])
rendered = Layout.render(gallery, base_path: "./", preview: preview, format: ".html")
Mix.Generator.create_file("#{preview_path}.html", rendered, force: true)

generate_html_preview(preview_path, preview.email)
generate_attachments(preview_path, preview)
end

defp generate_html_preview(preview_base_path, email) do
Mix.Generator.create_file(Path.join([preview_base_path, "preview.html"]), email.html_body,
force: true
)
end

defp generate_attachments(preview_base_path, %{email: email} = preview) do
Enum.with_index(email.attachments, fn attachment, index ->
with {:ok, %{data: data}} <- Gallery.read_email_attachment_at(preview, index) do
file_path =
Path.join([
preview_base_path,
"attachments",
Integer.to_string(index),
attachment.filename
])

Mix.Generator.create_file(file_path, data, force: true)
end
end)
end
end
Loading

0 comments on commit 79611a3

Please sign in to comment.