Skip to content

Commit dec024f

Browse files
Makes it possible to compile the project down to an executable
1 parent a09db06 commit dec024f

File tree

7 files changed

+119
-119
lines changed

7 files changed

+119
-119
lines changed

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ This project requires that you already have [elixir](http://elixir-lang.org/)
88
and its build tool `mix` installed, this can be done with `brew install elixir`
99
or similar.
1010

11-
- Clone this repository: `[email protected]:dragonwasrobot/json-schema-to-elm.git`
12-
- Build a mix archive: `mix archive.build`
13-
- Install the archive: `mix archive.install js2e-1.0.0.ez`
11+
- Clone this repository: `git clone [email protected]:dragonwasrobot/json-schema-to-elm.git`
12+
- Compile the project: `MIX_ENV=prod mix deps.get && mix compile`
13+
- Build an executable: `MIX_ENV=prod mix escript.build`
14+
- An executable, `js2e`, has now been created in your current working directory.
15+
16+
For further help, look up the documentation on how
17+
to [install escripts](https://hexdocs.pm/mix/Mix.Tasks.Escript.Install.html).
1418

1519
## Usage
1620

17-
See `mix help elm.gen` for usage instructions.
21+
See `./js2e` for usage instructions.
1822

1923
A proper description of which properties are mandatory are how the generator
2024
works is still in progress, but feel free to take a look at the `examples`

config/config.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
use Mix.Config
22

3-
config :logger, level: :info
3+
import_config "#{Mix.env}.exs"

config/dev.exs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use Mix.Config
2+
3+
config :logger, level: :debug

config/prod.exs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use Mix.Config
2+
3+
config :logger, level: :info

lib/js2e.ex

+98-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,108 @@
11
defmodule JS2E do
22
@moduledoc ~S"""
3-
Parses JSON schemas and prints Elm types and decoders.
3+
Transforms one or more JSON schema files into their corresponding
4+
Elm types and JSON decoders.
5+
6+
Expects a PATH to one or more JSON schema files from which to generate
7+
Elm code.
8+
9+
js2e PATH [--module-name NAME]
10+
11+
The JSON schema(s) at the given PATH will be converted to Elm types
12+
and JSON decoders.
13+
14+
## Options
15+
16+
* `--module-name` - the module name prefix for the printed Elm modules \
17+
default value is 'Domain'.
418
"""
519

620
require Logger
721
alias JS2E.{Parser, Printer}
822

23+
@spec main([String.t]) :: :ok
24+
def main(args) do
25+
Logger.debug "Arguments: #{inspect args}"
26+
27+
{options, paths, errors} =
28+
OptionParser.parse(args, switches: [module_name: :string])
29+
30+
if length(paths) == 0 do
31+
IO.puts @moduledoc
32+
exit(:normal)
33+
end
34+
35+
if length(errors) > 0 do
36+
IO.puts "Error: Found one or more errors in the supplied options"
37+
exit({:unknown_arguments, errors})
38+
end
39+
40+
files = resolve_all_paths(paths)
41+
Logger.debug "Files: #{inspect files}"
42+
43+
if length(files) == 0 do
44+
IO.puts "Error: Could not find any JSON files in path: #{inspect paths}"
45+
exit(:no_files)
46+
end
47+
48+
output_path = create_output_dir(options)
49+
JS2E.generate(files, output_path)
50+
end
51+
52+
@spec resolve_all_paths([String.t]) :: [String.t]
53+
defp resolve_all_paths(paths) do
54+
paths
55+
|> Enum.filter(&File.exists?/1)
56+
|> Enum.reduce([], fn (filename, files) ->
57+
cond do
58+
File.dir? filename ->
59+
walk_directory(filename) ++ files
60+
61+
String.ends_with?(filename, ".json") ->
62+
[filename | files]
63+
64+
true ->
65+
files
66+
end
67+
end)
68+
end
69+
70+
@spec walk_directory(String.t) :: [String.t]
71+
defp walk_directory(dir) do
72+
dir
73+
|> File.ls!
74+
|> Enum.reduce([], fn file, files ->
75+
filename = "#{dir}/#{file}"
76+
77+
cond do
78+
File.dir? filename ->
79+
walk_directory(filename) ++ files
80+
81+
String.ends_with?(file, ".json") ->
82+
[filename | files]
83+
84+
true ->
85+
files
86+
end
87+
end)
88+
end
89+
90+
@spec create_output_dir(list) :: String.t
91+
defp create_output_dir(options) do
92+
93+
output_path = if Keyword.has_key?(options, :module_name) do
94+
Keyword.get(options, :module_name)
95+
else
96+
"Domain"
97+
end
98+
99+
output_path
100+
|> Path.join("Decoders")
101+
|> File.mkdir_p!()
102+
103+
output_path
104+
end
105+
9106
@spec generate([String.t], String.t) :: :ok
10107
def generate(json_schema_paths, module_name) do
11108

lib/mix/tasks/elm_gen.ex

-113
This file was deleted.

mix.exs

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ defmodule JS2.Mixfile do
99
build_embedded: Mix.env == :prod,
1010
start_permanent: Mix.env == :prod,
1111

12+
# Packaging
13+
escript: [
14+
main_module: JS2E,
15+
name: "js2e"
16+
],
17+
1218
# Dialyxir
1319
dialyzer: [plt_add_deps: :project],
1420

0 commit comments

Comments
 (0)