fcb8c64c77cabe85fb6b4faeaae0920067e11fa4
[akkoma] / lib / pleroma / utils.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Utils do
6 def compile_dir(dir) when is_binary(dir) do
7 dir
8 |> File.ls!()
9 |> Enum.map(&Path.join(dir, &1))
10 |> Kernel.ParallelCompiler.compile()
11 end
12
13 @doc """
14 POSIX-compliant check if command is available in the system
15
16 ## Examples
17 iex> command_available?("git")
18 true
19 iex> command_available?("wrongcmd")
20 false
21
22 """
23 @spec command_available?(String.t()) :: boolean()
24 def command_available?(command) do
25 match?({_output, 0}, System.cmd("sh", ["-c", "command -v #{command}"]))
26 end
27
28 @doc "creates the uniq temporary directory"
29 @spec tmp_dir(String.t()) :: {:ok, String.t()} | {:error, :file.posix()}
30 def tmp_dir(prefix \\ "") do
31 sub_dir = [
32 prefix,
33 Timex.to_unix(Timex.now()),
34 :os.getpid(),
35 String.downcase(Integer.to_string(:rand.uniform(0x100000000), 36))
36 ]
37
38 tmp_dir = Path.join(System.tmp_dir!(), Enum.join(sub_dir, "-"))
39
40 case File.mkdir(tmp_dir) do
41 :ok -> {:ok, tmp_dir}
42 error -> error
43 end
44 end
45 end