Skip to content
Snippets Groups Projects
Commit 468b2924 authored by falood's avatar falood
Browse files

first commit

parents
No related branches found
No related tags found
No related merge requests found
/_build
/deps
erl_crash.dump
*.ez
LICENSE 0 → 100644
Copyright (c) 2014, Falood
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the {organization} nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
ExFSWatch
=========
Elixir version of fswatch base on [fswatch](https://github.com/emcrisostomo/fswatch)
C_drive version is developing.
## Usage
```elixir
defmodule Monitor do
use ExFSWatch, dirs: ["/tmp/fswatch"]
def callback(:stop) do
IO.puts "STOP"
end
def callback(file_path, events) do
IO.inspect {file_path, events}
end
end
```
```shell
iex > Monitor.start
```
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for third-
# party users, it should be done in your mix.exs file.
# Sample configuration:
#
# config :logger, :console,
# level: :info,
# format: "$date $time [$level] $metadata$message\n",
# metadata: [:user_id]
# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
defmodule ExFSWatch do
defmacro __using__([dirs: dirs]) do
quote do
def __dirs__, do: unquote(dirs)
def start, do: ExFSWatch.Supervisor.start_child __MODULE__
end
end
def start(_, _) do
ExFSWatch.Supervisor.start_link
end
end
defmodule ExFSWatch.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, [], name: __MODULE__)
end
def start_child(module) do
Supervisor.start_child(__MODULE__, [module])
end
def init([]) do
[ worker(ExFSWatch.Worker, [], [restart: :transient])
] |> supervise [strategy: :simple_one_for_one]
end
end
defmodule ExFSWatch.Worker do
use GenServer
defstruct [:port, :module]
def start_link(module) do
GenServer.start_link(__MODULE__, module, name: module)
end
def init(module) do
dirs = module.__dirs__ |> Enum.join " "
cmd = "fswatch -x #{dirs}"
port = Port.open(
{:spawn, cmd |> to_char_list},
[:binary, {:line, 1024}, :exit_status, :use_stdio, :stderr_to_stdout, :eof]
)
{:ok, %__MODULE__{port: port, module: module}}
end
def handle_info({port, {:data, {:eol, data}}}, %__MODULE__{port: port, module: module}=sd) do
[file_path | events] = data |> String.split
module.callback(file_path, events)
{:noreply, sd}
end
def handle_info({port, {:exit_status, 0}}, %__MODULE__{port: port, module: module}) do
Port.close(port)
module.callback(:stop)
{:stop, :killed}
end
def handle_info(_, sd) do
{:noreply, sd}
end
end
mix.exs 0 → 100644
defmodule ExFSWatch.Mixfile do
use Mix.Project
def project do
[ app: :exfswatch,
version: "0.0.1",
elixir: "~> 1.0",
deps: deps
]
end
def application do
[ mod: { ExFSWatch, [] },
applications: [:logger]
]
end
defp deps do
[]
end
end
defmodule ExfswatchTest do
use ExUnit.Case
test "the truth" do
assert 1 + 1 == 2
end
end
ExUnit.start()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment