annotate vext.sml @ 1732:76872ffc03a3 vext

Update Vext and lock file
author Chris Cannam
date Mon, 10 Jul 2017 13:46:20 +0100
parents e4352ff029cf
children 669bd699082d
rev   line source
Chris@1706 1 (* This file is automatically generated from the individual
Chris@1706 2 source files in the Vext repository. *)
Chris@1706 3
Chris@1706 4 (*
Chris@1706 5 Vext
Chris@1706 6
Chris@1706 7 A simple manager for third-party source code dependencies
Chris@1706 8
Chris@1706 9 Copyright 2017 Chris Cannam.
Chris@1706 10
Chris@1706 11 Permission is hereby granted, free of charge, to any person
Chris@1706 12 obtaining a copy of this software and associated documentation
Chris@1706 13 files (the "Software"), to deal in the Software without
Chris@1706 14 restriction, including without limitation the rights to use, copy,
Chris@1706 15 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@1706 16 of the Software, and to permit persons to whom the Software is
Chris@1706 17 furnished to do so, subject to the following conditions:
Chris@1706 18
Chris@1706 19 The above copyright notice and this permission notice shall be
Chris@1706 20 included in all copies or substantial portions of the Software.
Chris@1706 21
Chris@1706 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@1706 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@1706 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@1706 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@1706 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@1706 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@1706 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@1706 29
Chris@1706 30 Except as contained in this notice, the names of Chris Cannam and
Chris@1706 31 Particular Programs Ltd shall not be used in advertising or
Chris@1706 32 otherwise to promote the sale, use or other dealings in this
Chris@1706 33 Software without prior written authorization.
Chris@1706 34 *)
Chris@1706 35
Chris@1724 36 val vext_version = "0.9.4"
Chris@1706 37
Chris@1706 38
Chris@1706 39 datatype vcs =
Chris@1706 40 HG |
Chris@1706 41 GIT
Chris@1706 42
Chris@1706 43 datatype source =
Chris@1721 44 URL_SOURCE of string |
Chris@1721 45 SERVICE_SOURCE of {
Chris@1706 46 service : string,
Chris@1706 47 owner : string option,
Chris@1706 48 repo : string option
Chris@1706 49 }
Chris@1706 50
Chris@1706 51 datatype pin =
Chris@1706 52 UNPINNED |
Chris@1706 53 PINNED of string
Chris@1706 54
Chris@1706 55 datatype libstate =
Chris@1706 56 ABSENT |
Chris@1706 57 CORRECT |
Chris@1706 58 SUPERSEDED |
Chris@1706 59 WRONG
Chris@1706 60
Chris@1706 61 datatype localstate =
Chris@1706 62 MODIFIED |
Chris@1706 63 UNMODIFIED
Chris@1706 64
Chris@1706 65 datatype branch =
Chris@1706 66 BRANCH of string |
Chris@1706 67 DEFAULT_BRANCH
Chris@1706 68
Chris@1706 69 (* If we can recover from an error, for example by reporting failure
Chris@1706 70 for this one thing and going on to the next thing, then the error
Chris@1706 71 should usually be returned through a result type rather than an
Chris@1706 72 exception. *)
Chris@1706 73
Chris@1706 74 datatype 'a result =
Chris@1706 75 OK of 'a |
Chris@1706 76 ERROR of string
Chris@1706 77
Chris@1706 78 type libname = string
Chris@1706 79
Chris@1706 80 type id_or_tag = string
Chris@1706 81
Chris@1706 82 type libspec = {
Chris@1706 83 libname : libname,
Chris@1706 84 vcs : vcs,
Chris@1706 85 source : source,
Chris@1706 86 branch : branch,
Chris@1706 87 pin : pin
Chris@1706 88 }
Chris@1706 89
Chris@1706 90 type lock = {
Chris@1706 91 libname : libname,
Chris@1706 92 id_or_tag : id_or_tag
Chris@1706 93 }
Chris@1706 94
Chris@1706 95 type remote_spec = {
Chris@1706 96 anon : string option,
Chris@1706 97 auth : string option
Chris@1706 98 }
Chris@1706 99
Chris@1706 100 type provider = {
Chris@1706 101 service : string,
Chris@1706 102 supports : vcs list,
Chris@1706 103 remote_spec : remote_spec
Chris@1706 104 }
Chris@1706 105
Chris@1706 106 type account = {
Chris@1706 107 service : string,
Chris@1706 108 login : string
Chris@1706 109 }
Chris@1706 110
Chris@1706 111 type context = {
Chris@1706 112 rootpath : string,
Chris@1706 113 extdir : string,
Chris@1706 114 providers : provider list,
Chris@1706 115 accounts : account list
Chris@1706 116 }
Chris@1706 117
Chris@1706 118 type userconfig = {
Chris@1706 119 providers : provider list,
Chris@1706 120 accounts : account list
Chris@1706 121 }
Chris@1706 122
Chris@1706 123 type project = {
Chris@1706 124 context : context,
Chris@1706 125 libs : libspec list
Chris@1706 126 }
Chris@1706 127
Chris@1706 128 structure VextFilenames = struct
Chris@1706 129 val project_file = "vext-project.json"
Chris@1706 130 val project_lock_file = "vext-lock.json"
Chris@1706 131 val user_config_file = ".vext.json"
Chris@1706 132 end
Chris@1706 133
Chris@1706 134 signature VCS_CONTROL = sig
Chris@1706 135
Chris@1706 136 (** Test whether the library is present locally at all *)
Chris@1706 137 val exists : context -> libname -> bool result
Chris@1706 138
Chris@1706 139 (** Return the id (hash) of the current revision for the library *)
Chris@1706 140 val id_of : context -> libname -> id_or_tag result
Chris@1706 141
Chris@1706 142 (** Test whether the library is at the given id *)
Chris@1706 143 val is_at : context -> libname * id_or_tag -> bool result
Chris@1706 144
Chris@1706 145 (** Test whether the library is on the given branch, i.e. is at
Chris@1706 146 the branch tip or an ancestor of it *)
Chris@1706 147 val is_on_branch : context -> libname * branch -> bool result
Chris@1706 148
Chris@1706 149 (** Test whether the library is at the newest revision for the
Chris@1706 150 given branch. False may indicate that the branch has advanced
Chris@1706 151 or that the library is not on the branch at all. This function
Chris@1706 152 may use the network to check for new revisions *)
Chris@1706 153 val is_newest : context -> libname * branch -> bool result
Chris@1706 154
Chris@1706 155 (** Test whether the library is at the newest revision available
Chris@1706 156 locally for the given branch. False may indicate that the
Chris@1706 157 branch has advanced or that the library is not on the branch
Chris@1706 158 at all. This function must not use the network *)
Chris@1706 159 val is_newest_locally : context -> libname * branch -> bool result
Chris@1706 160
Chris@1706 161 (** Test whether the library has been modified in the local
Chris@1706 162 working copy *)
Chris@1706 163 val is_modified_locally : context -> libname -> bool result
Chris@1706 164
Chris@1706 165 (** Check out, i.e. clone a fresh copy of, the repo for the given
Chris@1706 166 library on the given branch *)
Chris@1706 167 val checkout : context -> libname * source * branch -> unit result
Chris@1706 168
Chris@1706 169 (** Update the library to the given branch tip *)
Chris@1706 170 val update : context -> libname * branch -> id_or_tag result
Chris@1706 171
Chris@1706 172 (** Update the library to the given specific id or tag *)
Chris@1706 173 val update_to : context -> libname * id_or_tag -> id_or_tag result
Chris@1706 174 end
Chris@1706 175
Chris@1706 176 signature LIB_CONTROL = sig
Chris@1706 177 val review : context -> libspec -> (libstate * localstate) result
Chris@1706 178 val status : context -> libspec -> (libstate * localstate) result
Chris@1706 179 val update : context -> libspec -> id_or_tag result
Chris@1706 180 end
Chris@1706 181
Chris@1706 182 structure FileBits :> sig
Chris@1706 183 val extpath : context -> string
Chris@1706 184 val libpath : context -> libname -> string
Chris@1706 185 val subpath : context -> libname -> string -> string
Chris@1706 186 val command_output : context -> libname -> string list -> string result
Chris@1706 187 val command : context -> libname -> string list -> unit result
Chris@1706 188 val file_contents : string -> string
Chris@1706 189 val mydir : unit -> string
Chris@1706 190 val homedir : unit -> string
Chris@1706 191 val mkpath : string -> unit result
Chris@1706 192 val project_spec_path : string -> string
Chris@1706 193 val project_lock_path : string -> string
Chris@1706 194 val verbose : unit -> bool
Chris@1706 195 end = struct
Chris@1706 196
Chris@1706 197 fun verbose () =
Chris@1706 198 case OS.Process.getEnv "VEXT_VERBOSE" of
Chris@1706 199 SOME "0" => false
Chris@1706 200 | SOME _ => true
Chris@1706 201 | NONE => false
Chris@1706 202
Chris@1706 203 fun extpath ({ rootpath, extdir, ... } : context) =
Chris@1706 204 let val { isAbs, vol, arcs } = OS.Path.fromString rootpath
Chris@1706 205 in OS.Path.toString {
Chris@1706 206 isAbs = isAbs,
Chris@1706 207 vol = vol,
Chris@1706 208 arcs = arcs @ [ extdir ]
Chris@1706 209 }
Chris@1706 210 end
Chris@1706 211
Chris@1706 212 fun subpath ({ rootpath, extdir, ... } : context) libname remainder =
Chris@1706 213 (* NB libname is allowed to be a path fragment, e.g. foo/bar *)
Chris@1706 214 let val { isAbs, vol, arcs } = OS.Path.fromString rootpath
Chris@1706 215 val split = String.fields (fn c => c = #"/")
Chris@1706 216 in OS.Path.toString {
Chris@1706 217 isAbs = isAbs,
Chris@1706 218 vol = vol,
Chris@1706 219 arcs = arcs @ [ extdir ] @ split libname @ split remainder
Chris@1706 220 }
Chris@1706 221 end
Chris@1706 222
Chris@1706 223 fun libpath context "" =
Chris@1706 224 extpath context
Chris@1706 225 | libpath context libname =
Chris@1706 226 subpath context libname ""
Chris@1706 227
Chris@1706 228 fun project_file_path rootpath filename =
Chris@1706 229 let val { isAbs, vol, arcs } = OS.Path.fromString rootpath
Chris@1706 230 in OS.Path.toString {
Chris@1706 231 isAbs = isAbs,
Chris@1706 232 vol = vol,
Chris@1706 233 arcs = arcs @ [ filename ]
Chris@1706 234 }
Chris@1706 235 end
Chris@1706 236
Chris@1706 237 fun project_spec_path rootpath =
Chris@1706 238 project_file_path rootpath (VextFilenames.project_file)
Chris@1706 239
Chris@1706 240 fun project_lock_path rootpath =
Chris@1706 241 project_file_path rootpath (VextFilenames.project_lock_file)
Chris@1706 242
Chris@1706 243 fun trim str =
Chris@1706 244 hd (String.fields (fn x => x = #"\n" orelse x = #"\r") str)
Chris@1706 245
Chris@1706 246 fun file_contents filename =
Chris@1706 247 let val stream = TextIO.openIn filename
Chris@1706 248 fun read_all str acc =
Chris@1706 249 case TextIO.inputLine str of
Chris@1706 250 SOME line => read_all str (trim line :: acc)
Chris@1706 251 | NONE => rev acc
Chris@1706 252 val contents = read_all stream []
Chris@1706 253 val _ = TextIO.closeIn stream
Chris@1706 254 in
Chris@1706 255 String.concatWith "\n" contents
Chris@1706 256 end
Chris@1706 257
Chris@1706 258 fun expand_commandline cmdlist =
Chris@1706 259 (* We are quite [too] strict about what we accept here, except
Chris@1706 260 for the first element in cmdlist which is assumed to be a
Chris@1706 261 known command location rather than arbitrary user input. NB
Chris@1706 262 only ASCII accepted at this point. *)
Chris@1706 263 let open Char
Chris@1706 264 fun quote arg =
Chris@1706 265 if List.all
Chris@1706 266 (fn c => isAlphaNum c orelse c = #"-" orelse c = #"_")
Chris@1706 267 (explode arg)
Chris@1706 268 then arg
Chris@1706 269 else "\"" ^ arg ^ "\""
Chris@1706 270 fun check arg =
Chris@1706 271 let val valid = explode " /#:;?,._-{}@="
Chris@1706 272 in
Chris@1706 273 app (fn c =>
Chris@1706 274 if isAlphaNum c orelse
Chris@1706 275 List.exists (fn v => v = c) valid
Chris@1706 276 then ()
Chris@1706 277 else raise Fail ("Invalid character '" ^
Chris@1706 278 (Char.toString c) ^
Chris@1706 279 "' in command list"))
Chris@1706 280 (explode arg);
Chris@1706 281 arg
Chris@1706 282 end
Chris@1706 283 in
Chris@1706 284 String.concatWith " "
Chris@1706 285 (map quote
Chris@1706 286 (hd cmdlist :: map check (tl cmdlist)))
Chris@1706 287 end
Chris@1706 288
Chris@1706 289 val tick_cycle = ref 0
Chris@1706 290 val tick_chars = Vector.fromList (map String.str (explode "|/-\\"))
Chris@1706 291
Chris@1716 292 fun tick libname cmdlist =
Chris@1706 293 let val n = Vector.length tick_chars
Chris@1706 294 fun pad_to n str =
Chris@1716 295 if n <= String.size str then str
Chris@1716 296 else pad_to n (str ^ " ")
Chris@1716 297 val name = if libname <> "" then libname
Chris@1716 298 else if cmdlist = nil then ""
Chris@1716 299 else hd (rev cmdlist)
Chris@1706 300 in
Chris@1716 301 print (" " ^
Chris@1706 302 Vector.sub(tick_chars, !tick_cycle) ^ " " ^
Chris@1716 303 pad_to 24 name ^
Chris@1716 304 "\r");
Chris@1706 305 tick_cycle := (if !tick_cycle = n - 1 then 0 else 1 + !tick_cycle)
Chris@1706 306 end
Chris@1706 307
Chris@1706 308 fun run_command context libname cmdlist redirect =
Chris@1706 309 let open OS
Chris@1706 310 val dir = libpath context libname
Chris@1706 311 val cmd = expand_commandline cmdlist
Chris@1706 312 val _ = if verbose ()
Chris@1706 313 then print ("Running: " ^ cmd ^
Chris@1706 314 " (in dir " ^ dir ^ ")...\n")
Chris@1716 315 else tick libname cmdlist
Chris@1706 316 val _ = FileSys.chDir dir
Chris@1706 317 val status = case redirect of
Chris@1706 318 NONE => Process.system cmd
Chris@1706 319 | SOME file => Process.system (cmd ^ ">" ^ file)
Chris@1706 320 in
Chris@1706 321 if Process.isSuccess status
Chris@1706 322 then OK ()
Chris@1706 323 else ERROR ("Command failed: " ^ cmd ^ " (in dir " ^ dir ^ ")")
Chris@1706 324 end
Chris@1706 325 handle ex => ERROR ("Unable to run command: " ^ exnMessage ex)
Chris@1706 326
Chris@1706 327 fun command context libname cmdlist =
Chris@1706 328 run_command context libname cmdlist NONE
Chris@1706 329
Chris@1706 330 fun command_output context libname cmdlist =
Chris@1706 331 let open OS
Chris@1706 332 val tmpFile = FileSys.tmpName ()
Chris@1706 333 val result = run_command context libname cmdlist (SOME tmpFile)
Chris@1706 334 val contents = file_contents tmpFile
Chris@1706 335 in
Chris@1706 336 FileSys.remove tmpFile handle _ => ();
Chris@1706 337 case result of
Chris@1706 338 OK () => OK contents
Chris@1706 339 | ERROR e => ERROR e
Chris@1706 340 end
Chris@1706 341
Chris@1706 342 fun mydir () =
Chris@1706 343 let open OS
Chris@1706 344 val { dir, file } = Path.splitDirFile (CommandLine.name ())
Chris@1706 345 in
Chris@1706 346 FileSys.realPath
Chris@1706 347 (if Path.isAbsolute dir
Chris@1706 348 then dir
Chris@1706 349 else Path.concat (FileSys.getDir (), dir))
Chris@1706 350 end
Chris@1706 351
Chris@1706 352 fun homedir () =
Chris@1706 353 (* Failure is not routine, so we use an exception here *)
Chris@1706 354 case (OS.Process.getEnv "HOME",
Chris@1706 355 OS.Process.getEnv "HOMEPATH") of
Chris@1706 356 (SOME home, _) => home
Chris@1706 357 | (NONE, SOME home) => home
Chris@1706 358 | (NONE, NONE) =>
Chris@1706 359 raise Fail "Failed to look up home directory from environment"
Chris@1706 360
Chris@1706 361 fun mkpath path =
Chris@1706 362 if OS.FileSys.isDir path handle _ => false
Chris@1706 363 then OK ()
Chris@1706 364 else case OS.Path.fromString path of
Chris@1706 365 { arcs = nil, ... } => OK ()
Chris@1706 366 | { isAbs = false, ... } => ERROR "mkpath requires absolute path"
Chris@1706 367 | { isAbs, vol, arcs } =>
Chris@1706 368 case mkpath (OS.Path.toString { (* parent *)
Chris@1706 369 isAbs = isAbs,
Chris@1706 370 vol = vol,
Chris@1706 371 arcs = rev (tl (rev arcs)) }) of
Chris@1706 372 ERROR e => ERROR e
Chris@1706 373 | OK () => ((OS.FileSys.mkDir path; OK ())
Chris@1706 374 handle OS.SysErr (e, _) =>
Chris@1706 375 ERROR ("Directory creation failed: " ^ e))
Chris@1706 376 end
Chris@1706 377
Chris@1706 378 functor LibControlFn (V: VCS_CONTROL) :> LIB_CONTROL = struct
Chris@1706 379
Chris@1706 380 (* Valid states for unpinned libraries:
Chris@1706 381
Chris@1706 382 - CORRECT: We are on the right branch and are up-to-date with
Chris@1706 383 it as far as we can tell. (If not using the network, this
Chris@1706 384 should be reported to user as "Present" rather than "Correct"
Chris@1706 385 as the remote repo may have advanced without us knowing.)
Chris@1706 386
Chris@1706 387 - SUPERSEDED: We are on the right branch but we can see that
Chris@1706 388 there is a newer revision either locally or on the remote (in
Chris@1706 389 Git terms, we are at an ancestor of the desired branch tip).
Chris@1706 390
Chris@1706 391 - WRONG: We are on the wrong branch (in Git terms, we are not
Chris@1706 392 at the desired branch tip or any ancestor of it).
Chris@1706 393
Chris@1706 394 - ABSENT: Repo doesn't exist here at all.
Chris@1706 395
Chris@1706 396 Valid states for pinned libraries:
Chris@1706 397
Chris@1706 398 - CORRECT: We are at the pinned revision.
Chris@1706 399
Chris@1706 400 - WRONG: We are at any revision other than the pinned one.
Chris@1706 401
Chris@1706 402 - ABSENT: Repo doesn't exist here at all.
Chris@1706 403 *)
Chris@1706 404
Chris@1706 405 fun check with_network context ({ libname, branch, pin, ... } : libspec) =
Chris@1706 406 let fun check_unpinned () =
Chris@1706 407 let val is_newest = if with_network
Chris@1706 408 then V.is_newest
Chris@1706 409 else V.is_newest_locally
Chris@1706 410 in
Chris@1706 411 case is_newest context (libname, branch) of
Chris@1706 412 ERROR e => ERROR e
Chris@1706 413 | OK true => OK CORRECT
Chris@1706 414 | OK false =>
Chris@1706 415 case V.is_on_branch context (libname, branch) of
Chris@1706 416 ERROR e => ERROR e
Chris@1706 417 | OK true => OK SUPERSEDED
Chris@1706 418 | OK false => OK WRONG
Chris@1706 419 end
Chris@1706 420 fun check_pinned target =
Chris@1706 421 case V.is_at context (libname, target) of
Chris@1706 422 ERROR e => ERROR e
Chris@1706 423 | OK true => OK CORRECT
Chris@1706 424 | OK false => OK WRONG
Chris@1706 425 fun check' () =
Chris@1706 426 case pin of
Chris@1706 427 UNPINNED => check_unpinned ()
Chris@1706 428 | PINNED target => check_pinned target
Chris@1706 429 in
Chris@1706 430 case V.exists context libname of
Chris@1706 431 ERROR e => ERROR e
Chris@1706 432 | OK false => OK (ABSENT, UNMODIFIED)
Chris@1706 433 | OK true =>
Chris@1706 434 case (check' (), V.is_modified_locally context libname) of
Chris@1706 435 (ERROR e, _) => ERROR e
Chris@1706 436 | (_, ERROR e) => ERROR e
Chris@1706 437 | (OK state, OK true) => OK (state, MODIFIED)
Chris@1706 438 | (OK state, OK false) => OK (state, UNMODIFIED)
Chris@1706 439 end
Chris@1706 440
Chris@1706 441 val review = check true
Chris@1706 442 val status = check false
Chris@1706 443
Chris@1706 444 fun update context ({ libname, source, branch, pin, ... } : libspec) =
Chris@1706 445 let fun update_unpinned () =
Chris@1706 446 case V.is_newest context (libname, branch) of
Chris@1706 447 ERROR e => ERROR e
Chris@1706 448 | OK true => V.id_of context libname
Chris@1706 449 | OK false => V.update context (libname, branch)
Chris@1706 450 fun update_pinned target =
Chris@1706 451 case V.is_at context (libname, target) of
Chris@1706 452 ERROR e => ERROR e
Chris@1706 453 | OK true => OK target
Chris@1706 454 | OK false => V.update_to context (libname, target)
Chris@1706 455 fun update' () =
Chris@1706 456 case pin of
Chris@1706 457 UNPINNED => update_unpinned ()
Chris@1706 458 | PINNED target => update_pinned target
Chris@1706 459 in
Chris@1706 460 case V.exists context libname of
Chris@1706 461 ERROR e => ERROR e
Chris@1706 462 | OK true => update' ()
Chris@1706 463 | OK false =>
Chris@1706 464 case V.checkout context (libname, source, branch) of
Chris@1706 465 ERROR e => ERROR e
Chris@1706 466 | OK () => update' ()
Chris@1706 467 end
Chris@1706 468 end
Chris@1706 469
Chris@1706 470 (* Simple Standard ML JSON parser
Chris@1706 471 ==============================
Chris@1706 472
Chris@1706 473 https://bitbucket.org/cannam/sml-simplejson
Chris@1706 474
Chris@1706 475 An RFC-compliant JSON parser in one SML file with no dependency
Chris@1706 476 on anything outside the Basis library. Also includes a simple
Chris@1706 477 serialiser.
Chris@1706 478
Chris@1706 479 Tested with MLton, Poly/ML, and SML/NJ compilers.
Chris@1706 480
Chris@1706 481 Parser notes:
Chris@1706 482
Chris@1706 483 * Complies with RFC 7159, The JavaScript Object Notation (JSON)
Chris@1706 484 Data Interchange Format
Chris@1706 485
Chris@1706 486 * Passes all of the JSONTestSuite parser accept/reject tests that
Chris@1706 487 exist at the time of writing, as listed in "Parsing JSON is a
Chris@1706 488 Minefield" (http://seriot.ch/parsing_json.php)
Chris@1706 489
Chris@1706 490 * Two-pass parser using naive exploded strings, therefore not
Chris@1706 491 particularly fast and not suitable for large input files
Chris@1706 492
Chris@1706 493 * Only supports UTF-8 input, not UTF-16 or UTF-32. Doesn't check
Chris@1706 494 that JSON strings are valid UTF-8 -- the caller must do that --
Chris@1706 495 but does handle \u escapes
Chris@1706 496
Chris@1706 497 * Converts all numbers to type "real". If that is a 64-bit IEEE
Chris@1706 498 float type (common but not guaranteed in SML) then we're pretty
Chris@1706 499 standard for a JSON parser
Chris@1706 500
Chris@1706 501 Copyright 2017 Chris Cannam.
Chris@1706 502 Parts based on the JSON parser in the Ponyo library by Phil Eaton.
Chris@1706 503
Chris@1706 504 Permission is hereby granted, free of charge, to any person
Chris@1706 505 obtaining a copy of this software and associated documentation
Chris@1706 506 files (the "Software"), to deal in the Software without
Chris@1706 507 restriction, including without limitation the rights to use, copy,
Chris@1706 508 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@1706 509 of the Software, and to permit persons to whom the Software is
Chris@1706 510 furnished to do so, subject to the following conditions:
Chris@1706 511
Chris@1706 512 The above copyright notice and this permission notice shall be
Chris@1706 513 included in all copies or substantial portions of the Software.
Chris@1706 514
Chris@1706 515 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@1706 516 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@1706 517 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@1706 518 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@1706 519 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@1706 520 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@1706 521 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@1706 522
Chris@1706 523 Except as contained in this notice, the names of Chris Cannam and
Chris@1706 524 Particular Programs Ltd shall not be used in advertising or
Chris@1706 525 otherwise to promote the sale, use or other dealings in this
Chris@1706 526 Software without prior written authorization.
Chris@1706 527 *)
Chris@1706 528
Chris@1706 529 signature JSON = sig
Chris@1706 530
Chris@1706 531 datatype json = OBJECT of (string * json) list
Chris@1706 532 | ARRAY of json list
Chris@1706 533 | NUMBER of real
Chris@1706 534 | STRING of string
Chris@1706 535 | BOOL of bool
Chris@1706 536 | NULL
Chris@1706 537
Chris@1706 538 datatype 'a result = OK of 'a
Chris@1706 539 | ERROR of string
Chris@1706 540
Chris@1706 541 val parse : string -> json result
Chris@1706 542 val serialise : json -> string
Chris@1706 543 val serialiseIndented : json -> string
Chris@1706 544
Chris@1706 545 end
Chris@1706 546
Chris@1706 547 structure Json :> JSON = struct
Chris@1706 548
Chris@1706 549 datatype json = OBJECT of (string * json) list
Chris@1706 550 | ARRAY of json list
Chris@1706 551 | NUMBER of real
Chris@1706 552 | STRING of string
Chris@1706 553 | BOOL of bool
Chris@1706 554 | NULL
Chris@1706 555
Chris@1706 556 datatype 'a result = OK of 'a
Chris@1706 557 | ERROR of string
Chris@1706 558
Chris@1706 559 structure T = struct
Chris@1706 560 datatype token = NUMBER of char list
Chris@1706 561 | STRING of string
Chris@1706 562 | BOOL of bool
Chris@1706 563 | NULL
Chris@1706 564 | CURLY_L
Chris@1706 565 | CURLY_R
Chris@1706 566 | SQUARE_L
Chris@1706 567 | SQUARE_R
Chris@1706 568 | COLON
Chris@1706 569 | COMMA
Chris@1706 570
Chris@1706 571 fun toString t =
Chris@1706 572 case t of NUMBER digits => implode digits
Chris@1706 573 | STRING s => s
Chris@1706 574 | BOOL b => Bool.toString b
Chris@1706 575 | NULL => "null"
Chris@1706 576 | CURLY_L => "{"
Chris@1706 577 | CURLY_R => "}"
Chris@1706 578 | SQUARE_L => "["
Chris@1706 579 | SQUARE_R => "]"
Chris@1706 580 | COLON => ":"
Chris@1706 581 | COMMA => ","
Chris@1706 582 end
Chris@1706 583
Chris@1706 584 fun bmpToUtf8 cp = (* convert a codepoint in Unicode BMP to utf8 bytes *)
Chris@1706 585 let open Word
Chris@1706 586 infix 6 orb andb >>
Chris@1706 587 in
Chris@1706 588 map (Char.chr o toInt)
Chris@1706 589 (if cp < 0wx80 then
Chris@1706 590 [cp]
Chris@1706 591 else if cp < 0wx800 then
Chris@1706 592 [0wxc0 orb (cp >> 0w6), 0wx80 orb (cp andb 0wx3f)]
Chris@1706 593 else if cp < 0wx10000 then
Chris@1706 594 [0wxe0 orb (cp >> 0w12),
Chris@1706 595 0wx80 orb ((cp >> 0w6) andb 0wx3f),
Chris@1706 596 0wx80 orb (cp andb 0wx3f)]
Chris@1706 597 else raise Fail ("Invalid BMP point " ^ (Word.toString cp)))
Chris@1706 598 end
Chris@1706 599
Chris@1706 600 fun error pos text = ERROR (text ^ " at character position " ^
Chris@1706 601 Int.toString (pos - 1))
Chris@1706 602 fun token_error pos = error pos ("Unexpected token")
Chris@1706 603
Chris@1706 604 fun lexNull pos acc (#"u" :: #"l" :: #"l" :: xs) =
Chris@1706 605 lex (pos + 3) (T.NULL :: acc) xs
Chris@1706 606 | lexNull pos acc _ = token_error pos
Chris@1706 607
Chris@1706 608 and lexTrue pos acc (#"r" :: #"u" :: #"e" :: xs) =
Chris@1706 609 lex (pos + 3) (T.BOOL true :: acc) xs
Chris@1706 610 | lexTrue pos acc _ = token_error pos
Chris@1706 611
Chris@1706 612 and lexFalse pos acc (#"a" :: #"l" :: #"s" :: #"e" :: xs) =
Chris@1706 613 lex (pos + 4) (T.BOOL false :: acc) xs
Chris@1706 614 | lexFalse pos acc _ = token_error pos
Chris@1706 615
Chris@1706 616 and lexChar tok pos acc xs =
Chris@1706 617 lex pos (tok :: acc) xs
Chris@1706 618
Chris@1706 619 and lexString pos acc cc =
Chris@1706 620 let datatype escaped = ESCAPED | NORMAL
Chris@1706 621 fun lexString' pos text ESCAPED [] =
Chris@1706 622 error pos "End of input during escape sequence"
Chris@1706 623 | lexString' pos text NORMAL [] =
Chris@1706 624 error pos "End of input during string"
Chris@1706 625 | lexString' pos text ESCAPED (x :: xs) =
Chris@1706 626 let fun esc c = lexString' (pos + 1) (c :: text) NORMAL xs
Chris@1706 627 in case x of
Chris@1706 628 #"\"" => esc x
Chris@1706 629 | #"\\" => esc x
Chris@1706 630 | #"/" => esc x
Chris@1706 631 | #"b" => esc #"\b"
Chris@1706 632 | #"f" => esc #"\f"
Chris@1706 633 | #"n" => esc #"\n"
Chris@1706 634 | #"r" => esc #"\r"
Chris@1706 635 | #"t" => esc #"\t"
Chris@1706 636 | _ => error pos ("Invalid escape \\" ^
Chris@1706 637 Char.toString x)
Chris@1706 638 end
Chris@1706 639 | lexString' pos text NORMAL (#"\\" :: #"u" ::a::b::c::d:: xs) =
Chris@1706 640 if List.all Char.isHexDigit [a,b,c,d]
Chris@1706 641 then case Word.fromString ("0wx" ^ (implode [a,b,c,d])) of
Chris@1706 642 SOME w => (let val utf = rev (bmpToUtf8 w) in
Chris@1706 643 lexString' (pos + 6) (utf @ text)
Chris@1706 644 NORMAL xs
Chris@1706 645 end
Chris@1706 646 handle Fail err => error pos err)
Chris@1706 647 | NONE => error pos "Invalid Unicode BMP escape sequence"
Chris@1706 648 else error pos "Invalid Unicode BMP escape sequence"
Chris@1706 649 | lexString' pos text NORMAL (x :: xs) =
Chris@1706 650 if Char.ord x < 0x20
Chris@1706 651 then error pos "Invalid unescaped control character"
Chris@1706 652 else
Chris@1706 653 case x of
Chris@1706 654 #"\"" => OK (rev text, xs, pos + 1)
Chris@1706 655 | #"\\" => lexString' (pos + 1) text ESCAPED xs
Chris@1706 656 | _ => lexString' (pos + 1) (x :: text) NORMAL xs
Chris@1706 657 in
Chris@1706 658 case lexString' pos [] NORMAL cc of
Chris@1706 659 OK (text, rest, newpos) =>
Chris@1706 660 lex newpos (T.STRING (implode text) :: acc) rest
Chris@1706 661 | ERROR e => ERROR e
Chris@1706 662 end
Chris@1706 663
Chris@1706 664 and lexNumber firstChar pos acc cc =
Chris@1706 665 let val valid = explode ".+-e"
Chris@1706 666 fun lexNumber' pos digits [] = (rev digits, [], pos)
Chris@1706 667 | lexNumber' pos digits (x :: xs) =
Chris@1706 668 if x = #"E" then lexNumber' (pos + 1) (#"e" :: digits) xs
Chris@1706 669 else if Char.isDigit x orelse List.exists (fn c => x = c) valid
Chris@1706 670 then lexNumber' (pos + 1) (x :: digits) xs
Chris@1706 671 else (rev digits, x :: xs, pos)
Chris@1706 672 val (digits, rest, newpos) =
Chris@1706 673 lexNumber' (pos - 1) [] (firstChar :: cc)
Chris@1706 674 in
Chris@1706 675 case digits of
Chris@1706 676 [] => token_error pos
Chris@1706 677 | _ => lex newpos (T.NUMBER digits :: acc) rest
Chris@1706 678 end
Chris@1706 679
Chris@1706 680 and lex pos acc [] = OK (rev acc)
Chris@1706 681 | lex pos acc (x::xs) =
Chris@1706 682 (case x of
Chris@1706 683 #" " => lex
Chris@1706 684 | #"\t" => lex
Chris@1706 685 | #"\n" => lex
Chris@1706 686 | #"\r" => lex
Chris@1706 687 | #"{" => lexChar T.CURLY_L
Chris@1706 688 | #"}" => lexChar T.CURLY_R
Chris@1706 689 | #"[" => lexChar T.SQUARE_L
Chris@1706 690 | #"]" => lexChar T.SQUARE_R
Chris@1706 691 | #":" => lexChar T.COLON
Chris@1706 692 | #"," => lexChar T.COMMA
Chris@1706 693 | #"\"" => lexString
Chris@1706 694 | #"t" => lexTrue
Chris@1706 695 | #"f" => lexFalse
Chris@1706 696 | #"n" => lexNull
Chris@1706 697 | x => lexNumber x) (pos + 1) acc xs
Chris@1706 698
Chris@1706 699 fun show [] = "end of input"
Chris@1706 700 | show (tok :: _) = T.toString tok
Chris@1706 701
Chris@1706 702 fun parseNumber digits =
Chris@1706 703 (* Note lexNumber already case-insensitised the E for us *)
Chris@1706 704 let open Char
Chris@1706 705
Chris@1706 706 fun okExpDigits [] = false
Chris@1706 707 | okExpDigits (c :: []) = isDigit c
Chris@1706 708 | okExpDigits (c :: cs) = isDigit c andalso okExpDigits cs
Chris@1706 709
Chris@1706 710 fun okExponent [] = false
Chris@1706 711 | okExponent (#"+" :: cs) = okExpDigits cs
Chris@1706 712 | okExponent (#"-" :: cs) = okExpDigits cs
Chris@1706 713 | okExponent cc = okExpDigits cc
Chris@1706 714
Chris@1706 715 fun okFracTrailing [] = true
Chris@1706 716 | okFracTrailing (c :: cs) =
Chris@1706 717 (isDigit c andalso okFracTrailing cs) orelse
Chris@1706 718 (c = #"e" andalso okExponent cs)
Chris@1706 719
Chris@1706 720 fun okFraction [] = false
Chris@1706 721 | okFraction (c :: cs) =
Chris@1706 722 isDigit c andalso okFracTrailing cs
Chris@1706 723
Chris@1706 724 fun okPosTrailing [] = true
Chris@1706 725 | okPosTrailing (#"." :: cs) = okFraction cs
Chris@1706 726 | okPosTrailing (#"e" :: cs) = okExponent cs
Chris@1706 727 | okPosTrailing (c :: cs) =
Chris@1706 728 isDigit c andalso okPosTrailing cs
Chris@1706 729
Chris@1706 730 fun okPositive [] = false
Chris@1706 731 | okPositive (#"0" :: []) = true
Chris@1706 732 | okPositive (#"0" :: #"." :: cs) = okFraction cs
Chris@1706 733 | okPositive (#"0" :: #"e" :: cs) = okExponent cs
Chris@1706 734 | okPositive (#"0" :: cs) = false
Chris@1706 735 | okPositive (c :: cs) = isDigit c andalso okPosTrailing cs
Chris@1706 736
Chris@1706 737 fun okNumber (#"-" :: cs) = okPositive cs
Chris@1706 738 | okNumber cc = okPositive cc
Chris@1706 739 in
Chris@1706 740 if okNumber digits
Chris@1706 741 then case Real.fromString (implode digits) of
Chris@1706 742 NONE => ERROR "Number out of range"
Chris@1706 743 | SOME r => OK r
Chris@1706 744 else ERROR ("Invalid number \"" ^ (implode digits) ^ "\"")
Chris@1706 745 end
Chris@1706 746
Chris@1706 747 fun parseObject (T.CURLY_R :: xs) = OK (OBJECT [], xs)
Chris@1706 748 | parseObject tokens =
Chris@1706 749 let fun parsePair (T.STRING key :: T.COLON :: xs) =
Chris@1706 750 (case parseTokens xs of
Chris@1706 751 ERROR e => ERROR e
Chris@1706 752 | OK (j, xs) => OK ((key, j), xs))
Chris@1706 753 | parsePair other =
Chris@1706 754 ERROR ("Object key/value pair expected around \"" ^
Chris@1706 755 show other ^ "\"")
Chris@1706 756 fun parseObject' acc [] = ERROR "End of input during object"
Chris@1706 757 | parseObject' acc tokens =
Chris@1706 758 case parsePair tokens of
Chris@1706 759 ERROR e => ERROR e
Chris@1706 760 | OK (pair, T.COMMA :: xs) =>
Chris@1706 761 parseObject' (pair :: acc) xs
Chris@1706 762 | OK (pair, T.CURLY_R :: xs) =>
Chris@1706 763 OK (OBJECT (rev (pair :: acc)), xs)
Chris@1706 764 | OK (_, _) => ERROR "Expected , or } after object element"
Chris@1706 765 in
Chris@1706 766 parseObject' [] tokens
Chris@1706 767 end
Chris@1706 768
Chris@1706 769 and parseArray (T.SQUARE_R :: xs) = OK (ARRAY [], xs)
Chris@1706 770 | parseArray tokens =
Chris@1706 771 let fun parseArray' acc [] = ERROR "End of input during array"
Chris@1706 772 | parseArray' acc tokens =
Chris@1706 773 case parseTokens tokens of
Chris@1706 774 ERROR e => ERROR e
Chris@1706 775 | OK (j, T.COMMA :: xs) => parseArray' (j :: acc) xs
Chris@1706 776 | OK (j, T.SQUARE_R :: xs) => OK (ARRAY (rev (j :: acc)), xs)
Chris@1706 777 | OK (_, _) => ERROR "Expected , or ] after array element"
Chris@1706 778 in
Chris@1706 779 parseArray' [] tokens
Chris@1706 780 end
Chris@1706 781
Chris@1706 782 and parseTokens [] = ERROR "Value expected"
Chris@1706 783 | parseTokens (tok :: xs) =
Chris@1706 784 (case tok of
Chris@1706 785 T.NUMBER d => (case parseNumber d of
Chris@1706 786 OK r => OK (NUMBER r, xs)
Chris@1706 787 | ERROR e => ERROR e)
Chris@1706 788 | T.STRING s => OK (STRING s, xs)
Chris@1706 789 | T.BOOL b => OK (BOOL b, xs)
Chris@1706 790 | T.NULL => OK (NULL, xs)
Chris@1706 791 | T.CURLY_L => parseObject xs
Chris@1706 792 | T.SQUARE_L => parseArray xs
Chris@1706 793 | _ => ERROR ("Unexpected token " ^ T.toString tok ^
Chris@1706 794 " before " ^ show xs))
Chris@1706 795
Chris@1706 796 fun parse str =
Chris@1706 797 case lex 1 [] (explode str) of
Chris@1706 798 ERROR e => ERROR e
Chris@1706 799 | OK tokens => case parseTokens tokens of
Chris@1706 800 OK (value, []) => OK value
Chris@1706 801 | OK (_, _) => ERROR "Extra data after input"
Chris@1706 802 | ERROR e => ERROR e
Chris@1706 803
Chris@1706 804 fun stringEscape s =
Chris@1706 805 let fun esc x = [x, #"\\"]
Chris@1706 806 fun escape' acc [] = rev acc
Chris@1706 807 | escape' acc (x :: xs) =
Chris@1706 808 escape' (case x of
Chris@1706 809 #"\"" => esc x @ acc
Chris@1706 810 | #"\\" => esc x @ acc
Chris@1706 811 | #"\b" => esc #"b" @ acc
Chris@1706 812 | #"\f" => esc #"f" @ acc
Chris@1706 813 | #"\n" => esc #"n" @ acc
Chris@1706 814 | #"\r" => esc #"r" @ acc
Chris@1706 815 | #"\t" => esc #"t" @ acc
Chris@1706 816 | _ =>
Chris@1706 817 let val c = Char.ord x
Chris@1706 818 in
Chris@1706 819 if c < 0x20
Chris@1706 820 then let val hex = Word.toString (Word.fromInt c)
Chris@1706 821 in (rev o explode) (if c < 0x10
Chris@1706 822 then ("\\u000" ^ hex)
Chris@1706 823 else ("\\u00" ^ hex))
Chris@1706 824 end @ acc
Chris@1706 825 else
Chris@1706 826 x :: acc
Chris@1706 827 end)
Chris@1706 828 xs
Chris@1706 829 in
Chris@1706 830 implode (escape' [] (explode s))
Chris@1706 831 end
Chris@1706 832
Chris@1706 833 fun serialise json =
Chris@1706 834 case json of
Chris@1706 835 OBJECT pp => "{" ^ String.concatWith
Chris@1706 836 "," (map (fn (key, value) =>
Chris@1706 837 serialise (STRING key) ^ ":" ^
Chris@1706 838 serialise value) pp) ^
Chris@1706 839 "}"
Chris@1706 840 | ARRAY arr => "[" ^ String.concatWith "," (map serialise arr) ^ "]"
Chris@1706 841 | NUMBER n => implode (map (fn #"~" => #"-" | c => c)
Chris@1706 842 (explode (Real.toString n)))
Chris@1706 843 | STRING s => "\"" ^ stringEscape s ^ "\""
Chris@1706 844 | BOOL b => Bool.toString b
Chris@1706 845 | NULL => "null"
Chris@1706 846
Chris@1706 847 fun serialiseIndented json =
Chris@1706 848 let fun indent 0 = ""
Chris@1706 849 | indent i = " " ^ indent (i - 1)
Chris@1706 850 fun serialiseIndented' i json =
Chris@1706 851 let val ser = serialiseIndented' (i + 1)
Chris@1706 852 in
Chris@1706 853 case json of
Chris@1706 854 OBJECT [] => "{}"
Chris@1706 855 | ARRAY [] => "[]"
Chris@1706 856 | OBJECT pp => "{\n" ^ indent (i + 1) ^
Chris@1706 857 String.concatWith
Chris@1706 858 (",\n" ^ indent (i + 1))
Chris@1706 859 (map (fn (key, value) =>
Chris@1706 860 ser (STRING key) ^ ": " ^
Chris@1706 861 ser value) pp) ^
Chris@1706 862 "\n" ^ indent i ^ "}"
Chris@1706 863 | ARRAY arr => "[\n" ^ indent (i + 1) ^
Chris@1706 864 String.concatWith
Chris@1706 865 (",\n" ^ indent (i + 1))
Chris@1706 866 (map ser arr) ^
Chris@1706 867 "\n" ^ indent i ^ "]"
Chris@1706 868 | other => serialise other
Chris@1706 869 end
Chris@1706 870 in
Chris@1706 871 serialiseIndented' 0 json ^ "\n"
Chris@1706 872 end
Chris@1706 873
Chris@1706 874 end
Chris@1706 875
Chris@1706 876
Chris@1706 877 structure JsonBits :> sig
Chris@1706 878 val load_json_from : string -> Json.json (* filename -> json *)
Chris@1706 879 val save_json_to : string -> Json.json -> unit
Chris@1706 880 val lookup_optional : Json.json -> string list -> Json.json option
Chris@1706 881 val lookup_optional_string : Json.json -> string list -> string option
Chris@1706 882 val lookup_mandatory : Json.json -> string list -> Json.json
Chris@1706 883 val lookup_mandatory_string : Json.json -> string list -> string
Chris@1706 884 end = struct
Chris@1706 885
Chris@1706 886 fun load_json_from filename =
Chris@1706 887 case Json.parse (FileBits.file_contents filename) of
Chris@1706 888 Json.OK json => json
Chris@1706 889 | Json.ERROR e => raise Fail ("Failed to parse file: " ^ e)
Chris@1706 890
Chris@1706 891 fun save_json_to filename json =
Chris@1732 892 (* using binary I/O to avoid ever writing CR/LF line endings *)
Chris@1706 893 let val jstr = Json.serialiseIndented json
Chris@1732 894 val stream = BinIO.openOut filename
Chris@1706 895 in
Chris@1732 896 BinIO.output (stream, Byte.stringToBytes jstr);
Chris@1732 897 BinIO.closeOut stream
Chris@1706 898 end
Chris@1706 899
Chris@1706 900 fun lookup_optional json kk =
Chris@1706 901 let fun lookup key =
Chris@1706 902 case json of
Chris@1706 903 Json.OBJECT kvs =>
Chris@1706 904 (case List.find (fn (k, v) => k = key) kvs of
Chris@1706 905 SOME (k, v) => SOME v
Chris@1706 906 | NONE => NONE)
Chris@1706 907 | _ => raise Fail "Object expected"
Chris@1706 908 in
Chris@1706 909 case kk of
Chris@1706 910 [] => NONE
Chris@1706 911 | key::[] => lookup key
Chris@1706 912 | key::kk => case lookup key of
Chris@1706 913 NONE => NONE
Chris@1706 914 | SOME j => lookup_optional j kk
Chris@1706 915 end
Chris@1706 916
Chris@1706 917 fun lookup_optional_string json kk =
Chris@1706 918 case lookup_optional json kk of
Chris@1706 919 SOME (Json.STRING s) => SOME s
Chris@1706 920 | SOME _ => raise Fail ("Value (if present) must be string: " ^
Chris@1706 921 (String.concatWith " -> " kk))
Chris@1706 922 | NONE => NONE
Chris@1706 923
Chris@1706 924 fun lookup_mandatory json kk =
Chris@1706 925 case lookup_optional json kk of
Chris@1706 926 SOME v => v
Chris@1706 927 | NONE => raise Fail ("Value is mandatory: " ^
Chris@1706 928 (String.concatWith " -> " kk) ^ " in json: " ^
Chris@1706 929 (Json.serialise json))
Chris@1706 930
Chris@1706 931 fun lookup_mandatory_string json kk =
Chris@1706 932 case lookup_optional json kk of
Chris@1706 933 SOME (Json.STRING s) => s
Chris@1706 934 | _ => raise Fail ("Value must be string: " ^
Chris@1706 935 (String.concatWith " -> " kk))
Chris@1706 936 end
Chris@1706 937
Chris@1706 938 structure Provider :> sig
Chris@1706 939 val load_providers : Json.json -> provider list
Chris@1706 940 val load_more_providers : provider list -> Json.json -> provider list
Chris@1706 941 val remote_url : context -> vcs -> source -> libname -> string
Chris@1706 942 end = struct
Chris@1706 943
Chris@1706 944 val known_providers : provider list =
Chris@1706 945 [ {
Chris@1706 946 service = "bitbucket",
Chris@1706 947 supports = [HG, GIT],
Chris@1706 948 remote_spec = {
Chris@1724 949 anon = SOME "https://bitbucket.org/{owner}/{repository}",
Chris@1724 950 auth = SOME "ssh://{vcs}@bitbucket.org/{owner}/{repository}"
Chris@1706 951 }
Chris@1706 952 },
Chris@1706 953 {
Chris@1706 954 service = "github",
Chris@1706 955 supports = [GIT],
Chris@1706 956 remote_spec = {
Chris@1724 957 anon = SOME "https://github.com/{owner}/{repository}",
Chris@1724 958 auth = SOME "ssh://{vcs}@github.com/{owner}/{repository}"
Chris@1706 959 }
Chris@1706 960 }
Chris@1706 961 ]
Chris@1706 962
Chris@1706 963 fun vcs_name vcs =
Chris@1706 964 case vcs of GIT => "git" |
Chris@1706 965 HG => "hg"
Chris@1706 966
Chris@1706 967 fun vcs_from_name name =
Chris@1706 968 case name of "git" => GIT
Chris@1706 969 | "hg" => HG
Chris@1706 970 | other => raise Fail ("Unknown vcs name \"" ^ name ^ "\"")
Chris@1706 971
Chris@1706 972 fun load_more_providers previously_loaded json =
Chris@1706 973 let open JsonBits
Chris@1706 974 fun load pjson pname : provider =
Chris@1706 975 {
Chris@1706 976 service = pname,
Chris@1706 977 supports =
Chris@1706 978 case lookup_mandatory pjson ["vcs"] of
Chris@1706 979 Json.ARRAY vv =>
Chris@1706 980 map (fn (Json.STRING v) => vcs_from_name v
Chris@1706 981 | _ => raise Fail "Strings expected in vcs array")
Chris@1706 982 vv
Chris@1706 983 | _ => raise Fail "Array expected for vcs",
Chris@1706 984 remote_spec = {
Chris@1724 985 anon = lookup_optional_string pjson ["anonymous"],
Chris@1724 986 auth = lookup_optional_string pjson ["authenticated"]
Chris@1706 987 }
Chris@1706 988 }
Chris@1706 989 val loaded =
Chris@1721 990 case lookup_optional json ["services"] of
Chris@1706 991 NONE => []
Chris@1706 992 | SOME (Json.OBJECT pl) => map (fn (k, v) => load v k) pl
Chris@1721 993 | _ => raise Fail "Object expected for services in config"
Chris@1706 994 val newly_loaded =
Chris@1706 995 List.filter (fn p => not (List.exists (fn pp => #service p =
Chris@1706 996 #service pp)
Chris@1706 997 previously_loaded))
Chris@1706 998 loaded
Chris@1706 999 in
Chris@1706 1000 previously_loaded @ newly_loaded
Chris@1706 1001 end
Chris@1706 1002
Chris@1706 1003 fun load_providers json =
Chris@1706 1004 load_more_providers known_providers json
Chris@1706 1005
Chris@1706 1006 fun expand_spec spec { vcs, service, owner, repo } login =
Chris@1706 1007 (* ugly *)
Chris@1706 1008 let fun replace str =
Chris@1706 1009 case str of
Chris@1706 1010 "vcs" => vcs_name vcs
Chris@1706 1011 | "service" => service
Chris@1706 1012 | "owner" =>
Chris@1706 1013 (case owner of
Chris@1706 1014 SOME ostr => ostr
Chris@1706 1015 | NONE => raise Fail ("Owner not specified for service " ^
Chris@1706 1016 service))
Chris@1724 1017 | "repository" => repo
Chris@1706 1018 | "account" =>
Chris@1706 1019 (case login of
Chris@1706 1020 SOME acc => acc
Chris@1706 1021 | NONE => raise Fail ("Account not given for service " ^
Chris@1706 1022 service))
Chris@1706 1023 | other => raise Fail ("Unknown variable \"" ^ other ^
Chris@1706 1024 "\" in spec for service " ^ service)
Chris@1706 1025 fun expand' acc sstr =
Chris@1706 1026 case Substring.splitl (fn c => c <> #"{") sstr of
Chris@1706 1027 (pfx, sfx) =>
Chris@1706 1028 if Substring.isEmpty sfx
Chris@1706 1029 then rev (pfx :: acc)
Chris@1706 1030 else
Chris@1706 1031 case Substring.splitl (fn c => c <> #"}") sfx of
Chris@1706 1032 (tok, remainder) =>
Chris@1706 1033 if Substring.isEmpty remainder
Chris@1706 1034 then rev (tok :: pfx :: acc)
Chris@1706 1035 else let val replacement =
Chris@1706 1036 replace
Chris@1706 1037 (* tok begins with "{": *)
Chris@1706 1038 (Substring.string
Chris@1706 1039 (Substring.triml 1 tok))
Chris@1706 1040 in
Chris@1706 1041 expand' (Substring.full replacement ::
Chris@1706 1042 pfx :: acc)
Chris@1706 1043 (* remainder begins with "}": *)
Chris@1706 1044 (Substring.triml 1 remainder)
Chris@1706 1045 end
Chris@1706 1046 in
Chris@1706 1047 Substring.concat (expand' [] (Substring.full spec))
Chris@1706 1048 end
Chris@1706 1049
Chris@1706 1050 fun provider_url req login providers =
Chris@1706 1051 case providers of
Chris@1706 1052 [] => raise Fail ("Unknown service \"" ^ (#service req) ^
Chris@1706 1053 "\" for vcs \"" ^ (vcs_name (#vcs req)) ^ "\"")
Chris@1706 1054 | ({ service, supports, remote_spec : remote_spec } :: rest) =>
Chris@1706 1055 if service <> (#service req) orelse
Chris@1706 1056 not (List.exists (fn v => v = (#vcs req)) supports)
Chris@1706 1057 then provider_url req login rest
Chris@1706 1058 else
Chris@1706 1059 case (login, #auth remote_spec, #anon remote_spec) of
Chris@1706 1060 (SOME _, SOME auth, _) => expand_spec auth req login
Chris@1706 1061 | (SOME _, _, SOME anon) => expand_spec anon req NONE
Chris@1706 1062 | (NONE, _, SOME anon) => expand_spec anon req NONE
Chris@1724 1063 | _ => raise Fail ("No suitable anonymous or authenticated " ^
Chris@1724 1064 "URL spec provided for service \"" ^
Chris@1724 1065 service ^ "\"")
Chris@1706 1066
Chris@1706 1067 fun login_for ({ accounts, ... } : context) service =
Chris@1706 1068 case List.find (fn a => service = #service a) accounts of
Chris@1706 1069 SOME { login, ... } => SOME login
Chris@1706 1070 | NONE => NONE
Chris@1706 1071
Chris@1706 1072 fun remote_url (context : context) vcs source libname =
Chris@1706 1073 case source of
Chris@1721 1074 URL_SOURCE u => u
Chris@1721 1075 | SERVICE_SOURCE { service, owner, repo } =>
Chris@1706 1076 provider_url { vcs = vcs,
Chris@1706 1077 service = service,
Chris@1706 1078 owner = owner,
Chris@1706 1079 repo = case repo of
Chris@1706 1080 SOME r => r
Chris@1706 1081 | NONE => libname }
Chris@1706 1082 (login_for context service)
Chris@1706 1083 (#providers context)
Chris@1706 1084 end
Chris@1706 1085
Chris@1706 1086 structure HgControl :> VCS_CONTROL = struct
Chris@1706 1087
Chris@1706 1088 type vcsstate = { id: string, modified: bool,
Chris@1706 1089 branch: string, tags: string list }
Chris@1706 1090
Chris@1706 1091 val hg_args = [ "--config", "ui.interactive=true" ]
Chris@1706 1092
Chris@1706 1093 fun hg_command context libname args =
Chris@1706 1094 FileBits.command context libname ("hg" :: hg_args @ args)
Chris@1706 1095
Chris@1706 1096 fun hg_command_output context libname args =
Chris@1706 1097 FileBits.command_output context libname ("hg" :: hg_args @ args)
Chris@1706 1098
Chris@1706 1099 fun exists context libname =
Chris@1706 1100 OK (OS.FileSys.isDir (FileBits.subpath context libname ".hg"))
Chris@1706 1101 handle _ => OK false
Chris@1706 1102
Chris@1706 1103 fun remote_for context (libname, source) =
Chris@1706 1104 Provider.remote_url context HG source libname
Chris@1706 1105
Chris@1706 1106 fun current_state context libname : vcsstate result =
Chris@1706 1107 let fun is_branch text = text <> "" andalso #"(" = hd (explode text)
Chris@1706 1108 and extract_branch b =
Chris@1706 1109 if is_branch b (* need to remove enclosing parens *)
Chris@1706 1110 then (implode o rev o tl o rev o tl o explode) b
Chris@1706 1111 else "default"
Chris@1706 1112 and is_modified id = id <> "" andalso #"+" = hd (rev (explode id))
Chris@1706 1113 and extract_id id =
Chris@1706 1114 if is_modified id (* need to remove trailing "+" *)
Chris@1706 1115 then (implode o rev o tl o rev o explode) id
Chris@1706 1116 else id
Chris@1706 1117 and split_tags tags = String.tokens (fn c => c = #"/") tags
Chris@1706 1118 and state_for (id, branch, tags) =
Chris@1706 1119 OK { id = extract_id id,
Chris@1706 1120 modified = is_modified id,
Chris@1706 1121 branch = extract_branch branch,
Chris@1706 1122 tags = split_tags tags }
Chris@1706 1123 in
Chris@1706 1124 case hg_command_output context libname ["id"] of
Chris@1706 1125 ERROR e => ERROR e
Chris@1706 1126 | OK out =>
Chris@1706 1127 case String.tokens (fn x => x = #" ") out of
Chris@1706 1128 [id, branch, tags] => state_for (id, branch, tags)
Chris@1706 1129 | [id, other] => if is_branch other
Chris@1706 1130 then state_for (id, other, "")
Chris@1706 1131 else state_for (id, "", other)
Chris@1706 1132 | [id] => state_for (id, "", "")
Chris@1706 1133 | _ => ERROR ("Unexpected output from hg id: " ^ out)
Chris@1706 1134 end
Chris@1706 1135
Chris@1706 1136 fun branch_name branch = case branch of
Chris@1706 1137 DEFAULT_BRANCH => "default"
Chris@1706 1138 | BRANCH "" => "default"
Chris@1706 1139 | BRANCH b => b
Chris@1706 1140
Chris@1706 1141 fun id_of context libname =
Chris@1706 1142 case current_state context libname of
Chris@1706 1143 ERROR e => ERROR e
Chris@1706 1144 | OK { id, ... } => OK id
Chris@1706 1145
Chris@1706 1146 fun is_at context (libname, id_or_tag) =
Chris@1706 1147 case current_state context libname of
Chris@1706 1148 ERROR e => ERROR e
Chris@1706 1149 | OK { id, tags, ... } =>
Chris@1706 1150 OK (String.isPrefix id_or_tag id orelse
Chris@1706 1151 String.isPrefix id id_or_tag orelse
Chris@1706 1152 List.exists (fn t => t = id_or_tag) tags)
Chris@1706 1153
Chris@1706 1154 fun is_on_branch context (libname, b) =
Chris@1706 1155 case current_state context libname of
Chris@1706 1156 ERROR e => ERROR e
Chris@1706 1157 | OK { branch, ... } => OK (branch = branch_name b)
Chris@1706 1158
Chris@1706 1159 fun is_newest_locally context (libname, branch) =
Chris@1706 1160 case hg_command_output context libname
Chris@1706 1161 ["log", "-l1",
Chris@1706 1162 "-b", branch_name branch,
Chris@1706 1163 "--template", "{node}"] of
Chris@1706 1164 ERROR e => ERROR e
Chris@1706 1165 | OK newest_in_repo => is_at context (libname, newest_in_repo)
Chris@1706 1166
Chris@1706 1167 fun pull context libname =
Chris@1706 1168 hg_command context libname
Chris@1706 1169 (if FileBits.verbose ()
Chris@1706 1170 then ["pull"]
Chris@1706 1171 else ["pull", "-q"])
Chris@1706 1172
Chris@1706 1173 fun is_newest context (libname, branch) =
Chris@1706 1174 case is_newest_locally context (libname, branch) of
Chris@1706 1175 ERROR e => ERROR e
Chris@1706 1176 | OK false => OK false
Chris@1706 1177 | OK true =>
Chris@1706 1178 case pull context libname of
Chris@1706 1179 ERROR e => ERROR e
Chris@1706 1180 | _ => is_newest_locally context (libname, branch)
Chris@1706 1181
Chris@1706 1182 fun is_modified_locally context libname =
Chris@1706 1183 case current_state context libname of
Chris@1706 1184 ERROR e => ERROR e
Chris@1706 1185 | OK { modified, ... } => OK modified
Chris@1706 1186
Chris@1706 1187 fun checkout context (libname, source, branch) =
Chris@1706 1188 let val url = remote_for context (libname, source)
Chris@1706 1189 in
Chris@1706 1190 case FileBits.mkpath (FileBits.extpath context) of
Chris@1706 1191 ERROR e => ERROR e
Chris@1706 1192 | _ => hg_command context ""
Chris@1706 1193 ["clone", "-u", branch_name branch,
Chris@1706 1194 url, libname]
Chris@1706 1195 end
Chris@1706 1196
Chris@1706 1197 fun update context (libname, branch) =
Chris@1706 1198 let val pull_result = pull context libname
Chris@1706 1199 in
Chris@1706 1200 case hg_command context libname ["update", branch_name branch] of
Chris@1706 1201 ERROR e => ERROR e
Chris@1706 1202 | _ =>
Chris@1706 1203 case pull_result of
Chris@1706 1204 ERROR e => ERROR e
Chris@1706 1205 | _ => id_of context libname
Chris@1706 1206 end
Chris@1706 1207
Chris@1706 1208 fun update_to context (libname, "") =
Chris@1706 1209 ERROR "Non-empty id (tag or revision id) required for update_to"
Chris@1706 1210 | update_to context (libname, id) =
Chris@1723 1211 let val pull_result = pull context libname
Chris@1723 1212 in
Chris@1723 1213 case hg_command context libname ["update", "-r", id] of
Chris@1723 1214 OK _ => id_of context libname
Chris@1723 1215 | ERROR e =>
Chris@1723 1216 case pull_result of
Chris@1723 1217 ERROR e' => ERROR e' (* this was the ur-error *)
Chris@1723 1218 | _ => ERROR e
Chris@1723 1219 end
Chris@1706 1220
Chris@1706 1221 end
Chris@1706 1222
Chris@1706 1223 structure GitControl :> VCS_CONTROL = struct
Chris@1706 1224
Chris@1706 1225 (* With Git repos we always operate in detached HEAD state. Even
Chris@1706 1226 the master branch is checked out using the remote reference,
Chris@1706 1227 origin/master. *)
Chris@1706 1228
Chris@1706 1229 fun git_command context libname args =
Chris@1706 1230 FileBits.command context libname ("git" :: args)
Chris@1706 1231
Chris@1706 1232 fun git_command_output context libname args =
Chris@1706 1233 FileBits.command_output context libname ("git" :: args)
Chris@1706 1234
Chris@1706 1235 fun exists context libname =
Chris@1706 1236 OK (OS.FileSys.isDir (FileBits.subpath context libname ".git"))
Chris@1706 1237 handle _ => OK false
Chris@1706 1238
Chris@1706 1239 fun remote_for context (libname, source) =
Chris@1706 1240 Provider.remote_url context GIT source libname
Chris@1706 1241
Chris@1706 1242 fun branch_name branch = case branch of
Chris@1706 1243 DEFAULT_BRANCH => "master"
Chris@1706 1244 | BRANCH "" => "master"
Chris@1706 1245 | BRANCH b => b
Chris@1706 1246
Chris@1706 1247 fun remote_branch_name branch = "origin/" ^ branch_name branch
Chris@1706 1248
Chris@1706 1249 fun checkout context (libname, source, branch) =
Chris@1706 1250 let val url = remote_for context (libname, source)
Chris@1706 1251 in
Chris@1706 1252 case FileBits.mkpath (FileBits.extpath context) of
Chris@1706 1253 OK () => git_command context ""
Chris@1706 1254 ["clone", "-b",
Chris@1706 1255 branch_name branch,
Chris@1706 1256 url, libname]
Chris@1706 1257 | ERROR e => ERROR e
Chris@1706 1258 end
Chris@1706 1259
Chris@1706 1260 (* NB git rev-parse HEAD shows revision id of current checkout;
Chris@1706 1261 git rev-list -1 <tag> shows revision id of revision with that tag *)
Chris@1706 1262
Chris@1706 1263 fun id_of context libname =
Chris@1706 1264 git_command_output context libname ["rev-parse", "HEAD"]
Chris@1706 1265
Chris@1706 1266 fun is_at context (libname, id_or_tag) =
Chris@1706 1267 case id_of context libname of
Chris@1706 1268 ERROR e => ERROR e
Chris@1706 1269 | OK id =>
Chris@1706 1270 if String.isPrefix id_or_tag id orelse
Chris@1706 1271 String.isPrefix id id_or_tag
Chris@1706 1272 then OK true
Chris@1706 1273 else
Chris@1706 1274 case git_command_output context libname
Chris@1723 1275 ["show-ref",
Chris@1723 1276 "refs/tags/" ^ id_or_tag] of
Chris@1723 1277 OK "" => OK false
Chris@1723 1278 | ERROR _ => OK false
Chris@1723 1279 | OK s => OK (id = hd (String.tokens (fn c => c = #" ") s))
Chris@1706 1280
Chris@1706 1281 fun branch_tip context (libname, branch) =
Chris@1706 1282 git_command_output context libname
Chris@1706 1283 ["rev-list", "-1",
Chris@1706 1284 remote_branch_name branch]
Chris@1706 1285
Chris@1706 1286 fun is_newest_locally context (libname, branch) =
Chris@1706 1287 case branch_tip context (libname, branch) of
Chris@1706 1288 ERROR e => ERROR e
Chris@1706 1289 | OK rev => is_at context (libname, rev)
Chris@1706 1290
Chris@1706 1291 fun is_on_branch context (libname, branch) =
Chris@1706 1292 case branch_tip context (libname, branch) of
Chris@1706 1293 ERROR e => ERROR e
Chris@1706 1294 | OK rev =>
Chris@1706 1295 case is_at context (libname, rev) of
Chris@1706 1296 ERROR e => ERROR e
Chris@1706 1297 | OK true => OK true
Chris@1706 1298 | OK false =>
Chris@1706 1299 case git_command context libname
Chris@1706 1300 ["merge-base", "--is-ancestor",
Chris@1706 1301 "HEAD", remote_branch_name branch] of
Chris@1706 1302 ERROR e => OK false (* cmd returns non-zero for no *)
Chris@1706 1303 | _ => OK true
Chris@1706 1304
Chris@1706 1305 fun is_newest context (libname, branch) =
Chris@1706 1306 case is_newest_locally context (libname, branch) of
Chris@1706 1307 ERROR e => ERROR e
Chris@1706 1308 | OK false => OK false
Chris@1706 1309 | OK true =>
Chris@1706 1310 case git_command context libname ["fetch"] of
Chris@1706 1311 ERROR e => ERROR e
Chris@1706 1312 | _ => is_newest_locally context (libname, branch)
Chris@1706 1313
Chris@1706 1314 fun is_modified_locally context libname =
Chris@1706 1315 case git_command_output context libname ["status", "--porcelain"] of
Chris@1706 1316 ERROR e => ERROR e
Chris@1706 1317 | OK "" => OK false
Chris@1706 1318 | OK _ => OK true
Chris@1706 1319
Chris@1706 1320 (* This function updates to the latest revision on a branch rather
Chris@1706 1321 than to a specific id or tag. We can't just checkout the given
Chris@1706 1322 branch, as that will succeed even if the branch isn't up to
Chris@1706 1323 date. We could checkout the branch and then fetch and merge,
Chris@1706 1324 but it's perhaps cleaner not to maintain a local branch at all,
Chris@1706 1325 but instead checkout the remote branch as a detached head. *)
Chris@1706 1326
Chris@1706 1327 fun update context (libname, branch) =
Chris@1706 1328 case git_command context libname ["fetch"] of
Chris@1706 1329 ERROR e => ERROR e
Chris@1706 1330 | _ =>
Chris@1706 1331 case git_command context libname ["checkout", "--detach",
Chris@1706 1332 remote_branch_name branch] of
Chris@1706 1333 ERROR e => ERROR e
Chris@1706 1334 | _ => id_of context libname
Chris@1706 1335
Chris@1706 1336 (* This function is dealing with a specific id or tag, so if we
Chris@1723 1337 can successfully check it out (detached) then that's all we
Chris@1723 1338 need to do, regardless of whether fetch succeeded or not. We do
Chris@1723 1339 attempt the fetch first, though, purely in order to avoid ugly
Chris@1723 1340 error messages in the common case where we're being asked to
Chris@1723 1341 update to a new pin (from the lock file) that hasn't been
Chris@1723 1342 fetched yet. *)
Chris@1706 1343
Chris@1706 1344 fun update_to context (libname, "") =
Chris@1706 1345 ERROR "Non-empty id (tag or revision id) required for update_to"
Chris@1706 1346 | update_to context (libname, id) =
Chris@1723 1347 let val fetch_result = git_command context libname ["fetch"]
Chris@1723 1348 in
Chris@1723 1349 case git_command context libname ["checkout", "--detach", id] of
Chris@1723 1350 OK _ => id_of context libname
Chris@1723 1351 | ERROR e =>
Chris@1723 1352 case fetch_result of
Chris@1723 1353 ERROR e' => ERROR e' (* this was the ur-error *)
Chris@1723 1354 | _ => ERROR e
Chris@1723 1355 end
Chris@1723 1356
Chris@1706 1357 end
Chris@1706 1358
Chris@1706 1359 structure AnyLibControl :> LIB_CONTROL = struct
Chris@1706 1360
Chris@1706 1361 structure H = LibControlFn(HgControl)
Chris@1706 1362 structure G = LibControlFn(GitControl)
Chris@1706 1363
Chris@1706 1364 fun review context (spec as { vcs, ... } : libspec) =
Chris@1706 1365 (fn HG => H.review | GIT => G.review) vcs context spec
Chris@1706 1366
Chris@1706 1367 fun status context (spec as { vcs, ... } : libspec) =
Chris@1706 1368 (fn HG => H.status | GIT => G.status) vcs context spec
Chris@1706 1369
Chris@1706 1370 fun update context (spec as { vcs, ... } : libspec) =
Chris@1706 1371 (fn HG => H.update | GIT => G.update) vcs context spec
Chris@1706 1372 end
Chris@1706 1373
Chris@1724 1374 val libobjname = "libraries"
Chris@1724 1375
Chris@1706 1376 fun load_libspec spec_json lock_json libname : libspec =
Chris@1706 1377 let open JsonBits
Chris@1724 1378 val libobj = lookup_mandatory spec_json [libobjname, libname]
Chris@1706 1379 val vcs = lookup_mandatory_string libobj ["vcs"]
Chris@1706 1380 val retrieve = lookup_optional_string libobj
Chris@1706 1381 val service = retrieve ["service"]
Chris@1706 1382 val owner = retrieve ["owner"]
Chris@1706 1383 val repo = retrieve ["repository"]
Chris@1706 1384 val url = retrieve ["url"]
Chris@1706 1385 val branch = retrieve ["branch"]
Chris@1706 1386 val user_pin = retrieve ["pin"]
Chris@1724 1387 val lock_pin = case lookup_optional lock_json [libobjname, libname] of
Chris@1706 1388 SOME ll => lookup_optional_string ll ["pin"]
Chris@1706 1389 | NONE => NONE
Chris@1706 1390 in
Chris@1706 1391 {
Chris@1706 1392 libname = libname,
Chris@1706 1393 vcs = case vcs of
Chris@1706 1394 "hg" => HG
Chris@1706 1395 | "git" => GIT
Chris@1706 1396 | other => raise Fail ("Unknown version-control system \"" ^
Chris@1706 1397 other ^ "\""),
Chris@1706 1398 source = case (url, service, owner, repo) of
Chris@1721 1399 (SOME u, NONE, _, _) => URL_SOURCE u
Chris@1706 1400 | (NONE, SOME ss, owner, repo) =>
Chris@1721 1401 SERVICE_SOURCE { service = ss, owner = owner, repo = repo }
Chris@1706 1402 | _ => raise Fail ("Must have exactly one of service " ^
Chris@1706 1403 "or url string"),
Chris@1706 1404 pin = case lock_pin of
Chris@1706 1405 SOME p => PINNED p
Chris@1706 1406 | NONE =>
Chris@1706 1407 case user_pin of
Chris@1706 1408 SOME p => PINNED p
Chris@1706 1409 | NONE => UNPINNED,
Chris@1706 1410 branch = case branch of
Chris@1706 1411 SOME b => BRANCH b
Chris@1706 1412 | NONE => DEFAULT_BRANCH
Chris@1706 1413 }
Chris@1706 1414 end
Chris@1706 1415
Chris@1706 1416 fun load_userconfig () : userconfig =
Chris@1706 1417 let val home = FileBits.homedir ()
Chris@1706 1418 val conf_json =
Chris@1706 1419 JsonBits.load_json_from
Chris@1706 1420 (OS.Path.joinDirFile {
Chris@1706 1421 dir = home,
Chris@1706 1422 file = VextFilenames.user_config_file })
Chris@1706 1423 handle IO.Io _ => Json.OBJECT []
Chris@1706 1424 in
Chris@1706 1425 {
Chris@1706 1426 accounts = case JsonBits.lookup_optional conf_json ["accounts"] of
Chris@1706 1427 NONE => []
Chris@1706 1428 | SOME (Json.OBJECT aa) =>
Chris@1706 1429 map (fn (k, (Json.STRING v)) =>
Chris@1706 1430 { service = k, login = v }
Chris@1706 1431 | _ => raise Fail
Chris@1706 1432 "String expected for account name")
Chris@1706 1433 aa
Chris@1706 1434 | _ => raise Fail "Array expected for accounts",
Chris@1706 1435 providers = Provider.load_providers conf_json
Chris@1706 1436 }
Chris@1706 1437 end
Chris@1706 1438
Chris@1732 1439 datatype pintype =
Chris@1732 1440 NO_LOCKFILE |
Chris@1732 1441 USE_LOCKFILE
Chris@1732 1442
Chris@1732 1443 fun load_project (userconfig : userconfig) rootpath pintype : project =
Chris@1706 1444 let val spec_file = FileBits.project_spec_path rootpath
Chris@1706 1445 val lock_file = FileBits.project_lock_path rootpath
Chris@1706 1446 val _ = if OS.FileSys.access (spec_file, [OS.FileSys.A_READ])
Chris@1706 1447 handle OS.SysErr _ => false
Chris@1706 1448 then ()
Chris@1706 1449 else raise Fail ("Failed to open project spec file " ^
Chris@1706 1450 (VextFilenames.project_file) ^ " in " ^
Chris@1706 1451 rootpath ^
Chris@1706 1452 ".\nPlease ensure the spec file is in the " ^
Chris@1706 1453 "project root and run this from there.")
Chris@1706 1454 val spec_json = JsonBits.load_json_from spec_file
Chris@1732 1455 val lock_json = if pintype = USE_LOCKFILE
Chris@1706 1456 then JsonBits.load_json_from lock_file
Chris@1706 1457 handle IO.Io _ => Json.OBJECT []
Chris@1706 1458 else Json.OBJECT []
Chris@1706 1459 val extdir = JsonBits.lookup_mandatory_string spec_json
Chris@1706 1460 ["config", "extdir"]
Chris@1724 1461 val spec_libs = JsonBits.lookup_optional spec_json [libobjname]
Chris@1724 1462 val lock_libs = JsonBits.lookup_optional lock_json [libobjname]
Chris@1706 1463 val providers = Provider.load_more_providers
Chris@1706 1464 (#providers userconfig) spec_json
Chris@1706 1465 val libnames = case spec_libs of
Chris@1706 1466 NONE => []
Chris@1706 1467 | SOME (Json.OBJECT ll) => map (fn (k, v) => k) ll
Chris@1706 1468 | _ => raise Fail "Object expected for libs"
Chris@1706 1469 in
Chris@1706 1470 {
Chris@1706 1471 context = {
Chris@1706 1472 rootpath = rootpath,
Chris@1706 1473 extdir = extdir,
Chris@1706 1474 providers = providers,
Chris@1706 1475 accounts = #accounts userconfig
Chris@1706 1476 },
Chris@1706 1477 libs = map (load_libspec spec_json lock_json) libnames
Chris@1706 1478 }
Chris@1706 1479 end
Chris@1706 1480
Chris@1706 1481 fun save_lock_file rootpath locks =
Chris@1706 1482 let val lock_file = FileBits.project_lock_path rootpath
Chris@1706 1483 open Json
Chris@1706 1484 val lock_json =
Chris@1706 1485 OBJECT [
Chris@1724 1486 (libobjname,
Chris@1724 1487 OBJECT (map (fn { libname, id_or_tag } =>
Chris@1724 1488 (libname,
Chris@1724 1489 OBJECT [ ("pin", STRING id_or_tag) ]))
Chris@1724 1490 locks))
Chris@1706 1491 ]
Chris@1706 1492 in
Chris@1706 1493 JsonBits.save_json_to lock_file lock_json
Chris@1706 1494 end
Chris@1706 1495
Chris@1706 1496 fun pad_to n str =
Chris@1706 1497 if n <= String.size str then str
Chris@1706 1498 else pad_to n (str ^ " ")
Chris@1706 1499
Chris@1706 1500 fun hline_to 0 = ""
Chris@1706 1501 | hline_to n = "-" ^ hline_to (n-1)
Chris@1706 1502
Chris@1706 1503 val libname_width = 25
Chris@1706 1504 val libstate_width = 11
Chris@1706 1505 val localstate_width = 9
Chris@1706 1506 val notes_width = 5
Chris@1706 1507 val divider = " | "
Chris@1706 1508
Chris@1706 1509 fun print_status_header () =
Chris@1706 1510 print ("\r" ^ pad_to 80 "" ^ "\n " ^
Chris@1706 1511 pad_to libname_width "Library" ^ divider ^
Chris@1706 1512 pad_to libstate_width "State" ^ divider ^
Chris@1706 1513 pad_to localstate_width "Local" ^ divider ^
Chris@1706 1514 "Notes" ^ "\n " ^
Chris@1706 1515 hline_to libname_width ^ "-+-" ^
Chris@1706 1516 hline_to libstate_width ^ "-+-" ^
Chris@1706 1517 hline_to localstate_width ^ "-+-" ^
Chris@1706 1518 hline_to notes_width ^ "\n")
Chris@1706 1519
Chris@1706 1520 fun print_outcome_header () =
Chris@1706 1521 print ("\r" ^ pad_to 80 "" ^ "\n " ^
Chris@1706 1522 pad_to libname_width "Library" ^ divider ^
Chris@1706 1523 pad_to libstate_width "Outcome" ^ divider ^
Chris@1706 1524 "Notes" ^ "\n " ^
Chris@1706 1525 hline_to libname_width ^ "-+-" ^
Chris@1706 1526 hline_to libstate_width ^ "-+-" ^
Chris@1706 1527 hline_to notes_width ^ "\n")
Chris@1706 1528
Chris@1706 1529 fun print_status with_network (libname, status) =
Chris@1706 1530 let val libstate_str =
Chris@1706 1531 case status of
Chris@1706 1532 OK (ABSENT, _) => "Absent"
Chris@1706 1533 | OK (CORRECT, _) => if with_network then "Correct" else "Present"
Chris@1706 1534 | OK (SUPERSEDED, _) => "Superseded"
Chris@1706 1535 | OK (WRONG, _) => "Wrong"
Chris@1706 1536 | ERROR _ => "Error"
Chris@1706 1537 val localstate_str =
Chris@1706 1538 case status of
Chris@1706 1539 OK (_, MODIFIED) => "Modified"
Chris@1706 1540 | OK (_, UNMODIFIED) => "Clean"
Chris@1706 1541 | _ => ""
Chris@1706 1542 val error_str =
Chris@1706 1543 case status of
Chris@1706 1544 ERROR e => e
Chris@1706 1545 | _ => ""
Chris@1706 1546 in
Chris@1706 1547 print (" " ^
Chris@1706 1548 pad_to libname_width libname ^ divider ^
Chris@1706 1549 pad_to libstate_width libstate_str ^ divider ^
Chris@1706 1550 pad_to localstate_width localstate_str ^ divider ^
Chris@1706 1551 error_str ^ "\n")
Chris@1706 1552 end
Chris@1706 1553
Chris@1706 1554 fun print_update_outcome (libname, outcome) =
Chris@1706 1555 let val outcome_str =
Chris@1706 1556 case outcome of
Chris@1706 1557 OK id => "Ok"
Chris@1706 1558 | ERROR e => "Failed"
Chris@1706 1559 val error_str =
Chris@1706 1560 case outcome of
Chris@1706 1561 ERROR e => e
Chris@1706 1562 | _ => ""
Chris@1706 1563 in
Chris@1706 1564 print (" " ^
Chris@1706 1565 pad_to libname_width libname ^ divider ^
Chris@1706 1566 pad_to libstate_width outcome_str ^ divider ^
Chris@1706 1567 error_str ^ "\n")
Chris@1706 1568 end
Chris@1706 1569
Chris@1706 1570 fun act_and_print action print_header print_line (libs : libspec list) =
Chris@1706 1571 let val lines = map (fn lib => (#libname lib, action lib)) libs
Chris@1706 1572 val _ = print_header ()
Chris@1706 1573 in
Chris@1706 1574 app print_line lines;
Chris@1706 1575 lines
Chris@1706 1576 end
Chris@1708 1577
Chris@1708 1578 fun return_code_for outcomes =
Chris@1708 1579 foldl (fn ((_, result), acc) =>
Chris@1708 1580 case result of
Chris@1708 1581 ERROR _ => OS.Process.failure
Chris@1708 1582 | _ => acc)
Chris@1708 1583 OS.Process.success
Chris@1708 1584 outcomes
Chris@1706 1585
Chris@1706 1586 fun status_of_project ({ context, libs } : project) =
Chris@1708 1587 return_code_for (act_and_print (AnyLibControl.status context)
Chris@1708 1588 print_status_header (print_status false)
Chris@1708 1589 libs)
Chris@1706 1590
Chris@1706 1591 fun review_project ({ context, libs } : project) =
Chris@1708 1592 return_code_for (act_and_print (AnyLibControl.review context)
Chris@1708 1593 print_status_header (print_status true)
Chris@1708 1594 libs)
Chris@1706 1595
Chris@1706 1596 fun update_project ({ context, libs } : project) =
Chris@1706 1597 let val outcomes = act_and_print
Chris@1706 1598 (AnyLibControl.update context)
Chris@1706 1599 print_outcome_header print_update_outcome libs
Chris@1708 1600 val locks =
Chris@1708 1601 List.concat
Chris@1708 1602 (map (fn (libname, result) =>
Chris@1708 1603 case result of
Chris@1708 1604 ERROR _ => []
Chris@1708 1605 | OK id => [{ libname = libname, id_or_tag = id }])
Chris@1708 1606 outcomes)
Chris@1708 1607 val return_code = return_code_for outcomes
Chris@1706 1608 in
Chris@1708 1609 if OS.Process.isSuccess return_code
Chris@1708 1610 then save_lock_file (#rootpath context) locks
Chris@1708 1611 else ();
Chris@1708 1612 return_code
Chris@1706 1613 end
Chris@1706 1614
Chris@1732 1615 fun load_local_project pintype =
Chris@1706 1616 let val userconfig = load_userconfig ()
Chris@1706 1617 val rootpath = OS.FileSys.getDir ()
Chris@1706 1618 in
Chris@1732 1619 load_project userconfig rootpath pintype
Chris@1706 1620 end
Chris@1706 1621
Chris@1732 1622 fun with_local_project pintype f =
Chris@1732 1623 let val return_code = f (load_local_project pintype)
Chris@1708 1624 handle e =>
Chris@1708 1625 (print ("Failed with exception: " ^
Chris@1708 1626 (exnMessage e) ^ "\n");
Chris@1708 1627 OS.Process.failure)
Chris@1708 1628 val _ = print "\n";
Chris@1708 1629 in
Chris@1708 1630 return_code
Chris@1708 1631 end
Chris@1706 1632
Chris@1732 1633 fun review () = with_local_project NO_LOCKFILE review_project
Chris@1732 1634 fun status () = with_local_project NO_LOCKFILE status_of_project
Chris@1732 1635 fun update () = with_local_project NO_LOCKFILE update_project
Chris@1732 1636 fun install () = with_local_project USE_LOCKFILE update_project
Chris@1706 1637
Chris@1706 1638 fun version () =
Chris@1708 1639 (print ("v" ^ vext_version ^ "\n");
Chris@1708 1640 OS.Process.success)
Chris@1706 1641
Chris@1706 1642 fun usage () =
Chris@1706 1643 (print "\nVext ";
Chris@1706 1644 version ();
Chris@1706 1645 print ("\nA simple manager for third-party source code dependencies.\n\n"
Chris@1706 1646 ^ "Usage:\n\n"
Chris@1706 1647 ^ " vext <command>\n\n"
Chris@1706 1648 ^ "where <command> is one of:\n\n"
Chris@1716 1649 ^ " status print quick report on local status only, without using network\n"
Chris@1706 1650 ^ " review check configured libraries against their providers, and report\n"
Chris@1706 1651 ^ " install update configured libraries according to project specs and lock file\n"
Chris@1706 1652 ^ " update update configured libraries and lock file according to project specs\n"
Chris@1708 1653 ^ " version print the Vext version number and exit\n\n");
Chris@1708 1654 OS.Process.failure)
Chris@1706 1655
Chris@1706 1656 fun vext args =
Chris@1708 1657 let val return_code =
Chris@1708 1658 case args of
Chris@1708 1659 ["review"] => review ()
Chris@1708 1660 | ["status"] => status ()
Chris@1708 1661 | ["install"] => install ()
Chris@1708 1662 | ["update"] => update ()
Chris@1708 1663 | ["version"] => version ()
Chris@1708 1664 | _ => usage ()
Chris@1708 1665 in
Chris@1708 1666 OS.Process.exit return_code;
Chris@1708 1667 ()
Chris@1708 1668 end
Chris@1706 1669
Chris@1706 1670 fun main () =
Chris@1706 1671 vext (CommandLine.arguments ())