@@ -42,8 +42,8 @@ defmodule MixDocker do
4242 Mix . shell . info " docker run -it --rm #{ image ( :release ) } foreground"
4343 end
4444
45- def publish ( _args ) do
46- name = image ( : version)
45+ def publish ( args ) do
46+ name = image ( version: image_version ( args ) )
4747
4848 docker :tag , image ( :release ) , name
4949 docker :push , name
@@ -63,13 +63,21 @@ defmodule MixDocker do
6363 end
6464
6565 defp git_head_sha do
66- { sha , 0 } = System . cmd "git" , [ "rev-parse" , "HEAD" ]
67- String . slice ( sha , 0 , 10 )
66+ with true <- File . regular? ( ".git" ) ,
67+ { sha , 0 } <- System . cmd ( "git" , [ "rev-parse" , "HEAD" ] ) do
68+ String . slice ( sha , 0 , 10 )
69+ else
70+ _ -> nil
71+ end
6872 end
6973
7074 defp git_commit_count do
71- { count , 0 } = System . cmd "git" , [ "rev-list" , "--count" , "HEAD" ]
72- String . trim ( count )
75+ with true <- File . regular? ( ".git" ) ,
76+ { count , 0 } <- System . cmd ( "git" , [ "rev-list" , "--count" , "HEAD" ] ) do
77+ String . trim ( count )
78+ else
79+ _ -> nil
80+ end
7381 end
7482
7583 defp image ( tag ) do
@@ -80,15 +88,19 @@ defmodule MixDocker do
8088 Application . get_env ( :mix_docker , :image ) || to_string ( Mix.Project . get . project [ :app ] )
8189 end
8290
83- defp image_tag ( : version) do
91+ defp image_tag ( version: version_template ) do
8492 version = Mix.Project . get . project [ :version ]
8593 count = git_commit_count ( )
8694 sha = git_head_sha ( )
8795
88- " #{ version } . #{ count } - #{ sha } "
96+ Code . eval_string ( version_template , mix_version: version , git_count: count , git_sha: sha ) |> elem ( 0 )
8997 end
9098 defp image_tag ( tag ) , do: tag
9199
100+ defp image_version ( args ) do
101+ version = ( OptionParser . parse ( args ) |> elem ( 0 ) |> Keyword . get ( :version , Application . get_env ( :mix_docker , :version , "\# {mix_version}.\# {git_count}-\# {git_sha}" ) ) )
102+ "\" " <> version <> "\" "
103+ end
92104
93105 defp docker ( :cp , cid , source , dest ) do
94106 system! "docker" , [ "cp" , "#{ cid } :#{ source } " , dest ]
0 commit comments