Access project version within elixir application

后端 未结 6 2115
故里飘歌
故里飘歌 2021-02-12 07:52

I have an elixir project with a defined version. How can I access this from within the running application.

in mix.exs

  def project do
    [app: :my_app         


        
6条回答
  •  不知归路
    2021-02-12 08:33

    I found the version inside of :application.which_applications, but it requires some parsing:

    defmodule AppHelper do
      @spec app_version(atom) :: {integer, integer, integer}
      def app_version(target_app) do
        :application.which_applications
        |> Enum.filter(fn({app, _, _}) ->
                        app == target_app
                       end)
        |> get_app_vsn
      end
    
      # I use a sensible fallback when we can't find the app,
      # you could omit the first signature and just crash when the app DNE.
      defp get_app_vsn([]), do: {0,0,0} 
      defp get_app_vsn([{_app, _desc, vsn}]) do
        [maj, min, rev] = vsn
                          |> List.to_string
                          |> String.split(".")
                          |> Enum.map(&String.to_integer/1)
        {maj, min, rev}
      end
    end
    

    And then for usage:

    iex(1)> AppHelper.app_version(:logger)
    {1, 0, 5}
    

    As always, there's probably a better way.

提交回复
热议问题