summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@plataformatec.com.br>2017-10-08 12:18:03 +0200
committerJosé Valim <jose.valim@plataformatec.com.br>2017-10-08 12:18:03 +0200
commit5e46354fe4851c7bf7fb840d5bfe4da8f48ca5e9 (patch)
tree058c69faab3fc45a46d7f88f50e58175ca5fbdea
parentba84c3b2d9b911adb4c96c973ac138fc2f32618d (diff)
downloadelixir-5e46354fe4851c7bf7fb840d5bfe4da8f48ca5e9.tar.gz
Add script for picking random files for formatting
-rw-r--r--.formatter.exs7
-rw-r--r--scripts/random_file.exs56
2 files changed, 62 insertions, 1 deletions
diff --git a/.formatter.exs b/.formatter.exs
index fa6844b64..57b824457 100644
--- a/.formatter.exs
+++ b/.formatter.exs
@@ -1,4 +1,9 @@
[
+ inputs: [
+ "lib/*/{lib,unicode,test}/**/*.{ex,exs}",
+ "lib/*/mix.exs"
+ ],
+
locals_without_parens: [
# Formatter tests
assert_format: 2,
@@ -9,4 +14,4 @@
# Mix tests
in_tmp: 2
]
-] \ No newline at end of file
+]
diff --git a/scripts/random_file.exs b/scripts/random_file.exs
new file mode 100644
index 000000000..b60b72ee9
--- /dev/null
+++ b/scripts/random_file.exs
@@ -0,0 +1,56 @@
+# bin/elixir scripts/random_file.exs
+
+defmodule RandomFile do
+ @pattern "lib/*/{lib,unicode,test}/**/*.{ex,exs}"
+
+ def run(args) do
+ popper =
+ if "--alphabetically" in args do
+ &Enum.split(&1, 1)
+ else
+ fn collection ->
+ random = Enum.random(collection)
+ {random, List.delete(collection, random)}
+ end
+ end
+
+ {opts, _} = Code.eval_file(".formatter.exs")
+
+ @pattern
+ |> Path.wildcard()
+ |> random_file(popper, opts)
+ end
+
+ defp random_file([], _popper, _opts) do
+ IO.puts("All files are formatted, hooray!")
+ end
+
+ defp random_file(collection, popper, opts) do
+ {file, collection} = popper.(collection)
+
+ IO.write("Checking #{file}... ")
+ input = File.read!(file)
+ output = IO.iodata_to_binary([Code.format_string!(file, opts), ?\n])
+
+ if input == output do
+ IO.puts("already formatted.")
+ random_file(collection, popper, opts)
+ else
+ IO.write("""
+ jackpot!
+
+ The file above needs formatting.
+ Please format it with the command below:
+
+ bin/elixir bin/mix format #{file}
+
+ Once formatted, check for any faults and submit a pull request.
+ For more information see: https://github.com/elixir-lang/elixir/issues/6643
+
+ Have fun!
+ """)
+ end
+ end
+end
+
+RandomFile.run(System.argv())