-
Notifications
You must be signed in to change notification settings - Fork 52
Added an option to let dogma parse source from stdin #194
Changes from 2 commits
f965b00
5e03d50
7378d15
f229701
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,20 +8,43 @@ defmodule Dogma do | |
|
||
alias Dogma.Rules | ||
alias Dogma.ScriptSources | ||
alias Dogma.Script | ||
|
||
@version Mix.Project.config[:version] | ||
|
||
def run(dir_or_file, config, dispatcher) do | ||
dir_or_file | ||
|> ScriptSources.find(config.exclude) | ||
|> ScriptSources.to_scripts | ||
|> get_sources(config) | ||
|> notify_start(dispatcher) | ||
|> Rules.test(config.rules, dispatcher) | ||
|> notify_finish(dispatcher) | ||
end | ||
|
||
def get_sources(dir_or_file, %{read_stdin: true} = _config) do | ||
if dir_or_file == nil, do: dir_or_file="stdin" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't define vars inside blocks and then use them outside of said blocks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what you mean here... |
||
case read_from_stdin do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we either use ()s or pass in the accumulator to this function to make it clear it is a call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. passing the accumulator now. |
||
{:ok, source} -> [source |> Script.parse(dir_or_file)] | ||
_ -> System.halt(666) | ||
end | ||
end | ||
|
||
def get_sources(dir_or_file, config) do | ||
dir_or_file | ||
|> ScriptSources.find(config.exclude) | ||
|> ScriptSources.to_scripts | ||
end | ||
|
||
def version, do: @version | ||
|
||
defp read_from_stdin(source \\ "") do | ||
case IO.read(:stdio, :line) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an argument to read entirely to EOF in one go? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking to read the source in one go so its easier to add a check later to detect if the source is valid before we hand it off for parsing ? |
||
{:error, reason} -> {:error, reason} | ||
:eof -> {:ok, source} | ||
data -> source = source <> data | ||
read_from_stdin(source) | ||
end | ||
end | ||
|
||
defp notify_start(scripts, dispatcher) do | ||
GenEvent.sync_notify(dispatcher, {:start, scripts}) | ||
scripts | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,9 @@ defmodule Dogma.Config do | |
""" | ||
|
||
defstruct( | ||
rules: [], | ||
exclude: [], | ||
rules: [], | ||
exclude: [], | ||
read_stdin: nil | ||
) | ||
|
||
|
||
|
@@ -15,10 +16,11 @@ defmodule Dogma.Config do | |
@doc """ | ||
Build the config struct for the current project. | ||
""" | ||
def build do | ||
def build(settings \\ %{read_stdin: false}) do | ||
%__MODULE__{ | ||
rules: get_rules, | ||
exclude: get_exclude, | ||
read_stdin: settings[:read_stdin] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Funny spacing here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
} | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,16 +10,15 @@ defmodule Mix.Tasks.Dogma do | |
@config_file_path "config/dogma.exs" | ||
|
||
def run(argv) do | ||
{dir, reporter, noerror} = argv |> parse_args | ||
{dir_or_file, reporter, noerror, read_stdin?} = argv |> parse_args | ||
if File.regular?(@config_file_path) do | ||
Mix.Tasks.Loadconfig.run([ @config_file_path]) | ||
end | ||
config = Config.build | ||
|
||
config = Config.build(read_stdin: read_stdin?) | ||
{:ok, dispatcher} = GenEvent.start_link([]) | ||
GenEvent.add_handler(dispatcher, reporter, []) | ||
|
||
dir | ||
dir_or_file | ||
|> Dogma.run(config, dispatcher) | ||
|> any_errors? | ||
|> if do | ||
|
@@ -30,18 +29,19 @@ defmodule Mix.Tasks.Dogma do | |
end | ||
|
||
def parse_args(argv) do | ||
switches = [format: :string, error: :boolean] | ||
switches = [format: :string, error: :boolean, stdin: :boolean] | ||
{switches, files, []} = OptionParser.parse(argv, switches: switches) | ||
|
||
noerror = !Keyword.get(switches, :error, true) | ||
read_stdin? = Keyword.get(switches, :stdin, false) | ||
format = Keyword.get(switches, :format) | ||
reporter = Map.get( | ||
Reporters.reporters, | ||
format, | ||
Reporters.default_reporter | ||
) | ||
|
||
{List.first(files), reporter, noerror} | ||
{List.first(files), reporter, noerror, read_stdin?} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tuple is starting to get a big unwieldy I think. Perhaps something we could fix in another pull request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed |
||
end | ||
|
||
defp any_errors?(scripts) do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't use _config, don't assign it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed