Skip to content

Commit bc2ce14

Browse files
committed
Now building a stand alone escript.
1 parent 8c735a9 commit bc2ce14

File tree

9 files changed

+83
-34
lines changed

9 files changed

+83
-34
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ erl_crash.dump
44
*.ez
55
ex2js
66
node_modules
7-
priv/test_project/dest
7+
priv/test_project/dest
8+
/dist
9+
deploy.sh

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ Clone the repo
2525
Get dependencies
2626

2727
mix deps.get
28+
npm install
2829

2930
Compile
3031

31-
mix compile (runs npm install if node_modules/escodegen is not present)
32+
mix compile
3233

3334
Test
3435

@@ -38,18 +39,24 @@ To build the escript
3839

3940
mix escript.build
4041

42+
To build distributable tarball
4143

42-
Usage
43-
===
44+
mix ex2js.dist
4445

45-
Add the following to your dependencies:
46+
`ex2js-version-tar.gz` will be in the `dist` folder
47+
48+
Installation
49+
==============
50+
51+
* uncompress `ex2js.tar.gz`.
52+
* use `ex2js` executable found in the uncompressed folder
4653

47-
{ :ex_to_js, github: "bryanjos/ex_to_js"}
48-
49-
You can use the included mix task to convert Elixir to JavaScript
54+
55+
Usage
56+
===
5057

5158
```
52-
$ mix ex2js -h
59+
$ ex2js -h
5360
usage: ex2js <input> [options]
5461
5562
<input> path to elixir files or
@@ -60,11 +67,11 @@ $ mix ex2js -h
6067
-o --output [path] places output at the given path
6168
-t --ast shows only produced spider monkey ast
6269
-ex --elixir read input as elixir code string
70+
-st --stdio reads from stdio
71+
--lib writes the standard lib js to standard out
6372
-h --help this message
6473
```
6574

66-
Alternatively, you can clone the repo, do `mix escript.build` and use the created `ex2js` escript without mix
67-
6875
#Current Limitations (Most if not all of these will be lifted as development goes on)
6976

7077
* No pattern matching

lib/ex_to_js.ex

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,9 @@ defmodule ExToJS do
6868
def javascript_ast_to_code(js_ast) do
6969
js_ast = Poison.encode!(js_ast)
7070

71-
path = try do
72-
Mix.Project.build_path <> "/lib/ex_to_js/priv/escodegen"
73-
rescue
74-
UndefinedFunctionError ->
75-
System.cwd <> "/_build/dev/lib/ex_to_js/priv/escodegen"
76-
end
71+
path = "#{operating_path}/alphonse.js"
7772

78-
case System.cmd(path, [js_ast]) do
73+
case System.cmd("node", [path, js_ast]) do
7974
{js_code, 0} ->
8075
{:ok, js_code }
8176
{error, _} ->
@@ -104,4 +99,16 @@ defmodule ExToJS do
10499

105100
File.write!(file_name, js)
106101
end
102+
103+
104+
def operating_path() do
105+
try do
106+
Mix.Project.build_path <> "/lib/ex_to_js/priv"
107+
rescue
108+
UndefinedFunctionError ->
109+
split_path = Path.split(Application.app_dir(:ex2js))
110+
replaced_path = List.delete_at(split_path, length(split_path) - 1)
111+
Path.join(replaced_path)
112+
end
113+
end
107114
end

lib/ex_to_js/cli.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ defmodule ExToJS.CLI do
66
end
77

88
def parse_args(args) do
9-
switches = [ output: :binary, ast: :boolean, elixir: :boolean, stdio: :boolean ]
9+
switches = [ output: :binary, ast: :boolean, elixir: :boolean, stdio: :boolean, lib: :boolean]
1010
aliases = [ o: :output, t: :ast, ex: :elixir, st: :stdio ]
1111

1212
parse = OptionParser.parse(args, switches: switches, aliases: aliases)
1313

1414
case parse do
1515
{ [stdio: true] , _ , _ } -> {:stdio}
16+
{ [lib: true] , _ , _ } -> {:lib}
1617
{ [ output: output, ast: true, elixir: true], [input], _ } -> { input, output, :ast, :elixir}
1718
{ [ output: output, ast: true], [input], _ } -> { input, output, :ast }
1819
{ [ast: true, elixir: true] , [input], _ } -> { input, :ast, :elixir }
@@ -86,6 +87,13 @@ defmodule ExToJS.CLI do
8687
end)
8788
end
8889

90+
def process({ :lib }) do
91+
path = ExToJS.operating_path
92+
93+
file = File.read!("#{path}/elixir.js")
94+
IO.write(file)
95+
end
96+
8997
def process({ input }) do
9098
input
9199
|> ExToJS.parse_elixir_files
@@ -108,6 +116,7 @@ defmodule ExToJS.CLI do
108116
-t --ast shows only produced spider monkey ast
109117
-ex --elixir read input as elixir code string
110118
-st --stdio reads from stdio
119+
--lib writes the standard lib js to standard out
111120
-h --help this message
112121
"""
113122
end

lib/mix/tasks/compile.npm.ex

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/mix/tasks/ex2js.dist.ex

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule Mix.Tasks.Ex2js.Dist do
2+
use Mix.Task
3+
4+
@shortdoc "Builds a release for ex2js"
5+
6+
def run(args) do
7+
Mix.Task.run "app.start"
8+
9+
dist_folder = "dist"
10+
folder_name = "#{dist_folder}/ex2js"
11+
12+
version = Mix.Project.config[:version]
13+
archive_file_name = "#{dist_folder}/ex2js.tar.gz"
14+
15+
if File.exists?(dist_folder) do
16+
File.rm_rf(dist_folder)
17+
end
18+
19+
Mix.Tasks.Escript.Build.run([])
20+
21+
File.mkdir_p(folder_name)
22+
File.cp!("priv/ex2js", "#{folder_name}/ex2js")
23+
File.cp!("priv/alphonse.js", "#{folder_name}/alphonse.js")
24+
File.cp_r!("node_modules", "#{folder_name}/node_modules")
25+
File.cp!("package.json", "#{folder_name}/package.json")
26+
27+
elixirjs_std_lib = Enum.reduce(Path.wildcard("priv/includes/*.js"), "", fn(x, concat) ->
28+
concat <> "\n" <> File.read!(x)
29+
end)
30+
31+
File.write!("#{folder_name}/elixir.js", elixirjs_std_lib)
32+
33+
System.cmd("tar", ["czf", archive_file_name, folder_name])
34+
35+
File.rm_rf(folder_name)
36+
end
37+
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule ExToJS.Mixfile do
55
[app: :ex_to_js,
66
version: "0.1.0",
77
elixir: "~> 1.0",
8-
compilers: Mix.compilers ++ [:ex2js],
8+
compilers: Mix.compilers,
99
escript: escript_config,
1010
deps: deps,
1111
description: description,

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
%{"elixir_v8": {:git, "git://github.com/le0pard/elixir_v8.git", "2817de7179e247c279d0fb76a00e976c8d0bcdad", []},
2+
"erlang_js": {:git, "git://github.com/basho/erlang_js.git", "07467d899ab90a2b719ad19ab0be0048c1c8d873", [tag: "1.3.0"]},
23
"erlang_v8": {:git, "git://github.com/strange/erlang-v8.git", "1ca13321d5746f3ec20f0c0bfd2bcc8293db71da", []},
34
"estree": {:git, "git://github.com/bryanjos/elixir-estree.git", "c69dc94d23c41647cf33a6236638abd845cf1c48", []},
45
"exjsx": {:git, "git://github.com/talentdeficit/jsex.git", "d3572b488f10e3b83fadb1fc595fc87f84c35809", []},

priv/escodegen renamed to priv/alphonse.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env node
21
var escodegen = require('escodegen');
32
var args = process.argv.slice(2);
43

0 commit comments

Comments
 (0)