|
| 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