@@ -2,11 +2,13 @@ defmodule ElixirScript.Compiler do
22 @ moduledoc """
33 The entry point for the ElixirScript compilation process.
44 Takes the given module(s) and compiles them and all modules
5- and functions they use into JavaScript
5+ and functions they use into JavaScript.
6+
7+ Will also take a path to Elixir files
68 """
79
810 @ doc """
9- Takes either a module name or a list of module names as
11+ Takes either a module name, list of module names, or a path as
1012 the entry point(s) of an application/library. From there
1113 it will determine which modules and functions are needed
1214 to be compiled.
@@ -22,36 +24,78 @@ defmodule ElixirScript.Compiler do
2224
2325 * `root`: Optional root for imports of FFI JavaScript modules. Defaults to `.`.
2426 """
25- @ spec compile ( atom | [ atom ] , [ ] ) :: nil
26- def compile ( entry_modules , opts \\ [ ] ) do
27- opts = build_compiler_options ( opts , entry_modules )
28- { :ok , pid } = ElixirScript.State . start_link ( )
27+ alias ElixirScript . {
28+ State ,
29+ Translate ,
30+ FindUsedModules ,
31+ FindUsedFunctions ,
32+ Output
33+ }
34+ alias ElixirScript.ModuleSystems.ES
35+ alias Kernel.ParallelCompiler
36+
37+ @ spec compile ( atom | [ atom ] | binary , [ ] ) :: nil
38+ def compile ( path , opts \\ [ ] )
39+
40+ def compile ( path , opts ) when is_binary ( path ) do
41+ opts = build_compiler_options ( opts )
42+ { :ok , pid } = State . start_link ( )
43+
44+ path = if String . ends_with? ( path , [ ".ex" , ".exs" ] ) do
45+ path
46+ else
47+ Path . join ( [ path , "**" , "*.{ex,exs}" ] )
48+ end
49+
50+ files = Path . wildcard ( path )
51+
52+ ParallelCompiler . files ( files , [
53+ each_module: & on_module_compile ( pid , & 1 , & 2 , & 3 )
54+ ] )
55+
56+ entry_modules = pid
57+ |> State . get_in_memory_modules
58+ |> Keyword . keys
59+
60+ do_compile ( entry_modules , pid , opts )
61+ end
62+
63+ def compile ( entry_modules , opts ) do
64+ opts = build_compiler_options ( opts )
65+ { :ok , pid } = State . start_link ( )
2966
3067 entry_modules = List . wrap ( entry_modules )
3168
32- ElixirScript.FindUsedModules . execute ( entry_modules , pid )
69+ do_compile ( entry_modules , pid , opts )
70+ end
3371
34- ElixirScript.FindUsedFunctions . execute ( entry_modules , pid )
72+ defp do_compile ( entry_modules , pid , opts ) do
73+ FindUsedModules . execute ( entry_modules , pid )
3574
36- modules = ElixirScript.State . list_modules ( pid )
37- ElixirScript.Translate . execute ( modules , pid )
75+ FindUsedFunctions . execute ( entry_modules , pid )
3876
39- modules = ElixirScript. State. list_modules ( pid )
40- result = ElixirScript.Output . execute ( modules , pid , opts )
77+ modules = State . list_modules ( pid )
78+ Translate . execute ( modules , pid )
4179
42- ElixirScript.State . stop ( pid )
80+ modules = State . list_modules ( pid )
81+ result = Output . execute ( modules , pid , opts )
82+
83+ State . stop ( pid )
4384
4485 result
4586 end
4687
47- defp build_compiler_options ( opts , entry_modules ) do
88+ defp build_compiler_options ( opts ) do
4889 default_options = Map . new
4990 |> Map . put ( :output , Keyword . get ( opts , :output ) )
5091 |> Map . put ( :format , :es )
51- |> Map . put ( :entry_modules , entry_modules )
5292 |> Map . put ( :root , Keyword . get ( opts , :root , "." ) )
5393
5494 options = default_options
55- Map . put ( options , :module_formatter , ElixirScript.ModuleSystems.ES )
95+ Map . put ( options , :module_formatter , ES )
96+ end
97+
98+ defp on_module_compile ( pid , _file , module , beam ) do
99+ State . put_in_memory_module ( pid , module , beam )
56100 end
57101end
0 commit comments