This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POD13-65 polyn.gen.release task (#30)
* gen release task and test * add polyn.gen.release task and don't assume which otp_app * inject polyn migrate functions into existing release file * update changelog * gross fix for flaky test
- Loading branch information
1 parent
91b03dd
commit 2116bc2
Showing
9 changed files
with
204 additions
and
55 deletions.
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
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
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,98 @@ | ||
defmodule Mix.Tasks.Polyn.Gen.Release do | ||
@moduledoc """ | ||
Use `mix polyn.gen.release` to generate a new polyn release module for your application | ||
""" | ||
@shortdoc "Generates a new polyn release file" | ||
|
||
use Mix.Task | ||
require Mix.Generator | ||
|
||
def run(args) do | ||
{options, []} = OptionParser.parse!(args, strict: [dir: :string]) | ||
path = Path.join([dir(options), Atom.to_string(app_name()), "release.ex"]) | ||
assigns = [mod: module_name(), app: app_name()] | ||
|
||
if File.exists?(path) do | ||
check_existing(path) | ||
else | ||
Mix.Generator.create_file(path, release_file_template(assigns)) | ||
end | ||
|
||
inject_into_existing(path) | ||
end | ||
|
||
defp dir(options) do | ||
Keyword.get(options, :dir, Path.join(File.cwd!(), "lib")) | ||
end | ||
|
||
defp app_name do | ||
Mix.Project.config() |> Keyword.fetch!(:app) | ||
end | ||
|
||
defp module_name do | ||
prefix = app_name() |> Atom.to_string() |> Macro.camelize() | ||
Module.concat([prefix, Release]) | ||
end | ||
|
||
defp check_existing(path) do | ||
unless prompt_allow_injection(path) do | ||
System.halt() | ||
end | ||
end | ||
|
||
defp prompt_allow_injection(path) do | ||
Mix.shell().yes?( | ||
"#{path} already exists. Would you like to inject Polyn release functions into it?" | ||
) | ||
end | ||
|
||
defp inject_into_existing(path) do | ||
file = File.read!(path) | ||
|
||
lines = | ||
String.trim_trailing(file) | ||
|> String.trim_trailing("end") | ||
|> String.split(["\n", "\r\n"]) | ||
|
||
lines = List.insert_at(lines, first_private_func_index(lines), polyn_migrate_text()) | ||
|
||
injected = Enum.join(lines, "\n") <> "end\n" | ||
File.write!(path, injected) | ||
end | ||
|
||
defp first_private_func_index(lines) do | ||
result = | ||
Enum.find_index(lines, fn line -> | ||
String.contains?(line, "defp ") | ||
end) | ||
|
||
# Use the very end of the file if there are no private functions | ||
case result do | ||
nil -> -1 | ||
index -> index | ||
end | ||
end | ||
|
||
Mix.Generator.embed_template(:release_file, """ | ||
defmodule <%= inspect @mod %> do | ||
@app <%= inspect @app %> | ||
defp load_app do | ||
Application.load(@app) | ||
end | ||
end | ||
""") | ||
|
||
Mix.Generator.embed_text( | ||
:polyn_migrate, | ||
""" | ||
def polyn_migrate do | ||
load_app() | ||
{:ok, _apps} = Application.ensure_all_started(:polyn) | ||
dir = Path.join([:code.priv_dir(@app), "polyn", "migrations"]) | ||
Polyn.Migration.Migrator.run(migrations_dir: dir) | ||
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
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
polyn_elixir_client/test/mix/tasks/polyn.gen.release_test.exs
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,76 @@ | ||
defmodule Mix.Tasks.Polyn.Gen.ReleaseTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Mix.Tasks.Polyn.Gen | ||
|
||
@moduletag :tmp_dir | ||
|
||
# send output to test process rather than stdio | ||
Mix.shell(Mix.Shell.Process) | ||
|
||
test "makes release file", %{tmp_dir: tmp_dir} do | ||
Gen.Release.run(["--dir", tmp_dir]) | ||
|
||
path = Path.join([tmp_dir, "polyn", "release.ex"]) | ||
|
||
file = File.read!(path) | ||
|
||
assert [{Polyn.Release, _binary}] = Code.compile_string(file) | ||
|
||
assert file == | ||
""" | ||
defmodule Polyn.Release do | ||
@app :polyn | ||
def polyn_migrate do | ||
load_app() | ||
{:ok, _apps} = Application.ensure_all_started(:polyn) | ||
dir = Path.join([:code.priv_dir(@app), "polyn", "migrations"]) | ||
Polyn.Migration.Migrator.run(migrations_dir: dir) | ||
end | ||
defp load_app do | ||
Application.load(@app) | ||
end | ||
end | ||
""" | ||
end | ||
|
||
test "injects polyn_migrate function if release_file exists already", %{tmp_dir: tmp_dir} do | ||
File.mkdir!(Path.join(tmp_dir, "polyn")) | ||
|
||
path = Path.join([tmp_dir, "polyn", "release.ex"]) | ||
|
||
File.write!(path, """ | ||
defmodule Polyn.Release do | ||
def do_other_stuff do | ||
end | ||
end | ||
""") | ||
|
||
send(self(), {:mix_shell_input, :yes?, true}) | ||
Gen.Release.run(["--dir", tmp_dir]) | ||
|
||
prompt = "#{path} already exists. Would you like to inject Polyn release functions into it?" | ||
assert_received {:mix_shell, :yes?, [^prompt]} | ||
|
||
file = File.read!(path) | ||
|
||
assert file == | ||
""" | ||
defmodule Polyn.Release do | ||
def do_other_stuff do | ||
end | ||
def polyn_migrate do | ||
load_app() | ||
{:ok, _apps} = Application.ensure_all_started(:polyn) | ||
dir = Path.join([:code.priv_dir(@app), "polyn", "migrations"]) | ||
Polyn.Migration.Migrator.run(migrations_dir: dir) | ||
end | ||
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