39befc81d204985bf7f83693d48c87723e76f3f7
[anni] / mix.exs
1 defmodule Pleroma.Mixfile do
2   use Mix.Project
3
4   def project do
5     [
6       app: :pleroma,
7       version: version("2.5.5"),
8       elixir: "~> 1.11",
9       elixirc_paths: elixirc_paths(Mix.env()),
10       compilers: [:phoenix, :gettext] ++ Mix.compilers(),
11       elixirc_options: [warnings_as_errors: warnings_as_errors()],
12       xref: [exclude: [:eldap]],
13       start_permanent: Mix.env() == :prod,
14       aliases: aliases(),
15       deps: deps(),
16       test_coverage: [tool: :covertool, summary: true],
17       # Docs
18       name: "Pleroma",
19       homepage_url: "https://pleroma.social/",
20       source_url: "https://git.pleroma.social/pleroma/pleroma",
21       docs: [
22         source_url_pattern:
23           "https://git.pleroma.social/pleroma/pleroma/blob/develop/%{path}#L%{line}",
24         logo: "priv/static/images/logo.png",
25         extras: ["README.md", "CHANGELOG.md"] ++ Path.wildcard("docs/**/*.md"),
26         groups_for_extras: [
27           "Installation manuals": Path.wildcard("docs/installation/*.md"),
28           Configuration: Path.wildcard("docs/config/*.md"),
29           Administration: Path.wildcard("docs/admin/*.md"),
30           "Pleroma's APIs and Mastodon API extensions": Path.wildcard("docs/api/*.md")
31         ],
32         main: "readme",
33         output: "priv/static/doc"
34       ],
35       releases: [
36         pleroma: [
37           include_executables_for: [:unix],
38           applications: [ex_syslogger: :load, syslog: :load, eldap: :transient],
39           steps: [:assemble, &put_otp_version/1, &copy_files/1, &copy_nginx_config/1],
40           config_providers: [{Pleroma.Config.ReleaseRuntimeProvider, []}]
41         ]
42       ]
43     ]
44   end
45
46   def put_otp_version(%{path: target_path} = release) do
47     File.write!(
48       Path.join([target_path, "OTP_VERSION"]),
49       Pleroma.OTPVersion.version()
50     )
51
52     release
53   end
54
55   def copy_files(%{path: target_path} = release) do
56     File.cp_r!("./rel/files", target_path)
57     release
58   end
59
60   def copy_nginx_config(%{path: target_path} = release) do
61     File.cp!(
62       "./installation/pleroma.nginx",
63       Path.join([target_path, "installation", "pleroma.nginx"])
64     )
65
66     release
67   end
68
69   # Configuration for the OTP application.
70   #
71   # Type `mix help compile.app` for more information.
72   def application do
73     [
74       mod: {Pleroma.Application, []},
75       extra_applications: [
76         :logger,
77         :runtime_tools,
78         :comeonin,
79         :fast_sanitize,
80         :os_mon,
81         :ssl,
82         :esshd
83       ],
84       included_applications: [:ex_syslogger]
85     ]
86   end
87
88   # Specifies which paths to compile per environment.
89   defp elixirc_paths(:benchmark), do: ["lib", "benchmarks", "priv/scrubbers"]
90   defp elixirc_paths(:test), do: ["lib", "test/support"]
91   defp elixirc_paths(_), do: ["lib"]
92
93   defp warnings_as_errors, do: System.get_env("CI") == "true"
94
95   # Specifies OAuth dependencies.
96   defp oauth_deps do
97     oauth_strategy_packages =
98       System.get_env("OAUTH_CONSUMER_STRATEGIES")
99       |> to_string()
100       |> String.split()
101       |> Enum.map(fn strategy_entry ->
102         with [_strategy, dependency] <- String.split(strategy_entry, ":") do
103           dependency
104         else
105           [strategy] -> "ueberauth_#{strategy}"
106         end
107       end)
108
109     for s <- oauth_strategy_packages, do: {String.to_atom(s), ">= 0.0.0"}
110   end
111
112   # Specifies your project dependencies.
113   #
114   # Type `mix help deps` for examples and options.
115   defp deps do
116     [
117       {:phoenix, "~> 1.6.0"},
118       {:phoenix_ecto, "~> 4.4.0"},
119       {:ecto_enum, "~> 1.4"},
120       {:postgrex, ">= 0.0.0"},
121       {:phoenix_html, "~> 3.1"},
122       {:phoenix_live_reload, "~> 1.3.3", only: :dev},
123       {:phoenix_live_view, "~> 0.17.1"},
124       {:phoenix_live_dashboard, "~> 0.6.2"},
125       {:telemetry_metrics, "~> 0.6.1"},
126       {:telemetry_poller, "~> 1.0"},
127       {:tzdata, "~> 1.0.3"},
128       {:plug_cowboy, "~> 2.3"},
129       {:oban, "~> 2.13"},
130       {:gettext,
131        git: "https://github.com/tusooa/gettext.git",
132        ref: "72fb2496b6c5280ed911bdc3756890e7f38a4808",
133        override: true},
134       {:bcrypt_elixir, "~> 2.2"},
135       {:trailing_format_plug, "~> 0.0.7"},
136       {:fast_sanitize, "~> 0.2.0"},
137       {:html_entities, "~> 0.5", override: true},
138       {:calendar, "~> 1.0"},
139       {:cachex, "~> 3.2"},
140       {:poison, "~> 3.0", override: true},
141       {:tesla, "~> 1.4.0", override: true},
142       {:castore, "~> 0.1"},
143       {:cowlib, "~> 2.9", override: true},
144       {:gun, "~> 2.0.0-rc.1", override: true},
145       {:finch, "~> 0.10.0"},
146       {:jason, "~> 1.2"},
147       {:mogrify, "~> 0.9.1"},
148       {:ex_aws, "~> 2.1.6"},
149       {:ex_aws_s3, "~> 2.0"},
150       {:sweet_xml, "~> 0.7.2"},
151       {:earmark, "~> 1.4.22"},
152       {:bbcode_pleroma, "~> 0.2.0"},
153       {:cors_plug, "~> 2.0"},
154       {:web_push_encryption, "~> 0.3.1"},
155       {:swoosh, "~> 1.0"},
156       {:phoenix_swoosh, "~> 1.1"},
157       {:gen_smtp, "~> 0.13"},
158       {:ex_syslogger, "~> 1.4"},
159       {:floki, "~> 0.27"},
160       {:timex, "~> 3.6"},
161       {:ueberauth, "~> 0.4"},
162        {:linkify,
163        git: "https://gitgud.io/mintplg/linkify.git",
164        branch: "domain-mention-parsing-fix"},
165       {:http_signatures, "~> 0.1.1"},
166       {:telemetry, "~> 1.0.0", override: true},
167       {:poolboy, "~> 1.5"},
168       {:prometheus, "~> 4.6"},
169       {:prometheus_ex,
170        git: "https://github.com/lanodan/prometheus.ex.git",
171        branch: "fix/elixir-1.14",
172        override: true},
173       {:prometheus_plugs, "~> 1.1"},
174       {:prometheus_phoenix, "~> 1.3"},
175       # Note: once `prometheus_phx` is integrated into `prometheus_phoenix`, remove the former:
176       {:prometheus_phx,
177        git: "https://git.pleroma.social/pleroma/elixir-libraries/prometheus-phx.git",
178        branch: "no-logging"},
179       {:prometheus_ecto, "~> 1.4"},
180       {:recon, "~> 2.5"},
181       {:joken, "~> 2.0"},
182       {:benchee, "~> 1.0"},
183       {:pot, "~> 1.0"},
184       {:esshd, "~> 0.1.0", runtime: Application.get_env(:esshd, :enabled, false)},
185       {:ex_const, "~> 0.2"},
186       {:plug_static_index_html, "~> 1.0.0"},
187       {:flake_id, "~> 0.1.0"},
188       {:concurrent_limiter, "~> 0.1.1"},
189       {:remote_ip,
190        git: "https://git.pleroma.social/pleroma/remote_ip.git",
191        ref: "b647d0deecaa3acb140854fe4bda5b7e1dc6d1c8"},
192       {:captcha,
193        git: "https://git.pleroma.social/pleroma/elixir-libraries/elixir-captcha.git",
194        ref: "e0f16822d578866e186a0974d65ad58cddc1e2ab"},
195       {:restarter, path: "./restarter"},
196       {:majic, "~> 1.0"},
197       {:eblurhash, "~> 1.2.2"},
198       {:open_api_spex, "~> 3.10"},
199       {:ecto_psql_extras, "~> 0.6"},
200
201       # indirect dependency version override
202       {:plug, "~> 1.10.4", override: true},
203
204       ## dev & test
205       {:ex_doc, "~> 0.22", only: :dev, runtime: false},
206       {:ex_machina, "~> 2.4", only: :test},
207       {:credo, "~> 1.6", only: [:dev, :test], runtime: false},
208       {:mock, "~> 0.3.5", only: :test},
209       {:covertool, "~> 2.0", only: :test},
210       {:hackney, "~> 1.18.0", override: true},
211       {:mox, "~> 1.0", only: :test},
212       {:websockex, "~> 0.4.3", only: :test}
213     ] ++ oauth_deps()
214   end
215
216   # Aliases are shortcuts or tasks specific to the current project.
217   # For example, to create, migrate and run the seeds file at once:
218   #
219   #     $ mix ecto.setup
220   #
221   # See the documentation for `Mix` for more info on aliases.
222   defp aliases do
223     [
224       "ecto.migrate": ["pleroma.ecto.migrate"],
225       "ecto.rollback": ["pleroma.ecto.rollback"],
226       "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
227       "ecto.reset": ["ecto.drop", "ecto.setup"],
228       test: ["ecto.create --quiet", "ecto.migrate", "test"],
229       docs: ["pleroma.docs", "docs"],
230       analyze: ["credo --strict --only=warnings,todo,fixme,consistency,readability"],
231       copyright: &add_copyright/1,
232       "copyright.bump": &bump_copyright/1
233     ]
234   end
235
236   # Builds a version string made of:
237   # * the application version
238   # * a pre-release if ahead of the tag: the describe string (-count-commithash)
239   # * branch name
240   # * build metadata:
241   #   * a build name if `PLEROMA_BUILD_NAME` or `:pleroma, :build_name` is defined
242   #   * the mix environment if different than prod
243   defp version(version) do
244     identifier_filter = ~r/[^0-9a-z\-]+/i
245
246     git_available? = match?({_output, 0}, System.cmd("sh", ["-c", "command -v git"]))
247     dotgit_present? = File.exists?(".git")
248
249     git_pre_release =
250       if git_available? and dotgit_present? do
251         {tag, tag_err} =
252           System.cmd("git", ["describe", "--tags", "--abbrev=0"], stderr_to_stdout: true)
253
254         {describe, describe_err} = System.cmd("git", ["describe", "--tags", "--abbrev=8"])
255         {commit_hash, commit_hash_err} = System.cmd("git", ["rev-parse", "--short", "HEAD"])
256
257         # Pre-release version, denoted from patch version with a hyphen
258         cond do
259           tag_err == 0 and describe_err == 0 ->
260             describe
261             |> String.trim()
262             |> String.replace(String.trim(tag), "")
263             |> String.trim_leading("-")
264             |> String.trim()
265
266           commit_hash_err == 0 ->
267             "0-g" <> String.trim(commit_hash)
268
269           true ->
270             nil
271         end
272       end
273
274     # Branch name as pre-release version component, denoted with a dot
275     branch_name =
276       with true <- git_available?,
277            true <- dotgit_present?,
278            {branch_name, 0} <- System.cmd("git", ["rev-parse", "--abbrev-ref", "HEAD"]),
279            branch_name <- String.trim(branch_name),
280            branch_name <- System.get_env("PLEROMA_BUILD_BRANCH") || branch_name,
281            true <-
282              !Enum.any?(["master", "HEAD", "release/", "stable"], fn name ->
283                String.starts_with?(name, branch_name)
284              end) do
285         branch_name =
286           branch_name
287           |> String.trim()
288           |> String.replace(identifier_filter, "-")
289
290         branch_name
291       else
292         _ -> ""
293       end
294
295     build_name =
296       cond do
297         name = Application.get_env(:pleroma, :build_name) -> name
298         name = System.get_env("PLEROMA_BUILD_NAME") -> name
299         true -> nil
300       end
301
302     env_name = if Mix.env() != :prod, do: to_string(Mix.env())
303     env_override = System.get_env("PLEROMA_BUILD_ENV")
304
305     env_name =
306       case env_override do
307         nil -> env_name
308         env_override when env_override in ["", "prod"] -> nil
309         env_override -> env_override
310       end
311
312     # Pre-release version, denoted by appending a hyphen
313     # and a series of dot separated identifiers
314     pre_release =
315       [git_pre_release, branch_name]
316       |> Enum.filter(fn string -> string && string != "" end)
317       |> Enum.join(".")
318       |> (fn
319             "" -> nil
320             string -> "-" <> String.replace(string, identifier_filter, "-")
321           end).()
322
323     # Build metadata, denoted with a plus sign
324     build_metadata =
325       [build_name, env_name]
326       |> Enum.filter(fn string -> string && string != "" end)
327       |> Enum.join(".")
328       |> (fn
329             "" -> nil
330             string -> "+" <> String.replace(string, identifier_filter, "-")
331           end).()
332
333     [version, pre_release, build_metadata]
334     |> Enum.filter(fn string -> string && string != "" end)
335     |> Enum.join()
336   end
337
338   defp add_copyright(_) do
339     year = NaiveDateTime.utc_now().year
340     template = ~s[\
341 # Pleroma: A lightweight social networking server
342 # Copyright © 2017-#{year} Pleroma Authors <https://pleroma.social/>
343 # SPDX-License-Identifier: AGPL-3.0-only
344
345 ] |> String.replace("\n", "\\n")
346
347     find = "find lib test priv -type f \\( -name '*.ex' -or -name '*.exs' \\) -exec "
348     grep = "grep -L '# Copyright © [0-9\-]* Pleroma' {} \\;"
349     xargs = "xargs -n1 sed -i'' '1s;^;#{template};'"
350
351     :os.cmd(String.to_charlist("#{find}#{grep} | #{xargs}"))
352   end
353
354   defp bump_copyright(_) do
355     year = NaiveDateTime.utc_now().year
356     find = "find lib test priv -type f \\( -name '*.ex' -or -name '*.exs' \\)"
357
358     xargs =
359       "xargs sed -i'' 's;# Copyright © [0-9\-]* Pleroma.*$;# Copyright © 2017-#{year} Pleroma Authors <https://pleroma.social/>;'"
360
361     :os.cmd(String.to_charlist("#{find} | #{xargs}"))
362   end
363 end