Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
44 changes: 27 additions & 17 deletions lib/remix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,60 +28,70 @@ defmodule Remix do
def handle_info(:poll_and_reload, state) do
paths = Application.get_all_env(:remix)[:paths]

new_state = Map.new paths, fn (path) ->
current_mtime = get_current_mtime path
last_mtime = case Map.fetch(state, path) do
{:ok, val} -> val
:error -> nil
end
handle_path path, current_mtime, last_mtime
end
new_state =
Map.new(paths, fn path ->
current_mtime = get_current_mtime(path)

last_mtime =
case Map.fetch(state, path) do
{:ok, val} -> val
:error -> nil
end

handle_path(path, current_mtime, last_mtime)
end)

Process.send_after(__MODULE__, :poll_and_reload, 1000)
{:noreply, new_state}
end

def handle_path(path, current_mtime, current_mtime), do: {path, current_mtime}

def handle_path(path, current_mtime, _) do
comp_elixir = fn -> Mix.Tasks.Compile.Elixir.run(["--ignore-module-conflict"]) end
comp_escript = fn -> Mix.Tasks.Escript.Build.run([]) end

case Application.get_all_env(:remix)[:silent] do
true ->
ExUnit.CaptureIO.capture_io(comp_elixir)

if Application.get_all_env(:remix)[:escript] == true do
ExUnit.CaptureIO.capture_io(comp_escript)
end

_ ->
comp_elixir.()

if Application.get_all_env(:remix)[:escript] == true do
comp_escript.()
end
end

{path, current_mtime}
end

def get_current_mtime(dir) do
case File.ls(dir) do
{:ok, files} -> get_current_mtime(files, [], dir)
_ -> nil
_ -> nil
end
end

def get_current_mtime([], mtimes, _cwd) do
mtimes
|> Enum.sort
|> Enum.reverse
|> List.first
|> Enum.sort()
|> Enum.reverse()
|> List.first()
end

def get_current_mtime([h | tail], mtimes, cwd) do
mtime = case File.dir?("#{cwd}/#{h}") do
true -> get_current_mtime("#{cwd}/#{h}")
false -> File.stat!("#{cwd}/#{h}").mtime
end
get_current_mtime tail, [mtime | mtimes], cwd
mtime =
case File.dir?("#{cwd}/#{h}") do
true -> get_current_mtime("#{cwd}/#{h}")
false -> File.stat!("#{cwd}/#{h}").mtime
end

get_current_mtime(tail, [mtime | mtimes], cwd)
end
end
end
17 changes: 9 additions & 8 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ defmodule Remix.Mixfile do
use Mix.Project

def project do
[app: :remix,
version: "0.0.2",
elixir: "~> 1.0",
package: package,
description: description,
deps: deps]
[
app: :remix,
version: "0.0.2",
elixir: "~> 1.0",
package: package(),
description: description(),
deps: deps()
]
end

def application do
[applications: [:logger],
mod: {Remix, []}]
[applications: [:logger], mod: {Remix, []}]
end

defp deps, do: []
Expand Down