-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcompiler.ex
More file actions
101 lines (76 loc) · 2.6 KB
/
compiler.ex
File metadata and controls
101 lines (76 loc) · 2.6 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
defmodule ElixirScript.Compiler do
@moduledoc """
The entry point for the ElixirScript compilation process.
Takes the given module(s) and compiles them and all modules
and functions they use into JavaScript.
Will also take a path to Elixir files
"""
@doc """
Takes either a module name, list of module names, or a path as
the entry point(s) of an application/library. From there
it will determine which modules and functions are needed
to be compiled.
Available options are:
* `output`: The path of the generated JavaScript file.
If output is `nil`, then generated code is sent to standard out
If output is a path, the generated code placed in that path.
If path ends in `.js` then that will be the name of the file.
If a directory is given, file will be named `elixirscript.build.js`
* `root`: Optional root for imports of FFI JavaScript modules. Defaults to `.`.
"""
alias ElixirScript.{
State,
Translate,
FindUsedModules,
FindUsedFunctions,
Output
}
alias ElixirScript.ModuleSystems.ES
alias Kernel.ParallelCompiler
@spec compile(atom | [atom] | binary, []) :: nil
def compile(path, opts \\ [])
def compile(path, opts) when is_binary(path) do
opts = build_compiler_options(opts)
{:ok, pid} = State.start_link()
path = if String.ends_with?(path, [".ex", ".exs"]) do
path
else
Path.join([path, "**", "*.{ex,exs}"])
end
files = Path.wildcard(path)
ParallelCompiler.files(files, [
each_module: &on_module_compile(pid, &1, &2, &3)
])
entry_modules = pid
|> State.get_in_memory_modules
|> Keyword.keys
do_compile(entry_modules, pid, opts)
end
def compile(entry_modules, opts) do
opts = build_compiler_options(opts)
{:ok, pid} = State.start_link()
entry_modules = List.wrap(entry_modules)
do_compile(entry_modules, pid, opts)
end
defp do_compile(entry_modules, pid, opts) do
FindUsedModules.execute(entry_modules, pid)
FindUsedFunctions.execute(entry_modules, pid)
modules = State.list_modules(pid)
Translate.execute(modules, pid)
modules = State.list_modules(pid)
result = Output.execute(modules, pid, opts)
State.stop(pid)
result
end
defp build_compiler_options(opts) do
default_options = Map.new
|> Map.put(:output, Keyword.get(opts, :output))
|> Map.put(:format, :es)
|> Map.put(:root, Keyword.get(opts, :root, "."))
options = default_options
Map.put(options, :module_formatter, ES)
end
defp on_module_compile(pid, _file, module, beam) do
State.put_in_memory_module(pid, module, beam)
end
end