Skip to content

Commit

Permalink
Improve Mix tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
qcam committed Mar 18, 2018
1 parent ffa3d87 commit 7401594
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/mix/tasks/nabo.gen.post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ defmodule Mix.Tasks.Nabo.Gen.Post do
@recursive true

@moduledoc """
Generates a post.
Generates a post with the given path.
## Example
mix nabo.gen.post first-post
mix nabo.gen.post first-post --path priv/posts
## Command line options
* `-p`, `--path` - the path where the posts locate
"""

@doc false
def run(args) do
case OptionParser.parse(args) do
{_, [slug], _} ->
switches = [path: :string]
aliases = [p: :path]

case OptionParser.parse(args, switches: switches, aliases: aliases) do
{options, [slug], _invalid} ->
root_path = Keyword.get(options, :root, "priv/posts/")
datetime = DateTime.utc_now()
posts_path = "priv/posts"
path = Path.relative_to(posts_path, Mix.Project.app_path)
path = Path.relative_to(root_path, Mix.Project.app_path)
file = Path.join(path, "#{format_datetime(datetime)}_#{slug}.md")
create_file(file, post_template(slug: slug, datetime: datetime))

_ ->
Mix.raise "expected nabo.gen.post to receive post slug, " <>
"got: #{inspect(Enum.join(args, " "))}"
Expand Down
1 change: 1 addition & 0 deletions lib/mix/tasks/nabo.gen.repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Mix.Tasks.Nabo.Gen.Repo do

create_directory(Path.dirname(file))
create_file(file, repo_template(mod: repo))

_ ->
Mix.raise "expected nabo.gen.repo to receive repo name, " <>
"got: #{inspect Enum.join(args, " ")}"
Expand Down

0 comments on commit 7401594

Please sign in to comment.