@@ -14,20 +14,40 @@ defmodule System do
14
14
with the VM or the host system.
15
15
"""
16
16
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
+
17
29
# Read and strip the version from the `VERSION` file.
18
30
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
20
35
end
21
36
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.
24
40
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
+ _ -> ""
31
51
end
32
52
end
33
53
@@ -133,15 +153,22 @@ defmodule System do
133
153
end
134
154
135
155
defp write_env_tmp_dir ( env ) do
136
- case System. get_env(env) do
156
+ case get_env ( env ) do
137
157
nil -> nil
138
158
tmp -> write_tmp_dir tmp
139
159
end
140
160
end
141
161
142
162
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
145
172
{ :error , _ } -> nil
146
173
end
147
174
end
0 commit comments