Skip to content

Commit 83e2da6

Browse files
authored
Merge pull request #957 from naymspace/feature/backpex-installer
Add `backpex.install` command
2 parents 9064174 + 0ae5a73 commit 83e2da6

File tree

8 files changed

+530
-4
lines changed

8 files changed

+530
-4
lines changed

demo/mix.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ defmodule Demo.MixProject do
4343
{:smokestack, "~> 0.9.2"},
4444
{:faker, "~> 0.18"},
4545
{:phoenix_test, "~> 0.7.0", only: :test, runtime: false},
46-
{:sourceror, "~> 1.7", only: [:dev, :test]},
4746
{:phoenix_test_playwright, "~> 0.7.0", only: :test, runtime: false},
4847
{:a11y_audit, "~> 0.2.3", only: :test},
4948
{:live_debugger, "~> 0.3", only: :dev},

demo/mix.lock

Lines changed: 12 additions & 0 deletions
Large diffs are not rendered by default.

guides/get_started/installation.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Installation
22

3-
The following guide will help you to install Backpex in your Phoenix application. We will guide you through the installation process and show you how to create a simple resource.
3+
The following guide will help you to install Backpex in your Phoenix application. We will guide you through the installation process and show you how to create a simple resource.
44

55
## Prerequisites
66

@@ -10,6 +10,10 @@ Backpex integrates seamlessly with your existing Phoenix LiveView application, b
1010

1111
Backpex is built on top of Phoenix LiveView, so you need to have Phoenix LiveView installed in your application. If you generate a new Phoenix application using the latest version of the `mix phx.new` generator, Phoenix LiveView is included by default.
1212

13+
> #### Info {: .info}
14+
>
15+
> We have created an initial version of the [Backpex installer task](Mix.Tasks.Backpex.Install.html), which runs with the help of the [Igniter](https://github.com/ash-project/igniter) framework. This installer automates the upcoming steps up to the "[Create an example resource](installation.html#create-an-example-resource)" step. Feedback is welcome!
16+
1317
### Tailwind CSS
1418

1519
Backpex uses Tailwind CSS for styling. Make sure you have Tailwind CSS installed in your application. You can install Tailwind CSS by following the [official installation guide](https://tailwindcss.com/docs/installation/framework-guides/phoenix). If you generate a new Phoenix application using the latest version of the `mix phx.new` generator, Tailwind CSS is included by default.

lib/mix/helpers.ex

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
defmodule Backpex.Mix.Helpers do
2+
@moduledoc """
3+
Helper functions for the Backpex mix tasks.
4+
"""
5+
6+
alias Igniter.Libs.Phoenix
7+
alias Igniter.Project.Application, as: IgniterApp
8+
alias Igniter.Project.Module, as: IgniterModule
9+
alias Igniter.Util.Warning
10+
11+
@doc """
12+
Gets the Phoenix PubSub module from the application configuration.
13+
"""
14+
def pubsub_module(igniter) do
15+
web_module = Phoenix.web_module(igniter)
16+
endpoint_module = Module.safe_concat(web_module, Endpoint)
17+
app_name = IgniterApp.app_name(igniter)
18+
19+
Application.get_env(app_name, endpoint_module)[:pubsub_server]
20+
end
21+
22+
@doc """
23+
Checks if a specific string exists within a module's source code.
24+
"""
25+
def exists_in_module?(igniter, module, line) do
26+
case IgniterModule.find_module(igniter, module) do
27+
{:ok, {igniter, source, _zipper}} ->
28+
{:ok, {igniter, string_in_source?(source, line)}}
29+
30+
{:error, igniter} ->
31+
Mix.shell().warning("Could not find module #{inspect(module)}")
32+
{:error, igniter}
33+
end
34+
end
35+
36+
@doc """
37+
Checks if a specific string exists within a source's content.
38+
"""
39+
def string_in_source?(source, string) do
40+
source
41+
|> Rewrite.Source.get(:content)
42+
|> String.contains?(string)
43+
end
44+
45+
@doc """
46+
Updates a file by adding a line if it doesn't already exist.
47+
"""
48+
def add_line_to_file(igniter, file_path, new_line) do
49+
if Igniter.exists?(igniter, file_path) do
50+
Igniter.update_file(igniter, file_path, &add_line(&1, file_path, new_line))
51+
else
52+
Warning.warn_with_code_sample(
53+
igniter,
54+
"File not found at #{file_path}. Please manually add the following line:",
55+
new_line
56+
)
57+
end
58+
end
59+
60+
defp add_line(source, file_path, line) do
61+
content = Rewrite.Source.get(source, :content)
62+
63+
if String.contains?(content, line) do
64+
Mix.shell().info("'#{line}' already exists in #{file_path}.")
65+
source
66+
else
67+
Rewrite.Source.update(source, :content, content <> "\n#{line}")
68+
end
69+
end
70+
71+
@doc """
72+
Gets the web folder path for the Phoenix application.
73+
"""
74+
def web_folder_path(igniter) do
75+
igniter
76+
|> IgniterApp.app_name()
77+
|> Mix.Phoenix.web_path()
78+
end
79+
80+
@doc """
81+
Checks if an npm package is already installed in the project.
82+
"""
83+
def npm_package_installed?(package_name) do
84+
env = [{"PATH", System.get_env("PATH")}]
85+
86+
case System.cmd("npm", ["list", "--depth=0", package_name], stderr_to_stdout: true, env: env) do
87+
{_output, 0} -> true
88+
{_output, _int} -> false
89+
end
90+
end
91+
end

0 commit comments

Comments
 (0)