comparison vext.sml @ 1776:316c4fd7e7bc

Update Vext
author Chris Cannam
date Wed, 21 Mar 2018 11:18:32 +0000
parents 128c4544036d
children
comparison
equal deleted inserted replaced
1775:4281d7059f3a 1776:316c4fd7e7bc
36 shall not be used in advertising or otherwise to promote the sale, 36 shall not be used in advertising or otherwise to promote the sale,
37 use or other dealings in this Software without prior written 37 use or other dealings in this Software without prior written
38 authorization. 38 authorization.
39 *) 39 *)
40 40
41 val vext_version = "0.9.97" 41 val vext_version = "0.9.98"
42 42
43 43
44 datatype vcs = 44 datatype vcs =
45 HG | 45 HG |
46 GIT | 46 GIT |
943 943
944 end 944 end
945 945
946 946
947 structure JsonBits :> sig 947 structure JsonBits :> sig
948 exception Config of string
948 val load_json_from : string -> Json.json (* filename -> json *) 949 val load_json_from : string -> Json.json (* filename -> json *)
949 val save_json_to : string -> Json.json -> unit 950 val save_json_to : string -> Json.json -> unit
950 val lookup_optional : Json.json -> string list -> Json.json option 951 val lookup_optional : Json.json -> string list -> Json.json option
951 val lookup_optional_string : Json.json -> string list -> string option 952 val lookup_optional_string : Json.json -> string list -> string option
952 val lookup_mandatory : Json.json -> string list -> Json.json 953 val lookup_mandatory : Json.json -> string list -> Json.json
953 val lookup_mandatory_string : Json.json -> string list -> string 954 val lookup_mandatory_string : Json.json -> string list -> string
954 end = struct 955 end = struct
955 956
957 exception Config of string
958
956 fun load_json_from filename = 959 fun load_json_from filename =
957 case Json.parse (FileBits.file_contents filename) of 960 case Json.parse (FileBits.file_contents filename) of
958 Json.OK json => json 961 Json.OK json => json
959 | Json.ERROR e => raise Fail ("Failed to parse file: " ^ e) 962 | Json.ERROR e => raise Config ("Failed to parse file: " ^ e)
960 963
961 fun save_json_to filename json = 964 fun save_json_to filename json =
962 (* using binary I/O to avoid ever writing CR/LF line endings *) 965 (* using binary I/O to avoid ever writing CR/LF line endings *)
963 let val jstr = Json.serialiseIndented json 966 let val jstr = Json.serialiseIndented json
964 val stream = BinIO.openOut filename 967 val stream = BinIO.openOut filename
969 972
970 fun lookup_optional json kk = 973 fun lookup_optional json kk =
971 let fun lookup key = 974 let fun lookup key =
972 case json of 975 case json of
973 Json.OBJECT kvs => 976 Json.OBJECT kvs =>
974 (case List.find (fn (k, v) => k = key) kvs of 977 (case List.filter (fn (k, v) => k = key) kvs of
975 SOME (k, v) => SOME v 978 [] => NONE
976 | NONE => NONE) 979 | [(_,v)] => SOME v
977 | _ => raise Fail "Object expected" 980 | _ => raise Config ("Duplicate key: " ^
981 (String.concatWith " -> " kk)))
982 | _ => raise Config "Object expected"
978 in 983 in
979 case kk of 984 case kk of
980 [] => NONE 985 [] => NONE
981 | key::[] => lookup key 986 | key::[] => lookup key
982 | key::kk => case lookup key of 987 | key::kk => case lookup key of
985 end 990 end
986 991
987 fun lookup_optional_string json kk = 992 fun lookup_optional_string json kk =
988 case lookup_optional json kk of 993 case lookup_optional json kk of
989 SOME (Json.STRING s) => SOME s 994 SOME (Json.STRING s) => SOME s
990 | SOME _ => raise Fail ("Value (if present) must be string: " ^ 995 | SOME _ => raise Config ("Value (if present) must be string: " ^
991 (String.concatWith " -> " kk)) 996 (String.concatWith " -> " kk))
992 | NONE => NONE 997 | NONE => NONE
993 998
994 fun lookup_mandatory json kk = 999 fun lookup_mandatory json kk =
995 case lookup_optional json kk of 1000 case lookup_optional json kk of
996 SOME v => v 1001 SOME v => v
997 | NONE => raise Fail ("Value is mandatory: " ^ 1002 | NONE => raise Config ("Value is mandatory: " ^
998 (String.concatWith " -> " kk) ^ " in json: " ^ 1003 (String.concatWith " -> " kk))
999 (Json.serialise json))
1000 1004
1001 fun lookup_mandatory_string json kk = 1005 fun lookup_mandatory_string json kk =
1002 case lookup_optional json kk of 1006 case lookup_optional json kk of
1003 SOME (Json.STRING s) => s 1007 SOME (Json.STRING s) => s
1004 | _ => raise Fail ("Value must be string: " ^ 1008 | _ => raise Config ("Value must be string: " ^
1005 (String.concatWith " -> " kk)) 1009 (String.concatWith " -> " kk))
1006 end 1010 end
1007 1011
1008 structure Provider :> sig 1012 structure Provider :> sig
1009 val load_providers : Json.json -> provider list 1013 val load_providers : Json.json -> provider list
1010 val load_more_providers : provider list -> Json.json -> provider list 1014 val load_more_providers : provider list -> Json.json -> provider list
2593 in 2597 in
2594 load_project userconfig rootpath pintype 2598 load_project userconfig rootpath pintype
2595 end 2599 end
2596 2600
2597 fun with_local_project pintype f = 2601 fun with_local_project pintype f =
2598 let val return_code = f (load_local_project pintype) 2602 let open OS.Process
2599 handle Fail msg => (print ("Error: " ^ msg); 2603 val return_code =
2600 OS.Process.failure) 2604 f (load_local_project pintype)
2601 handle e => (print ("Error: " ^ exnMessage e); 2605 handle Fail msg =>
2602 OS.Process.failure) 2606 failure before print ("Error: " ^ msg)
2607 | JsonBits.Config msg =>
2608 failure before print ("Error in configuration: " ^ msg)
2609 | e =>
2610 failure before print ("Error: " ^ exnMessage e)
2603 val _ = print "\n"; 2611 val _ = print "\n";
2604 in 2612 in
2605 return_code 2613 return_code
2606 end 2614 end
2607 2615