-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/existence-mix-task'
- Loading branch information
Showing
25 changed files
with
776 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
elixir 1.9.0-otp-22 | ||
erlang 22.0.7 | ||
elixir 1.10.1 | ||
erlang 22.2.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
use Mix.Config | ||
|
||
config :knigge, | ||
check_if_exists?: [except: :test], | ||
delegate_at_runtime?: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"skip_files": [ | ||
"lib/mix/tasks" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Knigge.CLI.Output do | ||
@moduledoc false | ||
|
||
@device :stdio | ||
|
||
def info(device \\ @device, message), do: print(device, [:blue, message]) | ||
def success(device \\ @device, message), do: print(device, [:green, message]) | ||
def error(device \\ @device, message), do: print(device, [:red, message]) | ||
def warn(device \\ @device, message), do: print(device, [:gold, message]) | ||
|
||
def print(device \\ @device, message) | ||
|
||
def print(:stdio, message), do: Bunt.puts(message) | ||
def print(:stderr, message), do: Bunt.warn(message) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,59 @@ | ||
defmodule Knigge.Module do | ||
@moduledoc false | ||
|
||
alias Knigge.Options | ||
module = inspect(__MODULE__) | ||
|
||
def ensure_exists!(module, opts, env) do | ||
unless Knigge.Module.exists?(module, opts) do | ||
def ensure_exists!(module, env) do | ||
unless Knigge.Module.exists?(module) do | ||
Knigge.Error.module_not_loaded!(module, env) | ||
end | ||
|
||
module | ||
end | ||
|
||
def exists?(module, %Options{} = opts) do | ||
if opts.check_if_exists? do | ||
Code.ensure_loaded?(module) or Module.open?(module) | ||
else | ||
@doc """ | ||
Returns true if a module exists, false otherwise. | ||
## Examples | ||
iex> #{module}.exists?(This.Does.Not.Exist) | ||
false | ||
iex> #{module}.exists?(Knigge) | ||
true | ||
""" | ||
@spec exists?(module :: module()) :: boolean() | ||
def exists?(module) do | ||
Module.open?(module) or Code.ensure_loaded?(module) | ||
end | ||
|
||
@doc """ | ||
Returns all modules which `use Knigge` for the given app. If the app does not | ||
exist an error is returned. To determine if `Knigge` is `use`d we check if the | ||
module exports the `__knigge__/0` function which acts as a "tag". | ||
`fetch_for_app/1` makes use of `Code.ensure_loaded?/1` to force the module | ||
being loaded. Since this results in a `GenServer.call/3` to the code server | ||
__do not use this__ in a hot code path, as it __will__ result in a slowdown! | ||
## Examples | ||
iex> #{module}.fetch_for_app(:this_does_not_exist) | ||
{:error, :undefined} | ||
iex> #{module}.fetch_for_app(:knigge) | ||
{:ok, []} | ||
""" | ||
@spec fetch_for_app(app :: atom()) :: {:ok, list(module())} | {:error, :undefined} | ||
def fetch_for_app(app) do | ||
with {:ok, modules} <- :application.get_key(app, :modules) do | ||
{:ok, Enum.filter(modules, &uses_knigge?/1)} | ||
else | ||
:undefined -> {:error, :undefined} | ||
end | ||
end | ||
|
||
defp uses_knigge?(module) do | ||
Code.ensure_loaded?(module) and function_exported?(module, :__knigge__, 0) | ||
end | ||
end |
Oops, something went wrong.