PhoenixMountable.Install behaviour (PhoenixMountable v0.2.5)

Copy Markdown View Source

A mountable library's installer, declared in one use:

defmodule Mix.Tasks.MyLib.Install do
  use PhoenixMountable.Install,
    otp_app: :my_lib,
    mounts: [
      {MyLib.Router, "/my_lib", scope: "/admin", options: "resolver: {web}.MyLibResolver"}
    ],
    migrations: MyLib.Migrations,
    hook: {"MyLib", "my_lib"},
    css: ["my_lib.css", {"my_lib-dark.css", :comment}]
end

Hosts run mix igniter.install my_lib, which adds the dependency and runs the task, or mix my_lib.install once the dependency is there. Igniter shows every change as a diff before it writes, and each step is idempotent, so running it again changes nothing.

The steps, each one when its option is given, in this order:

  1. :mounts - mounts each definition in the host's router (PhoenixMountable.Igniter.mount/4), with its options.
  2. :migrations - writes the migration that installs the library's tables, or upgrades them (PhoenixMountable.Igniter.gen_migration/3).
  3. :hook - adds a LiveView hook to assets/js/app.js (PhoenixMountable.Igniter.add_hook/4), for components that render in the host's own pages.
  4. :css - imports style sheets in assets/css/app.css (PhoenixMountable.Igniter.add_css_imports/4).
  5. :tailwind - makes the host's Tailwind scan the library (PhoenixMountable.Igniter.add_tailwind_source/2), for libraries styled by the host.
  6. :supervisor - starts the mounts' processes at boot (PhoenixMountable.Igniter.add_supervisor/2).
  7. :raw_body - gives the endpoint's Plug.Parsers the reader that keeps a body as it was sent, for the library's signed webhooks (PhoenixMountable.Igniter.add_raw_body/2).
  8. :html_helpers - adds lines to the html_helpers of the host's web module, for the library's components or functions in every template (PhoenixMountable.Igniter.add_html_helpers/3).

Then the library's own steps, in install/1 (a child of the host's application with its options, a module of the host's to write):

@impl PhoenixMountable.Install
def install(igniter) do
  Igniter.Project.Application.add_new_child(igniter, {MyLib, repo: MyApp.Repo})
end

Without Igniter

Igniter is an optional dependency of the library: in a host without it, the task says how to get it, and how to make the changes by hand (the :manual option, the text of the library's guide).

Options

  • :otp_app (atom/0) - Required. The library's application.

  • :mounts (list of term/0) - {definition, path} or {definition, path, opts}, opts as in PhoenixMountable.Igniter.mount/4. The default value is [].

  • :migrations (atom/0) - The library's PhoenixMountable.Migrations module.

  • :hook (tuple of String.t/0, String.t/0 values) - {hook, package}: the hook's name and its npm package.

  • :css (list of term/0) - Style sheets, as in PhoenixMountable.Igniter.add_css_imports/4. The default value is [].

  • :tailwind (boolean/0) - Add the library to the host's Tailwind sources. The default value is false.

  • :supervisor (boolean/0) - Start the mounts' processes at boot. The default value is false.

  • :raw_body (boolean/0) - Give the host's endpoint the reader that keeps a body as it was sent, for the library's routes that check a signature of it (PhoenixMountable.RawBody). The default value is false.

  • :html_helpers (list of String.t/0) - Lines for the html_helpers of the host's web module, such as "import MyLib.Components"; {app} and {web} are the host's module prefix and web module. The default value is [].

  • :shortdoc (String.t/0) - The task's short doc. Defaults to Installs <library> in a Phoenix app.

  • :manual (String.t/0) - What to do by hand, for a host without Igniter.

Summary

Callbacks

The library's own steps, after the declared ones.

Callbacks

install(igniter)

(optional)
@callback install(igniter :: term()) :: term()

The library's own steps, after the declared ones.