@@ -16,6 +16,16 @@ defmodule Lexical.VM.Versions do
1616 @ type t :: % { elixir: version_string ( ) , erlang: version_string ( ) }
1717 @ type versioned_t :: % { elixir: Version . t ( ) , erlang: Version . t ( ) }
1818
19+ defmacrop cache_in_persistent_term ( key , do: materializer ) do
20+ quote do
21+ with :not_found <- :persistent_term . get ( unquote ( key ) , :not_found ) do
22+ result = unquote ( materializer )
23+ :persistent_term . put ( unquote ( key ) , result )
24+ result
25+ end
26+ end
27+ end
28+
1929 @ doc """
2030 Returns the versions of elixir and erlang in the currently running VM
2131 """
@@ -27,6 +37,15 @@ defmodule Lexical.VM.Versions do
2737 }
2838 end
2939
40+ @ doc """
41+ Returns true if the current version of elixir matches the requirement
42+ """
43+ def current_elixir_matches? ( requirement ) do
44+ cache_in_persistent_term { :current_elixir_matches? , requirement } do
45+ Version . match? ( elixir_version ( ) , requirement )
46+ end
47+ end
48+
3049 @ doc """
3150 Returns the compiled-in versions of elixir and erlang.
3251
@@ -138,32 +157,26 @@ defmodule Lexical.VM.Versions do
138157 end
139158
140159 defp elixir_version do
141- System . version ( )
160+ cache_in_persistent_term { __MODULE__ , :current_elixir } do
161+ System . version ( )
162+ end
142163 end
143164
144165 defp erlang_version do
145- case :persistent_term . get ( { __MODULE__ , :current_erlang } , :not_found ) do
146- :not_found ->
147- major = :otp_release |> :erlang . system_info ( ) |> List . to_string ( )
148- version_file = Path . join ( [ :code . root_dir ( ) , "releases" , major , "OTP_VERSION" ] )
149-
150- erlang_version =
151- try do
152- { :ok , contents } = read_file ( version_file )
153- String . split ( contents , "\n " , trim: true )
154- else
155- [ full ] -> full
156- _ -> major
157- catch
158- :error ->
159- major
160- end
161-
162- :persistent_term . put ( { __MODULE__ , :current_erlang } , erlang_version )
163- erlang_version ( )
164-
165- erlang_version ->
166- erlang_version
166+ cache_in_persistent_term { __MODULE__ , :current_erlang } do
167+ major = :otp_release |> :erlang . system_info ( ) |> List . to_string ( )
168+ version_file = Path . join ( [ :code . root_dir ( ) , "releases" , major , "OTP_VERSION" ] )
169+
170+ try do
171+ { :ok , contents } = read_file ( version_file )
172+ String . split ( contents , "\n " , trim: true )
173+ else
174+ [ full ] -> full
175+ _ -> major
176+ catch
177+ :error ->
178+ major
179+ end
167180 end
168181 end
169182
0 commit comments