Skip to content

Commit

Permalink
slight changes about writing files
Browse files Browse the repository at this point in the history
  • Loading branch information
gfngfn committed Dec 31, 2023
1 parent 1e8cc8d commit 2e06c69
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
4 changes: 4 additions & 0 deletions bin-saphe/configError.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ type config_error =
}
| PackageRegistryFetcherError of PackageRegistryFetcher.error
| CanonicalRegistryUrlError of CanonicalRegistryUrl.error
| CannotWriteEnvelopeConfig of {
message : string;
path : abs_path;
}
18 changes: 10 additions & 8 deletions bin-saphe/envelopeConfig.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

open MyUtil
open ConfigError


type relative_path = string
Expand Down Expand Up @@ -74,12 +75,13 @@ let envelope_config_encoder (envelope_config : t) : Yaml.value =
])


let write (abspath_envelope_config : abs_path) (envelope_config : t) : unit =
let write (abspath_envelope_config : abs_path) (envelope_config : t) : (unit, config_error) result =
let open ResultMonad in
let yaml = envelope_config_encoder envelope_config in
match Yaml.to_string ~encoding:`Utf8 ~layout_style:`Block ~scalar_style:`Plain yaml with
| Ok(data) ->
Core.Out_channel.write_all (get_abs_path_string abspath_envelope_config) ~data;
Logging.end_envelope_config_output abspath_envelope_config

| Error(_) ->
assert false
let data = encode_yaml yaml in
try
Core.Out_channel.write_all (get_abs_path_string abspath_envelope_config) ~data;
return ()
with
| Sys_error(message) ->
err @@ CannotWriteEnvelopeConfig{ message; path = abspath_envelope_config }
10 changes: 3 additions & 7 deletions bin-saphe/lockConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ let load (abspath_lock_config : abs_path) : t ok =

let write (abspath_lock_config : abs_path) (lock_config : t) : unit =
let yaml = lock_config_encoder lock_config in
match Yaml.to_string ~encoding:`Utf8 ~layout_style:`Block ~scalar_style:`Plain yaml with
| Ok(data) ->
Core.Out_channel.write_all (get_abs_path_string abspath_lock_config) ~data;
Logging.end_lock_config_output abspath_lock_config

| Error(_) ->
assert false
let data = encode_yaml yaml in
Core.Out_channel.write_all (get_abs_path_string abspath_lock_config) ~data;
Logging.end_lock_config_output abspath_lock_config
11 changes: 10 additions & 1 deletion bin-saphe/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ let report_config_error = function
]
end

| CannotWriteEnvelopeConfig{ message; path } ->
report_error [
NormalLine(Printf.sprintf "cannot output the envelope config to '%s' (message: '%s')" (get_abs_path_string path) message);
]


type solve_input =
| PackageSolveInput of {
Expand Down Expand Up @@ -558,8 +563,12 @@ let solve ~(fpath_in : string) =
(* Selects the minimum version according to the user's designation for the moment.
TODO: take dependencies into account when selecting a language version *)
in

(* Writes the envelope config: *)
let envelope_config = make_envelope_config package_contents in
EnvelopeConfig.write abspath_envelope_config envelope_config;
let* () = EnvelopeConfig.write abspath_envelope_config envelope_config in
Logging.end_envelope_config_output abspath_envelope_config;

begin
match package_contents with
| PackageConfig.Library{ dependencies; test_dependencies; _ } ->
Expand Down
6 changes: 6 additions & 0 deletions src-util/myUtil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ let is_directory (abspath : abs_path) : bool =
Sys.is_directory (get_abs_path_string abspath)


let encode_yaml (yaml : Yaml.value) : string =
match Yaml.to_string ~encoding:`Utf8 ~layout_style:`Block ~scalar_style:`Plain yaml with
| Ok(data) -> data
| Error(_) -> assert false


type 'a cycle =
| Loop of 'a
| Cycle of 'a TupleList.t
Expand Down
2 changes: 2 additions & 0 deletions src-util/myUtil.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ val make_absolute_if_relative : origin:string -> string -> abs_path

val is_directory : abs_path -> bool

val encode_yaml : Yaml.value -> string

val read_file : abs_path -> (string, string) result

type 'a cycle =
Expand Down

0 comments on commit 2e06c69

Please sign in to comment.