-
Notifications
You must be signed in to change notification settings - Fork 14
/
compile.bundlex.ex
82 lines (63 loc) · 2.38 KB
/
compile.bundlex.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
defmodule Mix.Tasks.Compile.Bundlex do
@shortdoc "Builds natives specified in bundlex.exs file"
@moduledoc """
#{@shortdoc}
Accepts the following command line arguments:
- `--store-scripts` - if set, shell scripts are stored in the project
root folder for further analysis.
Add `:bundlex` to compilers in your Mix project to have this task executed
each time the project is compiled.
"""
use Mix.Task.Compiler
alias Bundlex.{BuildScript, Native, Output, Platform, Project}
alias Bundlex.Helper.MixHelper
@recursive true
@impl true
def run(_args) do
{:ok, _apps} = Application.ensure_all_started(:bundlex)
commands = []
app = MixHelper.get_app!()
platform = Platform.get_target!()
project =
with {:ok, project} <- Project.get(app) do
project
else
{:error, reason} ->
Output.raise("Cannot get project for app: #{inspect(app)}, reason: #{inspect(reason)}")
end
commands = commands ++ Platform.get_module(platform).toolchain_module().before_all!(platform)
commands =
commands ++
case Native.resolve_natives(project, platform) do
{:ok, nifs_commands} ->
nifs_commands
{:error, {app, reason}} ->
Output.raise(
"Error resolving natives for app #{inspect(app)}, reason: #{inspect(reason)}"
)
end
build_script = BuildScript.new(commands)
{cmdline_options, _argv, _errors} =
OptionParser.parse(System.argv(), switches: [store_scripts: :boolean])
if cmdline_options[:store_scripts] do
{:ok, {filename, _script}} = build_script |> BuildScript.store(platform)
Output.info("Stored build script at #{File.cwd!() |> Path.join(filename)}")
end
case build_script |> BuildScript.run(platform) do
:ok ->
:ok
{:error, {:run_build_script, return_code: ret, command: cmd}} ->
Output.raise("""
Failed to build the native part of package #{app}. Errors may have been logged above.
Make sure that all required packages are properly installed in your system.
Requirements and installation guide may be found in the readme of package #{app}.
Returned code: #{ret}
Build script:
#{cmd}
""")
{:error, reason} ->
Output.raise("Error running build script, reason #{inspect(reason)}")
end
{:ok, []}
end
end