-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create command to generate a blog post
- Loading branch information
1 parent
932a689
commit 5d775df
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule Mix.Tasks.SimpleBlog.Post.New do | ||
use Mix.Task | ||
|
||
@moduledoc """ | ||
Module responsible for generate a new blog post. | ||
""" | ||
|
||
@doc """ | ||
It generates a new blog post | ||
## Examples | ||
# iex> Mix.Tasks.SimpleBlog.Post.New.run(["My first blog post"]) | ||
# :ok | ||
""" | ||
@impl Mix.Task | ||
def run([]), do: Mix.shell().info(usage()) | ||
|
||
def run([title]) do | ||
today = | ||
Date.utc_today() | ||
|> Date.to_string() | ||
|
||
filename = SimpleBlog.Post.generate_filename(%SimpleBlog.Post{title: title, date: today}) | ||
|
||
case File.open(filename, [:write]) do | ||
{:ok, file} -> | ||
IO.binwrite(file, "## " <> title) | ||
File.close(file) | ||
|
||
{:error, :enoent} -> | ||
Mix.shell().info(""" | ||
There is a directory missing, please run the following command: | ||
$ mix simple_blog.new . | ||
""") | ||
end | ||
end | ||
|
||
def usage() do | ||
""" | ||
To generate a new blog post you should pass a title as string: | ||
$ mix simple_blog.post.new "My first blog post" | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule SimpleBlog.Post do | ||
@moduledoc """ | ||
Module responsible for Post | ||
""" | ||
|
||
@extension "md" | ||
|
||
defstruct title: "", tags: [], body: "", date: "" | ||
|
||
@doc """ | ||
Generate filename for blog post | ||
## Examples | ||
iex> SimpleBlog.Post.generate_filename(%SimpleBlog.Post{ title: "My first blog post", date: ~D[2023-10-04] }) | ||
"blog/_posts/2023-10-04-my-first-blog-post.md" | ||
""" | ||
def generate_filename(%SimpleBlog.Post{title: title, date: date}) do | ||
normalized_title = | ||
title | ||
|> String.downcase() | ||
|> String.replace(" ", "-", global: true) | ||
|
||
filename = "#{date}-#{normalized_title}.#{@extension}" | ||
"blog/_posts/#{filename}" | ||
end | ||
end |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule Mix.Tasks.SimpleBlog.Post.NewTest do | ||
use ExUnit.Case | ||
import ExUnit.CaptureIO | ||
doctest Mix.Tasks.SimpleBlog.Post.New | ||
|
||
@instructions """ | ||
To generate a new blog post you should pass a title as string: | ||
$ mix simple_blog.post.new "My first blog post" | ||
""" | ||
|
||
@error_instructions """ | ||
There is a directory missing, please run the following command: | ||
$ mix simple_blog.new . | ||
""" | ||
|
||
describe "run" do | ||
setup do | ||
on_exit(fn -> File.rm_rf("blog") end) | ||
end | ||
|
||
test "returns instructions for no arguments" do | ||
Mix.Tasks.SimpleBlog.New.run(["."]) | ||
message = capture_io(fn -> Mix.Tasks.SimpleBlog.Post.New.run([]) end) | ||
|
||
assert message == "#{@instructions}\n" | ||
end | ||
|
||
test "show success message for created blog post" do | ||
Mix.Tasks.SimpleBlog.New.run(["."]) | ||
Mix.Tasks.SimpleBlog.Post.New.run(["My First Blog Post"]) | ||
|
||
today = Date.utc_today() |> Date.to_string() | ||
|
||
assert File.exists?("blog/_posts/#{today}-my-first-blog-post.md") | ||
end | ||
|
||
test "show error message when blog does not exist" do | ||
message = capture_io(fn -> Mix.Tasks.SimpleBlog.Post.New.run(["My First Blog Post"]) end) | ||
assert message == "#{@error_instructions}\n" | ||
end | ||
end | ||
|
||
describe "usage" do | ||
test "returns instructions" do | ||
assert Mix.Tasks.SimpleBlog.Post.New.usage() == @instructions | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule SimpleBlog.PostTest do | ||
use ExUnit.Case | ||
doctest SimpleBlog.Post | ||
|
||
describe "generate_filename" do | ||
test "generate filename with date" do | ||
title = "Introduction to elixir" | ||
date = ~D[2023-10-04] | ||
filename = SimpleBlog.Post.generate_filename(%SimpleBlog.Post{title: title, date: date}) | ||
assert "blog/_posts/2023-10-04-introduction-to-elixir.md" == filename | ||
end | ||
end | ||
end |