Chris@1706: (* This file is automatically generated from the individual Chris@1706: source files in the Vext repository. *) Chris@1706: Chris@1706: (* Chris@1706: Vext Chris@1706: Chris@1706: A simple manager for third-party source code dependencies Chris@1706: Chris@1706: Copyright 2017 Chris Cannam. Chris@1706: Chris@1706: Permission is hereby granted, free of charge, to any person Chris@1706: obtaining a copy of this software and associated documentation Chris@1706: files (the "Software"), to deal in the Software without Chris@1706: restriction, including without limitation the rights to use, copy, Chris@1706: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@1706: of the Software, and to permit persons to whom the Software is Chris@1706: furnished to do so, subject to the following conditions: Chris@1706: Chris@1706: The above copyright notice and this permission notice shall be Chris@1706: included in all copies or substantial portions of the Software. Chris@1706: Chris@1706: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@1706: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@1706: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@1706: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@1706: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@1706: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@1706: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@1706: Chris@1706: Except as contained in this notice, the names of Chris Cannam and Chris@1706: Particular Programs Ltd shall not be used in advertising or Chris@1706: otherwise to promote the sale, use or other dealings in this Chris@1706: Software without prior written authorization. Chris@1706: *) Chris@1706: Chris@1723: val vext_version = "0.9.3" Chris@1706: Chris@1706: Chris@1706: datatype vcs = Chris@1706: HG | Chris@1706: GIT Chris@1706: Chris@1706: datatype source = Chris@1721: URL_SOURCE of string | Chris@1721: SERVICE_SOURCE of { Chris@1706: service : string, Chris@1706: owner : string option, Chris@1706: repo : string option Chris@1706: } Chris@1706: Chris@1706: datatype pin = Chris@1706: UNPINNED | Chris@1706: PINNED of string Chris@1706: Chris@1706: datatype libstate = Chris@1706: ABSENT | Chris@1706: CORRECT | Chris@1706: SUPERSEDED | Chris@1706: WRONG Chris@1706: Chris@1706: datatype localstate = Chris@1706: MODIFIED | Chris@1706: UNMODIFIED Chris@1706: Chris@1706: datatype branch = Chris@1706: BRANCH of string | Chris@1706: DEFAULT_BRANCH Chris@1706: Chris@1706: (* If we can recover from an error, for example by reporting failure Chris@1706: for this one thing and going on to the next thing, then the error Chris@1706: should usually be returned through a result type rather than an Chris@1706: exception. *) Chris@1706: Chris@1706: datatype 'a result = Chris@1706: OK of 'a | Chris@1706: ERROR of string Chris@1706: Chris@1706: type libname = string Chris@1706: Chris@1706: type id_or_tag = string Chris@1706: Chris@1706: type libspec = { Chris@1706: libname : libname, Chris@1706: vcs : vcs, Chris@1706: source : source, Chris@1706: branch : branch, Chris@1706: pin : pin Chris@1706: } Chris@1706: Chris@1706: type lock = { Chris@1706: libname : libname, Chris@1706: id_or_tag : id_or_tag Chris@1706: } Chris@1706: Chris@1706: type remote_spec = { Chris@1706: anon : string option, Chris@1706: auth : string option Chris@1706: } Chris@1706: Chris@1706: type provider = { Chris@1706: service : string, Chris@1706: supports : vcs list, Chris@1706: remote_spec : remote_spec Chris@1706: } Chris@1706: Chris@1706: type account = { Chris@1706: service : string, Chris@1706: login : string Chris@1706: } Chris@1706: Chris@1706: type context = { Chris@1706: rootpath : string, Chris@1706: extdir : string, Chris@1706: providers : provider list, Chris@1706: accounts : account list Chris@1706: } Chris@1706: Chris@1706: type userconfig = { Chris@1706: providers : provider list, Chris@1706: accounts : account list Chris@1706: } Chris@1706: Chris@1706: type project = { Chris@1706: context : context, Chris@1706: libs : libspec list Chris@1706: } Chris@1706: Chris@1706: structure VextFilenames = struct Chris@1706: val project_file = "vext-project.json" Chris@1706: val project_lock_file = "vext-lock.json" Chris@1706: val user_config_file = ".vext.json" Chris@1706: end Chris@1706: Chris@1706: signature VCS_CONTROL = sig Chris@1706: Chris@1706: (** Test whether the library is present locally at all *) Chris@1706: val exists : context -> libname -> bool result Chris@1706: Chris@1706: (** Return the id (hash) of the current revision for the library *) Chris@1706: val id_of : context -> libname -> id_or_tag result Chris@1706: Chris@1706: (** Test whether the library is at the given id *) Chris@1706: val is_at : context -> libname * id_or_tag -> bool result Chris@1706: Chris@1706: (** Test whether the library is on the given branch, i.e. is at Chris@1706: the branch tip or an ancestor of it *) Chris@1706: val is_on_branch : context -> libname * branch -> bool result Chris@1706: Chris@1706: (** Test whether the library is at the newest revision for the Chris@1706: given branch. False may indicate that the branch has advanced Chris@1706: or that the library is not on the branch at all. This function Chris@1706: may use the network to check for new revisions *) Chris@1706: val is_newest : context -> libname * branch -> bool result Chris@1706: Chris@1706: (** Test whether the library is at the newest revision available Chris@1706: locally for the given branch. False may indicate that the Chris@1706: branch has advanced or that the library is not on the branch Chris@1706: at all. This function must not use the network *) Chris@1706: val is_newest_locally : context -> libname * branch -> bool result Chris@1706: Chris@1706: (** Test whether the library has been modified in the local Chris@1706: working copy *) Chris@1706: val is_modified_locally : context -> libname -> bool result Chris@1706: Chris@1706: (** Check out, i.e. clone a fresh copy of, the repo for the given Chris@1706: library on the given branch *) Chris@1706: val checkout : context -> libname * source * branch -> unit result Chris@1706: Chris@1706: (** Update the library to the given branch tip *) Chris@1706: val update : context -> libname * branch -> id_or_tag result Chris@1706: Chris@1706: (** Update the library to the given specific id or tag *) Chris@1706: val update_to : context -> libname * id_or_tag -> id_or_tag result Chris@1706: end Chris@1706: Chris@1706: signature LIB_CONTROL = sig Chris@1706: val review : context -> libspec -> (libstate * localstate) result Chris@1706: val status : context -> libspec -> (libstate * localstate) result Chris@1706: val update : context -> libspec -> id_or_tag result Chris@1706: end Chris@1706: Chris@1706: structure FileBits :> sig Chris@1706: val extpath : context -> string Chris@1706: val libpath : context -> libname -> string Chris@1706: val subpath : context -> libname -> string -> string Chris@1706: val command_output : context -> libname -> string list -> string result Chris@1706: val command : context -> libname -> string list -> unit result Chris@1706: val file_contents : string -> string Chris@1706: val mydir : unit -> string Chris@1706: val homedir : unit -> string Chris@1706: val mkpath : string -> unit result Chris@1706: val project_spec_path : string -> string Chris@1706: val project_lock_path : string -> string Chris@1706: val verbose : unit -> bool Chris@1706: end = struct Chris@1706: Chris@1706: fun verbose () = Chris@1706: case OS.Process.getEnv "VEXT_VERBOSE" of Chris@1706: SOME "0" => false Chris@1706: | SOME _ => true Chris@1706: | NONE => false Chris@1706: Chris@1706: fun extpath ({ rootpath, extdir, ... } : context) = Chris@1706: let val { isAbs, vol, arcs } = OS.Path.fromString rootpath Chris@1706: in OS.Path.toString { Chris@1706: isAbs = isAbs, Chris@1706: vol = vol, Chris@1706: arcs = arcs @ [ extdir ] Chris@1706: } Chris@1706: end Chris@1706: Chris@1706: fun subpath ({ rootpath, extdir, ... } : context) libname remainder = Chris@1706: (* NB libname is allowed to be a path fragment, e.g. foo/bar *) Chris@1706: let val { isAbs, vol, arcs } = OS.Path.fromString rootpath Chris@1706: val split = String.fields (fn c => c = #"/") Chris@1706: in OS.Path.toString { Chris@1706: isAbs = isAbs, Chris@1706: vol = vol, Chris@1706: arcs = arcs @ [ extdir ] @ split libname @ split remainder Chris@1706: } Chris@1706: end Chris@1706: Chris@1706: fun libpath context "" = Chris@1706: extpath context Chris@1706: | libpath context libname = Chris@1706: subpath context libname "" Chris@1706: Chris@1706: fun project_file_path rootpath filename = Chris@1706: let val { isAbs, vol, arcs } = OS.Path.fromString rootpath Chris@1706: in OS.Path.toString { Chris@1706: isAbs = isAbs, Chris@1706: vol = vol, Chris@1706: arcs = arcs @ [ filename ] Chris@1706: } Chris@1706: end Chris@1706: Chris@1706: fun project_spec_path rootpath = Chris@1706: project_file_path rootpath (VextFilenames.project_file) Chris@1706: Chris@1706: fun project_lock_path rootpath = Chris@1706: project_file_path rootpath (VextFilenames.project_lock_file) Chris@1706: Chris@1706: fun trim str = Chris@1706: hd (String.fields (fn x => x = #"\n" orelse x = #"\r") str) Chris@1706: Chris@1706: fun file_contents filename = Chris@1706: let val stream = TextIO.openIn filename Chris@1706: fun read_all str acc = Chris@1706: case TextIO.inputLine str of Chris@1706: SOME line => read_all str (trim line :: acc) Chris@1706: | NONE => rev acc Chris@1706: val contents = read_all stream [] Chris@1706: val _ = TextIO.closeIn stream Chris@1706: in Chris@1706: String.concatWith "\n" contents Chris@1706: end Chris@1706: Chris@1706: fun expand_commandline cmdlist = Chris@1706: (* We are quite [too] strict about what we accept here, except Chris@1706: for the first element in cmdlist which is assumed to be a Chris@1706: known command location rather than arbitrary user input. NB Chris@1706: only ASCII accepted at this point. *) Chris@1706: let open Char Chris@1706: fun quote arg = Chris@1706: if List.all Chris@1706: (fn c => isAlphaNum c orelse c = #"-" orelse c = #"_") Chris@1706: (explode arg) Chris@1706: then arg Chris@1706: else "\"" ^ arg ^ "\"" Chris@1706: fun check arg = Chris@1706: let val valid = explode " /#:;?,._-{}@=" Chris@1706: in Chris@1706: app (fn c => Chris@1706: if isAlphaNum c orelse Chris@1706: List.exists (fn v => v = c) valid Chris@1706: then () Chris@1706: else raise Fail ("Invalid character '" ^ Chris@1706: (Char.toString c) ^ Chris@1706: "' in command list")) Chris@1706: (explode arg); Chris@1706: arg Chris@1706: end Chris@1706: in Chris@1706: String.concatWith " " Chris@1706: (map quote Chris@1706: (hd cmdlist :: map check (tl cmdlist))) Chris@1706: end Chris@1706: Chris@1706: val tick_cycle = ref 0 Chris@1706: val tick_chars = Vector.fromList (map String.str (explode "|/-\\")) Chris@1706: Chris@1716: fun tick libname cmdlist = Chris@1706: let val n = Vector.length tick_chars Chris@1706: fun pad_to n str = Chris@1716: if n <= String.size str then str Chris@1716: else pad_to n (str ^ " ") Chris@1716: val name = if libname <> "" then libname Chris@1716: else if cmdlist = nil then "" Chris@1716: else hd (rev cmdlist) Chris@1706: in Chris@1716: print (" " ^ Chris@1706: Vector.sub(tick_chars, !tick_cycle) ^ " " ^ Chris@1716: pad_to 24 name ^ Chris@1716: "\r"); Chris@1706: tick_cycle := (if !tick_cycle = n - 1 then 0 else 1 + !tick_cycle) Chris@1706: end Chris@1706: Chris@1706: fun run_command context libname cmdlist redirect = Chris@1706: let open OS Chris@1706: val dir = libpath context libname Chris@1706: val cmd = expand_commandline cmdlist Chris@1706: val _ = if verbose () Chris@1706: then print ("Running: " ^ cmd ^ Chris@1706: " (in dir " ^ dir ^ ")...\n") Chris@1716: else tick libname cmdlist Chris@1706: val _ = FileSys.chDir dir Chris@1706: val status = case redirect of Chris@1706: NONE => Process.system cmd Chris@1706: | SOME file => Process.system (cmd ^ ">" ^ file) Chris@1706: in Chris@1706: if Process.isSuccess status Chris@1706: then OK () Chris@1706: else ERROR ("Command failed: " ^ cmd ^ " (in dir " ^ dir ^ ")") Chris@1706: end Chris@1706: handle ex => ERROR ("Unable to run command: " ^ exnMessage ex) Chris@1706: Chris@1706: fun command context libname cmdlist = Chris@1706: run_command context libname cmdlist NONE Chris@1706: Chris@1706: fun command_output context libname cmdlist = Chris@1706: let open OS Chris@1706: val tmpFile = FileSys.tmpName () Chris@1706: val result = run_command context libname cmdlist (SOME tmpFile) Chris@1706: val contents = file_contents tmpFile Chris@1706: in Chris@1706: FileSys.remove tmpFile handle _ => (); Chris@1706: case result of Chris@1706: OK () => OK contents Chris@1706: | ERROR e => ERROR e Chris@1706: end Chris@1706: Chris@1706: fun mydir () = Chris@1706: let open OS Chris@1706: val { dir, file } = Path.splitDirFile (CommandLine.name ()) Chris@1706: in Chris@1706: FileSys.realPath Chris@1706: (if Path.isAbsolute dir Chris@1706: then dir Chris@1706: else Path.concat (FileSys.getDir (), dir)) Chris@1706: end Chris@1706: Chris@1706: fun homedir () = Chris@1706: (* Failure is not routine, so we use an exception here *) Chris@1706: case (OS.Process.getEnv "HOME", Chris@1706: OS.Process.getEnv "HOMEPATH") of Chris@1706: (SOME home, _) => home Chris@1706: | (NONE, SOME home) => home Chris@1706: | (NONE, NONE) => Chris@1706: raise Fail "Failed to look up home directory from environment" Chris@1706: Chris@1706: fun mkpath path = Chris@1706: if OS.FileSys.isDir path handle _ => false Chris@1706: then OK () Chris@1706: else case OS.Path.fromString path of Chris@1706: { arcs = nil, ... } => OK () Chris@1706: | { isAbs = false, ... } => ERROR "mkpath requires absolute path" Chris@1706: | { isAbs, vol, arcs } => Chris@1706: case mkpath (OS.Path.toString { (* parent *) Chris@1706: isAbs = isAbs, Chris@1706: vol = vol, Chris@1706: arcs = rev (tl (rev arcs)) }) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK () => ((OS.FileSys.mkDir path; OK ()) Chris@1706: handle OS.SysErr (e, _) => Chris@1706: ERROR ("Directory creation failed: " ^ e)) Chris@1706: end Chris@1706: Chris@1706: functor LibControlFn (V: VCS_CONTROL) :> LIB_CONTROL = struct Chris@1706: Chris@1706: (* Valid states for unpinned libraries: Chris@1706: Chris@1706: - CORRECT: We are on the right branch and are up-to-date with Chris@1706: it as far as we can tell. (If not using the network, this Chris@1706: should be reported to user as "Present" rather than "Correct" Chris@1706: as the remote repo may have advanced without us knowing.) Chris@1706: Chris@1706: - SUPERSEDED: We are on the right branch but we can see that Chris@1706: there is a newer revision either locally or on the remote (in Chris@1706: Git terms, we are at an ancestor of the desired branch tip). Chris@1706: Chris@1706: - WRONG: We are on the wrong branch (in Git terms, we are not Chris@1706: at the desired branch tip or any ancestor of it). Chris@1706: Chris@1706: - ABSENT: Repo doesn't exist here at all. Chris@1706: Chris@1706: Valid states for pinned libraries: Chris@1706: Chris@1706: - CORRECT: We are at the pinned revision. Chris@1706: Chris@1706: - WRONG: We are at any revision other than the pinned one. Chris@1706: Chris@1706: - ABSENT: Repo doesn't exist here at all. Chris@1706: *) Chris@1706: Chris@1706: fun check with_network context ({ libname, branch, pin, ... } : libspec) = Chris@1706: let fun check_unpinned () = Chris@1706: let val is_newest = if with_network Chris@1706: then V.is_newest Chris@1706: else V.is_newest_locally Chris@1706: in Chris@1706: case is_newest context (libname, branch) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK true => OK CORRECT Chris@1706: | OK false => Chris@1706: case V.is_on_branch context (libname, branch) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK true => OK SUPERSEDED Chris@1706: | OK false => OK WRONG Chris@1706: end Chris@1706: fun check_pinned target = Chris@1706: case V.is_at context (libname, target) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK true => OK CORRECT Chris@1706: | OK false => OK WRONG Chris@1706: fun check' () = Chris@1706: case pin of Chris@1706: UNPINNED => check_unpinned () Chris@1706: | PINNED target => check_pinned target Chris@1706: in Chris@1706: case V.exists context libname of Chris@1706: ERROR e => ERROR e Chris@1706: | OK false => OK (ABSENT, UNMODIFIED) Chris@1706: | OK true => Chris@1706: case (check' (), V.is_modified_locally context libname) of Chris@1706: (ERROR e, _) => ERROR e Chris@1706: | (_, ERROR e) => ERROR e Chris@1706: | (OK state, OK true) => OK (state, MODIFIED) Chris@1706: | (OK state, OK false) => OK (state, UNMODIFIED) Chris@1706: end Chris@1706: Chris@1706: val review = check true Chris@1706: val status = check false Chris@1706: Chris@1706: fun update context ({ libname, source, branch, pin, ... } : libspec) = Chris@1706: let fun update_unpinned () = Chris@1706: case V.is_newest context (libname, branch) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK true => V.id_of context libname Chris@1706: | OK false => V.update context (libname, branch) Chris@1706: fun update_pinned target = Chris@1706: case V.is_at context (libname, target) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK true => OK target Chris@1706: | OK false => V.update_to context (libname, target) Chris@1706: fun update' () = Chris@1706: case pin of Chris@1706: UNPINNED => update_unpinned () Chris@1706: | PINNED target => update_pinned target Chris@1706: in Chris@1706: case V.exists context libname of Chris@1706: ERROR e => ERROR e Chris@1706: | OK true => update' () Chris@1706: | OK false => Chris@1706: case V.checkout context (libname, source, branch) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK () => update' () Chris@1706: end Chris@1706: end Chris@1706: Chris@1706: (* Simple Standard ML JSON parser Chris@1706: ============================== Chris@1706: Chris@1706: https://bitbucket.org/cannam/sml-simplejson Chris@1706: Chris@1706: An RFC-compliant JSON parser in one SML file with no dependency Chris@1706: on anything outside the Basis library. Also includes a simple Chris@1706: serialiser. Chris@1706: Chris@1706: Tested with MLton, Poly/ML, and SML/NJ compilers. Chris@1706: Chris@1706: Parser notes: Chris@1706: Chris@1706: * Complies with RFC 7159, The JavaScript Object Notation (JSON) Chris@1706: Data Interchange Format Chris@1706: Chris@1706: * Passes all of the JSONTestSuite parser accept/reject tests that Chris@1706: exist at the time of writing, as listed in "Parsing JSON is a Chris@1706: Minefield" (http://seriot.ch/parsing_json.php) Chris@1706: Chris@1706: * Two-pass parser using naive exploded strings, therefore not Chris@1706: particularly fast and not suitable for large input files Chris@1706: Chris@1706: * Only supports UTF-8 input, not UTF-16 or UTF-32. Doesn't check Chris@1706: that JSON strings are valid UTF-8 -- the caller must do that -- Chris@1706: but does handle \u escapes Chris@1706: Chris@1706: * Converts all numbers to type "real". If that is a 64-bit IEEE Chris@1706: float type (common but not guaranteed in SML) then we're pretty Chris@1706: standard for a JSON parser Chris@1706: Chris@1706: Copyright 2017 Chris Cannam. Chris@1706: Parts based on the JSON parser in the Ponyo library by Phil Eaton. Chris@1706: Chris@1706: Permission is hereby granted, free of charge, to any person Chris@1706: obtaining a copy of this software and associated documentation Chris@1706: files (the "Software"), to deal in the Software without Chris@1706: restriction, including without limitation the rights to use, copy, Chris@1706: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@1706: of the Software, and to permit persons to whom the Software is Chris@1706: furnished to do so, subject to the following conditions: Chris@1706: Chris@1706: The above copyright notice and this permission notice shall be Chris@1706: included in all copies or substantial portions of the Software. Chris@1706: Chris@1706: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@1706: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@1706: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@1706: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@1706: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@1706: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@1706: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@1706: Chris@1706: Except as contained in this notice, the names of Chris Cannam and Chris@1706: Particular Programs Ltd shall not be used in advertising or Chris@1706: otherwise to promote the sale, use or other dealings in this Chris@1706: Software without prior written authorization. Chris@1706: *) Chris@1706: Chris@1706: signature JSON = sig Chris@1706: Chris@1706: datatype json = OBJECT of (string * json) list Chris@1706: | ARRAY of json list Chris@1706: | NUMBER of real Chris@1706: | STRING of string Chris@1706: | BOOL of bool Chris@1706: | NULL Chris@1706: Chris@1706: datatype 'a result = OK of 'a Chris@1706: | ERROR of string Chris@1706: Chris@1706: val parse : string -> json result Chris@1706: val serialise : json -> string Chris@1706: val serialiseIndented : json -> string Chris@1706: Chris@1706: end Chris@1706: Chris@1706: structure Json :> JSON = struct Chris@1706: Chris@1706: datatype json = OBJECT of (string * json) list Chris@1706: | ARRAY of json list Chris@1706: | NUMBER of real Chris@1706: | STRING of string Chris@1706: | BOOL of bool Chris@1706: | NULL Chris@1706: Chris@1706: datatype 'a result = OK of 'a Chris@1706: | ERROR of string Chris@1706: Chris@1706: structure T = struct Chris@1706: datatype token = NUMBER of char list Chris@1706: | STRING of string Chris@1706: | BOOL of bool Chris@1706: | NULL Chris@1706: | CURLY_L Chris@1706: | CURLY_R Chris@1706: | SQUARE_L Chris@1706: | SQUARE_R Chris@1706: | COLON Chris@1706: | COMMA Chris@1706: Chris@1706: fun toString t = Chris@1706: case t of NUMBER digits => implode digits Chris@1706: | STRING s => s Chris@1706: | BOOL b => Bool.toString b Chris@1706: | NULL => "null" Chris@1706: | CURLY_L => "{" Chris@1706: | CURLY_R => "}" Chris@1706: | SQUARE_L => "[" Chris@1706: | SQUARE_R => "]" Chris@1706: | COLON => ":" Chris@1706: | COMMA => "," Chris@1706: end Chris@1706: Chris@1706: fun bmpToUtf8 cp = (* convert a codepoint in Unicode BMP to utf8 bytes *) Chris@1706: let open Word Chris@1706: infix 6 orb andb >> Chris@1706: in Chris@1706: map (Char.chr o toInt) Chris@1706: (if cp < 0wx80 then Chris@1706: [cp] Chris@1706: else if cp < 0wx800 then Chris@1706: [0wxc0 orb (cp >> 0w6), 0wx80 orb (cp andb 0wx3f)] Chris@1706: else if cp < 0wx10000 then Chris@1706: [0wxe0 orb (cp >> 0w12), Chris@1706: 0wx80 orb ((cp >> 0w6) andb 0wx3f), Chris@1706: 0wx80 orb (cp andb 0wx3f)] Chris@1706: else raise Fail ("Invalid BMP point " ^ (Word.toString cp))) Chris@1706: end Chris@1706: Chris@1706: fun error pos text = ERROR (text ^ " at character position " ^ Chris@1706: Int.toString (pos - 1)) Chris@1706: fun token_error pos = error pos ("Unexpected token") Chris@1706: Chris@1706: fun lexNull pos acc (#"u" :: #"l" :: #"l" :: xs) = Chris@1706: lex (pos + 3) (T.NULL :: acc) xs Chris@1706: | lexNull pos acc _ = token_error pos Chris@1706: Chris@1706: and lexTrue pos acc (#"r" :: #"u" :: #"e" :: xs) = Chris@1706: lex (pos + 3) (T.BOOL true :: acc) xs Chris@1706: | lexTrue pos acc _ = token_error pos Chris@1706: Chris@1706: and lexFalse pos acc (#"a" :: #"l" :: #"s" :: #"e" :: xs) = Chris@1706: lex (pos + 4) (T.BOOL false :: acc) xs Chris@1706: | lexFalse pos acc _ = token_error pos Chris@1706: Chris@1706: and lexChar tok pos acc xs = Chris@1706: lex pos (tok :: acc) xs Chris@1706: Chris@1706: and lexString pos acc cc = Chris@1706: let datatype escaped = ESCAPED | NORMAL Chris@1706: fun lexString' pos text ESCAPED [] = Chris@1706: error pos "End of input during escape sequence" Chris@1706: | lexString' pos text NORMAL [] = Chris@1706: error pos "End of input during string" Chris@1706: | lexString' pos text ESCAPED (x :: xs) = Chris@1706: let fun esc c = lexString' (pos + 1) (c :: text) NORMAL xs Chris@1706: in case x of Chris@1706: #"\"" => esc x Chris@1706: | #"\\" => esc x Chris@1706: | #"/" => esc x Chris@1706: | #"b" => esc #"\b" Chris@1706: | #"f" => esc #"\f" Chris@1706: | #"n" => esc #"\n" Chris@1706: | #"r" => esc #"\r" Chris@1706: | #"t" => esc #"\t" Chris@1706: | _ => error pos ("Invalid escape \\" ^ Chris@1706: Char.toString x) Chris@1706: end Chris@1706: | lexString' pos text NORMAL (#"\\" :: #"u" ::a::b::c::d:: xs) = Chris@1706: if List.all Char.isHexDigit [a,b,c,d] Chris@1706: then case Word.fromString ("0wx" ^ (implode [a,b,c,d])) of Chris@1706: SOME w => (let val utf = rev (bmpToUtf8 w) in Chris@1706: lexString' (pos + 6) (utf @ text) Chris@1706: NORMAL xs Chris@1706: end Chris@1706: handle Fail err => error pos err) Chris@1706: | NONE => error pos "Invalid Unicode BMP escape sequence" Chris@1706: else error pos "Invalid Unicode BMP escape sequence" Chris@1706: | lexString' pos text NORMAL (x :: xs) = Chris@1706: if Char.ord x < 0x20 Chris@1706: then error pos "Invalid unescaped control character" Chris@1706: else Chris@1706: case x of Chris@1706: #"\"" => OK (rev text, xs, pos + 1) Chris@1706: | #"\\" => lexString' (pos + 1) text ESCAPED xs Chris@1706: | _ => lexString' (pos + 1) (x :: text) NORMAL xs Chris@1706: in Chris@1706: case lexString' pos [] NORMAL cc of Chris@1706: OK (text, rest, newpos) => Chris@1706: lex newpos (T.STRING (implode text) :: acc) rest Chris@1706: | ERROR e => ERROR e Chris@1706: end Chris@1706: Chris@1706: and lexNumber firstChar pos acc cc = Chris@1706: let val valid = explode ".+-e" Chris@1706: fun lexNumber' pos digits [] = (rev digits, [], pos) Chris@1706: | lexNumber' pos digits (x :: xs) = Chris@1706: if x = #"E" then lexNumber' (pos + 1) (#"e" :: digits) xs Chris@1706: else if Char.isDigit x orelse List.exists (fn c => x = c) valid Chris@1706: then lexNumber' (pos + 1) (x :: digits) xs Chris@1706: else (rev digits, x :: xs, pos) Chris@1706: val (digits, rest, newpos) = Chris@1706: lexNumber' (pos - 1) [] (firstChar :: cc) Chris@1706: in Chris@1706: case digits of Chris@1706: [] => token_error pos Chris@1706: | _ => lex newpos (T.NUMBER digits :: acc) rest Chris@1706: end Chris@1706: Chris@1706: and lex pos acc [] = OK (rev acc) Chris@1706: | lex pos acc (x::xs) = Chris@1706: (case x of Chris@1706: #" " => lex Chris@1706: | #"\t" => lex Chris@1706: | #"\n" => lex Chris@1706: | #"\r" => lex Chris@1706: | #"{" => lexChar T.CURLY_L Chris@1706: | #"}" => lexChar T.CURLY_R Chris@1706: | #"[" => lexChar T.SQUARE_L Chris@1706: | #"]" => lexChar T.SQUARE_R Chris@1706: | #":" => lexChar T.COLON Chris@1706: | #"," => lexChar T.COMMA Chris@1706: | #"\"" => lexString Chris@1706: | #"t" => lexTrue Chris@1706: | #"f" => lexFalse Chris@1706: | #"n" => lexNull Chris@1706: | x => lexNumber x) (pos + 1) acc xs Chris@1706: Chris@1706: fun show [] = "end of input" Chris@1706: | show (tok :: _) = T.toString tok Chris@1706: Chris@1706: fun parseNumber digits = Chris@1706: (* Note lexNumber already case-insensitised the E for us *) Chris@1706: let open Char Chris@1706: Chris@1706: fun okExpDigits [] = false Chris@1706: | okExpDigits (c :: []) = isDigit c Chris@1706: | okExpDigits (c :: cs) = isDigit c andalso okExpDigits cs Chris@1706: Chris@1706: fun okExponent [] = false Chris@1706: | okExponent (#"+" :: cs) = okExpDigits cs Chris@1706: | okExponent (#"-" :: cs) = okExpDigits cs Chris@1706: | okExponent cc = okExpDigits cc Chris@1706: Chris@1706: fun okFracTrailing [] = true Chris@1706: | okFracTrailing (c :: cs) = Chris@1706: (isDigit c andalso okFracTrailing cs) orelse Chris@1706: (c = #"e" andalso okExponent cs) Chris@1706: Chris@1706: fun okFraction [] = false Chris@1706: | okFraction (c :: cs) = Chris@1706: isDigit c andalso okFracTrailing cs Chris@1706: Chris@1706: fun okPosTrailing [] = true Chris@1706: | okPosTrailing (#"." :: cs) = okFraction cs Chris@1706: | okPosTrailing (#"e" :: cs) = okExponent cs Chris@1706: | okPosTrailing (c :: cs) = Chris@1706: isDigit c andalso okPosTrailing cs Chris@1706: Chris@1706: fun okPositive [] = false Chris@1706: | okPositive (#"0" :: []) = true Chris@1706: | okPositive (#"0" :: #"." :: cs) = okFraction cs Chris@1706: | okPositive (#"0" :: #"e" :: cs) = okExponent cs Chris@1706: | okPositive (#"0" :: cs) = false Chris@1706: | okPositive (c :: cs) = isDigit c andalso okPosTrailing cs Chris@1706: Chris@1706: fun okNumber (#"-" :: cs) = okPositive cs Chris@1706: | okNumber cc = okPositive cc Chris@1706: in Chris@1706: if okNumber digits Chris@1706: then case Real.fromString (implode digits) of Chris@1706: NONE => ERROR "Number out of range" Chris@1706: | SOME r => OK r Chris@1706: else ERROR ("Invalid number \"" ^ (implode digits) ^ "\"") Chris@1706: end Chris@1706: Chris@1706: fun parseObject (T.CURLY_R :: xs) = OK (OBJECT [], xs) Chris@1706: | parseObject tokens = Chris@1706: let fun parsePair (T.STRING key :: T.COLON :: xs) = Chris@1706: (case parseTokens xs of Chris@1706: ERROR e => ERROR e Chris@1706: | OK (j, xs) => OK ((key, j), xs)) Chris@1706: | parsePair other = Chris@1706: ERROR ("Object key/value pair expected around \"" ^ Chris@1706: show other ^ "\"") Chris@1706: fun parseObject' acc [] = ERROR "End of input during object" Chris@1706: | parseObject' acc tokens = Chris@1706: case parsePair tokens of Chris@1706: ERROR e => ERROR e Chris@1706: | OK (pair, T.COMMA :: xs) => Chris@1706: parseObject' (pair :: acc) xs Chris@1706: | OK (pair, T.CURLY_R :: xs) => Chris@1706: OK (OBJECT (rev (pair :: acc)), xs) Chris@1706: | OK (_, _) => ERROR "Expected , or } after object element" Chris@1706: in Chris@1706: parseObject' [] tokens Chris@1706: end Chris@1706: Chris@1706: and parseArray (T.SQUARE_R :: xs) = OK (ARRAY [], xs) Chris@1706: | parseArray tokens = Chris@1706: let fun parseArray' acc [] = ERROR "End of input during array" Chris@1706: | parseArray' acc tokens = Chris@1706: case parseTokens tokens of Chris@1706: ERROR e => ERROR e Chris@1706: | OK (j, T.COMMA :: xs) => parseArray' (j :: acc) xs Chris@1706: | OK (j, T.SQUARE_R :: xs) => OK (ARRAY (rev (j :: acc)), xs) Chris@1706: | OK (_, _) => ERROR "Expected , or ] after array element" Chris@1706: in Chris@1706: parseArray' [] tokens Chris@1706: end Chris@1706: Chris@1706: and parseTokens [] = ERROR "Value expected" Chris@1706: | parseTokens (tok :: xs) = Chris@1706: (case tok of Chris@1706: T.NUMBER d => (case parseNumber d of Chris@1706: OK r => OK (NUMBER r, xs) Chris@1706: | ERROR e => ERROR e) Chris@1706: | T.STRING s => OK (STRING s, xs) Chris@1706: | T.BOOL b => OK (BOOL b, xs) Chris@1706: | T.NULL => OK (NULL, xs) Chris@1706: | T.CURLY_L => parseObject xs Chris@1706: | T.SQUARE_L => parseArray xs Chris@1706: | _ => ERROR ("Unexpected token " ^ T.toString tok ^ Chris@1706: " before " ^ show xs)) Chris@1706: Chris@1706: fun parse str = Chris@1706: case lex 1 [] (explode str) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK tokens => case parseTokens tokens of Chris@1706: OK (value, []) => OK value Chris@1706: | OK (_, _) => ERROR "Extra data after input" Chris@1706: | ERROR e => ERROR e Chris@1706: Chris@1706: fun stringEscape s = Chris@1706: let fun esc x = [x, #"\\"] Chris@1706: fun escape' acc [] = rev acc Chris@1706: | escape' acc (x :: xs) = Chris@1706: escape' (case x of Chris@1706: #"\"" => esc x @ acc Chris@1706: | #"\\" => esc x @ acc Chris@1706: | #"\b" => esc #"b" @ acc Chris@1706: | #"\f" => esc #"f" @ acc Chris@1706: | #"\n" => esc #"n" @ acc Chris@1706: | #"\r" => esc #"r" @ acc Chris@1706: | #"\t" => esc #"t" @ acc Chris@1706: | _ => Chris@1706: let val c = Char.ord x Chris@1706: in Chris@1706: if c < 0x20 Chris@1706: then let val hex = Word.toString (Word.fromInt c) Chris@1706: in (rev o explode) (if c < 0x10 Chris@1706: then ("\\u000" ^ hex) Chris@1706: else ("\\u00" ^ hex)) Chris@1706: end @ acc Chris@1706: else Chris@1706: x :: acc Chris@1706: end) Chris@1706: xs Chris@1706: in Chris@1706: implode (escape' [] (explode s)) Chris@1706: end Chris@1706: Chris@1706: fun serialise json = Chris@1706: case json of Chris@1706: OBJECT pp => "{" ^ String.concatWith Chris@1706: "," (map (fn (key, value) => Chris@1706: serialise (STRING key) ^ ":" ^ Chris@1706: serialise value) pp) ^ Chris@1706: "}" Chris@1706: | ARRAY arr => "[" ^ String.concatWith "," (map serialise arr) ^ "]" Chris@1706: | NUMBER n => implode (map (fn #"~" => #"-" | c => c) Chris@1706: (explode (Real.toString n))) Chris@1706: | STRING s => "\"" ^ stringEscape s ^ "\"" Chris@1706: | BOOL b => Bool.toString b Chris@1706: | NULL => "null" Chris@1706: Chris@1706: fun serialiseIndented json = Chris@1706: let fun indent 0 = "" Chris@1706: | indent i = " " ^ indent (i - 1) Chris@1706: fun serialiseIndented' i json = Chris@1706: let val ser = serialiseIndented' (i + 1) Chris@1706: in Chris@1706: case json of Chris@1706: OBJECT [] => "{}" Chris@1706: | ARRAY [] => "[]" Chris@1706: | OBJECT pp => "{\n" ^ indent (i + 1) ^ Chris@1706: String.concatWith Chris@1706: (",\n" ^ indent (i + 1)) Chris@1706: (map (fn (key, value) => Chris@1706: ser (STRING key) ^ ": " ^ Chris@1706: ser value) pp) ^ Chris@1706: "\n" ^ indent i ^ "}" Chris@1706: | ARRAY arr => "[\n" ^ indent (i + 1) ^ Chris@1706: String.concatWith Chris@1706: (",\n" ^ indent (i + 1)) Chris@1706: (map ser arr) ^ Chris@1706: "\n" ^ indent i ^ "]" Chris@1706: | other => serialise other Chris@1706: end Chris@1706: in Chris@1706: serialiseIndented' 0 json ^ "\n" Chris@1706: end Chris@1706: Chris@1706: end Chris@1706: Chris@1706: Chris@1706: structure JsonBits :> sig Chris@1706: val load_json_from : string -> Json.json (* filename -> json *) Chris@1706: val save_json_to : string -> Json.json -> unit Chris@1706: val lookup_optional : Json.json -> string list -> Json.json option Chris@1706: val lookup_optional_string : Json.json -> string list -> string option Chris@1706: val lookup_mandatory : Json.json -> string list -> Json.json Chris@1706: val lookup_mandatory_string : Json.json -> string list -> string Chris@1706: end = struct Chris@1706: Chris@1706: fun load_json_from filename = Chris@1706: case Json.parse (FileBits.file_contents filename) of Chris@1706: Json.OK json => json Chris@1706: | Json.ERROR e => raise Fail ("Failed to parse file: " ^ e) Chris@1706: Chris@1706: fun save_json_to filename json = Chris@1706: let val jstr = Json.serialiseIndented json Chris@1706: val stream = TextIO.openOut filename Chris@1706: in Chris@1706: TextIO.output (stream, jstr); Chris@1706: TextIO.closeOut stream Chris@1706: end Chris@1706: Chris@1706: fun lookup_optional json kk = Chris@1706: let fun lookup key = Chris@1706: case json of Chris@1706: Json.OBJECT kvs => Chris@1706: (case List.find (fn (k, v) => k = key) kvs of Chris@1706: SOME (k, v) => SOME v Chris@1706: | NONE => NONE) Chris@1706: | _ => raise Fail "Object expected" Chris@1706: in Chris@1706: case kk of Chris@1706: [] => NONE Chris@1706: | key::[] => lookup key Chris@1706: | key::kk => case lookup key of Chris@1706: NONE => NONE Chris@1706: | SOME j => lookup_optional j kk Chris@1706: end Chris@1706: Chris@1706: fun lookup_optional_string json kk = Chris@1706: case lookup_optional json kk of Chris@1706: SOME (Json.STRING s) => SOME s Chris@1706: | SOME _ => raise Fail ("Value (if present) must be string: " ^ Chris@1706: (String.concatWith " -> " kk)) Chris@1706: | NONE => NONE Chris@1706: Chris@1706: fun lookup_mandatory json kk = Chris@1706: case lookup_optional json kk of Chris@1706: SOME v => v Chris@1706: | NONE => raise Fail ("Value is mandatory: " ^ Chris@1706: (String.concatWith " -> " kk) ^ " in json: " ^ Chris@1706: (Json.serialise json)) Chris@1706: Chris@1706: fun lookup_mandatory_string json kk = Chris@1706: case lookup_optional json kk of Chris@1706: SOME (Json.STRING s) => s Chris@1706: | _ => raise Fail ("Value must be string: " ^ Chris@1706: (String.concatWith " -> " kk)) Chris@1706: end Chris@1706: Chris@1706: structure Provider :> sig Chris@1706: val load_providers : Json.json -> provider list Chris@1706: val load_more_providers : provider list -> Json.json -> provider list Chris@1706: val remote_url : context -> vcs -> source -> libname -> string Chris@1706: end = struct Chris@1706: Chris@1706: val known_providers : provider list = Chris@1706: [ { Chris@1706: service = "bitbucket", Chris@1706: supports = [HG, GIT], Chris@1706: remote_spec = { Chris@1706: anon = SOME "https://bitbucket.org/{owner}/{repo}", Chris@1706: auth = SOME "ssh://{vcs}@bitbucket.org/{owner}/{repo}" Chris@1706: } Chris@1706: }, Chris@1706: { Chris@1706: service = "github", Chris@1706: supports = [GIT], Chris@1706: remote_spec = { Chris@1706: anon = SOME "https://github.com/{owner}/{repo}", Chris@1706: auth = SOME "ssh://{vcs}@github.com/{owner}/{repo}" Chris@1706: } Chris@1706: } Chris@1706: ] Chris@1706: Chris@1706: fun vcs_name vcs = Chris@1706: case vcs of GIT => "git" | Chris@1706: HG => "hg" Chris@1706: Chris@1706: fun vcs_from_name name = Chris@1706: case name of "git" => GIT Chris@1706: | "hg" => HG Chris@1706: | other => raise Fail ("Unknown vcs name \"" ^ name ^ "\"") Chris@1706: Chris@1706: fun load_more_providers previously_loaded json = Chris@1706: let open JsonBits Chris@1706: fun load pjson pname : provider = Chris@1706: { Chris@1706: service = pname, Chris@1706: supports = Chris@1706: case lookup_mandatory pjson ["vcs"] of Chris@1706: Json.ARRAY vv => Chris@1706: map (fn (Json.STRING v) => vcs_from_name v Chris@1706: | _ => raise Fail "Strings expected in vcs array") Chris@1706: vv Chris@1706: | _ => raise Fail "Array expected for vcs", Chris@1706: remote_spec = { Chris@1706: anon = lookup_optional_string pjson ["anon"], Chris@1706: auth = lookup_optional_string pjson ["auth"] Chris@1706: } Chris@1706: } Chris@1706: val loaded = Chris@1721: case lookup_optional json ["services"] of Chris@1706: NONE => [] Chris@1706: | SOME (Json.OBJECT pl) => map (fn (k, v) => load v k) pl Chris@1721: | _ => raise Fail "Object expected for services in config" Chris@1706: val newly_loaded = Chris@1706: List.filter (fn p => not (List.exists (fn pp => #service p = Chris@1706: #service pp) Chris@1706: previously_loaded)) Chris@1706: loaded Chris@1706: in Chris@1706: previously_loaded @ newly_loaded Chris@1706: end Chris@1706: Chris@1706: fun load_providers json = Chris@1706: load_more_providers known_providers json Chris@1706: Chris@1706: fun expand_spec spec { vcs, service, owner, repo } login = Chris@1706: (* ugly *) Chris@1706: let fun replace str = Chris@1706: case str of Chris@1706: "vcs" => vcs_name vcs Chris@1706: | "service" => service Chris@1706: | "owner" => Chris@1706: (case owner of Chris@1706: SOME ostr => ostr Chris@1706: | NONE => raise Fail ("Owner not specified for service " ^ Chris@1706: service)) Chris@1706: | "repo" => repo Chris@1706: | "account" => Chris@1706: (case login of Chris@1706: SOME acc => acc Chris@1706: | NONE => raise Fail ("Account not given for service " ^ Chris@1706: service)) Chris@1706: | other => raise Fail ("Unknown variable \"" ^ other ^ Chris@1706: "\" in spec for service " ^ service) Chris@1706: fun expand' acc sstr = Chris@1706: case Substring.splitl (fn c => c <> #"{") sstr of Chris@1706: (pfx, sfx) => Chris@1706: if Substring.isEmpty sfx Chris@1706: then rev (pfx :: acc) Chris@1706: else Chris@1706: case Substring.splitl (fn c => c <> #"}") sfx of Chris@1706: (tok, remainder) => Chris@1706: if Substring.isEmpty remainder Chris@1706: then rev (tok :: pfx :: acc) Chris@1706: else let val replacement = Chris@1706: replace Chris@1706: (* tok begins with "{": *) Chris@1706: (Substring.string Chris@1706: (Substring.triml 1 tok)) Chris@1706: in Chris@1706: expand' (Substring.full replacement :: Chris@1706: pfx :: acc) Chris@1706: (* remainder begins with "}": *) Chris@1706: (Substring.triml 1 remainder) Chris@1706: end Chris@1706: in Chris@1706: Substring.concat (expand' [] (Substring.full spec)) Chris@1706: end Chris@1706: Chris@1706: fun provider_url req login providers = Chris@1706: case providers of Chris@1706: [] => raise Fail ("Unknown service \"" ^ (#service req) ^ Chris@1706: "\" for vcs \"" ^ (vcs_name (#vcs req)) ^ "\"") Chris@1706: | ({ service, supports, remote_spec : remote_spec } :: rest) => Chris@1706: if service <> (#service req) orelse Chris@1706: not (List.exists (fn v => v = (#vcs req)) supports) Chris@1706: then provider_url req login rest Chris@1706: else Chris@1706: case (login, #auth remote_spec, #anon remote_spec) of Chris@1706: (SOME _, SOME auth, _) => expand_spec auth req login Chris@1706: | (SOME _, _, SOME anon) => expand_spec anon req NONE Chris@1706: | (NONE, _, SOME anon) => expand_spec anon req NONE Chris@1706: | _ => raise Fail ("No suitable anon/auth URL spec " ^ Chris@1706: "provided for service \"" ^ service ^ "\"") Chris@1706: Chris@1706: fun login_for ({ accounts, ... } : context) service = Chris@1706: case List.find (fn a => service = #service a) accounts of Chris@1706: SOME { login, ... } => SOME login Chris@1706: | NONE => NONE Chris@1706: Chris@1706: fun remote_url (context : context) vcs source libname = Chris@1706: case source of Chris@1721: URL_SOURCE u => u Chris@1721: | SERVICE_SOURCE { service, owner, repo } => Chris@1706: provider_url { vcs = vcs, Chris@1706: service = service, Chris@1706: owner = owner, Chris@1706: repo = case repo of Chris@1706: SOME r => r Chris@1706: | NONE => libname } Chris@1706: (login_for context service) Chris@1706: (#providers context) Chris@1706: end Chris@1706: Chris@1706: structure HgControl :> VCS_CONTROL = struct Chris@1706: Chris@1706: type vcsstate = { id: string, modified: bool, Chris@1706: branch: string, tags: string list } Chris@1706: Chris@1706: val hg_args = [ "--config", "ui.interactive=true" ] Chris@1706: Chris@1706: fun hg_command context libname args = Chris@1706: FileBits.command context libname ("hg" :: hg_args @ args) Chris@1706: Chris@1706: fun hg_command_output context libname args = Chris@1706: FileBits.command_output context libname ("hg" :: hg_args @ args) Chris@1706: Chris@1706: fun exists context libname = Chris@1706: OK (OS.FileSys.isDir (FileBits.subpath context libname ".hg")) Chris@1706: handle _ => OK false Chris@1706: Chris@1706: fun remote_for context (libname, source) = Chris@1706: Provider.remote_url context HG source libname Chris@1706: Chris@1706: fun current_state context libname : vcsstate result = Chris@1706: let fun is_branch text = text <> "" andalso #"(" = hd (explode text) Chris@1706: and extract_branch b = Chris@1706: if is_branch b (* need to remove enclosing parens *) Chris@1706: then (implode o rev o tl o rev o tl o explode) b Chris@1706: else "default" Chris@1706: and is_modified id = id <> "" andalso #"+" = hd (rev (explode id)) Chris@1706: and extract_id id = Chris@1706: if is_modified id (* need to remove trailing "+" *) Chris@1706: then (implode o rev o tl o rev o explode) id Chris@1706: else id Chris@1706: and split_tags tags = String.tokens (fn c => c = #"/") tags Chris@1706: and state_for (id, branch, tags) = Chris@1706: OK { id = extract_id id, Chris@1706: modified = is_modified id, Chris@1706: branch = extract_branch branch, Chris@1706: tags = split_tags tags } Chris@1706: in Chris@1706: case hg_command_output context libname ["id"] of Chris@1706: ERROR e => ERROR e Chris@1706: | OK out => Chris@1706: case String.tokens (fn x => x = #" ") out of Chris@1706: [id, branch, tags] => state_for (id, branch, tags) Chris@1706: | [id, other] => if is_branch other Chris@1706: then state_for (id, other, "") Chris@1706: else state_for (id, "", other) Chris@1706: | [id] => state_for (id, "", "") Chris@1706: | _ => ERROR ("Unexpected output from hg id: " ^ out) Chris@1706: end Chris@1706: Chris@1706: fun branch_name branch = case branch of Chris@1706: DEFAULT_BRANCH => "default" Chris@1706: | BRANCH "" => "default" Chris@1706: | BRANCH b => b Chris@1706: Chris@1706: fun id_of context libname = Chris@1706: case current_state context libname of Chris@1706: ERROR e => ERROR e Chris@1706: | OK { id, ... } => OK id Chris@1706: Chris@1706: fun is_at context (libname, id_or_tag) = Chris@1706: case current_state context libname of Chris@1706: ERROR e => ERROR e Chris@1706: | OK { id, tags, ... } => Chris@1706: OK (String.isPrefix id_or_tag id orelse Chris@1706: String.isPrefix id id_or_tag orelse Chris@1706: List.exists (fn t => t = id_or_tag) tags) Chris@1706: Chris@1706: fun is_on_branch context (libname, b) = Chris@1706: case current_state context libname of Chris@1706: ERROR e => ERROR e Chris@1706: | OK { branch, ... } => OK (branch = branch_name b) Chris@1706: Chris@1706: fun is_newest_locally context (libname, branch) = Chris@1706: case hg_command_output context libname Chris@1706: ["log", "-l1", Chris@1706: "-b", branch_name branch, Chris@1706: "--template", "{node}"] of Chris@1706: ERROR e => ERROR e Chris@1706: | OK newest_in_repo => is_at context (libname, newest_in_repo) Chris@1706: Chris@1706: fun pull context libname = Chris@1706: hg_command context libname Chris@1706: (if FileBits.verbose () Chris@1706: then ["pull"] Chris@1706: else ["pull", "-q"]) Chris@1706: Chris@1706: fun is_newest context (libname, branch) = Chris@1706: case is_newest_locally context (libname, branch) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK false => OK false Chris@1706: | OK true => Chris@1706: case pull context libname of Chris@1706: ERROR e => ERROR e Chris@1706: | _ => is_newest_locally context (libname, branch) Chris@1706: Chris@1706: fun is_modified_locally context libname = Chris@1706: case current_state context libname of Chris@1706: ERROR e => ERROR e Chris@1706: | OK { modified, ... } => OK modified Chris@1706: Chris@1706: fun checkout context (libname, source, branch) = Chris@1706: let val url = remote_for context (libname, source) Chris@1706: in Chris@1706: case FileBits.mkpath (FileBits.extpath context) of Chris@1706: ERROR e => ERROR e Chris@1706: | _ => hg_command context "" Chris@1706: ["clone", "-u", branch_name branch, Chris@1706: url, libname] Chris@1706: end Chris@1706: Chris@1706: fun update context (libname, branch) = Chris@1706: let val pull_result = pull context libname Chris@1706: in Chris@1706: case hg_command context libname ["update", branch_name branch] of Chris@1706: ERROR e => ERROR e Chris@1706: | _ => Chris@1706: case pull_result of Chris@1706: ERROR e => ERROR e Chris@1706: | _ => id_of context libname Chris@1706: end Chris@1706: Chris@1706: fun update_to context (libname, "") = Chris@1706: ERROR "Non-empty id (tag or revision id) required for update_to" Chris@1706: | update_to context (libname, id) = Chris@1723: let val pull_result = pull context libname Chris@1723: in Chris@1723: case hg_command context libname ["update", "-r", id] of Chris@1723: OK _ => id_of context libname Chris@1723: | ERROR e => Chris@1723: case pull_result of Chris@1723: ERROR e' => ERROR e' (* this was the ur-error *) Chris@1723: | _ => ERROR e Chris@1723: end Chris@1706: Chris@1706: end Chris@1706: Chris@1706: structure GitControl :> VCS_CONTROL = struct Chris@1706: Chris@1706: (* With Git repos we always operate in detached HEAD state. Even Chris@1706: the master branch is checked out using the remote reference, Chris@1706: origin/master. *) Chris@1706: Chris@1706: fun git_command context libname args = Chris@1706: FileBits.command context libname ("git" :: args) Chris@1706: Chris@1706: fun git_command_output context libname args = Chris@1706: FileBits.command_output context libname ("git" :: args) Chris@1706: Chris@1706: fun exists context libname = Chris@1706: OK (OS.FileSys.isDir (FileBits.subpath context libname ".git")) Chris@1706: handle _ => OK false Chris@1706: Chris@1706: fun remote_for context (libname, source) = Chris@1706: Provider.remote_url context GIT source libname Chris@1706: Chris@1706: fun branch_name branch = case branch of Chris@1706: DEFAULT_BRANCH => "master" Chris@1706: | BRANCH "" => "master" Chris@1706: | BRANCH b => b Chris@1706: Chris@1706: fun remote_branch_name branch = "origin/" ^ branch_name branch Chris@1706: Chris@1706: fun checkout context (libname, source, branch) = Chris@1706: let val url = remote_for context (libname, source) Chris@1706: in Chris@1706: case FileBits.mkpath (FileBits.extpath context) of Chris@1706: OK () => git_command context "" Chris@1706: ["clone", "-b", Chris@1706: branch_name branch, Chris@1706: url, libname] Chris@1706: | ERROR e => ERROR e Chris@1706: end Chris@1706: Chris@1706: (* NB git rev-parse HEAD shows revision id of current checkout; Chris@1706: git rev-list -1 shows revision id of revision with that tag *) Chris@1706: Chris@1706: fun id_of context libname = Chris@1706: git_command_output context libname ["rev-parse", "HEAD"] Chris@1706: Chris@1706: fun is_at context (libname, id_or_tag) = Chris@1706: case id_of context libname of Chris@1706: ERROR e => ERROR e Chris@1706: | OK id => Chris@1706: if String.isPrefix id_or_tag id orelse Chris@1706: String.isPrefix id id_or_tag Chris@1706: then OK true Chris@1706: else Chris@1706: case git_command_output context libname Chris@1723: ["show-ref", Chris@1723: "refs/tags/" ^ id_or_tag] of Chris@1723: OK "" => OK false Chris@1723: | ERROR _ => OK false Chris@1723: | OK s => OK (id = hd (String.tokens (fn c => c = #" ") s)) Chris@1706: Chris@1706: fun branch_tip context (libname, branch) = Chris@1706: git_command_output context libname Chris@1706: ["rev-list", "-1", Chris@1706: remote_branch_name branch] Chris@1706: Chris@1706: fun is_newest_locally context (libname, branch) = Chris@1706: case branch_tip context (libname, branch) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK rev => is_at context (libname, rev) Chris@1706: Chris@1706: fun is_on_branch context (libname, branch) = Chris@1706: case branch_tip context (libname, branch) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK rev => Chris@1706: case is_at context (libname, rev) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK true => OK true Chris@1706: | OK false => Chris@1706: case git_command context libname Chris@1706: ["merge-base", "--is-ancestor", Chris@1706: "HEAD", remote_branch_name branch] of Chris@1706: ERROR e => OK false (* cmd returns non-zero for no *) Chris@1706: | _ => OK true Chris@1706: Chris@1706: fun is_newest context (libname, branch) = Chris@1706: case is_newest_locally context (libname, branch) of Chris@1706: ERROR e => ERROR e Chris@1706: | OK false => OK false Chris@1706: | OK true => Chris@1706: case git_command context libname ["fetch"] of Chris@1706: ERROR e => ERROR e Chris@1706: | _ => is_newest_locally context (libname, branch) Chris@1706: Chris@1706: fun is_modified_locally context libname = Chris@1706: case git_command_output context libname ["status", "--porcelain"] of Chris@1706: ERROR e => ERROR e Chris@1706: | OK "" => OK false Chris@1706: | OK _ => OK true Chris@1706: Chris@1706: (* This function updates to the latest revision on a branch rather Chris@1706: than to a specific id or tag. We can't just checkout the given Chris@1706: branch, as that will succeed even if the branch isn't up to Chris@1706: date. We could checkout the branch and then fetch and merge, Chris@1706: but it's perhaps cleaner not to maintain a local branch at all, Chris@1706: but instead checkout the remote branch as a detached head. *) Chris@1706: Chris@1706: fun update context (libname, branch) = Chris@1706: case git_command context libname ["fetch"] of Chris@1706: ERROR e => ERROR e Chris@1706: | _ => Chris@1706: case git_command context libname ["checkout", "--detach", Chris@1706: remote_branch_name branch] of Chris@1706: ERROR e => ERROR e Chris@1706: | _ => id_of context libname Chris@1706: Chris@1706: (* This function is dealing with a specific id or tag, so if we Chris@1723: can successfully check it out (detached) then that's all we Chris@1723: need to do, regardless of whether fetch succeeded or not. We do Chris@1723: attempt the fetch first, though, purely in order to avoid ugly Chris@1723: error messages in the common case where we're being asked to Chris@1723: update to a new pin (from the lock file) that hasn't been Chris@1723: fetched yet. *) Chris@1706: Chris@1706: fun update_to context (libname, "") = Chris@1706: ERROR "Non-empty id (tag or revision id) required for update_to" Chris@1706: | update_to context (libname, id) = Chris@1723: let val fetch_result = git_command context libname ["fetch"] Chris@1723: in Chris@1723: case git_command context libname ["checkout", "--detach", id] of Chris@1723: OK _ => id_of context libname Chris@1723: | ERROR e => Chris@1723: case fetch_result of Chris@1723: ERROR e' => ERROR e' (* this was the ur-error *) Chris@1723: | _ => ERROR e Chris@1723: end Chris@1723: Chris@1706: end Chris@1706: Chris@1706: structure AnyLibControl :> LIB_CONTROL = struct Chris@1706: Chris@1706: structure H = LibControlFn(HgControl) Chris@1706: structure G = LibControlFn(GitControl) Chris@1706: Chris@1706: fun review context (spec as { vcs, ... } : libspec) = Chris@1706: (fn HG => H.review | GIT => G.review) vcs context spec Chris@1706: Chris@1706: fun status context (spec as { vcs, ... } : libspec) = Chris@1706: (fn HG => H.status | GIT => G.status) vcs context spec Chris@1706: Chris@1706: fun update context (spec as { vcs, ... } : libspec) = Chris@1706: (fn HG => H.update | GIT => G.update) vcs context spec Chris@1706: end Chris@1706: Chris@1706: fun load_libspec spec_json lock_json libname : libspec = Chris@1706: let open JsonBits Chris@1706: val libobj = lookup_mandatory spec_json ["libs", libname] Chris@1706: val vcs = lookup_mandatory_string libobj ["vcs"] Chris@1706: val retrieve = lookup_optional_string libobj Chris@1706: val service = retrieve ["service"] Chris@1706: val owner = retrieve ["owner"] Chris@1706: val repo = retrieve ["repository"] Chris@1706: val url = retrieve ["url"] Chris@1706: val branch = retrieve ["branch"] Chris@1706: val user_pin = retrieve ["pin"] Chris@1706: val lock_pin = case lookup_optional lock_json ["libs", libname] of Chris@1706: SOME ll => lookup_optional_string ll ["pin"] Chris@1706: | NONE => NONE Chris@1706: in Chris@1706: { Chris@1706: libname = libname, Chris@1706: vcs = case vcs of Chris@1706: "hg" => HG Chris@1706: | "git" => GIT Chris@1706: | other => raise Fail ("Unknown version-control system \"" ^ Chris@1706: other ^ "\""), Chris@1706: source = case (url, service, owner, repo) of Chris@1721: (SOME u, NONE, _, _) => URL_SOURCE u Chris@1706: | (NONE, SOME ss, owner, repo) => Chris@1721: SERVICE_SOURCE { service = ss, owner = owner, repo = repo } Chris@1706: | _ => raise Fail ("Must have exactly one of service " ^ Chris@1706: "or url string"), Chris@1706: pin = case lock_pin of Chris@1706: SOME p => PINNED p Chris@1706: | NONE => Chris@1706: case user_pin of Chris@1706: SOME p => PINNED p Chris@1706: | NONE => UNPINNED, Chris@1706: branch = case branch of Chris@1706: SOME b => BRANCH b Chris@1706: | NONE => DEFAULT_BRANCH Chris@1706: } Chris@1706: end Chris@1706: Chris@1706: fun load_userconfig () : userconfig = Chris@1706: let val home = FileBits.homedir () Chris@1706: val conf_json = Chris@1706: JsonBits.load_json_from Chris@1706: (OS.Path.joinDirFile { Chris@1706: dir = home, Chris@1706: file = VextFilenames.user_config_file }) Chris@1706: handle IO.Io _ => Json.OBJECT [] Chris@1706: in Chris@1706: { Chris@1706: accounts = case JsonBits.lookup_optional conf_json ["accounts"] of Chris@1706: NONE => [] Chris@1706: | SOME (Json.OBJECT aa) => Chris@1706: map (fn (k, (Json.STRING v)) => Chris@1706: { service = k, login = v } Chris@1706: | _ => raise Fail Chris@1706: "String expected for account name") Chris@1706: aa Chris@1706: | _ => raise Fail "Array expected for accounts", Chris@1706: providers = Provider.load_providers conf_json Chris@1706: } Chris@1706: end Chris@1706: Chris@1706: fun load_project (userconfig : userconfig) rootpath use_locks : project = Chris@1706: let val spec_file = FileBits.project_spec_path rootpath Chris@1706: val lock_file = FileBits.project_lock_path rootpath Chris@1706: val _ = if OS.FileSys.access (spec_file, [OS.FileSys.A_READ]) Chris@1706: handle OS.SysErr _ => false Chris@1706: then () Chris@1706: else raise Fail ("Failed to open project spec file " ^ Chris@1706: (VextFilenames.project_file) ^ " in " ^ Chris@1706: rootpath ^ Chris@1706: ".\nPlease ensure the spec file is in the " ^ Chris@1706: "project root and run this from there.") Chris@1706: val spec_json = JsonBits.load_json_from spec_file Chris@1706: val lock_json = if use_locks Chris@1706: then JsonBits.load_json_from lock_file Chris@1706: handle IO.Io _ => Json.OBJECT [] Chris@1706: else Json.OBJECT [] Chris@1706: val extdir = JsonBits.lookup_mandatory_string spec_json Chris@1706: ["config", "extdir"] Chris@1706: val spec_libs = JsonBits.lookup_optional spec_json ["libs"] Chris@1706: val lock_libs = JsonBits.lookup_optional lock_json ["libs"] Chris@1706: val providers = Provider.load_more_providers Chris@1706: (#providers userconfig) spec_json Chris@1706: val libnames = case spec_libs of Chris@1706: NONE => [] Chris@1706: | SOME (Json.OBJECT ll) => map (fn (k, v) => k) ll Chris@1706: | _ => raise Fail "Object expected for libs" Chris@1706: in Chris@1706: { Chris@1706: context = { Chris@1706: rootpath = rootpath, Chris@1706: extdir = extdir, Chris@1706: providers = providers, Chris@1706: accounts = #accounts userconfig Chris@1706: }, Chris@1706: libs = map (load_libspec spec_json lock_json) libnames Chris@1706: } Chris@1706: end Chris@1706: Chris@1706: fun save_lock_file rootpath locks = Chris@1706: let val lock_file = FileBits.project_lock_path rootpath Chris@1706: open Json Chris@1706: val lock_json = Chris@1706: OBJECT [ Chris@1706: ("libs", OBJECT Chris@1706: (map (fn { libname, id_or_tag } => Chris@1706: (libname, Chris@1706: OBJECT [ ("pin", STRING id_or_tag) ])) Chris@1706: locks)) Chris@1706: ] Chris@1706: in Chris@1706: JsonBits.save_json_to lock_file lock_json Chris@1706: end Chris@1706: Chris@1706: fun pad_to n str = Chris@1706: if n <= String.size str then str Chris@1706: else pad_to n (str ^ " ") Chris@1706: Chris@1706: fun hline_to 0 = "" Chris@1706: | hline_to n = "-" ^ hline_to (n-1) Chris@1706: Chris@1706: val libname_width = 25 Chris@1706: val libstate_width = 11 Chris@1706: val localstate_width = 9 Chris@1706: val notes_width = 5 Chris@1706: val divider = " | " Chris@1706: Chris@1706: fun print_status_header () = Chris@1706: print ("\r" ^ pad_to 80 "" ^ "\n " ^ Chris@1706: pad_to libname_width "Library" ^ divider ^ Chris@1706: pad_to libstate_width "State" ^ divider ^ Chris@1706: pad_to localstate_width "Local" ^ divider ^ Chris@1706: "Notes" ^ "\n " ^ Chris@1706: hline_to libname_width ^ "-+-" ^ Chris@1706: hline_to libstate_width ^ "-+-" ^ Chris@1706: hline_to localstate_width ^ "-+-" ^ Chris@1706: hline_to notes_width ^ "\n") Chris@1706: Chris@1706: fun print_outcome_header () = Chris@1706: print ("\r" ^ pad_to 80 "" ^ "\n " ^ Chris@1706: pad_to libname_width "Library" ^ divider ^ Chris@1706: pad_to libstate_width "Outcome" ^ divider ^ Chris@1706: "Notes" ^ "\n " ^ Chris@1706: hline_to libname_width ^ "-+-" ^ Chris@1706: hline_to libstate_width ^ "-+-" ^ Chris@1706: hline_to notes_width ^ "\n") Chris@1706: Chris@1706: fun print_status with_network (libname, status) = Chris@1706: let val libstate_str = Chris@1706: case status of Chris@1706: OK (ABSENT, _) => "Absent" Chris@1706: | OK (CORRECT, _) => if with_network then "Correct" else "Present" Chris@1706: | OK (SUPERSEDED, _) => "Superseded" Chris@1706: | OK (WRONG, _) => "Wrong" Chris@1706: | ERROR _ => "Error" Chris@1706: val localstate_str = Chris@1706: case status of Chris@1706: OK (_, MODIFIED) => "Modified" Chris@1706: | OK (_, UNMODIFIED) => "Clean" Chris@1706: | _ => "" Chris@1706: val error_str = Chris@1706: case status of Chris@1706: ERROR e => e Chris@1706: | _ => "" Chris@1706: in Chris@1706: print (" " ^ Chris@1706: pad_to libname_width libname ^ divider ^ Chris@1706: pad_to libstate_width libstate_str ^ divider ^ Chris@1706: pad_to localstate_width localstate_str ^ divider ^ Chris@1706: error_str ^ "\n") Chris@1706: end Chris@1706: Chris@1706: fun print_update_outcome (libname, outcome) = Chris@1706: let val outcome_str = Chris@1706: case outcome of Chris@1706: OK id => "Ok" Chris@1706: | ERROR e => "Failed" Chris@1706: val error_str = Chris@1706: case outcome of Chris@1706: ERROR e => e Chris@1706: | _ => "" Chris@1706: in Chris@1706: print (" " ^ Chris@1706: pad_to libname_width libname ^ divider ^ Chris@1706: pad_to libstate_width outcome_str ^ divider ^ Chris@1706: error_str ^ "\n") Chris@1706: end Chris@1706: Chris@1706: fun act_and_print action print_header print_line (libs : libspec list) = Chris@1706: let val lines = map (fn lib => (#libname lib, action lib)) libs Chris@1706: val _ = print_header () Chris@1706: in Chris@1706: app print_line lines; Chris@1706: lines Chris@1706: end Chris@1708: Chris@1708: fun return_code_for outcomes = Chris@1708: foldl (fn ((_, result), acc) => Chris@1708: case result of Chris@1708: ERROR _ => OS.Process.failure Chris@1708: | _ => acc) Chris@1708: OS.Process.success Chris@1708: outcomes Chris@1706: Chris@1706: fun status_of_project ({ context, libs } : project) = Chris@1708: return_code_for (act_and_print (AnyLibControl.status context) Chris@1708: print_status_header (print_status false) Chris@1708: libs) Chris@1706: Chris@1706: fun review_project ({ context, libs } : project) = Chris@1708: return_code_for (act_and_print (AnyLibControl.review context) Chris@1708: print_status_header (print_status true) Chris@1708: libs) Chris@1706: Chris@1706: fun update_project ({ context, libs } : project) = Chris@1706: let val outcomes = act_and_print Chris@1706: (AnyLibControl.update context) Chris@1706: print_outcome_header print_update_outcome libs Chris@1708: val locks = Chris@1708: List.concat Chris@1708: (map (fn (libname, result) => Chris@1708: case result of Chris@1708: ERROR _ => [] Chris@1708: | OK id => [{ libname = libname, id_or_tag = id }]) Chris@1708: outcomes) Chris@1708: val return_code = return_code_for outcomes Chris@1706: in Chris@1708: if OS.Process.isSuccess return_code Chris@1708: then save_lock_file (#rootpath context) locks Chris@1708: else (); Chris@1708: return_code Chris@1706: end Chris@1706: Chris@1706: fun load_local_project use_locks = Chris@1706: let val userconfig = load_userconfig () Chris@1706: val rootpath = OS.FileSys.getDir () Chris@1706: in Chris@1706: load_project userconfig rootpath use_locks Chris@1706: end Chris@1706: Chris@1706: fun with_local_project use_locks f = Chris@1708: let val return_code = f (load_local_project use_locks) Chris@1708: handle e => Chris@1708: (print ("Failed with exception: " ^ Chris@1708: (exnMessage e) ^ "\n"); Chris@1708: OS.Process.failure) Chris@1708: val _ = print "\n"; Chris@1708: in Chris@1708: return_code Chris@1708: end Chris@1706: Chris@1706: fun review () = with_local_project false review_project Chris@1706: fun status () = with_local_project false status_of_project Chris@1706: fun update () = with_local_project false update_project Chris@1706: fun install () = with_local_project true update_project Chris@1706: Chris@1706: fun version () = Chris@1708: (print ("v" ^ vext_version ^ "\n"); Chris@1708: OS.Process.success) Chris@1706: Chris@1706: fun usage () = Chris@1706: (print "\nVext "; Chris@1706: version (); Chris@1706: print ("\nA simple manager for third-party source code dependencies.\n\n" Chris@1706: ^ "Usage:\n\n" Chris@1706: ^ " vext \n\n" Chris@1706: ^ "where is one of:\n\n" Chris@1716: ^ " status print quick report on local status only, without using network\n" Chris@1706: ^ " review check configured libraries against their providers, and report\n" Chris@1706: ^ " install update configured libraries according to project specs and lock file\n" Chris@1706: ^ " update update configured libraries and lock file according to project specs\n" Chris@1708: ^ " version print the Vext version number and exit\n\n"); Chris@1708: OS.Process.failure) Chris@1706: Chris@1706: fun vext args = Chris@1708: let val return_code = Chris@1708: case args of Chris@1708: ["review"] => review () Chris@1708: | ["status"] => status () Chris@1708: | ["install"] => install () Chris@1708: | ["update"] => update () Chris@1708: | ["version"] => version () Chris@1708: | _ => usage () Chris@1708: in Chris@1708: OS.Process.exit return_code; Chris@1708: () Chris@1708: end Chris@1706: Chris@1706: fun main () = Chris@1706: vext (CommandLine.arguments ())