-
-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Igniter task to install Phoenix #140
Draft
leandrocp
wants to merge
7
commits into
ash-project:main
Choose a base branch
from
leandrocp:lp-phx-installer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a043b6
wip: install_phoenix task
leandrocp 3ca5ef9
Merge branch 'main' into lp-phx-installer
leandrocp c430ecf
phx.new
leandrocp 7ec291e
mix format
leandrocp c1ab8da
Merge branch 'main' into lp-phx-installer
leandrocp 831845e
Adress feedback:
leandrocp 7f2c38f
Do not expand %Project{} to avoid compilation errors
leandrocp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule Igniter.Phoenix.Generator do | ||
@moduledoc false | ||
# Wrap Phx.New.Generator | ||
# https://github.com/phoenixframework/phoenix/blob/7586cbee9e37afbe0b3cdbd560b9e6aa60d32bf6/installer/lib/phx_new/generator.ex#L69 | ||
|
||
def copy_from(igniter, project, mod, name) when is_atom(name) do | ||
mapping = mod.template_files(name) | ||
|
||
templates = | ||
for {format, _project_location, files} <- mapping, | ||
{source, target_path} <- files, | ||
source = to_string(source) do | ||
target = expand_path_with_bindings(target_path, project) | ||
{format, source, target} | ||
end | ||
|
||
Enum.reduce(templates, igniter, fn {format, source, target}, acc -> | ||
case format do | ||
:keep -> | ||
acc | ||
|
||
:text -> | ||
contents = mod.render(name, source, project.binding) | ||
Igniter.create_new_file(acc, target, contents, on_exists: :overwrite) | ||
leandrocp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
:config -> | ||
contents = mod.render(name, source, project.binding) | ||
config_inject(acc, target, contents) | ||
|
||
:prod_config -> | ||
contents = mod.render(name, source, project.binding) | ||
prod_only_config_inject(acc, target, contents) | ||
|
||
:eex -> | ||
contents = mod.render(name, source, project.binding) | ||
Igniter.create_new_file(acc, target, contents, on_exists: :overwrite) | ||
end | ||
end) | ||
end | ||
|
||
defp expand_path_with_bindings(path, project) do | ||
Regex.replace(Regex.recompile!(~r/:[a-zA-Z0-9_]+/), path, fn ":" <> key, _ -> | ||
project |> Map.fetch!(:"#{key}") |> to_string() | ||
end) | ||
end | ||
|
||
defp config_inject(igniter, file, to_inject) do | ||
patterns = [ | ||
""" | ||
import Config | ||
__cursor__() | ||
""" | ||
] | ||
|
||
Igniter.create_or_update_elixir_file(igniter, file, to_inject, fn zipper -> | ||
case Igniter.Code.Common.move_to_cursor_match_in_scope(zipper, patterns) do | ||
{:ok, zipper} -> | ||
{:ok, Igniter.Code.Common.add_code(zipper, to_inject)} | ||
|
||
_ -> | ||
{:warning, | ||
""" | ||
Could not automatically inject the following config into #{file} | ||
|
||
#{to_inject} | ||
"""} | ||
end | ||
end) | ||
end | ||
|
||
defp prod_only_config_inject(igniter, file, to_inject) do | ||
patterns = [ | ||
""" | ||
if config_env() == :prod do | ||
__cursor__() | ||
end | ||
""", | ||
""" | ||
if :prod == config_env() do | ||
__cursor__() | ||
end | ||
""" | ||
] | ||
|
||
Igniter.create_or_update_elixir_file(igniter, file, to_inject, fn zipper -> | ||
case Igniter.Code.Common.move_to_cursor_match_in_scope(zipper, patterns) do | ||
{:ok, zipper} -> | ||
{:ok, Igniter.Code.Common.add_code(zipper, to_inject)} | ||
|
||
_ -> | ||
{:warning, | ||
""" | ||
Could not automatically inject the following config into #{file} | ||
|
||
#{to_inject} | ||
"""} | ||
end | ||
end) | ||
end | ||
|
||
def gen_ecto_config(igniter, %{binding: binding}) do | ||
adapter_config = binding[:adapter_config] | ||
|
||
config_inject(igniter, "config/dev.exs", """ | ||
# Configure your database | ||
config :#{binding[:app_name]}, #{binding[:app_module]}.Repo#{kw_to_config(adapter_config[:dev])} | ||
""") | ||
end | ||
|
||
defp kw_to_config(kw) do | ||
Enum.map(kw, fn | ||
{k, {:literal, v}} -> ",\n #{k}: #{v}" | ||
{k, v} -> ",\n #{k}: #{inspect(v)}" | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
defmodule Igniter.Phoenix.Single do | ||
@moduledoc false | ||
# Wrap Phx.New.Single | ||
# https://github.com/phoenixframework/phoenix/blob/7586cbee9e37afbe0b3cdbd560b9e6aa60d32bf6/installer/lib/phx_new/single.ex | ||
|
||
alias Phx.New.Project | ||
alias Igniter.Phoenix.Generator | ||
|
||
@mod Phx.New.Single | ||
|
||
def generate(igniter, project) do | ||
generators = [ | ||
{true, &gen_new/2}, | ||
{Project.ecto?(project), &gen_ecto/2}, | ||
{Project.html?(project), &gen_html/2}, | ||
{Project.mailer?(project), &gen_mailer/2}, | ||
{Project.gettext?(project), &gen_gettext/2}, | ||
{true, &gen_assets/2} | ||
] | ||
|
||
Enum.reduce(generators, igniter, fn | ||
{true, gen_fun}, acc -> gen_fun.(acc, project) | ||
_, acc -> acc | ||
end) | ||
end | ||
|
||
def gen_new(igniter, project) do | ||
Generator.copy_from(igniter, project, @mod, :new) | ||
end | ||
|
||
def gen_ecto(igniter, project) do | ||
igniter | ||
|> Generator.copy_from(project, @mod, :ecto) | ||
|> Generator.gen_ecto_config(project) | ||
end | ||
|
||
def gen_html(igniter, project) do | ||
Generator.copy_from(igniter, project, @mod, :html) | ||
end | ||
|
||
def gen_mailer(igniter, project) do | ||
Generator.copy_from(igniter, project, @mod, :mailer) | ||
end | ||
|
||
def gen_gettext(igniter, project) do | ||
Generator.copy_from(igniter, project, @mod, :gettext) | ||
end | ||
|
||
def gen_assets(igniter, project) do | ||
javascript? = Project.javascript?(project) | ||
css? = Project.css?(project) | ||
html? = Project.html?(project) | ||
|
||
igniter = Generator.copy_from(igniter, project, @mod, :static) | ||
|
||
igniter = | ||
if html? or javascript? do | ||
command = if javascript?, do: :js, else: :no_js | ||
Generator.copy_from(igniter, project, @mod, command) | ||
else | ||
igniter | ||
end | ||
|
||
if html? or css? do | ||
command = if css?, do: :css, else: :no_css | ||
Generator.copy_from(igniter, project, @mod, command) | ||
else | ||
igniter | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some files do not have a valid string content, for eg binary files like
favicon.ico
but I think we still need to display that such file will be created. Might need to improve this code to detect updates as well.