Skip to content

Commit d55a170

Browse files
committed
Bring system.ex back to bootstrap. Closes #1155
1 parent 75a1373 commit d55a170

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

lib/elixir/lib/system.ex

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/elixir/src/elixir_compiler.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ core_main() ->
222222
"lib/elixir/lib/binary/chars.ex",
223223
"lib/elixir/lib/io.ex",
224224
"lib/elixir/lib/path.ex",
225+
"lib/elixir/lib/system.ex",
225226
"lib/elixir/lib/kernel/cli.ex",
226227
"lib/elixir/lib/kernel/error_handler.ex",
227228
"lib/elixir/lib/kernel/parallel_compiler.ex",
@@ -246,4 +247,4 @@ format_errors(File, Errors) ->
246247
format_warnings(Bootstrap, File, Warnings) ->
247248
lists:foreach(fun ({_, Each}) ->
248249
lists:foreach(fun (Warning) -> elixir_errors:handle_file_warning(Bootstrap, File, Warning) end, Each)
249-
end, Warnings).
250+
end, Warnings).

0 commit comments

Comments
 (0)