@@ -14,20 +14,40 @@ defmodule System do
1414 with the VM or the host system.
1515 """
1616
17+ defp strip_re ( iodata , pattern ) do
18+ :re . replace ( iodata , pattern , "" , [ return: :binary ] )
19+ end
20+
21+ defp read_stripped ( path ) do
22+ case :file . read_file ( path ) do
23+ { :ok , binary } ->
24+ strip_re ( binary , "^\s +|\s +$" )
25+ _ -> ""
26+ end
27+ end
28+
1729 # Read and strip the version from the `VERSION` file.
1830 defmacrop get_version do
19- Regex . replace % r / ^ \s+ | \s+$/, File.read! ( "VERSION" ) , ""
31+ case read_stripped ( "VERSION" ) do
32+ "" -> raise CompileError , message: "could not read the version number from VERSION"
33+ data -> data
34+ end
2035 end
2136
22- # Tries to run `git describe --always --tags`. In case of success
23- # returns the most recent tag, otherwise returns an empty string.
37+ # Tries to run `git describe --always --tags`. In the case of success returns
38+ # the most recent tag. If that is not available, tries to read the commit hash
39+ # from .git/HEAD. If that fails, returns an empty string.
2440 defmacrop get_describe do
25- dotgit = Path.join(File.cwd!, " . git ")
26- if :os.find_executable('git') && File.exists?(dotgit) do
27- data = :os.cmd('git describe --always --tags')
28- Regex.replace %r/\n /, to_binary(data), " "
29- else
30- " "
41+ dirpath = ".git"
42+ case :file . read_file_info ( dirpath ) do
43+ { :ok , _ } ->
44+ if :os . find_executable ( 'git' ) do
45+ data = :os . cmd ( 'git describe --always --tags' )
46+ strip_re ( data , "\n " )
47+ else
48+ read_stripped ( :filename . join ( ".git" , "HEAD" ) )
49+ end
50+ _ -> ""
3151 end
3252 end
3353
@@ -133,15 +153,22 @@ defmodule System do
133153 end
134154
135155 defp write_env_tmp_dir ( env ) do
136- case System. get_env(env) do
156+ case get_env ( env ) do
137157 nil -> nil
138158 tmp -> write_tmp_dir tmp
139159 end
140160 end
141161
142162 defp write_tmp_dir ( dir ) do
143- case File.stat(dir) do
144- { :ok, File.Stat[type: :directory, access: access] } when access in [:read_write, :write] -> dir
163+ case :file . read_file_info ( dir ) do
164+ { :ok , info } ->
165+ type_index = File.Stat . __index__ :type
166+ access_index = File.Stat . __index__ :access
167+ case { elem ( info , type_index ) , elem ( info , access_index ) } do
168+ { :directory , access } when access in [ :read_write , :write ] ->
169+ dir
170+ _ -> nil
171+ end
145172 { :error , _ } -> nil
146173 end
147174 end
0 commit comments