Current section

Files

Jump to
star_view lib mix tasks star_view.setup.datastar.ex
Raw

lib/mix/tasks/star_view.setup.datastar.ex

defmodule Mix.Tasks.StarView.Setup.Datastar.Docs do
@moduledoc false
def short_doc(), do: "Configures StarView dev URL and HTTPS"
def example(), do: "mix star_view.setup.datastar"
def long_doc(), do: "#{short_doc()}"
end
if Code.ensure_loaded?(Igniter) do
defmodule Mix.Tasks.StarView.Setup.Datastar do
@shortdoc "#{__MODULE__.Docs.short_doc()}"
@moduledoc __MODULE__.Docs.long_doc()
use Igniter.Mix.Task
@impl Igniter.Mix.Task
def info(_argv, _composing_task) do
%Igniter.Mix.Task.Info{
group: :star_view,
adds_deps: [],
installs: [],
example: __MODULE__.Docs.example(),
only: nil,
positional: [],
composes: [],
schema: [https: :boolean],
defaults: [https: true],
aliases: [],
required: []
}
end
@impl Igniter.Mix.Task
def igniter(igniter) do
https? = Keyword.get(igniter.args.options, :https, true)
app_name = Igniter.Project.Application.app_name(igniter)
web_module = Igniter.Libs.Phoenix.web_module(igniter)
endpoint_module = Module.concat(web_module, Endpoint)
{phoenix?, igniter} = Igniter.Project.Module.module_exists(igniter, endpoint_module)
igniter
|> maybe_setup_https(https?, app_name, endpoint_module, phoenix?)
end
defp maybe_setup_https(igniter, false, _, _, _), do: igniter
defp maybe_setup_https(igniter, _https?, _app_name, _endpoint, false), do: igniter
defp maybe_setup_https(igniter, true, app_name, endpoint_module, true) do
host = dev_host(app_name)
port = 4001
url = "https://#{host}:#{port}"
Igniter.Project.Config.configure(
igniter,
"dev.exs",
app_name,
[endpoint_module, :url],
{:code,
Sourceror.parse_string!("""
[
scheme: "https",
host: "#{host}",
port: #{port}
]
""")}
)
|> Igniter.Project.Config.configure(
"dev.exs",
app_name,
[endpoint_module, :https],
{:code,
Sourceror.parse_string!("""
[
port: #{port},
cipher_suite: :strong,
keyfile: "priv/cert/selfsigned_key.pem",
certfile: "priv/cert/selfsigned.pem"
]
""")}
)
|> Igniter.Project.Config.configure(
"dev.exs",
app_name,
[:star_view, :dev_url],
url
)
|> Igniter.delay_task("phx.gen.cert", [host, "localhost"])
|> Igniter.add_notice("""
StarView dev URL configured: #{url}
HTTPS configured for dev on port #{port}.
A dev certificate for #{host} has been queued.
To add #{host} to your hosts file and trust the self-signed HTTPS certificate, run:
mix star_view.trust --host #{host}
This optional command requires sudo privileges.
Then run: `mix dev`.
""")
|> Igniter.add_warning("""
StarView cannot run `mix star_view.trust` as an Igniter queued task because interactive stdin is not reliably forwarded to child Mix tasks.
Run it directly after install if you want StarView to configure local host and certificate trust.
""")
end
defp dev_host(app_name) do
app_name
|> to_string()
|> String.replace("_", "-")
|> Kernel.<>(".test")
end
end
else
defmodule Mix.Tasks.StarView.Setup.Datastar do
@shortdoc "#{__MODULE__.Docs.short_doc()} | Install `igniter` to use"
@moduledoc __MODULE__.Docs.long_doc()
use Mix.Task
@impl Mix.Task
def run(_argv) do
Mix.shell().error("Requires igniter.")
exit({:shutdown, 1})
end
end
end