Skip to content

Commit 7150eee

Browse files
committed
Update elixirscript cli and mix task to use new compiler
1 parent 037b29b commit 7150eee

File tree

8 files changed

+54
-115
lines changed

8 files changed

+54
-115
lines changed

lib/elixir_script/cli.ex

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ defmodule ElixirScript.CLI do
44
@app_version Mix.Project.config()[:version]
55

66
@switches [
7-
output: :string, elixir: :boolean,
8-
help: :boolean, full_build: :boolean, version: :boolean,
9-
watch: :boolean, format: :string, js_module: [:string, :keep], remove_unused: :boolean
7+
output: :string,
8+
help: :boolean,
9+
version: :boolean,
10+
watch: :boolean,
11+
format: :string,
12+
js_module: [:string, :keep]
1013
]
1114

1215
@aliases [
13-
o: :output, e: :elixir, h: :help, v: :version, f: :format
16+
o: :output,
17+
h: :help,
18+
v: :version,
19+
f: :format
1420
]
1521

1622
def main(argv) do
@@ -39,19 +45,15 @@ defmodule ElixirScript.CLI do
3945

4046
def help_message() do
4147
"""
42-
usage: elixirscript <input> [options]
43-
<input> path to elixir files or
44-
the elixir code string if passed the -e flag
48+
usage: elixirscript <module> [options]
49+
<module> the entry module of your application
4550
4651
options:
4752
--js-module [<identifer>:<path>] A js module used in your code. ex: React:react
4853
Multiple can be defined
4954
-f --format [format] module format of output. options: es (default), common, umd
5055
-o --output [path] places output at the given path.
5156
Can be a directory or filename.
52-
-e --elixir read input as elixir code string
53-
--remove-unused Removes unused modules from output
54-
--full-build informs the compiler to do a full build instead of an incremental one
5557
-v --version the current version number
5658
-h --help this message
5759
"""
@@ -79,27 +81,18 @@ defmodule ElixirScript.CLI do
7981
js_modules = Keyword.get_values(options, :js_module)
8082
|> build_js_modules
8183

82-
compile_opts = %{
83-
include_path: true,
84-
core_path: Keyword.get(options, :core_path, "Elixir.Bootstrap"),
85-
full_build: Keyword.get(options, :full_build, false),
84+
compile_opts = [
8685
output: Keyword.get(options, :output, :stdout),
8786
format: String.to_atom(Keyword.get(options, :format, "es")),
8887
js_modules: js_modules,
89-
remove_unused: Keyword.get(options, :remove_unused, false)
90-
}
88+
]
9189

92-
case options[:elixir] do
93-
true ->
94-
ElixirScript.compile(input, compile_opts)
95-
_ ->
96-
input = handle_input(input)
97-
ElixirScript.compile_path(input, compile_opts)
98-
99-
if watch do
100-
ElixirScript.Watcher.start_link(input, compile_opts)
101-
:timer.sleep :infinity
102-
end
90+
input = handle_input(input)
91+
ElixirScript.Compiler.compile(input, compile_opts)
92+
93+
if watch do
94+
ElixirScript.Watcher.start_link(input, compile_opts)
95+
:timer.sleep :infinity
10396
end
10497
end
10598

@@ -117,6 +110,7 @@ defmodule ElixirScript.CLI do
117110
input = input
118111
|> Enum.map(fn(x) -> String.split(x, [" ", ","], trim: true) end)
119112
|> List.flatten
113+
|> Enum.map(fn(x) -> Module.concat([x]) end)
120114
end
121115

122116
defp build_js_modules(values) do

lib/elixir_script/watcher.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule ElixirScript.Watcher do
2424
try do
2525
if input_changed?(to_string(path), state) do
2626
Logger.debug "Event: #{inspect event} Path: #{path}"
27-
ElixirScript.compile_path(state[:input], state[:options])
27+
ElixirScript.Compiler.compile(state[:input], state[:options])
2828
end
2929
rescue
3030
x ->

lib/mix/tasks/compile.elixir_script.ex

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ defmodule Mix.Tasks.Compile.ElixirScript do
1414
version: "0.1.0",
1515
elixir: "~> 1.0",
1616
deps: deps,
17-
elixir_script: [ input: "src/exjs", output: "dest/js"],
18-
compilers: [:elixir_script] ++ Mix.compilers
17+
elixir_script: [ entry: Example, output: "dest/js"],
18+
compilers: Mix.compilers ++ [:elixir_script]
1919
]
2020
end
2121
2222
Available options are:
23-
* `input`: The folder to look for Elixirscript files in. (defaults to `lib/elixirscript`)
23+
* `input`: The module or modules that are the entry to your application (required)
2424
* `output`: The path of the generated JavaScript file. (defaults to `priv/elixirscript`)
2525
2626
If path ends in `.js` then that will be the name of the file. If a directory is given,
@@ -47,50 +47,30 @@ defmodule Mix.Tasks.Compile.ElixirScript do
4747
end
4848

4949
defp do_compile(elixirscript_base, app) do
50-
elixirscript_config = get_elixirscript_config()
51-
File.mkdir_p!(elixirscript_base)
52-
elixirscript_path = Path.join([elixirscript_base, "#{app}"])
53-
54-
input_path = elixirscript_config
55-
|> Keyword.get(:input)
56-
|> List.wrap
57-
|> Enum.map(fn(path) ->
58-
Path.absname(path)
59-
end)
60-
|> Enum.join("\n")
61-
62-
File.write!(elixirscript_path, input_path)
63-
64-
paths = [elixirscript_base, "*"]
65-
|> Path.join()
66-
|> Path.wildcard
67-
|> Enum.map(fn(path) ->
68-
app = Path.basename(path)
69-
paths = path |> File.read!() |> String.split("\n")
70-
{app, paths}
71-
end)
72-
|> Map.new
73-
74-
output_path = Keyword.get(elixirscript_config, :output)
75-
format = Keyword.get(elixirscript_config, :format)
76-
js_modules = Keyword.get(elixirscript_config, :js_modules, [])
77-
78-
ElixirScript.compile_path(paths, %{output: output_path, format: format, js_modules: js_modules})
50+
51+
{input, opts} = get_compiler_params()
52+
ElixirScript.compiler.compile(input, opts)
7953
end
8054

8155
def clean do
82-
elixirscript_config = get_elixirscript_config()
83-
output_path = Keyword.get(elixirscript_config, :output)
84-
85-
path = ElixirScript.Passes.HandleOutput.get_js_path(output_path)
86-
87-
if File.exists?(path) do
88-
File.rm!(path)
89-
end
90-
56+
#TODO: Figure out how to clean
9157
:ok
9258
end
9359

60+
@doc false
61+
def get_compiler_params() do
62+
elixirscript_config = get_elixirscript_config()
63+
input = Keyword.fetch!(elixirscript_config, :input)
64+
opts = [
65+
output: Keyword.get(elixirscript_config, :output),
66+
format: Keyword.get(elixirscript_config, :format),
67+
js_modules: Keyword.get(elixirscript_config, :js_modules, [])
68+
]
69+
70+
{input, opts}
71+
end
72+
73+
9474
defp get_elixirscript_config() do
9575
config = Mix.Project.config
9676
exjs_config = cond do
@@ -107,7 +87,6 @@ defmodule Mix.Tasks.Compile.ElixirScript do
10787

10888
defp defaults() do
10989
[
110-
input: "lib/elixirscript",
11190
output: "priv/elixirscript",
11291
format: :es
11392
]

lib/mix/tasks/elixirscript.watch.ex

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,23 @@ defmodule Mix.Tasks.Elixirscript.Watch do
1414
version: "0.1.0",
1515
elixir: "~> 1.0",
1616
deps: deps,
17-
elixir_script: [ input: "src/exjs", output: "dest/js"],
18-
compilers: [:elixir_script] ++ Mix.compilers
17+
elixir_script: [ input: Example, output: "dest/js"],
18+
compilers: Mix.compilers ++ [:elixir_script]
1919
]
2020
end
2121
"""
2222

23-
24-
2523
def run(_) do
2624
Mix.Task.run "app.start"
2725

28-
elixirscript_config = get_elixirscript_config()
29-
input_path = Keyword.get(elixirscript_config, :input)
30-
output_path = Keyword.get(elixirscript_config, :output)
31-
format = Keyword.get(elixirscript_config, :format)
32-
js_modules = Keyword.get(elixirscript_config, :js_modules, [])
26+
{input, opts} = Mix.Tasks.Compile.ElixirScript.get_compiler_params()
3327

3428
{:ok, _} = ElixirScript.Watcher.start_link(
35-
input_path,
36-
%{output: output_path, format: format, js_modules: js_modules}
29+
input,
30+
opts
3731
)
3832

3933
:timer.sleep :infinity
4034
end
4135

42-
defp get_elixirscript_config() do
43-
config = Mix.Project.config
44-
exjs_config = cond do
45-
Keyword.has_key?(config, :elixir_script) ->
46-
Keyword.get(config, :elixir_script, [])
47-
Keyword.has_key?(config, :elixirscript) ->
48-
Keyword.get(config, :elixirscript, [])
49-
true ->
50-
defaults()
51-
end
52-
53-
Keyword.merge(defaults(), exjs_config)
54-
end
55-
56-
defp defaults() do
57-
[
58-
input: "lib/elixirscript",
59-
output: "priv/elixirscript",
60-
format: :es
61-
]
62-
end
63-
6436
end

mix.exs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ defmodule ElixirScript.Mixfile do
55
[
66
app: :elixir_script,
77
version: "0.28.0-dev",
8-
elixir: "~> 1.0",
9-
elixirc_paths: elixirc_paths(),
8+
elixir: "~> 1.5-dev",
9+
elixirc_paths: elixirc_paths(Mix.env),
1010
escript: escript_config(),
1111
deps: deps(),
1212
description: description(),
@@ -36,7 +36,8 @@ defmodule ElixirScript.Mixfile do
3636
]
3737
end
3838

39-
defp elixirc_paths(), do: ["lib", "priv/std_lib"]
39+
defp elixirc_paths(:test), do: ["lib", "priv/std_lib", "test/support"]
40+
defp elixirc_paths(_), do: ["lib", "priv/std_lib"]
4041

4142
defp escript_config do
4243
[main_module: ElixirScript.CLI, name: "elixirscript"]
@@ -62,8 +63,7 @@ defmodule ElixirScript.Mixfile do
6263

6364
defp aliases do
6465
[dist: &dist/1,
65-
install: &install/1,
66-
supported: &supported/1]
66+
install: &install/1]
6767
end
6868

6969
def dist(_) do
@@ -106,10 +106,4 @@ defmodule ElixirScript.Mixfile do
106106
IO.puts("installed at /usr/local/elixirscript")
107107
end
108108

109-
def supported(_) do
110-
Mix.Task.run "app.start"
111-
112-
ElixirScript.Gen.Supported.generate()
113-
end
114-
115109
end

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"build": "rollup -c rollup.config.js",
1313
"clean": "rm -rf priv/build",
1414
"test": "nyc ava src/javascript/tests",
15-
"build:test-app": "mix elixirscript test/app/src/ -f common -o test/app/build/",
15+
"build:test-app": "MIX_ENV=test mix elixirscript Main -f common -o test/app/build/",
1616
"test-app": "yarn run build:test-app && NODE_ENV=test ava 'test/app/spec/**/*.spec.js'"
1717
},
1818
"repository": {

0 commit comments

Comments
 (0)