Current section
Files
Jump to
Current section
Files
lib/dockerize_elixir.ex
defmodule DockerizeElixir do
def dockerize(absolute_path) do
app = DockerizeElixir.Mix.Project.app(absolute_path)
template_path =
:code.priv_dir(:dockerize_elixir)
|> Path.join("templates")
email =
case DockerizeElixir.Git.user_email() do
{:ok, email} -> email
:error -> ""
end
transformations = [
{"PROJECT_NAME", to_string(app)},
{"USER_EMAIL", email}
]
DockerizeElixir.File.ls_files_recursive!(template_path)
|> Enum.map(fn source_path ->
file_relative_path = Path.relative_to(source_path, template_path)
destination_path = Path.join(absolute_path, file_relative_path)
transform(source_path, destination_path, transformations)
maybe_set_execute_permission(destination_path)
end)
append_config(absolute_path, app)
end
defp append_config(absolute_path, app) do
base_module_string =
DockerizeElixir.Mix.Project.base_module(absolute_path)
|> DockerizeElixir.Module.to_string_without_elixir_prefix()
repo_module = base_module_string <> ".Repo"
endpoint_module = base_module_string <> "Web.Endpoint"
dev_config_path = Path.join(absolute_path, "config/dev.exs")
append_database_config(dev_config_path, app, repo_module)
append_phoenix_config(dev_config_path, app, endpoint_module)
test_config_path = Path.join(absolute_path, "config/test.exs")
append_database_config(test_config_path, app, repo_module)
end
defp append_database_config(config_path, app, repo_module) do
if File.exists?(config_path) do
DockerizeElixir.File.append(
config_path,
"\nconfig :#{app}, #{repo_module}, hostname: \"postgres\"\n"
)
IO.puts("Adjusting database config in \"#{config_path}\"")
end
end
defp append_phoenix_config(config_path, app, endpoint_module) do
if File.exists?(config_path) do
DockerizeElixir.File.append(
config_path,
"\nconfig :#{app}, #{endpoint_module}, http: [ip: {0, 0, 0, 0}, port: 4000]\n"
)
IO.puts("Adjusting phoenix config in \"#{config_path}\"")
end
end
# mix archive creates a .zip file, which loses executable permissions
# so we have to manually add them back
defp maybe_set_execute_permission(path) do
if should_be_executable?(path) do
case File.chmod(path, 0o775) do
:ok ->
:ok
{:error, reason} ->
IO.puts("Error setting executable permissions on \"#{path}\": #{reason}")
{:error, reason}
end
end
end
defp should_be_executable?(path) do
cond do
Path.extname(path) == ".sh" -> true
Path.basename(Path.dirname(path)) == "bin" -> true
true -> false
end
end
defp transform(source_path, destination_path, transformations) do
relative_destination_path = DockerizeElixir.Path.relative_to(destination_path, File.cwd!())
transform_result =
DockerizeElixir.File.cp_and_transform_lines(source_path, destination_path, fn stream ->
Enum.reduce(transformations, stream, fn {from, to}, stream ->
Stream.map(stream, &String.replace(&1, from, to))
end)
end)
case transform_result do
:ok ->
IO.puts("Wrote \"#{relative_destination_path}\"")
{:error, :file_exists} ->
IO.puts("Skipping \"#{relative_destination_path}\" because it already exists!")
{:error, reason} ->
IO.puts("Error writing \"#{relative_destination_path}\": #{reason}")
end
end
end