Skip to content

Commit

Permalink
Merge pull request #296 from nimblehq/release/4.5.0
Browse files Browse the repository at this point in the history
Release - 4.5.0
  • Loading branch information
byhbt authored Nov 22, 2022
2 parents e5545fc + 9a9db8a commit 6742751
Show file tree
Hide file tree
Showing 30 changed files with 683 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
{Credo.Check.Readability.StrictModuleLayout,
[
order:
~w/shortdoc moduledoc behaviour use import alias require module_attribute defstruct callback public_fun private_fun/a,
~w/shortdoc moduledoc behaviour use import alias require module_attribute defstruct type callback public_fun private_fun/a,
ignore: ~w/callback_impl/a
]},
{Credo.Check.Consistency.MultiAliasImportRequireUse, []},
Expand Down
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @byhbt

# Team Members
* @andyduong1920 @bterone @hanam1ni @junan @longnd @rosle @topnimble @Nihisil @nvminhtue @liamstevens111
* @andyduong1920 @bterone @hanam1ni @longnd @rosle @topnimble @Nihisil @nvminhtue @liamstevens111

# Engineering Leads
CODEOWNERS @nimblehq/engineering-leads
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Step 2: Add `nimble_template` dependency to `mix.exs`:
```elixir
def deps do
[
{:nimble_template, "~> 4.4.0", only: :dev, runtime: false},
{:nimble_template, "~> 4.5.0", only: :dev, runtime: false},
# other dependencies ...
]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nimble_template/addons/addon.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule NimbleTemplate.Addons.Addon do
alias NimbleTemplate.ProjectHelper

def apply(%Project{} = project, opts \\ %{}) when is_map(opts) do
Generator.print_log("* applying ", inspect(__MODULE__))
Generator.info_log("* applying ", inspect(__MODULE__))

project
|> do_apply(opts)
Expand Down
8 changes: 5 additions & 3 deletions lib/nimble_template/addons/credo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ defmodule NimbleTemplate.Addons.Credo do
end

defp inject_mix_dependency(project) do
Generator.inject_mix_dependency(
{:credo, latest_package_version(:credo), only: [:dev, :test], runtime: false}
)
Generator.inject_mix_dependency([
{:credo, latest_package_version(:credo), only: [:dev, :test], runtime: false},
{:compass_credo_plugin, latest_package_version(:compass_credo_plugin),
only: [:dev, :test], runtime: false}
])

project
end
Expand Down
2 changes: 2 additions & 0 deletions lib/nimble_template/addons/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ defmodule NimbleTemplate.Addons.Github do
%{
github_workflows_readme: true,
with_test_workflow?: with_test_workflow?,
with_github_wiki?: with_github_wiki?,
with_deploy_to_heroku_workflow?: with_deploy_to_heroku_workflow?
}
) do
Expand All @@ -65,6 +66,7 @@ defmodule NimbleTemplate.Addons.Github do
{:eex, ".github/workflows/README.md.eex", ".github/workflows/README.md"}
],
with_test_workflow?: with_test_workflow?,
with_github_wiki?: with_github_wiki?,
with_deploy_to_heroku_workflow?: with_deploy_to_heroku_workflow?
)

Expand Down
42 changes: 40 additions & 2 deletions lib/nimble_template/addons/variants/phoenix/health_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ defmodule NimbleTemplate.Addons.Phoenix.HealthPlug do
defp edit_files(project) do
project
|> edit_config()
|> create_router_helper()
|> edit_router()
|> edit_test_helper()
end
Expand Down Expand Up @@ -73,7 +74,44 @@ defmodule NimbleTemplate.Addons.Phoenix.HealthPlug do
project
end

defp edit_router(%Project{web_path: web_path, web_module: web_module, otp_app: otp_app} = project) do
defp create_router_helper(
%Project{
web_module: web_module,
web_path: web_path,
web_test_path: web_test_path,
otp_app: otp_app
} = project
) do
binding = [
web_module: web_module,
otp_app: otp_app
]

files = [
{:eex, "lib/otp_app_web/helpers/router_helper.ex.eex",
"#{web_path}/helpers/router_helper.ex"},
{:eex, "test/otp_app_web/helpers/router_helper_test.exs.eex",
"#{web_test_path}/helpers/router_helper_test.exs"}
]

Generator.copy_file(files, binding)

project
end

defp edit_router(%Project{web_path: web_path, web_module: web_module} = project) do
Generator.replace_content(
"#{web_path}/router.ex",
"""
use #{web_module}, :router
""",
"""
use #{web_module}, :router
alias #{web_module}.RouterHelper
"""
)

Generator.replace_content(
"#{web_path}/router.ex",
"""
Expand All @@ -92,7 +130,7 @@ defmodule NimbleTemplate.Addons.Phoenix.HealthPlug do
# coveralls-ignore-stop
forward Application.compile_env(:#{otp_app}, #{web_module}.Endpoint)[:health_path], #{web_module}.HealthPlug
forward RouterHelper.health_path(), #{web_module}.HealthPlug
"""
)

Expand Down
4 changes: 1 addition & 3 deletions lib/nimble_template/addons/variants/phoenix/oban.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ defmodule NimbleTemplate.Addons.Phoenix.Oban do
"""
# Conditionally disable crontab, queues, or plugins here.
defp oban_config do
Application.get_env(:#{otp_app}, Oban)
end
defp oban_config, do: Application.get_env(:#{otp_app}, Oban)
"""
)

Expand Down
4 changes: 1 addition & 3 deletions lib/nimble_template/addons/variants/phoenix/web/wallaby.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ defmodule NimbleTemplate.Addons.Phoenix.Web.Wallaby do
"""
use Phoenix.Endpoint, otp_app: :#{otp_app}
if Application.compile_env(:#{otp_app}, :sql_sandbox) do
plug Phoenix.Ecto.SQL.Sandbox
end
if Application.compile_env(:#{otp_app}, :sql_sandbox), do: plug Phoenix.Ecto.SQL.Sandbox
"""
)

Expand Down
10 changes: 10 additions & 0 deletions lib/nimble_template/helpers/credo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule NimbleTemplate.CredoHelper do
alias NimbleTemplate.Generator

@spec disable_rule(String.t(), String.t()) :: :ok | {:error, :failed_to_read_file}
def disable_rule(file_path, rule) do
Generator.prepend_content(file_path, """
# credo:disable-for-this-file #{rule}
""")
end
end
45 changes: 40 additions & 5 deletions lib/nimble_template/helpers/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ defmodule NimbleTemplate.Generator do

def rename_file(old_path, new_path), do: File.rename(old_path, new_path)

def replace_content(file_path, anchor, content, raise_exception \\ true) do
def replace_content(file_path, anchor, content, raise_exception \\ true)

def replace_content(_file_path, anchor, content, _raise_exception) when anchor == content,
do: :ok

def replace_content(file_path, anchor, content, raise_exception) do
file = Path.join([file_path])

file_content =
Expand All @@ -35,7 +40,7 @@ defmodule NimbleTemplate.Generator do

case split_with_self(file_content, anchor) do
[left, _middle, right] ->
print_log("* replacing ", Path.relative_to_cwd(file_path))
info_log("* replacing ", Path.relative_to_cwd(file_path))

File.write!(file, [left, content, right])

Expand All @@ -48,6 +53,8 @@ defmodule NimbleTemplate.Generator do
end
end

def replace_content_all(_file_path, anchor, content) when anchor == content, do: :ok

def replace_content_all(file_path, anchor, content) do
case replace_content(file_path, anchor, content, false) do
:ok ->
Expand All @@ -73,7 +80,7 @@ defmodule NimbleTemplate.Generator do

case split_with_self(file_content, anchor) do
[left, middle, right] ->
print_log("* injecting ", Path.relative_to_cwd(file_path))
info_log("* injecting ", Path.relative_to_cwd(file_path))

File.write!(file, [left, middle, content, right])

Expand All @@ -82,6 +89,32 @@ defmodule NimbleTemplate.Generator do
end
end

@spec prepend_content(String.t(), String.t()) :: :ok | {:error, :failed_to_read_file}
def prepend_content(file_path, content) do
case File.read(file_path) do
{:ok, file_content} ->
info_log("* prepending ", Path.relative_to_cwd(file_path))
File.write!(file_path, [content, file_content])

{:error, _} ->
error_log("Can't read #{file_path}")

{:error, :failed_to_read_file}
end
end

@spec prepend_content!(String.t(), String.t()) :: :ok
def prepend_content!(file_path, content) do
case File.read(file_path) do
{:ok, file_content} ->
info_log("* prepending ", Path.relative_to_cwd(file_path))
File.write!(file_path, [content, file_content])

{:error, _} ->
Mix.raise(~s[Can't read #{file_path}])
end
end

def append_content(file_path, content) do
file = Path.join([file_path])

Expand All @@ -91,7 +124,7 @@ defmodule NimbleTemplate.Generator do
{:error, _} -> Mix.raise(~s[Can't read #{file}])
end

print_log("* appending ", Path.relative_to_cwd(file_path))
info_log("* appending ", Path.relative_to_cwd(file_path))
File.write!(file, [file_content, content])
end

Expand Down Expand Up @@ -141,7 +174,9 @@ defmodule NimbleTemplate.Generator do
end
end

def print_log(prefix, content \\ ""), do: Mix.shell().info([:green, prefix, :reset, content])
def info_log(prefix, content \\ ""), do: Mix.shell().info([:green, prefix, :reset, content])

def error_log(content \\ ""), do: Mix.shell().error(content)

defp split_with_self(contents, text) do
case :binary.split(contents, text) do
Expand Down
84 changes: 77 additions & 7 deletions lib/nimble_template/projects/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,54 @@ defmodule NimbleTemplate.Projects.Project do
mix_project?: false,
installed_addons: []

@type t :: %__MODULE__{
base_module: String.t() | nil,
base_path: String.t() | nil,
base_test_path: String.t() | nil,
otp_app: String.t() | nil,
web_module: String.t() | nil,
web_path: String.t() | nil,
web_test_path: String.t() | nil,
alpine_version: String.t(),
elixir_version: String.t(),
erlang_version: String.t(),
node_asdf_version: String.t(),
elixir_asdf_version: String.t() | nil,
api_project?: boolean(),
live_project?: boolean(),
web_project?: boolean(),
mix_project?: boolean(),
installed_addons: list(atom())
}

@spec new(map()) :: __MODULE__.t()
def new(opts \\ %{}) do
%__MODULE__{
base_module: base_module(),
base_path: "lib/" <> Atom.to_string(otp_app()),
base_test_path: "test/" <> Atom.to_string(otp_app()),
otp_app: otp_app(),
web_module: base_module() <> "Web",
web_path: "lib/" <> Atom.to_string(otp_app()) <> "_web",
web_test_path: "test/" <> Atom.to_string(otp_app()) <> "_web",
api_project?: api_project?(opts),
web_project?: web_project?(opts),
live_project?: live_project?(opts),
mix_project?: mix_project?(opts),
elixir_asdf_version: "#{@elixir_version}-otp-#{get_otp_major_version(@erlang_version)}",
installed_addons: []
}
|> init_base_module_fields()
|> maybe_init_web_fields()
end

@spec alpine_version :: String.t()
def alpine_version, do: @alpine_version

@spec elixir_version :: String.t()
def elixir_version, do: @elixir_version

@spec erlang_version :: String.t()
def erlang_version, do: @erlang_version

@spec node_asdf_version :: String.t()
def node_asdf_version, do: @node_asdf_version

@spec get_otp_major_version(String.t()) :: String.t()
def get_otp_major_version(erlang_version) do
erlang_version
|> String.split(".")
Expand All @@ -68,5 +90,53 @@ defmodule NimbleTemplate.Projects.Project do

defp otp_app(), do: Mix.Phoenix.otp_app()

defp base_module(), do: Mix.Phoenix.base()
defp init_base_module_fields(
%{
mix_project?: true
} = project
) do
Map.merge(project, %{base_module: base_module()})
end

defp init_base_module_fields(project) do
base_module_path =
otp_app()
|> Atom.to_string()
|> Macro.underscore()

Map.merge(project, %{
base_module: base_module(),
base_path: "lib/" <> base_module_path,
base_test_path: "test/" <> base_module_path
})
end

defp maybe_init_web_fields(
%{
base_module: base_module,
base_path: "lib/" <> base_path,
api_project?: api_project?,
live_project?: live_project?,
web_project?: web_project?
} = project
)
when true in [api_project?, live_project?, web_project?] do
Map.merge(project, %{
web_module: base_module <> "Web",
web_path: "lib/" <> base_path <> "_web",
web_test_path: "test/" <> base_path <> "_web"
})
end

defp maybe_init_web_fields(project), do: project

defp base_module do
mix_file_content = File.read!("mix.exs")

~r/defmodule (.*) do/
|> Regex.run(mix_file_content)
|> List.last()
|> String.split(".")
|> List.first()
end
end
Loading

0 comments on commit 6742751

Please sign in to comment.