ocamlspot / compdir.ml

(*                                                                     *)
(*                            OCamlSpotter                             *)
(*                                                                     *)
(*                             Jun FURUSE                              *)
(*                                                                     *)
(*   Copyright 2008-2012 Jun Furuse. All rights reserved.              *)
(*   This file is distributed under the terms of the GNU Library       *)
(*   General Public License, with the special exception on linking     *)
(*   described in file LICENSE.                                        *)
(*                                                                     *)

(** ocamlbuild compilation directory tweak *)

open Utils

module FP = Filepath

let comp_dir fp0 =
  assert (FP.is_absolute fp0);

  let rec f rev_bases fp = 
    let dir = FP.to_string fp in
    let ocamlbuild_path = dir ^/ "_build" in
    let diropt = 
      if Unix.is_dir ocamlbuild_path then Some ocamlbuild_path
      else None
    match diropt with
    | Some _ -> Some (FP.(^/) fp (Filename.concats ("_build" :: List.rev rev_bases)))
    | None ->
        if FP.is_root fp then Some fp0
        else match FP.dirbase fp with
        | dir, Some base -> f (base :: rev_bases) dir
        | _ -> assert false
  Option.default (f [] fp0) (fun () -> fp0)

let comp_dir = Hashtbl.memoize (Hashtbl.create 107) comp_dir

let src_dir fp0 =
  assert (FP.is_absolute fp0);

  Debug.format "Compdir.src_dir: %s@." (FP.to_string fp0);

  let rec f dirs fp = 
    match FP.dirbase fp with
    | dir, Some "_build" -> FP.(^/) dir (Filename.concats dirs)
    | _, None -> fp0
    | dir, Some x -> f (x::dirs) dir
  let res = f [] fp0 in
  Debug.format "Compdir.src_dir => %s@." (FP.to_string res); 

let src_dir = Hashtbl.memoize (Hashtbl.create 107) src_dir
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.