Loading shared functions in mix.exs for use in a Mix umbrella project

I have an umbrella project with multiple sub-projects. I have a function which I use to determine the version for each of the sub-projects:

def version() do                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
  with {descrip, 0} when length(descrip) > 0 <- System.cmd("git", ~w[describe]) do                                                                                                                                                                                                                                                                                                                                                                                                            
    String.trim(descrip)                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    |> String.split("-")                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    |> Enum.take(2)                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
    |> Enum.join(".")                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
  else                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
    _ -> "0.0.1"                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
  end                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
end                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Currently, I have put this code in its own app, and reference it in all the other mix files using a statement like:

"../docker/lib/mix/*.exs"                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
|> Path.wildcard()                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
|> Enum.map(&Code.require_file/1)                                                                                                                                                                                                                                                                                                                                                                                                                                                             

I’ve been searching documentation, but haven’t come across anything that indicates how to do this sort of thing. It would be nice if there was a place I could put my version.exs file which would be auto-loaded by mix, but I havn’t found anything like that.

Is there a better approach?

1 Like