Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

Commit

Permalink
Change formatters to feed from GenEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
5thWall committed Dec 10, 2015
1 parent d28091c commit b0b225e
Show file tree
Hide file tree
Showing 24 changed files with 494 additions and 648 deletions.
17 changes: 5 additions & 12 deletions docs/formatters.md → docs/reporters.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# Dogma Formatters
# Dogma Reporters

You can pass a format to the mix task using the `--format` flag.
You can pass a reporter to the mix task using the `--format` flag.

```
> mix dogma --format=flycheck
lib/dogma/rules.ex:23:1: W: Blank lines detected at end of file
test/dogma/formatter_test.exs:9:1: W: Trailing whitespace detected
test/dogma/reporters_test.exs:9:1: W: Trailing whitespace detected
```

The default formatter is [Simple](#simple).
The default reporter is [Simple](#simple).

## Contents

* [Flycheck](#flycheck)
* [JSON](#json)
* [Null](#null)
* [Simple](#simple)


Expand Down Expand Up @@ -69,16 +68,10 @@ The JSON structure is like the following example:
}


### Null
`null`

A formatter that prints nothing. Ever.


### Simple
`simple`

A formatter that prints a dot per file, followed by details at the end.
A reporter that prints a dot per file, followed by details at the end.

Inspecting 27 files.

Expand Down
19 changes: 14 additions & 5 deletions lib/dogma.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@ defmodule Dogma do
other modules through the divine `run/3` function.
"""

alias Dogma.Formatter
alias Dogma.Rules
alias Dogma.ScriptSources

@version Mix.Project.config[:version]

def run(dir_or_file, config, formatter) do
def run(dir_or_file, config, dispatcher) do
dir_or_file
|> ScriptSources.find(config.exclude)
|> ScriptSources.to_scripts
|> Formatter.start(formatter)
|> Rules.test(config.rules, formatter)
|> Formatter.finish(formatter)
|> notify_start(dispatcher)
|> Rules.test(config.rules, dispatcher)
|> notify_finish(dispatcher)
end

def version, do: @version

defp notify_start(scripts, dispatcher) do
GenEvent.sync_notify(dispatcher, {:start, scripts})
scripts
end

defp notify_finish(scripts, dispatcher) do
GenEvent.sync_notify(dispatcher, {:finished, scripts})
scripts
end
end
94 changes: 0 additions & 94 deletions lib/dogma/documentation/formatters_list.ex

This file was deleted.

94 changes: 94 additions & 0 deletions lib/dogma/documentation/reporters_list.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
defmodule Dogma.Documentation.ReportersList do
@moduledoc """
Generates a documentation file for reporters.
"""

alias Dogma.Reporters

@doc """
Writes reporter documentation to `docs/reporters.md`.
"""
def write! do
File.write!("docs/reporters.md", reporters_doc)
IO.puts "Generated docs/reporters.md"
end

@doc """
Generate a markdown string documenting the available reporters.
Available reporters are determined by `Dogma.Reporters.reporters` and the
information for each reporter is pulled from that module's `@moduledoc`
"""
def reporters_doc do
reporters =
Reporters.reporters
|> Enum.map(&extract_doc/1)

default_name = printable_name(Reporters.default_reporter)
default_id = String.downcase(default_name)

"""
# Dogma Reporters
You can pass a reporter to the mix task using the `--format` flag.
```
> mix dogma --format=flycheck
lib/dogma/rules.ex:23:1: W: Blank lines detected at end of file
test/dogma/reporters_test.exs:9:1: W: Trailing whitespace detected
```
The default reporter is [#{default_name}](##{default_id}).
## Contents
#{contents(reporters)}
---
#{moduledoc(reporters)}
"""
end

defp extract_doc({key, reporter}) do
name = printable_name(reporter)
{_, doc} = Code.get_docs(reporter, :moduledoc)

{name, doc, key}
end

defp printable_name(module) do
module
|> Module.split
|> List.last
end

defp contents(reporters) when is_list(reporters) do
reporters
|> Enum.map(&contents/1)
end

defp contents({name, _, _}) do
id = String.downcase(name)
"""
* [#{name}](##{id})
"""
end

defp moduledoc(reporters) when is_list(reporters) do
reporters
|> Enum.map(&moduledoc/1)
|> Enum.join("\n")
end

defp moduledoc({name, doc, key}) do
"""
### #{name}
`#{key}`
#{doc}
"""
end
end
71 changes: 0 additions & 71 deletions lib/dogma/formatter.ex

This file was deleted.

25 changes: 0 additions & 25 deletions lib/dogma/formatter/null.ex

This file was deleted.

19 changes: 8 additions & 11 deletions lib/dogma/formatter/flycheck.ex → lib/dogma/reporter/flycheck.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Dogma.Formatter.Flycheck do
defmodule Dogma.Reporter.Flycheck do
@moduledoc """
A machine-readable format suitable for integration with tools like
[Flycheck](https://github.com/flycheck/flycheck) or
Expand All @@ -8,19 +8,16 @@ defmodule Dogma.Formatter.Flycheck do
/project/lib/test.ex:14:1: W: Comparison to a boolean is pointless
"""

@behaviour Dogma.Formatter
use GenEvent

alias Dogma.Script
def handle_event({:finished, scripts}, _) do
IO.write finish(scripts)
{:ok, []}
end

@doc """
Runs at the start of the test suite, displaying nothing
"""
def start(_scripts), do: ""
def handle_event(_,_), do: {:ok, []}

@doc """
Runs after each script is tested. Prints nothing.
"""
def script(_script), do: ""
alias Dogma.Script

@doc """
Runs at the end of the test suite, displaying errors.
Expand Down
Loading

0 comments on commit b0b225e

Please sign in to comment.