Skip to content

Commit

Permalink
Merge pull request #4 from rkotze/re-run-tasks
Browse files Browse the repository at this point in the history
Re run tasks
  • Loading branch information
rkotze committed Feb 21, 2016
2 parents e6cd8b3 + b694e4d commit 0abb8c6
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 12 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Add `eye_drops` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:eye_drops, "~> 1.0.0"}]
[{:eye_drops, "~> 1.1.0"}]
end
```

Expand All @@ -38,19 +38,32 @@ config :eye_drops,
name: "Unit tests",
cmd: "mix test",
paths: ["web/*", "test/*"]
},
%{
id: :acceptance,
name: "Acceptance tests",
cmd: "mix acceptance",
paths: ["web/*", "features/*"]
}
]
```

You can setup multiple tasks and it will run one after the other.

## task properties
## Task properties

- `id` unique atom to identify tasks
- `name` provide a name for task
- `cmd` the actual command to run
- `path` is a list. Can be exact, glob pattern or just a folder name

## Commands while running eye_drops

When EyeDrops has started you can run all or a specific task without needing to stop or change a file

- `all` this will run all your tasks
- `task_id` run a specific task from your EyeDrops config

## Optional switches

- `mix eye_drops --include-tasks "unit_tests,acceptance"` provide the id of tasks to watch instead of all
Expand Down
24 changes: 24 additions & 0 deletions lib/commands/commands.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule EyeDrops.Commands do
alias EyeDrops.Task
alias EyeDrops.Tasks
@switches [:include_tasks]

def parse([]), do: {:ok, %{}}
Expand All @@ -15,6 +17,28 @@ defmodule EyeDrops.Commands do
{:ok, %{:include_tasks => include_list}}
end

def watch() do
task_id = IO.gets ""
rerun(task_id)
watch
end

def rerun(task_id) do
case to_atom(task_id) do
:all ->
Tasks.get |>
Tasks.exec
task_id_atom ->
Task.to_exec(task_id_atom) |>
Task.exec
end
end

defp to_atom(task_id) do
String.replace(task_id, ~r/[^a-z_]+/, "") |>
String.to_atom
end

defp validate_switches!(arg_list) do
Enum.each(arg_list, fn {switch,value} ->
if !switch in @switches do
Expand Down
27 changes: 26 additions & 1 deletion lib/commands/commands_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule EyeDrops.CommandsTest do
use ExUnit.Case
use ExUnit.Case, async: false
import Mock
alias EyeDrops.Commands

test "Get specified task to watch" do
Expand All @@ -23,4 +24,28 @@ defmodule EyeDrops.CommandsTest do
assert result == {:ok, %{}}
end

test "rerun all tasks" do
with_mock EyeDrops.Tasks, [
get: fn -> ["tasks", "list"] end,
exec: fn (_tasks) -> "run tasks" end]
do
Commands.rerun("all")

assert called EyeDrops.Tasks.get
assert called EyeDrops.Tasks.exec(["tasks", "list"])
end
end

test "rerun specific task by task_id :unit_tests" do
with_mock EyeDrops.Task, [
to_exec: fn (_task_id_atom) -> {:ok, "task"} end,
exec: fn (_tasks) -> "run task" end]
do
Commands.rerun("unit_tests")

assert called EyeDrops.Task.to_exec(:unit_tests)
assert called EyeDrops.Task.exec({:ok, "task"})
end
end

end
4 changes: 2 additions & 2 deletions lib/mix/tasks/eye_drops.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ defmodule Mix.Tasks.EyeDrops do

{:ok, switches } = Commands.parse(args)
{:ok, _} = EyeBall.open(switches)
:timer.sleep :infinity

Commands.watch
end

end
8 changes: 8 additions & 0 deletions lib/tasks/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ defmodule EyeDrops.Task do

def to_exec(_task_id), do: {:error, "task id atom required"}

def exec({:ok, task}) do
IO.puts "Running #{task.name}..."
Mix.Shell.IO.cmd(task.cmd)
IO.puts "Finished #{task.name}..."
end

def exec({:error, message}), do: IO.puts message

defp get(task_id) do
EyeDrops.Tasks.get() |>
Enum.find(fn(task) ->
Expand Down
19 changes: 18 additions & 1 deletion lib/tasks/task_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule EyeDrops.TaskTest do
use ExUnit.Case
use ExUnit.Case, async: false
alias EyeDrops.Task
import Mock

test "Find command to run" do
{status, task} = Task.to_exec(:unit_tests)
Expand All @@ -12,4 +13,20 @@ defmodule EyeDrops.TaskTest do
assert Task.to_exec(:unit_test) == {:error, "Task.cmd not found"}
end

test "Execute task command" do
with_mock Mix.Shell.IO, [cmd: fn (_cmd) -> "command" end] do
Task.exec({:ok, %{name: "test", cmd: "my_command"}})

assert called Mix.Shell.IO.cmd("my_command")
end
end

test "Try execute task command which does not exist" do
with_mock IO, [puts: fn (_message) -> "run message" end] do
Task.exec({:error, "error message"})

assert called IO.puts("error message")
end
end

end
7 changes: 3 additions & 4 deletions lib/tasks/tasks.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule EyeDrops.Tasks do
alias EyeDrops.Tasks.Path
alias EyeDrops.Task

def get do
Application.get_env(:eye_drops, :tasks) |>
Expand All @@ -21,10 +22,8 @@ defmodule EyeDrops.Tasks do
end

def exec([task|tasks]) do
IO.puts "Running #{task.name}..."
Mix.Shell.IO.cmd(task.cmd)
IO.puts "Finished #{task.name}..."
exec(tasks)
Task.exec({:ok, task})
exec(tasks)
end

def exec([]) do
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Mix.Tasks.EyeDrops.Mixfile do

def project do
[app: :eye_drops,
version: "1.0.1",
version: "1.1.0",
elixir: "~> 1.1",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand All @@ -24,7 +24,8 @@ defmodule Mix.Tasks.EyeDrops.Mixfile do

defp deps do
[
{:fs, "~> 0.9.1"}
{:fs, "~> 0.9.1"},
{:mock, "~> 0.1.1", only: :test}
]
end

Expand Down

0 comments on commit 0abb8c6

Please sign in to comment.