Parcourir la source

Stop using tree command in fix-indent script

Leo il y a 8 ans
Parent
commit
d066fe0c95
1 fichiers modifiés avec 24 ajouts et 13 suppressions
  1. 24 13
      fix-indent.ml

+ 24 - 13
fix-indent.ml

@@ -53,21 +53,32 @@ let ignored =
   |> String.split ~on:' '
   |> List.filter_map ~f:(function
          | "" | " " | "  " | "   " | "    " -> None
-         | path -> Some ("-e " ^ path ^ " "))
-  |> String.concat
+         | path -> Some path)
 ;;
 
-(* List ml files, basic regexp, should be enough *)
-let list_mlfiles path =
-  (* 1st grep: select ocaml files,
-   * 2nd grep: remove symlinks and ignored files *)
-  sprintf "tree -if %s | grep -e '.*\\.ml$' | grep -v -e '.* -> .*' %s"
-    path ignored
-  |> (fun cmd -> print_endline cmd; cmd)
-  |> Unix.open_process_in
-  |> In_channel.input_all
-  |> String.split ~on:'\n'
-  |> List.filter ~f:(function "" -> false | _ -> true)
+(* List ml files *)
+let list_mlfiles ?(follow_symlinks=true) path =
+  let is_dotml file =
+    String.split ~on:'.' file
+    |> List.last
+    |> function
+        | None -> false
+        | Some ext -> ext = "ml"
+  in
+  let is_ignored file =
+    List.find ignored (fun to_ignore -> to_ignore = file)
+    |> Option.is_some
+  in
+  let rec ls_rec s =
+    (* If follow_symlinks is set to false then any symlink seems do not to be a
+     * file *)
+    if Sys.is_file_exn ~follow_symlinks s
+    then [s]
+    else Sys.ls_dir s |> List.map ~f:(fun sub -> ls_rec (s ^/ sub))
+        |> List.concat
+  in
+  List.filter (ls_rec path)
+    ~f:(fun file -> not (is_ignored file) && is_dotml file)
 ;;
 
 (* Call ocp-indent for indentation *)