lib/mix/tasks/arke.new.ex

defmodule Mix.Tasks.Arke.New do
  @moduledoc """
  Creates a new Arke project.

  It expects the path of the project as an argument.

      $ mix arke.new PATH [--app PROJECT-NAME]

  A project at the given PATH will be created. The
  application name and module name will be retrieved
  from the path, unless `--app` is given.

  ## Options

    * `--app` - the name of the ARKE application


    * `--db` - specify the database adapter for Ecto. One of:

        * `postgres` - via https://github.com/elixir-ecto/postgrex

      Please check the driver docs for more information
      and requirements. Defaults to "postgres".

    * `--dev` - Contribute to the Arke developement and use local version of the packages

    * `--dashboard` - Enable PhoenixLiveView

    * `--mailer` - specify the mailer to use with [Swoosh](https://hexdocs.pm/swoosh/Swoosh.html). One of:

        * `mailgun` - via https://hexdocs.pm/swoosh/Swoosh.Adapters.Mailgun.html#content


    * `--terraform` - create a folder containing a terraform template for arke

    * `--verbose` - use verbose output


    * `-v`, `--version` - prints the Arke starter version

  ## Installation

  `mix arke.new` by default prompts you to fetch and install your
  dependencies. You can enable this behaviour by passing the
  `--install` flag or disable it by using `--install=false`.

  ## Examples

      $ mix arke.new my_arke_project
  """
  use Mix.Task
  alias Arke.New.{Project, Single, Generator}

  @version Mix.Project.config()[:version]
  @shortdoc "Creates a new Arke v#{@version} application"

  @switches [
    app: :string,
    db: :string,
    mailer: :string,
    dashboard: :boolean,
    dev: :boolean,
    terraform: :boolean,
    install: :boolean
  ]

  @impl true
  def run([version]) when version in ~w(-v --version) do
    Mix.shell().info("Arke installer v#{@version}")
  end

  def run(argv) do
    elixir_version_check!()

    case OptionParser.parse!(argv, strict: @switches) do
      {_opts, []} ->
        Mix.Tasks.Help.run(["arke.new"])

      {opts, [base_path | _]} ->
        generator = Single
        generate(base_path, generator, :project_path, opts)
    end
  end

  @doc false
  def run(argv, generator, path) do
    elixir_version_check!()

    case OptionParser.parse!(argv, strict: @switches) do
      {_opts, []} -> Mix.Tasks.Help.run(["arke.new"])
      {opts, [base_path | _]} -> generate(base_path, generator, path, opts)
    end
  end

  defp generate(base_path, generator, path, opts) do
    base_path
    |> Project.new(opts)
    |> generator.prepare_project()
    |> Generator.put_binding()
    |> validate_project(path)
    |> generator.generate()
    |> prompt_to_install_deps(generator, path)
  end

  defp validate_project(%Project{opts: opts} = project, path) do
    check_app_name!(project.app, !!opts[:app])
    check_directory_existence!(Map.fetch!(project, path))
    check_module_name_validity!(project.root_mod)
    check_module_name_availability!(project.root_mod)

    project
  end

  defp prompt_to_install_deps(%Project{} = project, generator, path_key) do
    path = Map.fetch!(project, path_key)

    install? =
      Keyword.get_lazy(project.opts, :install, fn ->
        Mix.shell().yes?("\nFetch and install dependencies?")
      end)

    cd_step = ["$ cd #{relative_app_path(path)}"]

    maybe_cd(path, fn ->
      mix_step = install_mix(project, install?)

      if mix_step == [] do
        if rebar_available?() do
          cmd(project, "mix deps.compile")
        end
      end

      print_missing_steps(cd_step ++ mix_step)

      print_mix_info(generator)
    end)
  end

  defp maybe_cd(path, func), do: path && File.cd!(path, func)

  defp install_mix(project, install?) do
    if install? && hex_available?() do
      cmd(project, "mix deps.get")
    else
      ["$ mix deps.get"]
    end
  end

  # TODO: Elixir v1.15 automatically installs Hex/Rebar if missing, so we can simplify this.
  defp hex_available? do
    Code.ensure_loaded?(Hex)
  end

  defp rebar_available? do
    Mix.Rebar.rebar_cmd(:rebar3)
  end

  defp print_missing_steps(steps) do
    Mix.shell().info("""

    We are almost there! The following steps are missing:

        #{Enum.join(steps, "\n    ")}
    """)
  end


  defp print_ecto_info(_gen) do
    Mix.shell().info("""
    Then configure your database in config/dev.exs and run:

        $ mix ecto.create
    """)
  end

  defp print_mix_info(Ecto) do
    Mix.shell().info("""
    You can run your app inside IEx (Interactive Elixir) as:

        $ iex -S mix
    """)
  end

  defp print_mix_info(_gen) do
    Mix.shell().info("""
    Create the secrets for the env file (you can use the same secret for both keys)

        $ mix guardian.gen.secret (SECRET_KEY_AUTH)
        $ mix phx.gen.secret (SECRET_KEY_BASE)

    Create the database and the project following the steps in the README

    Once all the environments variables are set run

        $ source .env

    Start your Arke app with:

        $ mix phx.server

    You can also run your app inside IEx (Interactive Elixir) as:

        $ iex -S mix phx.server
    """)
  end

  defp relative_app_path(path) do
    case Path.relative_to_cwd(path) do
      ^path -> Path.basename(path)
      rel -> rel
    end
  end

  ## Helpers

  defp cmd(%Project{} = project, cmd, log? \\ true) do
    if log? do
      Mix.shell().info([:green, "* running ", :reset, cmd])
    end

    case Mix.shell().cmd(cmd, cmd_opts(project)) do
      0 -> []
      _ -> ["$ #{cmd}"]
    end
  end

  defp cmd_opts(%Project{} = project) do
    if Project.verbose?(project) do
      []
    else
      [quiet: true]
    end
  end

  defp check_app_name!(name, from_app_flag) do
    unless name =~ Regex.recompile!(~r/^[a-z][\w_]*$/) do
      extra =
        if !from_app_flag do
          ". The application name is inferred from the path, if you'd like to " <>
            "explicitly name the application then use the `--app APP` option."
        else
          ""
        end

      Mix.raise(
        "Application name must start with a letter and have only lowercase " <>
          "letters, numbers and underscore, got: #{inspect(name)}" <> extra
      )
    end
  end

  defp check_module_name_validity!(name) do
    unless inspect(name) =~ Regex.recompile!(~r/^[A-Z]\w*(\.[A-Z]\w*)*$/) do
      Mix.raise(
        "Module name must be a valid Elixir alias (for example: Foo.Bar), got: #{inspect(name)}"
      )
    end
  end

  defp check_module_name_availability!(name) do
    [name]
    |> Module.concat()
    |> Module.split()
    |> Enum.reduce([], fn name, acc ->
      mod = Module.concat([Elixir, name | acc])

      if Code.ensure_loaded?(mod) do
        Mix.raise("Module name #{inspect(mod)} is already taken, please choose another name")
      else
        [name | acc]
      end
    end)
  end

  defp check_directory_existence!(path) do
    if File.dir?(path) and
         not Mix.shell().yes?(
           "The directory #{path} already exists. Are you sure you want to continue?"
         ) do
      Mix.raise("Please select another directory for installation.")
    end
  end

  defp elixir_version_check! do
    unless Version.match?(System.version(), "~> 1.13") do
      Mix.raise(
        "Arke v#{@version} requires at least Elixir v1.14\n " <>
          "You have #{System.version()}. Please update accordingly"
      )
    end
  end
end