|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: 2021 The Elixir Team |
| 3 | +# SPDX-FileCopyrightText: 2012 Plataformatec |
| 4 | + |
| 5 | +# Beam files compiled on demand |
| 6 | +path = Path.expand("../tmp/beams", __DIR__) |
| 7 | +File.rm_rf!(path) |
| 8 | +File.mkdir_p!(path) |
| 9 | +Code.prepend_path(path) |
| 10 | + |
| 11 | +defmodule PathHelpers do |
| 12 | + def fixture_path() do |
| 13 | + Path.expand("../test/elixir/fixtures", __DIR__) |
| 14 | + end |
| 15 | + |
| 16 | + def tmp_path() do |
| 17 | + Path.expand("../tmp", __DIR__) |
| 18 | + end |
| 19 | + |
| 20 | + def fixture_path(extra) do |
| 21 | + Path.join(fixture_path(), extra) |
| 22 | + end |
| 23 | + |
| 24 | + def tmp_path(extra) do |
| 25 | + Path.join(tmp_path(), extra) |
| 26 | + end |
| 27 | + |
| 28 | + def elixir(args, executable_extension \\ "") do |
| 29 | + run_cmd(elixir_executable(executable_extension), args) |
| 30 | + end |
| 31 | + |
| 32 | + def elixir_executable(extension \\ "") do |
| 33 | + executable_path("elixir", extension) |
| 34 | + end |
| 35 | + |
| 36 | + def elixirc(args, executable_extension \\ "") do |
| 37 | + run_cmd(elixirc_executable(executable_extension), args) |
| 38 | + end |
| 39 | + |
| 40 | + def elixirc_executable(extension \\ "") do |
| 41 | + executable_path("elixirc", extension) |
| 42 | + end |
| 43 | + |
| 44 | + def iex(args, executable_extension \\ "") do |
| 45 | + run_cmd(iex_executable(executable_extension), args) |
| 46 | + end |
| 47 | + |
| 48 | + def iex_executable(extension \\ "") do |
| 49 | + executable_path("iex", extension) |
| 50 | + end |
| 51 | + |
| 52 | + def write_beam({:module, name, bin, _} = res) do |
| 53 | + File.mkdir_p!(unquote(path)) |
| 54 | + beam_path = Path.join(unquote(path), Atom.to_string(name) <> ".beam") |
| 55 | + File.write!(beam_path, bin) |
| 56 | + res |
| 57 | + end |
| 58 | + |
| 59 | + defp run_cmd(executable, args) do |
| 60 | + ~c"#{executable} #{IO.chardata_to_string(args)}#{redirect_std_err_on_win()}" |
| 61 | + |> :os.cmd() |
| 62 | + |> :unicode.characters_to_binary() |
| 63 | + end |
| 64 | + |
| 65 | + defp executable_path(name, extension) do |
| 66 | + Path.expand("../../../bin/#{name}#{extension}", __DIR__) |
| 67 | + end |
| 68 | + |
| 69 | + if match?({:win32, _}, :os.type()) do |
| 70 | + def windows?, do: true |
| 71 | + def executable_extension, do: ".bat" |
| 72 | + def redirect_std_err_on_win, do: " 2>&1" |
| 73 | + else |
| 74 | + def windows?, do: false |
| 75 | + def executable_extension, do: "" |
| 76 | + def redirect_std_err_on_win, do: "" |
| 77 | + end |
| 78 | +end |
0 commit comments