Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command line argument for generating reason #150

Merged
merged 9 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions example/1-hello/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"esy": "^0.6.10"
}
}
aantron marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions example/2-middleware/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"esy": "^0.6.10"
}
}
5 changes: 5 additions & 0 deletions example/h-sql/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"esy": "^0.6.10"
}
}
66 changes: 66 additions & 0 deletions example/r-template-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# `w-template-files`
aantron marked this conversation as resolved.
Show resolved Hide resolved

<br>

While templates can be written with other code in `.ml` files, they can also
aantron marked this conversation as resolved.
Show resolved Hide resolved
live in their own source files. This can be useful as templates become larger,
or when you have many templates.

If your template file is mostly HTML, you can give it a name like
`template.eml.html`, to trigger HTML syntax highlighting by your editor.
Additionally, if you are using `ocamlformat`, the `.html` extension will
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason users will typically use refmt rather than ocamlformat, and I haven't directly tested what will happen if you apply refmt to an eml file, or what it looks like in an editor.

prevent errors that come from `ocamlformat` attempting to format the syntax of
the template.

This example does just that. It splits the code of the basic template example,
[**7-template**](../7-template#files), into two files. The first is the
aantron marked this conversation as resolved.
Show resolved Hide resolved
template, in
[`template.eml.html`](https://github.com/aantron/dream/blob/master/example/w-template-files/template.eml.html):
aantron marked this conversation as resolved.
Show resolved Hide resolved

```html
let render param =
<html>
<body>
<h1>The URL parameter was <%s param %>!</h1>
</body>
</html>
aantron marked this conversation as resolved.
Show resolved Hide resolved
```

After preprocessing by the templater, this file becomes `template.ml`, so it
defines a module `Template`, containing a function `Template.render`. We call
this function from the main server in
[`server.ml`](https://github.com/aantron/dream/blob/master/example/w-template-files/server.ml):

```ocaml
let () =
Dream.run
@@ Dream.logger
@@ Dream.router [

Dream.get "/:word"
(fun request ->
Dream.param "word" request
|> Template.render
|> Dream.html);

]
@@ Dream.not_found
aantron marked this conversation as resolved.
Show resolved Hide resolved
```

<pre><code><b>$ cd example/w-template-files</b>
aantron marked this conversation as resolved.
Show resolved Hide resolved
<b>$ npm install esy && npx esy</b>
<b>$ npx esy start</b></code></pre>

<br>

**See also:**

- [**7-template**](../7-template#files) for comments on the
aantron marked this conversation as resolved.
Show resolved Hide resolved
[`dune` file](https://github.com/aantron/dream/blob/master/example/w-template-files/dune)
and [security
information](https://github.com/aantron/dream/tree/master/example/7-template#security).
- [**w-template-files**](../w-template-files) for the OCaml version of this example.

<br>

[Up to the example index](../#examples)
10 changes: 10 additions & 0 deletions example/r-template-files/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(executable
(name server)
(libraries dream))

(rule
(targets template.re)
(deps template.eml.html)
(action (run dream_eml %{deps} --workspace %{workspace_root} --syntax-reason)))

(data_only_dirs _esy esy.lock lib node_modules)
1 change: 1 addition & 0 deletions example/r-template-files/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 2.0)
17 changes: 17 additions & 0 deletions example/r-template-files/esy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"dependencies": {
"@opam/dream": "1.0.0~alpha2",
"@opam/dune": "^2.0",
"ocaml": "4.12.x"
},
"devDependencies": {
"@opam/ocaml-lsp-server": "*",
"@opam/ocamlfind-secondary": "*"
},
"resolutions": {
"@opam/conf-libev": "esy-packages/libev:package.json#0b5eb6685b688649045aceac55dc559f6f21b829"
},
"scripts": {
"start": "dune exec --root . ./server.exe"
}
}
9 changes: 9 additions & 0 deletions example/r-template-files/server.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let () =
Dream.run @@
Dream.logger @@
Dream.router([
Dream.get("/:word", request =>
Dream.param("word", request) |> Template.render |> Dream.html
),
]) @@
Dream.not_found;
7 changes: 7 additions & 0 deletions example/r-template-files/template.eml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let render = param => {
<html>
<body>
<h1>The URL parameter was <%s param %>!</h1>
</body>
</html>
};
aantron marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions example/w-template-files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ let () =
[`dune` file](https://github.com/aantron/dream/blob/master/example/w-template-files/dune)
and [security
information](https://github.com/aantron/dream/tree/master/example/7-template#security).
- [**r-template-files**](../r-template-files) for the Reason syntax version of this example.

<br>

Expand Down
6 changes: 3 additions & 3 deletions src/eml/eml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,9 @@ end



let process_file (input_file, location) =

let reason, extension =
let process_file (input_file, location, reason_syntax) =
let reason, extension = if (reason_syntax) then true, ".re" else
(* If there was no explicit command line argument, decide using file extension *)
match Filename.extension input_file with
| ".re" -> true, ".re"
| _ -> false, ".ml"
Expand Down
10 changes: 8 additions & 2 deletions src/eml/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

module Command_line :
sig
val parse : unit -> (string * string) list
val parse : unit -> (string * string * bool) list
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the way the code is currently written, there is a global syntax flag for all the files, so the result type should be

(string * string) list * bool

However, I think it would be even better to move the code from process_file in eml.ml, which checks the extension, into the command-line parser, and decide here what syntax each file will use. Then, the result type should be either what this PR has now, or, even more helpfully

(string * string * [ `OCaml | `Reason ]) list

end =
struct
let usage = {|Usage:
Expand All @@ -21,10 +21,16 @@ struct
let workspace_path =
ref ""

let reason_syntax =
ref false

let options = Arg.align [
"--workspace",
Arg.Set_string workspace_path,
"PATH Relative path to the Dune workspace for better locations";
"--syntax-reason",
aantron marked this conversation as resolved.
Show resolved Hide resolved
Arg.Set reason_syntax,
"Whether the templater should emit Reason syntax after preprocessing the template";
aantron marked this conversation as resolved.
Show resolved Hide resolved
]

let set_file file =
Expand Down Expand Up @@ -58,7 +64,7 @@ struct
let prefix = build_prefix (Sys.getcwd ()) "" !workspace_path in

input_files
|> List.map (fun file -> file, Filename.concat prefix file)
|> List.map (fun file -> file, Filename.concat prefix file, !reason_syntax)
end

let () =
Expand Down