Add a polyfill for Application.put_all_env/2
This commit is contained in:
parent
1dd809515c
commit
bec058f79a
|
@ -5,6 +5,7 @@
|
|||
defmodule Mix.Tasks.Pleroma.Instance do
|
||||
use Mix.Task
|
||||
import Mix.Pleroma
|
||||
alias Pleroma.Polyfill
|
||||
|
||||
@shortdoc "Manages Pleroma instance"
|
||||
@moduledoc """
|
||||
|
@ -226,15 +227,17 @@ defmodule Mix.Tasks.Pleroma.Instance do
|
|||
shell_info("Writing the postgres script to #{psql_path}.")
|
||||
File.write(psql_path, result_psql)
|
||||
|
||||
Config.Reader.read!(config_path)
|
||||
|> Enum.map(fn {application, config} ->
|
||||
config =
|
||||
Application.get_all_env(application)
|
||||
|> Config.Reader.merge(config)
|
||||
new_config =
|
||||
Config.Reader.read!(config_path)
|
||||
|> Enum.map(fn {application, config} ->
|
||||
config =
|
||||
Application.get_all_env(application)
|
||||
|> Config.Reader.merge(config)
|
||||
|
||||
{application, config}
|
||||
end)
|
||||
|> Application.put_all_env()
|
||||
{application, config}
|
||||
end)
|
||||
|
||||
Polyfill.apply_or_fallback(Application, :put_all_env, [new_config])
|
||||
|
||||
write_robots_txt(indexable, template_dir)
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
defmodule Pleroma.Polyfill do
|
||||
@doc "Provides fallbacks for features not available on Elixir versions supported"
|
||||
|
||||
def apply_or_fallback(module, function_name, args) do
|
||||
if function_exported?(module, function_name, length(args)) do
|
||||
apply(module, function_name, args)
|
||||
else
|
||||
if fallback_available?(module, function_name, length(args)) do
|
||||
apply(Module.concat(__MODULE__, module), function_name, args)
|
||||
else
|
||||
raise "No fallback for #{module}.#{to_string(function_name)} found!"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def fallback_available?(module, function_name, arity) do
|
||||
function_exported?(Module.concat(__MODULE__, module), function_name, arity)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.Polyfill.Application do
|
||||
# Taken from https://github.com/elixir-lang/elixir/blob/b50e0f4ea7d1d6860bc8d9081f5dbe16047375ef/lib/elixir/lib/application.ex
|
||||
# Copyright 2012 Plataformatec
|
||||
def put_all_env(config, opts \\ []) when is_list(config) and is_list(opts) do
|
||||
if function_exported?(:application, :set_env, 2) do
|
||||
# Directly calling it would result in a compilation warning on OTP <22
|
||||
apply(:application, :set_env, [config, opts])
|
||||
else
|
||||
for app_keyword <- config,
|
||||
{app, keyword} = app_keyword,
|
||||
key_value <- keyword,
|
||||
{key, value} = key_value do
|
||||
:application.set_env(app, key, value, opts)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
end
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
defmodule Pleroma.InstanceTest do
|
||||
use ExUnit.Case, async: true
|
||||
alias Pleroma.Polyfill
|
||||
|
||||
setup do
|
||||
File.mkdir_p!(tmp_path())
|
||||
|
@ -16,7 +17,7 @@ defmodule Pleroma.InstanceTest do
|
|||
|
||||
on_exit(fn ->
|
||||
File.rm_rf(tmp_path())
|
||||
Application.put_all_env(config)
|
||||
Polyfill.apply_or_fallback(Application, :put_all_env, [config])
|
||||
end)
|
||||
|
||||
:ok
|
||||
|
|
Loading…
Reference in New Issue