-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extension section module imports, generated .formatter.exs
- Loading branch information
1 parent
c776da9
commit 2dbf716
Showing
11 changed files
with
168 additions
and
30 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
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,4 +1,4 @@ | ||
name: Elixir CI | ||
name: Ash CI | ||
|
||
on: | ||
push: | ||
|
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
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,96 @@ | ||
defmodule Mix.Tasks.Ash.Formatter do | ||
@moduledoc "Generates a .formatter.exs from a list of extensions, and writes it." | ||
use Mix.Task | ||
|
||
@formatter_exs_template """ | ||
# THIS FILE IS AUTOGENERATED USING `mix ash.formatter` | ||
# DONT MODIFY IT BY HAND | ||
locals_without_parens = [ | ||
<%= for {function, arity} <- locals_without_parens do %> | ||
<%= function %>: <%= arity %>, | ||
<% end %> | ||
] | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], | ||
locals_without_parens: locals_without_parens, | ||
export: [ | ||
locals_without_parens: locals_without_parens | ||
] | ||
] | ||
""" | ||
|
||
@shortdoc @moduledoc | ||
def run(opts) do | ||
{opts, []} = OptionParser.parse!(opts, strict: [check: :boolean, extensions: :string]) | ||
|
||
unless opts[:extensions] do | ||
raise "Must supply a comma separated list of extensions to generate a .formatter.exs for" | ||
end | ||
|
||
locals_without_parens = | ||
opts[:extensions] | ||
|> String.split(",") | ||
|> Enum.flat_map(fn extension -> | ||
extension_mod = Module.concat([extension]) | ||
|
||
all_entity_builders(extension_mod.sections()) | ||
end) | ||
|> Enum.uniq() | ||
|
||
contents = | ||
@formatter_exs_template | ||
|> EEx.eval_string(locals_without_parens: locals_without_parens) | ||
|> Code.format_string!() | ||
|> Enum.join() | ||
|> Kernel.<>("\n") | ||
|
||
if opts[:check] do | ||
if File.read!(".formatter.exs") != contents do | ||
raise """ | ||
.formatter.exs is not up to date! | ||
Run the following command and commit the result: | ||
mix ash.formatter --extensions #{opts[:extensions]} | ||
""" | ||
end | ||
else | ||
File.write!(".formatter.exs", contents) | ||
end | ||
end | ||
|
||
defp all_entity_builders(sections) do | ||
Enum.flat_map(sections, fn section -> | ||
Enum.concat([ | ||
entity_option_builders(section), | ||
section_option_builders(section), | ||
entity_builders(section) | ||
]) | ||
end) | ||
end | ||
|
||
defp entity_builders(section) do | ||
Enum.flat_map(section.entities, fn entity -> | ||
arg_count = Enum.count(entity.args) | ||
[{entity.name, arg_count}, {entity.name, arg_count + 1}] | ||
end) ++ all_entity_builders(section.sections()) | ||
end | ||
|
||
defp entity_option_builders(section) do | ||
Enum.flat_map(section.entities, fn entity -> | ||
entity.schema | ||
|> Keyword.drop(entity.args) | ||
|> Enum.map(fn {key, _schema} -> | ||
{key, 1} | ||
end) | ||
end) | ||
end | ||
|
||
defp section_option_builders(section) do | ||
Enum.map(section.schema, fn {key, _} -> | ||
{key, 1} | ||
end) | ||
end | ||
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