Skip to content

Commit 94718b9

Browse files
committed
Reshuffle files for libraries and executables into different folders
1 parent 63e6223 commit 94718b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+210
-132
lines changed

Makefile

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ OS:=$(shell uname -s)
44
TARGET_BUCKLESCRIPT:=$(findstring $(shell opam switch show),4.02.3)
55

66
build:
7-
$(DUNE) build src/graphql_ppx_bucklescript.exe
8-
# if [ "$(TARGET_BUCKLESCRIPT)" = "" ]; then $(DUNE) build src/graphql_ppx_native.a; fi ## FIXME: Native library does not build
9-
cp _build/default/src/graphql_ppx_bucklescript.exe graphql_ppx.exe
7+
$(DUNE) build src/bucklescript/graphql_ppx.exe
8+
if [ "$(TARGET_BUCKLESCRIPT)" = "" ]; then $(DUNE) build src/native/graphql_ppx.a; fi
9+
cp _build/default/src/bucklescript/graphql_ppx.exe .
1010

1111
buildall:
12-
# $(DUNE) build --workspace=dune-workspace.dev-native src/graphql_ppx_native.a ## FIXME: Native library does not build
13-
$(DUNE) build --workspace=dune-workspace.dev-bs src/graphql_ppx_bucklescript.exe
14-
([ -x _build/4.02.3/src/graphql_ppx_bucklescript.exe ] \
15-
&& cp _build/4.02.3/src/graphql_ppx_bucklescript.exe graphql_ppx.exe)
12+
$(DUNE) build --workspace=dune-workspace.dev-native src/native/graphql_ppx.a
13+
$(DUNE) build --workspace=dune-workspace.dev-bs src/bucklescript/graphql_ppx.exe
14+
([ -x _build/4.02.3/src/bucklescript/graphql_ppx.exe ] \
15+
&& cp _build/4.02.3/src/bucklescript/graphql_ppx.exe .)
1616

1717
test: build only-test
1818

dune

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(alias
22
(name graphql_ppx)
3-
(deps src/graphql_ppx_bucklescript.exe
4-
src/graphql_ppx_native.a))
3+
(deps src/bucklescript/graphql_ppx.exe
4+
src/native/graphql_ppx.a))
File renamed without changes.
File renamed without changes.

src/compat.ml src/base/compat.ml

File renamed without changes.
File renamed without changes.

src/base/dune

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(library
2+
(name graphql_ppx_base)
3+
(public_name graphql_ppx_base)
4+
(libraries unix
5+
result
6+
yojson)
7+
(preprocess (pps ppx_tools_versioned.metaquot_402)))
8+
9+
(rule
10+
(targets build_config.ml)
11+
(deps (:first-dep build_config.cppo.ml))
12+
(action
13+
(chdir ${ROOT}
14+
(run %{bin:cppo} -V OCAML:%{ocaml_version} %{first-dep} -o %{targets}))))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/log.ml src/base/log.ml

File renamed without changes.
File renamed without changes.

src/option.ml src/base/option.ml

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/schema.ml src/base/schema.ml

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/bucklescript/dune

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(executable
2+
(name graphql_ppx)
3+
(libraries ocaml-migrate-parsetree
4+
graphql_ppx_base)
5+
(flags (:include ../../discover/dune.flags))
6+
(preprocess (pps ppx_tools_versioned.metaquot_402)))
File renamed without changes.
File renamed without changes.

src/dune

-65
Original file line numberDiff line numberDiff line change
@@ -1,65 +0,0 @@
1-
(executable
2-
(name graphql_ppx_bucklescript)
3-
(libraries ocaml-migrate-parsetree
4-
graphql_ppx_base)
5-
(modules graphql_ppx_bucklescript
6-
output_bucklescript_decoder
7-
output_bucklescript_encoder
8-
output_bucklescript_module
9-
output_bucklescript_unifier
10-
output_bucklescript_utils)
11-
(flags (:include ../discover/dune.flags))
12-
(preprocess (pps ppx_tools_versioned.metaquot_402)))
13-
14-
(library
15-
(name graphql_ppx_native)
16-
(public_name graphql_ppx)
17-
(modules graphql_ppx_native)
18-
(wrapped false)
19-
(kind ppx_rewriter)
20-
(libraries ocaml-migrate-parsetree
21-
graphql_ppx_base
22-
ppxlib)
23-
(preprocess (pps ppx_tools_versioned.metaquot_402)))
24-
25-
(library
26-
(name graphql_ppx_base)
27-
(public_name graphql_ppx_base)
28-
(modules ast_serializer_apollo
29-
build_config
30-
compat
31-
dirty_checker
32-
generator_utils
33-
graphql_ast
34-
graphql_lexer
35-
graphql_parser_document
36-
graphql_parser_value
37-
graphql_parser
38-
graphql_ppx_base
39-
graphql_printer
40-
log
41-
multi_visitor
42-
option
43-
ppx_config
44-
read_schema
45-
result_decoder
46-
result_ext
47-
result_structure
48-
rule_known_argument_names
49-
rule_no_unused_variables
50-
schema
51-
source_pos
52-
traversal_utils
53-
type_utils
54-
validations)
55-
(libraries unix
56-
result
57-
yojson)
58-
(preprocess (pps ppx_tools_versioned.metaquot_402)))
59-
60-
(rule
61-
(targets build_config.ml)
62-
(deps (:first-dep build_config.cppo.ml))
63-
(action
64-
(chdir ${ROOT}
65-
(run %{bin:cppo} -V OCAML:%{ocaml_version} %{first-dep} -o %{targets}))))

src/graphql_ppx_native.ml

-54
This file was deleted.

src/native/dune

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(library
2+
(name graphql_ppx)
3+
(public_name graphql_ppx)
4+
(wrapped false)
5+
(kind ppx_rewriter)
6+
(libraries graphql_ppx_base
7+
ppxlib)
8+
(preprocess (pps ppxlib.metaquot)))

src/native/graphql_ppx.ml

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
open Graphql_ppx_base
2+
open Ppxlib
3+
4+
open Source_pos
5+
6+
open Output_native_utils
7+
8+
let argv = Sys.argv |> Array.to_list
9+
10+
let add_pos delimLength base pos =
11+
{
12+
pos_fname = base.pos_fname;
13+
pos_lnum = base.pos_lnum + pos.line;
14+
pos_bol = 0;
15+
pos_cnum = if pos.line = 0 then delimLength + pos.col else pos.col;
16+
}
17+
18+
let add_loc delimLength base span =
19+
{
20+
loc_start = add_pos delimLength base.loc_start (fst span);
21+
loc_end = add_pos delimLength base.loc_start (snd span);
22+
loc_ghost = false;
23+
}
24+
25+
let fmt_lex_err err =
26+
let open Graphql_lexer in
27+
match err with
28+
| Unknown_character ch -> Printf.sprintf "Unknown character %c" ch
29+
| Unexpected_character ch -> Printf.sprintf "Unexpected character %c" ch
30+
| Unterminated_string -> Printf.sprintf "Unterminated string literal"
31+
| Unknown_character_in_string ch -> Printf.sprintf "Unknown character in string literal: %c" ch
32+
| Unknown_escape_sequence s -> Printf.sprintf "Unknown escape sequence in string literal: %s" s
33+
| Unexpected_end_of_file -> Printf.sprintf "Unexpected end of query"
34+
| Invalid_number -> Printf.sprintf "Invalid number"
35+
36+
let fmt_parse_err err =
37+
let open Graphql_parser in
38+
match err with
39+
| Unexpected_token t -> Printf.sprintf "Unexpected token %s" (Graphql_lexer.string_of_token t)
40+
| Unexpected_end_of_file -> "Unexpected end of query"
41+
| Lexer_error err -> fmt_lex_err err
42+
43+
let is_prefixed prefix str =
44+
let i = 0 in
45+
let len = String.length prefix in
46+
let j = ref 0 in
47+
while !j < len && String.unsafe_get prefix !j =
48+
String.unsafe_get str (i + !j) do
49+
incr j
50+
done;
51+
(!j = len)
52+
53+
let make_error_expr loc message =
54+
let ext = Location.Error.to_extension (Location.Error.createf ~loc "%s" message) in
55+
Ast_helper.Exp.extension ~loc ext
56+
57+
let drop_prefix prefix str =
58+
let len = String.length prefix in
59+
let rest = (String.length str) - len in
60+
String.sub str len rest
61+
62+
let () = Ppx_config.(set_config {
63+
verbose_logging = (match List.find ((=) "-verbose") argv with
64+
| _ -> true
65+
| exception Not_found -> false);
66+
output_mode = (match List.find ((=) "-ast-out") argv with
67+
| _ -> Ppx_config.Apollo_AST
68+
| exception Not_found -> Ppx_config.String);
69+
verbose_error_handling = (match List.find ((=) "-o") argv with
70+
| _ -> false
71+
| exception Not_found -> begin match Sys.getenv "NODE_ENV" with
72+
| "production" -> false
73+
| _ -> true
74+
| exception Not_found -> true
75+
end);
76+
root_directory = Sys.getcwd ();
77+
schema_file = (match List.find (is_prefixed "-schema=") argv with
78+
| arg -> drop_prefix "-schema=" arg
79+
| exception Not_found -> "graphql_schema.json");
80+
raise_error_with_loc = fun loc message ->
81+
let loc = conv_loc loc in
82+
raise (Location.Error (Location.Error.createf ~loc "%s" message))
83+
})
84+
85+
let rewrite_query loc delim query =
86+
let lexer = Graphql_lexer.make query in
87+
let delimLength = match delim with | Some s -> 2 + String.length s | None -> 1 in
88+
match Graphql_lexer.consume lexer with
89+
| Result.Error e -> raise (Location.Error (
90+
Location.Error.createf ~loc:(add_loc delimLength loc e.span |> conv_loc) "%s" (fmt_lex_err e.item)
91+
))
92+
| Result.Ok tokens ->
93+
let parser = Graphql_parser.make tokens in
94+
match Graphql_parser_document.parse_document parser with
95+
| Result.Error e -> raise (Location.Error (
96+
Location.Error.createf ~loc:(add_loc delimLength loc e.span |> conv_loc) "%s" (fmt_parse_err e.item)
97+
))
98+
| Result.Ok document ->
99+
let config = {
100+
Generator_utils.map_loc = add_loc delimLength loc;
101+
delimiter = delim;
102+
full_document = document;
103+
(* the only call site of schema, make it lazy! *)
104+
schema = Lazy.force (Read_schema.get_schema ());
105+
} in
106+
match Validations.run_validators config document with
107+
| Some errs ->
108+
Ast_helper.Mod.mk
109+
(Pmod_structure (List.map (fun (loc, msg) ->
110+
let loc = conv_loc loc in
111+
[%stri let _ = [%e make_error_expr loc msg]]) errs))
112+
| None ->
113+
let parts = Result_decoder.unify_document_schema config document in
114+
Output_native_module.generate_modules config parts
115+
116+
117+
let rewrite ~loc ~path:_ expr =
118+
let open Parsetree in
119+
match expr with
120+
| { pexp_desc = Pexp_constant (Pconst_string (query, delim)); _ } ->
121+
rewrite_query
122+
(conv_loc_from_ast loc)
123+
delim
124+
query
125+
| _ -> raise (Location.Error (
126+
Location.Error.createf ~loc:loc "[%%graphql] accepts a string, e.g. [%%graphql {| { query |}]"
127+
))
128+
129+
let ext = Extension.declare
130+
"graphql"
131+
Extension.Context.module_expr
132+
Ast_pattern.(single_expr_payload __)
133+
rewrite
134+
135+
let () = Ppxlib.Driver.register_transformation "graphql"

src/native/output_native_module.ml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
open Ppxlib
2+
3+
open Ast_helper
4+
5+
let generate_modules _ _ =
6+
Mod.mk (Pmod_structure [])

src/native/output_native_utils.ml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
open Graphql_ppx_base
2+
3+
let conv_pos pos =
4+
{
5+
Lexing.pos_fname = pos.Source_pos.pos_fname;
6+
Lexing.pos_lnum = pos.Source_pos.pos_lnum;
7+
Lexing.pos_bol = pos.Source_pos.pos_bol;
8+
Lexing.pos_cnum = pos.Source_pos.pos_cnum;
9+
}
10+
11+
let conv_loc loc =
12+
{
13+
Location.loc_start = conv_pos loc.Source_pos.loc_start;
14+
Location.loc_end = conv_pos loc.Source_pos.loc_end;
15+
Location.loc_ghost = loc.Source_pos.loc_ghost;
16+
}
17+
18+
let conv_pos_from_ast pos =
19+
{
20+
Source_pos.pos_fname = pos.Lexing.pos_fname;
21+
Source_pos.pos_lnum = pos.Lexing.pos_lnum;
22+
Source_pos.pos_bol = pos.Lexing.pos_bol;
23+
Source_pos.pos_cnum = pos.Lexing.pos_cnum;
24+
}
25+
26+
let conv_loc_from_ast loc =
27+
{
28+
Source_pos.loc_start = conv_pos_from_ast loc.Location.loc_start;
29+
Source_pos.loc_end = conv_pos_from_ast loc.Location.loc_end;
30+
Source_pos.loc_ghost = loc.Location.loc_ghost;
31+
}
32+

src/output_native_module.ml

-4
This file was deleted.

0 commit comments

Comments
 (0)