11open Core__.Common
22
3- type options = { compile : string -> sexp }
3+ type deps = (string * string ) list
4+
5+ type options = {
6+ compile :
7+ deps :deps -> current_dir :string -> module_name :string -> string -> sexp ;
8+ deps : deps ;
9+ current_dir : string ;
10+ }
11+
412type context = { opt : options }
513
14+ let module_to_path module_name =
15+ String. split_on_char '.' module_name |> String. concat Filename. dir_sep
16+
17+ let get_root_module module_name =
18+ match String. split_on_char '.' module_name with
19+ | root :: _ -> root
20+ | [] -> module_name
21+
22+ let resolve_module_path ~deps ~current_dir module_name =
23+ let relative_path = module_to_path module_name in
24+ let local_path = Filename. concat current_dir (relative_path ^ " .clj" ) in
25+ if FileReader. file_exists local_path then FileReader. realpath local_path
26+ else
27+ let root_module = get_root_module module_name in
28+ match List. assoc_opt root_module deps with
29+ | Some version ->
30+ let packages_dir =
31+ match Sys. getenv_opt " LY2K_PACKAGES_DIR" with
32+ | Some dir -> dir
33+ | None -> failwith " LY2K_PACKAGES_DIR environment variable is not set"
34+ in
35+ let pkg_path =
36+ Printf. sprintf " %s/%s/%s/%s.clj" packages_dir root_module version
37+ relative_path
38+ in
39+ if FileReader. file_exists pkg_path then pkg_path
40+ else
41+ failwith
42+ (Printf. sprintf " Module not found in package: %s at %s" module_name
43+ pkg_path)
44+ | None ->
45+ failwith
46+ (Printf. sprintf " Module not found: %s (checked: %s)" module_name
47+ local_path)
48+
49+ let extract_deps node =
50+ let rec find_deps = function
51+ | SAtom _ -> []
52+ | SList (_ , SAtom (_ , "do*" ) :: children ) ->
53+ List. concat_map find_deps children
54+ | SList
55+ ( _,
56+ SAtom (_, " def*" )
57+ :: SAtom (_, " __deps" )
58+ :: SList (_, [ SAtom (_, " quote*" ); SList (_, items) ])
59+ :: _ ) ->
60+ items
61+ |> List. map (function
62+ | SList (_ , [ SAtom (_ , name ); SAtom (_ , version ) ]) -> (name, version)
63+ | x -> failsexp __LOC__ [ x ])
64+ | SList _ -> []
65+ in
66+ find_deps node
67+
668let rec invoke (ctx : context ) = function
769 | SAtom _ as x -> (ctx, x)
870 | SList (m , (SAtom (_ , "do*" ) as do_ ) :: children ) ->
@@ -17,7 +79,24 @@ let rec invoke (ctx : context) = function
1779 let items =
1880 items |> List. split_into_pairs
1981 |> List. map (function
20- | _ , SList (_ , [ _ ; SAtom (_ , path ); _ ]) -> ctx.opt.compile path
82+ (* New format : 2 elements - [ns_id, module_path] * )
83+ | _ , SList (_ , [ _ ; SAtom (_ , module_name ) ]) ->
84+ let resolved_path =
85+ resolve_module_path ~deps: ctx.opt.deps
86+ ~current_dir: ctx.opt.current_dir module_name
87+ in
88+ let new_current_dir = Filename. dirname resolved_path in
89+ ctx.opt.compile ~deps: ctx.opt.deps ~current_dir: new_current_dir
90+ ~module_name resolved_path
91+ (* Old format: 3 elements - [ns_id, path_unquoted, path_quoted] *)
92+ | _ , SList (_ , [ _ ; SAtom (_ , path ); _ ]) ->
93+ let resolved_path =
94+ resolve_module_path ~deps: ctx.opt.deps
95+ ~current_dir: ctx.opt.current_dir path
96+ in
97+ let new_current_dir = Filename. dirname resolved_path in
98+ ctx.opt.compile ~deps: ctx.opt.deps ~current_dir: new_current_dir
99+ ~module_name: path resolved_path
21100 | k , v -> failsexp __LOC__ [ k; v ])
22101 in
23102 (ctx, SList (meta_empty, SAtom (meta_empty, " do*" ) :: items))
@@ -28,5 +107,5 @@ let rec invoke (ctx : context) = function
28107 | SList (_ , _ :: _ ) as x -> (ctx, x)
29108 | x -> failsexp __LOC__ [ x ]
30109
31- let do_invoke compile (node : sexp ) : sexp =
32- invoke { opt = { compile } } node |> snd
110+ let do_invoke ~ deps ~ current_dir compile (node : sexp ) : sexp =
111+ invoke { opt = { compile; deps; current_dir } } node |> snd
0 commit comments