-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
- Loading branch information
1 parent
eeb99f9
commit 88df01d
Showing
7 changed files
with
112 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
open Stdune | ||
open Dune | ||
|
||
module Kind = struct | ||
type t = | ||
| Explicit | ||
| Dune_workspace | ||
| Jbuild_workspace | ||
| Dune_project | ||
| Cwd | ||
|
||
let priority = function | ||
| Explicit -> 0 | ||
| Dune_workspace -> 1 | ||
| Jbuild_workspace -> 2 | ||
| Dune_project -> 3 | ||
| Cwd -> 4 | ||
|
||
let of_dir_contents files = | ||
if String.Set.mem files Workspace.filename then | ||
Some Dune_workspace | ||
else if Wp.t = Jbuilder && String.Set.exists files ~f:(fun fn -> | ||
String.is_prefix fn ~prefix:"jbuild-workspace") then | ||
Some Jbuild_workspace | ||
else if String.Set.mem files Dune_project.filename then | ||
Some Dune_project | ||
else | ||
None | ||
end | ||
|
||
type t = | ||
{ dir : string | ||
; to_cwd : string list | ||
; kind : Kind.t | ||
} | ||
|
||
let make kind dir = | ||
{ kind | ||
; dir | ||
; to_cwd = [] | ||
} | ||
|
||
let find () = | ||
let cwd = Sys.getcwd () in | ||
let rec loop counter ~candidate ~to_cwd dir = | ||
match Sys.readdir dir with | ||
| exception (Sys_error msg) -> | ||
Errors.warn Loc.none | ||
"Unable to read directory %s. \ | ||
Will not look for root in parent directories@.\ | ||
Reason: %s@.\ | ||
To remove this warning, set your root explicitly using --root.@." | ||
dir msg; | ||
candidate | ||
| files -> | ||
let files = String.Set.of_list (Array.to_list files) in | ||
let candidate = | ||
match Kind.of_dir_contents files with | ||
| Some kind when Kind.priority kind <= Kind.priority candidate.kind -> | ||
{ kind; dir; to_cwd } | ||
| _ -> | ||
candidate | ||
in | ||
cont counter ~candidate dir ~to_cwd | ||
and cont counter ~candidate ~to_cwd dir = | ||
if counter > String.length cwd then | ||
candidate | ||
else | ||
let parent = Filename.dirname dir in | ||
if parent = dir then | ||
candidate | ||
else | ||
let base = Filename.basename dir in | ||
loop (counter + 1) parent ~candidate ~to_cwd:(base :: to_cwd) | ||
in | ||
loop 0 ~to_cwd:[] cwd | ||
~candidate:{ kind = Cwd; dir = cwd; to_cwd = [] } | ||
|
||
let create ~specified_by_user = | ||
match specified_by_user with | ||
| Some dn -> make Explicit dn | ||
| None -> | ||
if Config.inside_dune then | ||
make Cwd "." | ||
else | ||
find () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(** Finding the root of the workspace *) | ||
|
||
module Kind : sig | ||
type t = | ||
| Explicit | ||
| Dune_workspace | ||
| Jbuild_workspace | ||
| Dune_project | ||
| Cwd | ||
end | ||
|
||
type t = | ||
{ dir : string | ||
; to_cwd : string list (** How to reach the cwd from the root *) | ||
; kind : Kind.t | ||
} | ||
|
||
val create : specified_by_user:string option -> t |