annotate vext.sml @ 315:9ebb9ac79bdf

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