Skip to content

Commit

Permalink
Add support to 'new' command
Browse files Browse the repository at this point in the history
  • Loading branch information
luanguimaraesla committed Jun 1, 2018
1 parent b86546c commit 8afe75a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RUN apt-get install -y gnupg2 && \
&& curl -L http://packages.erlang-solutions.com/debian/erlang_solutions.asc | apt-key add - \
&& apt-get update \
&& mkdir -p /usr/share/man/man1/ \
&& apt-get install -y elixir erlang-inets erlang --no-install-recommends \
&& apt-get install -y git elixir erlang-inets erlang --no-install-recommends \
&& apt-get clean -y

ADD mix.exs /tmp/mix.exs
Expand Down
18 changes: 10 additions & 8 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@ config :pandocker, envs: %{
}

config :pandocker, defaults: %{
command: "help",
config_yaml: "pandocker.yml",
project_root: "/code",
source_path: 'src',
output_file: 'out.pdf',
files: "/pandocker/examples/example.md"
command: "help",
config_yaml: "pandocker.yml",
project_root: "/code",
source_path: 'src',
output_file: 'out.pdf',
files: "/pandocker/examples/example.md",
templates_url: "https://github.com/luanguimaraesla/pandocker-templates",
}

config :pandocker, tokens: %{
command: ~r/^\s*(?P<command>(compile|help|new))/,
config_yaml: ~r/-f (?P<config_yaml>\/?(\w+\/?)*\w+\.(yaml|yml))/,
command: ~r/^\s*(?P<command>(compile|help|new))/,
template: ~r/(-t|--template)\s*(?<template>[-\._\w]+)/,
config_yaml: ~r/-f (?P<config_yaml>\/?(\w+\/?)*\w+\.(yaml|yml))/,
}

# It is also possible to import configuration files, relative to this
Expand Down
10 changes: 4 additions & 6 deletions lib/command/executor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ defmodule Command.Executor do
:ok
"""
def dispatch(commands) do
def dispatch({execution_path, commands}) do
commands
|> List.insert_at(0, goto_source_command())
|> List.insert_at(0, goto_execution_path(execution_path))
|> make_os_command
|> execute
:ok
Expand All @@ -48,9 +48,7 @@ defmodule Command.Executor do
String.to_charlist(os_command)
end

defp goto_source_command do
root = Configuration.Manager.get_env(:project_root)
source = Configuration.Manager.get_config(:pandoc, :source_path, &List.to_string/1)
"cd " <> Path.join(root, source)
defp goto_execution_path(execution_path) do
"cd " <> execution_path
end
end
32 changes: 32 additions & 0 deletions lib/command/new.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Command.New do
@moduledoc """
Command to create a new blank structure for a new project
according to a template stored at Github
"""

alias Configuration.Manager
alias Command.Executor

@doc """
Function to be executed by Pandocker
"""
def exec do
build_command()
|> Executor.dispatch
end

defp build_command do
execution_path = Manager.get_env(:project_root)
{execution_path, [git_clone_command(), copy_template_command()]}
end

defp git_clone_command do
template_url = Manager.get_env(:templates_url)
"git clone " <> template_url <> " /tmp/tmplt"
end

defp copy_template_command do
template_name = Manager.get_env(:template)
"cp -r " <> Path.join(["/tmp/tmplt/", template_name, "*"]) <> " ."
end
end
5 changes: 4 additions & 1 deletion lib/command/pandoc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ defmodule Command.Pandoc do
end

defp build_command(flags, files) do
[pandoc_command(flags, files), copyback_command()]
root = Configuration.Manager.get_env(:project_root)
source = Configuration.Manager.get_config(:pandoc, :source_path, &List.to_string/1)
execution_path = Path.join(root, source)
{execution_path, [pandoc_command(flags, files), copyback_command()]}
end

defp copyback_command do
Expand Down
1 change: 1 addition & 0 deletions lib/pandocker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Pandocker do
command = Configuration.Manager.get_env(:command) |> String.to_atom

{module, arg} = case command do
:new -> {"Elixir.Command.New", nil}
:compile -> {"Elixir.Command.Pandoc", Configuration.Manager.get_yaml_section(:sections)}
:help -> {"Elixir.Command.Help", nil}
_ -> raise "Missing function"
Expand Down

0 comments on commit 8afe75a

Please sign in to comment.