zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

gen.check.ex (2464B)


      1 defmodule Credo.CLI.Command.GenCheck do
      2   @moduledoc false
      3 
      4   @check_template_filename ".template.check.ex"
      5   @default_check_template_file File.read!(@check_template_filename)
      6 
      7   use Credo.CLI.Command,
      8     short_description: "Create a new custom check"
      9 
     10   alias Credo.CLI.Output.UI
     11 
     12   @doc false
     13   def call(exec, _opts) do
     14     check_file =
     15       case exec.cli_options do
     16         %Credo.CLI.Options{args: [], switches: %{files_included: [single_file]}} ->
     17           single_file
     18 
     19         %Credo.CLI.Options{args: [single_file]} ->
     20           single_file
     21 
     22         _ ->
     23           nil
     24       end
     25 
     26     create_check_file(check_file)
     27 
     28     exec
     29   end
     30 
     31   defp create_check_file(nil) do
     32     output = [
     33       :red,
     34       :bright,
     35       "Please provide a filename:",
     36       "\n\n",
     37       "  mix credo gen.check lib/my_first_credo_check.ex",
     38       "\n"
     39     ]
     40 
     41     UI.puts(output)
     42   end
     43 
     44   defp create_check_file(filename) do
     45     check_name = check_name_for(filename)
     46 
     47     if File.exists?(filename) do
     48       UI.puts([:red, :bright, "File exists: #{filename}, aborted."])
     49     else
     50       write_template_file(filename, check_name)
     51 
     52       UI.puts([:green, "* creating ", :reset, "#{filename}"])
     53       UI.puts()
     54 
     55       print_config_instructions(filename, check_name)
     56     end
     57   end
     58 
     59   def check_name_for(filename) do
     60     filename
     61     |> String.replace(~r/(\A|(.+)\/)(lib|web)\//, "")
     62     |> String.replace(~r/\.ex$/, "")
     63     |> Macro.camelize()
     64     |> String.replace(~r/\_/, "")
     65   end
     66 
     67   defp write_template_file(filename, check_name) do
     68     filename
     69     |> Path.dirname()
     70     |> File.mkdir_p!()
     71 
     72     assigns = [check_name: check_name]
     73     contents = EEx.eval_string(@default_check_template_file, assigns: assigns)
     74 
     75     File.write!(filename, contents)
     76   end
     77 
     78   defp print_config_instructions(filename, check_name) do
     79     UI.puts("Add the generated file to the list of `requires` in `.credo.exs`:")
     80     UI.puts()
     81 
     82     UI.puts([
     83       "    requires: [",
     84       :green,
     85       "\"",
     86       filename,
     87       "\"",
     88       :reset,
     89       "], ",
     90       :faint,
     91       "# <-- add file here"
     92     ])
     93 
     94     UI.puts()
     95     UI.puts("Remember to add the generated module to the list of `checks`:")
     96     UI.puts()
     97     UI.puts(["    checks: ["])
     98 
     99     UI.puts([
    100       "      {",
    101       :green,
    102       check_name,
    103       :reset,
    104       "}, ",
    105       :faint,
    106       "# <-- add check here"
    107     ])
    108 
    109     UI.puts(["    ]"])
    110     UI.puts()
    111     UI.puts(["If you do not have a exec file yet, use `mix credo gen.config`"])
    112   end
    113 end