Skip to content

Commit

Permalink
Merge pull request #6 from rkotze/run_on_start
Browse files Browse the repository at this point in the history
Run on start
  • Loading branch information
rkotze committed Mar 9, 2016
2 parents 2e0176a + 6b75524 commit a4c9ab4
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Add `eye_drops` to your list of dependencies in `mix.exs`:

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

Expand All @@ -43,6 +43,7 @@ config :eye_drops,
%{
id: :unit_tests,
name: "Unit tests",
run_on_start: true,
cmd: "mix test",
paths: ["web/*", "test/*"]
},
Expand All @@ -61,6 +62,7 @@ You can setup multiple tasks and it will run one after the other.

- `id` unique atom to identify tasks
- `name` provide a name for task
- `run_on_start` default is false. When EyeDrops starts, tasks with this set to true will run as well
- `cmd` the actual command to run
- `path` is a list. Can be exact, glob pattern or just a folder name

Expand Down
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ config :eye_drops,
tasks: [
%{
id: :unit_tests,
run_on_start: true,
name: "unit tests",
cmd: "mix test",
paths: ["lib/*"]
},
%{
id: :acceptance,
run_on_start: true,
name: "acceptance tests",
cmd: "mix acceptance",
paths: ["feature/*"]
Expand Down
10 changes: 10 additions & 0 deletions lib/eye_ball/eye_ball.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ defmodule EyeDrops.EyeBall do
GenServer.call(server, {:lookup, key})
end

def run_on_start(server) do
GenServer.call(server, :run_on_start)
end

# GenServer implementation
def init(tasks) do
:ok = :fs.subscribe
Expand All @@ -36,6 +40,12 @@ defmodule EyeDrops.EyeBall do
{:reply, Map.fetch(state, name), state}
end

def handle_call(:run_on_start, _from, state) do
tasks = Tasks.run_on_start(state.tasks)
:ok = Tasks.exec(tasks)
{:reply, state, state}
end

defp finish do
receive do
_ -> finish
Expand Down
12 changes: 12 additions & 0 deletions lib/eye_ball/eye_ball_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule EyeDrops.EyeBallTest do
use ExUnit.Case, async: false
import Mock
alias EyeDrops.Commands

test "Eye ball looks at include tasks" do
Expand All @@ -16,4 +17,15 @@ defmodule EyeDrops.EyeBallTest do
assert Enum.count(tasks) == 2
end

test "Eye ball run tasks on start of eye drops" do
with_mock EyeDrops.Tasks, [:passthrough], [
exec: fn (_tasks) -> :ok end]
do
{:ok, all_tasks} = EyeDrops.EyeBall.open(%{})
EyeDrops.EyeBall.run_on_start(all_tasks)

assert called EyeDrops.Tasks.exec(:_)
end
end

end
3 changes: 2 additions & 1 deletion lib/mix/tasks/eye_drops.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defmodule Mix.Tasks.EyeDrops do
IO.puts "Eye drops applied"

{:ok, switches} = Commands.parse(args)
{:ok, _} = EyeBall.open(switches)
{:ok, server} = EyeBall.open(switches)
EyeBall.run_on_start(server)

Commands.watch
end
Expand Down
6 changes: 5 additions & 1 deletion lib/tasks/task.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
defmodule EyeDrops.Task do
defstruct id: "", name: "", cmd: "", path: ""
defstruct id: "",
name: "",
cmd: "",
path: "",
run_on_start: false

def to_exec(task_id) when is_atom(task_id) do
task = get(task_id)
Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ defmodule EyeDrops.Tasks do
:ok
end

def run_on_start(tasks) do
Enum.filter(tasks, fn(task) ->
Map.has_key?(task, :run_on_start) && task.run_on_start == true
end)
end

defp has_tasks(empty_tasks) when empty_tasks == [], do: raise TasksError, message: "No tasks found"

defp has_tasks(tasks), do: tasks
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/tasks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule EyeDrops.TasksTest do
%{
id: :demo2,
name: "demo2",
run_on_start: true,
cmd: "echo demo2",
paths: ["lib/*"]
}]
Expand Down Expand Up @@ -63,4 +64,11 @@ defmodule EyeDrops.TasksTest do
task2 = Enum.at(tasks,1)
assert String.contains?(printed, ["Running", task1.name, task2.name, "Finished"])
end

test "Find tasks to execute when EyeDrops starts", %{:tasks => tasks} do
start_tasks = EyeDrops.Tasks.run_on_start(tasks)
task = Enum.at(start_tasks, 0)
assert Enum.count(start_tasks) == 1
assert task.id == :demo2
end
end
13 changes: 9 additions & 4 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.1.1",
version: "1.2.0",
elixir: ">= 1.1.0 and <= 1.2.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
Expand All @@ -19,7 +19,12 @@ defmodule Mix.Tasks.EyeDrops.Mixfile do
end

defp aliases do
[ci: ci_mix]
[ci: ci_mix,
acceptance: [&accept/1]]
end

def accept(_) do
Mix.shell.info "****** ACCEPTANCE RAN ******"
end

defp deps do
Expand All @@ -46,11 +51,11 @@ defmodule Mix.Tasks.EyeDrops.Mixfile do
"Docs" => "https://github.com/rkotze/eye_drops/blob/master/README.md"}]
end

defp ci_mix do
Mix.env(:test)
defp ci_mix() do
[
"credo",
"test"
]
end

end

0 comments on commit a4c9ab4

Please sign in to comment.