diff --git a/lib/mix/tasks/docker.ex b/lib/mix/tasks/docker.ex index 524bbad..7194663 100644 --- a/lib/mix/tasks/docker.ex +++ b/lib/mix/tasks/docker.ex @@ -41,3 +41,12 @@ defmodule Mix.Tasks.Docker.Shipit do defdelegate run(args), to: MixDocker, as: :shipit end + +defmodule Mix.Tasks.Docker.Customize do + use Mix.Task + + @shortdoc "Copy & customize Dockerfiles" + @preferred_cli_env :prod + + defdelegate run(args), to: MixDocker, as: :customize +end diff --git a/lib/mix_docker.ex b/lib/mix_docker.ex index 7c80eb5..34ed5d5 100644 --- a/lib/mix_docker.ex +++ b/lib/mix_docker.ex @@ -57,6 +57,11 @@ defmodule MixDocker do publish(args) end + def customize([]) do + try_copy_dockerfile @dockerfile_build + try_copy_dockerfile @dockerfile_release + end + defp git_head_sha do {sha, 0} = System.cmd "git", ["rev-parse", "HEAD"] String.slice(sha, 0, 10) @@ -109,19 +114,12 @@ defmodule MixDocker do system! "docker", ["push", image] end - defp with_dockerfile(name, fun) do if File.exists?(name) do fun.() else - app = Mix.Project.get.project[:app] - try do - content = [@dockerfile_path, name] - |> Path.join - |> File.read! - |> String.replace("${APP}", to_string(app)) - File.write!(name, content) + copy_dockerfile(name) fun.() after File.rm(name) @@ -129,6 +127,23 @@ defmodule MixDocker do end end + defp copy_dockerfile(name) do + app = Mix.Project.get.project[:app] + content = [@dockerfile_path, name] + |> Path.join + |> File.read! + |> String.replace("${APP}", to_string(app)) + File.write!(name, content) + end + + defp try_copy_dockerfile(name) do + if File.exists?(name) do + Logger.warn("#{name} already exists") + else + copy_dockerfile(name) + end + end + defp system(cmd, args) do Logger.debug "$ #{cmd} #{args |> Enum.join(" ")}" System.cmd(cmd, args, into: IO.stream(:stdio, :line)) diff --git a/test-app/.gitignore b/test-app/.gitignore index be9a5a7..942862c 100644 --- a/test-app/.gitignore +++ b/test-app/.gitignore @@ -21,3 +21,5 @@ app.tar.gz /rel .dockerignore mix.lock +Dockerfile.build +Dockerfile.release diff --git a/test-app/mix.exs b/test-app/mix.exs index 87720cc..9c8fc23 100644 --- a/test-app/mix.exs +++ b/test-app/mix.exs @@ -16,8 +16,10 @@ defmodule App.Mixfile do defp deps do [ - # can't use path since it will be unavailable inside docker container - {:mix_docker, github: "recruitee/mix_docker", branch: "master"} + case Mix.env do + :dev -> {:mix_docker, path: ".."} + :prod -> {:mix_docker, github: "recruitee/mix_docker", branch: "master"} + end ] end end diff --git a/test/mix_docker_test.exs b/test/mix_docker_test.exs index 8448a92..8ed274a 100644 --- a/test/mix_docker_test.exs +++ b/test/mix_docker_test.exs @@ -39,4 +39,13 @@ defmodule MixDockerTest do mix "docker.release" end end + + test "customize" do + inapp do + mix "docker.customize" + + assert File.exists?("Dockerfile.build") + assert File.exists?("Dockerfile.release") + end + end end