Switch over to Elixir option parsing.
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
defmodule ElixirKoans do
|
defmodule ElixirKoans do
|
||||||
use Application
|
use Application
|
||||||
alias Options
|
|
||||||
|
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
import Supervisor.Spec
|
import Supervisor.Spec
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
defmodule Mix.Tasks.Meditate do
|
defmodule Mix.Tasks.Meditate do
|
||||||
use Mix.Task
|
use Mix.Task
|
||||||
alias Options
|
|
||||||
|
|
||||||
@shortdoc "Start the koans"
|
@shortdoc "Start the koans"
|
||||||
|
|
||||||
@@ -8,26 +7,34 @@ defmodule Mix.Tasks.Meditate do
|
|||||||
Application.ensure_all_started(:elixir_koans)
|
Application.ensure_all_started(:elixir_koans)
|
||||||
Code.compiler_options(ignore_module_conflict: true)
|
Code.compiler_options(ignore_module_conflict: true)
|
||||||
|
|
||||||
Options.start(args)
|
|
||||||
|
|
||||||
{parsed, _, _} = OptionParser.parse(args)
|
{parsed, _, _} = OptionParser.parse(args)
|
||||||
if Keyword.has_key?(parsed, :no_clear_screen) do
|
|
||||||
Display.disable_clear()
|
|
||||||
end
|
|
||||||
|
|
||||||
modules = Options.initial_koan
|
modules = parsed
|
||||||
|
|> initial_module
|
||||||
|> ok?
|
|> ok?
|
||||||
|> Runner.modules_to_run
|
|> Runner.modules_to_run
|
||||||
|
|
||||||
Tracker.set_total(modules)
|
Tracker.set_total(modules)
|
||||||
Tracker.notify_on_complete(self())
|
Tracker.notify_on_complete(self())
|
||||||
|
|
||||||
|
set_clear_screen(parsed)
|
||||||
Runner.run(modules)
|
Runner.run(modules)
|
||||||
|
|
||||||
Tracker.wait_until_complete()
|
Tracker.wait_until_complete()
|
||||||
Display.congratulate()
|
Display.congratulate()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp initial_module(parsed) do
|
||||||
|
name = Keyword.get(parsed, :koan, "Equalities")
|
||||||
|
String.to_atom("Elixir."<> name)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp set_clear_screen(parsed) do
|
||||||
|
if Keyword.has_key?(parsed, :no_clear_screen) do
|
||||||
|
Display.disable_clear()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp ok?(koan) do
|
defp ok?(koan) do
|
||||||
if Runner.koan?(koan) do
|
if Runner.koan?(koan) do
|
||||||
koan
|
koan
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
defmodule Options do
|
|
||||||
@defaults %{
|
|
||||||
initial_koan: Equalities,
|
|
||||||
}
|
|
||||||
|
|
||||||
def start(args) do
|
|
||||||
Agent.start_link(fn -> parse(args) end, name: __MODULE__)
|
|
||||||
end
|
|
||||||
|
|
||||||
def initial_koan() do
|
|
||||||
Agent.get(__MODULE__, &Map.fetch!(&1, :initial_koan))
|
|
||||||
end
|
|
||||||
|
|
||||||
defp parse(args) do
|
|
||||||
Enum.reduce(args, @defaults, fn(arg, acc) ->
|
|
||||||
Map.merge(acc, parse_argument(arg))
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_argument("--koan="<>module), do: %{ initial_koan: String.to_atom("Elixir."<> module)}
|
|
||||||
def parse_argument(_), do: %{}
|
|
||||||
end
|
|
||||||
@@ -25,7 +25,6 @@ defmodule Runner do
|
|||||||
String.to_integer(number)
|
String.to_integer(number)
|
||||||
end
|
end
|
||||||
|
|
||||||
def modules_to_run, do: Options.initial_koan |> modules_to_run
|
|
||||||
def modules_to_run(start_module), do: Enum.drop_while(modules(), &(&1 != start_module))
|
def modules_to_run(start_module), do: Enum.drop_while(modules(), &(&1 != start_module))
|
||||||
|
|
||||||
def start_link do
|
def start_link do
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
defmodule OptionTest do
|
|
||||||
use ExUnit.Case, async: true
|
|
||||||
|
|
||||||
test "can target specifc koans" do
|
|
||||||
Options.start(["--koan=Strings"])
|
|
||||||
assert Options.initial_koan() == Strings
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Reference in New Issue
Block a user