Commits

Peter Szilagyi committed b29b233

partially implemented saving only user-modified omake-server project environment variables

Comments (0)

Files changed (5)

ocaml/omake/config.ml

 
 let lookup t ~key ~default =
   let key = String.lowercase key in
-  let default = String.lowercase default in
-  Hashtbl.find t key |! Option.value ~default
+  match T.find t key with
+  | None -> String.lowercase default
+  | Some value -> value
+
+let mem t key =
+  let key = String.lowercase key in
+  T.mem t key
 
 let read_file file =
   try
 
 let write_file f t =
   let s = Sexp.to_string_hum (T.sexp_of_t String.sexp_of_t t) in
-  let header = ";; -*- scheme -*- ;;\n\n" in
-  Out_channel.write_all f ~data:(header ^ s)
+  let header = ";; -*- scheme -*-\n\n" in
+  let footer = "\n" in
+  Out_channel.write_all f ~data:(header ^ s ^ footer)
 
 let update t ~key ~value =
   let key = String.lowercase key in
   (* CR pszilagyi: Why downcase the value? *)
-  let value = String.lowercase value in
-  T.replace t ~key ~data:value
+  T.set t ~key ~data:(String.lowercase value)
+
+let remove t key =
+  T.remove t key
+
+let iter t ~f =
+  T.iter t ~f:(fun ~key ~data -> f ~key ~value:data)

ocaml/omake/config.mli

 val write_file : string -> t -> unit
 
 val lookup : t -> key:string -> default:string -> string
+val mem : t -> string -> bool
 val update : t -> key:string -> value:string -> unit
+val remove : t -> string -> unit
+val iter : t -> f:(key:string -> value:string -> unit) -> unit

ocaml/omake/env.ml

 (* These defaults should match the variables in OMakeroot.  We
    thought about parsing OMakeroot to get the defaults, but decided
    it was too much trouble.  They shouldn't ever change. *)
+(* CR pszilagyi: They should and do change. *)
 let default = function
 | FOUR_POINT_ZERO         -> false
 | LIMIT_SUBDIRS_FOR_SPEED -> false
   (Elisp.env_value (value t VERSION_UTIL_SUPPORT))
   (Elisp.env_value (value t X_LIBRARY_INLINING))
 
-(* Make sure all the env variables are set. *)
-let () =
-  let assure_set key =
-    let default = Bool.to_string (default key) in
-    let key = var_to_string key in
-    match Sys.getenv key with
-    | None -> Unix.putenv ~key ~data:default
-    | Some _ -> ()
-  in
-  List.iter ~f:assure_set all_vars
-
 (* Save the variables in a file in ~/.omake-server/PROJ/env.sexp *)
 
-let get_exn key = match Sys.getenv key with
-| None -> failwithf "No env: %s" key ()
-| Some data -> data
-
-let home = get_exn "HOME"
+let home = Sys.getenv_exn "HOME"
 
 let env_dir id = sprintf "%s/.omake-server/%s" home id
 
   Shell.mkdir ~p:() (env_dir ids);
   Config.write_file (env_file ids) t
 
-let ensure_set t var =
-  let env key = get_exn (var_to_string key) |! Bool.of_string in
-  let key = var_to_string var in
-  let default = Bool.to_string (env var) in
-  ignore (Config.lookup t ~key ~default)
-
-(* Read the initial state of the variables out of the process environment
-   if there is no file.  We do this instead of using the defaults because
-   the user may set the variable in their .bashrc *)
 let create_default id =
   let t = Config.create () in
-  List.iter ~f:(ensure_set t) all_vars;
   save id t;
   t
 
     begin
       match Config.read_file file with
       | Ok t ->
-        List.iter ~f:(ensure_set t) all_vars;
         save id t;
         t
       | Error exn ->
         Log.printf
-          "Can't read env file for %s (%s).  Writing new file from bash env."
+          "Can't read env file for %s (%s).  Writing new file."
           (Id.to_string id)
           (Exn.to_string exn);
         create_default id
 (* Set the variable of a project. *)
 let set id x b =
   let t = get id in
-  Config.update t ~key:(var_to_string x) ~value:(Bool.to_string b);
+  let key = var_to_string x in
+  Config.update t ~key ~value:(Bool.to_string b);
   save id t
 
+let remove id x =
+  let t = get id in
+  let key = var_to_string x in
+  Config.remove t key
+
 (* Set the variables before starting the process. *)
 let set_env t =
-  let put k =
-    let key = var_to_string k in
-    let default = Bool.to_string (default k) in
-    let data = Config.lookup t ~key ~default in
-    Unix.putenv ~key ~data
-  in
-  List.iter ~f:put all_vars
+  Config.iter t ~f:(fun ~key ~value ->
+    (* See the CRs in config.ml.  This effectively does String.uppercase. *)
+    let key = var_to_string (var_of_string key) in
+    Unix.putenv ~key ~data:value)

ocaml/omake/env.mli

 val to_elisp : t -> Elisp.t
 val get : Id.t -> t
 val set : Id.t -> var -> bool -> unit
+val remove : Id.t -> var -> unit
 val set_env : t -> unit

ocaml/omake/server_config.ml

   Config.lookup t ~key:"warn_when_setting_env_vars"
     ~default:(Bool.to_string default_warn_when_setting_env_vars)
 
-let ensure_all_fields t =
-  begin
-    ignore (omake_command t);
-    ignore (kill_when_unwatched t);
-    ignore (warn_when_setting_env_vars t);
-  end
-
 let default =
   let t = Config.create () in
-  ensure_all_fields t;
   t
 
 let t = ref default
 
 let init () =
   begin
+    (* This also serves to create the config dir that other code expects to exist: *)
     Shell.mkdir ~p:() config_dir;
     write_default_file ();
     match Config.read_file config_file with
     | Ok c ->
-      ensure_all_fields c;
       t := c
     | Error exn ->
       Log.printf "Can't read server config file (%s)." config_file;
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.