annotate vext.sml @ 53:f8110ba54f1b

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