text
stringlengths
12
786k
type meta_file = std_file * bool
let meta_file ( ? lint = true ) true ? install file = std_file ? install file , lint
type opam_file = std_file * bool * string list option
let opam_file ( ? lint = true ) true ( ? lint_deps_excluding = Some [ ] ) ? install file = std_file ? install file , lint , lint_deps_excluding
let std_files_installs readmes licenses change_logs metas opams = let add field ( p , install ) install acc = if install then field p :: acc else acc in List . fold_right ( add Topkg_install . doc ) doc readmes @@ List . fold_right ( add Topkg_install . doc ) doc licenses @@ List . fold_right ...
type t = { name : string ; delegate : Topkg_cmd . t option ; readmes : std_file list ; licenses : std_file list ; change_logs : std_file list ; metas : meta_file list ; opams : opam_file list ; lint_files : Topkg_fpath . t list option ; lint_custom ( : unit -> R . msg result list ) l...
let v ? delegate ( ? readmes = [ ( " README . md " , true ) true ] true ) true ( ? licenses = [ ( " LICENSE . md " , true ) true ] true ) true ( ? change_logs = [ ( " CHANGES . md " , true ) true ] true ) true ( ? metas = [ meta_file " pkg / META ] ...
let empty = v " " ( fun _ -> Ok [ ] )
let with_name_and_build_dir ? name ? build_dir p = let name = match name with None -> p . name | Some n -> n in let build = match build_dir with | None -> p . build | Some build_dir -> Topkg_build . with_dir p . build build_dir in { p with name ; build }
let name p = p . name
let delegate p = p . delegate
let readmes p = List . map fst p . readmes
let change_logs p = List . map fst p . change_logs
let licenses p = List . map fst p . licenses
let distrib p = p . distrib
let build p = p . build
let install p c = p . installs c >>= fun installs -> let std_files = std_files_installs p . readmes p . licenses p . change_logs p . metas p . opams in Ok ( List . rev_append std_files installs ) installs
let std_files p = List . map fst p . readmes @ List . map fst p . licenses @ List . map fst p . change_logs @ List ( . rev_append ( rev_map ( fun ( m , _ ) _ -> fst m ) m p . metas ) metas ( rev ( rev_map ( fun ( o , _ , _ ) _ -> fst o ) o p . opams ) opams ) o...
let build_dir p = Topkg_build . dir p . build
let opam ~ name p = let has_name ( o , _ , _ ) _ = Topkg_fpath ( . basename @@ rem_ext @@ fst o ) o = name in let opams = p . opams in match try Some ( List . find has_name opams ) opams with Not_found -> None with | Some ( opam , _ , _ ) _ -> fst opam | None -> if name <...
let codec = let stub _ = invalid_arg " not executable outside package definition " in let string_list_option = Topkg_codec ( . option @@ list string ) string in let std_file = Topkg_codec ( . pair string bool ) bool in let meta_file = Topkg_codec ( . pair std_file bool ) bool in let opam_fi...
let distrib_version_opam_files p ~ version ~ opam_adds = let version = Topkg_string . drop_initial_v version in let version_opam_file acc ( ( file , _ ) _ , _ , _ ) _ = acc >>= fun ( ) -> Topkg_os . File . read file >>= fun o -> Ok ( Topkg_string . strf " version : " \% s " \\...
let distrib_uri p = Topkg_distrib . uri p . distrib
let distrib_prepare p ~ dist_build_dir ~ name ~ version ~ opam ~ opam_adds = let d = distrib p in let ws = Topkg_distrib . watermarks d in let ws_defs = Topkg_distrib . define_watermarks ws ~ name ~ version ~ opam in Topkg_os . Dir . set_current dist_build_dir >>= fun ( ) -> Topkg_distrib . files_t...
let distrib_prepare_pin p = let name = name p in let opam = opam ~ name p in let d = distrib p in let ws = Topkg_distrib . watermarks d in Topkg_vcs . get ( ) >>= fun repo -> Topkg_vcs . describe ~ dirty : true repo >>= fun version -> Ok ( OkTopkg_distrib . define_watermarks ws ~ name ~ version...
let publish_artefacts p = Topkg_publish . artefacts p . publish
let tests_file p = Topkg_fpath ( . build_dir p // Topkg_string . strf " _topkg -% s . tests " ( name p ) p ) p
let tests_file_codec = Topkg_codec ( . option @@ list Topkg_test . codec ) codec
let write_tests_file p tests = Topkg_codec . write ( tests_file p ) p tests_file_codec tests
let tests_run tests ~ args = let run_test root_dir acc t = let exec = Topkg_test . exec t in let args = match args with Some args -> args | None -> Topkg_test . args t in let cmd = Topkg_cmd ( . v Topkg_fpath ( . root_dir // exec ) exec %% args ) args in let run ( ) = Topkg_log . info ( ...
let tests_list tests = let list_test t = let args = Topkg_cmd . to_list ( Topkg_test . args t ) t in let args = String . concat " " ( List . map Filename . quote args ) args in let exec = Topkg_test . exec t in let run = if Topkg_test . run t then [ " default ] default " else " " in...
let tests_select queries tests = match queries with let query q = let add_test acc t = let exec = Topkg_test . exec t in if q = exec then t :: acc else let exec = Topkg_fpath . basename exec in if q = exec then t :: acc else let exec = Topkg_fpath . rem_ext exec in if q = exec then t :: acc else acc in m...
let test p ~ list ~ tests : queries ~ args = let tests_file = tests_file p in Topkg_os . File . exists tests_file >>= function | false -> R . error_msgf " % s : no such file . Did you forget to build the package " ? tests_file | true -> Topkg_codec . read tests_file tests_file_codec >>= function | ...
let install_file p = p . name ^ " . install "
let write_opam_install_file p install = let file = install_file p in let install = Topkg_opam . Install . to_string install in Topkg_os . File . write file install >>| fun ( ) -> Topkg_log . info ( fun m -> m " Wrote opam install file % s " file ) file
let run_build_hook kind hook c = ( hook c ) c |> R . reword_error_msg ~ replace : true ( fun e -> R . msgf " % s - build hook failed : % s " kind e ) e
let build p ~ kind c os = match kind with install p c >>= fun is -> Ok ( Topkg_install . to_build ~ header : p . name c os is ) is >>= fun ( targets , install , tests ) tests -> match kind = ` Dry_run with | true -> write_opam_install_file p install >>= fun ( ) -> Ok 0 | false -> l...
let clean p os = let file = install_file p in let build_dir = build_dir p in Topkg_os . File . delete file >>= fun ( ) -> ( Topkg_build . clean p . build ) build os ~ build_dir >>= fun ( ) -> Ok 0
let lint_custom p = p . lint_custom
let lint_files p = match p . lint_files with
let lint_metas p = List . map ( fun ( ( p , _ ) _ , lint ) lint -> ( p , lint ) lint ) lint p . metas
let lint_opams p = List . map ( fun ( ( p , _ ) _ , lint , lint_deps ) lint_deps -> ( p , lint , lint_deps ) lint_deps ) lint_deps p . opams
let ( >>= ) v f = match v with Ok v -> f v | Error _ as e -> e
let ( >>| ) v f = match v with Ok v -> Ok ( f v ) v | Error _ as e -> e
type ( ' a , ' b ) ' b r = ( ' a , ' b ) ' b result = Ok of ' a | Error of ' b
type ' a result = ( ' a , [ ` Msg of string ] string ) string r
module R = struct type msg = [ ` Msg of string ] let msgf fmt = let kmsg _ = ` Msg ( Format . flush_str_formatter ( ) ) in Format . kfprintf kmsg Format . str_formatter fmt let reword_error reword = function | Ok _ as r -> r | Error e -> Error ( reword e ) e let error_msg m = Error (...
let head s = if s = " " then None else Some s [ . 0 ] 0
let is_prefix ~ affix s = let len_a = length affix in let len_s = length s in if len_a > len_s then false else let max_idx_a = len_a - 1 in let rec loop i = if i > max_idx_a then true else if unsafe_get affix i <> unsafe_get s i then false else loop ( i + 1 ) 1 in loop 0
let is_suffix ~ affix s = let max_idx_a = length affix - 1 in let max_idx_s = length s - 1 in if max_idx_a > max_idx_s then false else let rec loop i = if i > max_idx_a then true else if unsafe_get affix ( max_idx_a - i ) i <> unsafe_get s ( max_idx_s - i ) i then false else loop ( i + 1 ) 1...
let for_all sat s = let max_idx = length s - 1 in let rec loop i = if i > max_idx then true else if sat ( unsafe_get s i ) i then loop ( i + 1 ) 1 else false in loop 0
let exists sat s = let max_idx = length s - 1 in let rec loop i = if i > max_idx then false else if sat ( unsafe_get s i ) i then true else loop ( i + 1 ) 1 in loop 0
let find_byte ( ? start = 0 ) 0 c s = let max = String . length s - 1 in if start > max then None else try Some ( String . index_from s start c ) c with Not_found -> None
let with_index_range ( ? first = 0 ) 0 ? last s = let max = String . length s - 1 in let last = match last with | None -> max | Some l when l > max -> max | Some l -> l in let first = if first < 0 then 0 else first in if first > last then " " else String . sub s first ( last - first + ...
let cut ( ? rev = false ) false ~ sep s = let find_index = if rev then String . rindex else String . index in match try Some ( find_index s sep ) sep with Not_found -> None with | None -> None | Some i -> Some ( String . sub s 0 i , String . sub s ( i + 1 ) 1 ( String . length s ...
let cuts ( ? empty = true ) true ~ sep s = let no_empty = not empty in let rec loop acc s = match cut ~ sep s with | Some ( v , vs ) vs -> loop ( if no_empty && v = " " then acc else ( v :: acc ) acc ) acc vs | None -> List . rev ( if no_empty && s = " " then acc else ( s :: ...
let parse_version v = let version = if is_prefix ~ affix " : v " v then with_index_range ~ first : 1 v else v in let cut_left_plus_or_tilde s = let cut = match String . index_opt s ' + ' , String . index_opt s ' ~ ' with | None , None -> None | ( Some _ as i ) i , None | None , (...
let drop_initial_v version = match head version with
let pp_text ppf s = let is_nl_or_sp c = c = ' \ n ' || c = ' ' in let rec stop_at sat ~ start ~ max s = if start > max then start else if sat s [ . start ] start then start else stop_at sat ~ start ( : start + 1 ) 1 ~ max s in let sub s start stop ~ max = if start = stop then " " ...
let parse_changes lines = try let parse_line l = match Topkg_string . cut ~ sep : ' ' l with | None -> failwith ( Topkg_string . strf " % S : can ' t parse log line " l ) l | Some cut -> cut in Ok ( List ( . rev @@ rev_map parse_line lines ) lines ) lines with Failure msg -> Error ( ...
type kind = [ ` Git | ` Hg ]
let pp_kind ppf = function
let dirtify id = id ^ " - dirty "
type t = kind * Topkg_cmd . t * Topkg_fpath . t
let git = let git = Topkg_cmd . v ( Topkg_os . Env . opt_var " TOPKG_GIT " ~ absent " : git ) " in lazy ( Topkg_os . Cmd . exists git >>= fun exists -> Ok ( exists , git ) git ) git
let hg = let hg = Topkg_cmd . v ( Topkg_os . Env . opt_var " TOPKG_HG " ~ absent " : hg ) " in lazy ( Topkg_os . Cmd . exists hg >>= fun exists -> Ok ( exists , hg ) hg ) hg
let vcs_cmd kind cmd dir = match kind with
let v k cmd ~ dir = ( k , cmd , dir ) dir
let kind ( k , _ , _ ) _ = k
let dir ( _ , _ , dir ) dir = dir
let cmd ( kind , cmd , dir ) dir = vcs_cmd kind cmd dir
let git_work_tree ( _ , _ , dir ) dir = Topkg_cmd ( . v " -- work - tree " % Topkg_fpath . dirname dir ) dir
let find_git ( ) = Lazy . force git >>= function let git_dir = Topkg_cmd ( . git % " rev - parse " % " -- git - dir ) " in Topkg_os . Cmd ( . run_out ~ err : Topkg_os . File . null git_dir |> out_string ) out_string >>= function | ( dir , ( _ , ` Exited 0 ) 0 ) ...
let err_git cmd c = R . error_msgf " % a exited with % d " Topkg_cmd . dump cmd c
let run_git r args out = let git = Topkg_cmd ( . cmd r %% args ) args in Topkg_os . Cmd ( . run_out git |> out ) out >>= function | ( v , ( _ , ` Exited 0 ) 0 ) 0 -> Ok v | ( _ , ( _ , ` Exited c ) c ) c -> err_git git c
let git_is_dirty r = let status = Topkg_cmd ( . cmd r %% git_work_tree r % " status " % " -- porcelain ) " in Topkg_os . Cmd ( . run_out ~ err : Topkg_os . File . null status |> out_string ) out_string >>= function | ( " " , ( _ , ` Exited 0 ) 0 ) 0 -> Ok false | (...
let git_file_is_dirty r file = let diff = Topkg_cmd ( . cmd r %% git_work_tree r % " diff - index " % " -- quiet " % " HEAD " % p file ) file in Topkg_os . Cmd ( . run_status ~ err : Topkg_os . File . null diff ) diff >>= function | ` Exited 0 -> Ok false | ` Exited 1 -...
let dirtify_if ~ dirty r id = match dirty with git_is_dirty r >>= fun is_dirty -> Ok ( if is_dirty then dirtify id else id ) id
let git_head ~ dirty r = run_git r Topkg_cmd ( . v " rev - parse " % " HEAD ) " Topkg_os . Cmd . out_string >>= fun id -> dirtify_if ~ dirty r id
let git_commit_id ~ dirty r commit_ish = let dirty = dirty && commit_ish = " HEAD " in let id = Topkg_cmd ( . v " rev - parse " % " -- verify " % ( commit_ish ^ { " ^ commit } commit ) ) " in run_git r id Topkg_os . Cmd . out_string >>= fun id -> dirtify_if ~ dirty r id
let git_commit_ptime_s r commit_ish = let time = Topkg_cmd ( . v " show " % " - s " % " -- format =% ct " % commit_ish ) commit_ish in run_git r time Topkg_os . Cmd . out_string >>= fun ptime -> try Ok ( int_of_string ptime ) ptime with | Failure _ -> R . error_msgf " Could not parse ...
let git_describe ~ dirty r commit_ish = let dirty = dirty && commit_ish = " HEAD " in run_git r Topkg_cmd ( . git_work_tree r % " describe " % " -- always " %% on dirty ( v " -- dirty ) " %% on ( not dirty ) dirty ( v commit_ish ) commit_ish ) commit_ish Topkg_os . Cmd . out_str...
let git_tags r = run_git r Topkg_cmd ( . v " tag " % " -- list ) " Topkg_os . Cmd . out_lines
let git_changes r ~ after ~ until = let range = if after = " " then until else Topkg_string . strf " % s . . % s " after until in let changes = Topkg_cmd ( . v " log " % " -- oneline " % " -- no - decorate " % range ) range in run_git r changes Topkg_os . Cmd . out_lines >>= fun commi...
let git_tracked_files r ~ tree_ish = let tracked = Topkg_cmd ( . git_work_tree r % " ls - tree " % " -- name - only " % " - r " % tree_ish ) tree_ish in run_git r tracked Topkg_os . Cmd . out_lines
let git_clone r ~ dir : d = let clone = Topkg_cmd ( . v " clone " % " -- local " % ( dir r ) r % d ) d in run_git r clone Topkg_os . Cmd . out_stdout >>= fun _ -> Ok ( )
let git_checkout r ~ branch ~ commit_ish = let branch = match branch with | None -> Topkg_cmd . empty | Some branch -> Topkg_cmd ( . v " - b " % branch ) branch in run_git r Topkg_cmd ( . v " checkout " % " -- quiet " %% branch % commit_ish ) commit_ish Topkg_os . Cmd . out_string >...
let git_commit_files r ~ msg files = let msg = match msg with | None -> Topkg_cmd . empty | Some m -> Topkg_cmd ( . v " - m " % m ) m in let files = Topkg_cmd ( . of_list @@ List . map p files ) files in run_git r Topkg_cmd ( . v " commit " %% msg %% files ) files Topkg_os . Cmd ....
let git_tag r ~ force ~ sign ~ msg ~ commit_ish tag = let msg = match msg with | None -> Topkg_cmd . empty | Some m -> Topkg_cmd ( . v " - m " % m ) m in let flags = Topkg_cmd ( . on force ( v " - f ) " %% on sign ( v " - s ) ) " in run_git r Topkg_cmd ( . v " tag " %...
let git_delete_tag r tag = run_git r Topkg_cmd ( . v " tag " % " - d " % tag ) tag Topkg_os . Cmd . out_stdout
let hg_rev commit_ish = match commit_ish with " HEAD " -> " tip " | c -> c
let find_hg ( ) = Lazy . force hg >>= function let hg_root = Topkg_cmd ( . hg % " root ) " in Topkg_os . Cmd ( . run_out ~ err : Topkg_os . File . null hg_root |> out_string ) out_string >>= function | ( dir , ( _ , ` Exited 0 ) 0 ) 0 -> Ok ( Some ( v ` Hg hg...
let err_hg cmd c = R . error_msgf " % a exited with % d " Topkg_cmd . dump cmd c
let run_hg r args out = let hg = Topkg_cmd ( . cmd r %% args ) args in Topkg_os . Cmd ( . run_out hg |> out ) out >>= function | ( v , ( _ , ` Exited 0 ) 0 ) 0 -> Ok v | ( _ , ( _ , ` Exited c ) c ) c -> err_hg hg c
let hg_id r ~ rev = run_hg r Topkg_cmd ( . v " id " % " - i " % " -- rev " % rev ) rev Topkg_os . Cmd . out_string >>= fun id -> let len = String . length id in let is_dirty = String . length id > 0 && id [ . len - 1 ] 1 = ' + ' in let id = if is_dirty then String . sub i...
let hg_is_dirty r = hg_id r ~ rev " : tip " >>= function ( id , is_dirty ) is_dirty -> Ok is_dirty
let hg_file_is_dirty r file = run_hg r Topkg_cmd ( . v " status " % p file ) file Topkg_os . Cmd . out_string >>= function | " " -> Ok false | _ -> Ok true
let hg_head ~ dirty r = hg_id r ~ rev " : tip " >>= function ( id , is_dirty ) is_dirty -> Ok ( if is_dirty && dirty then dirtify id else id ) id
let hg_commit_id ~ dirty r ~ rev = hg_id r ~ rev >>= fun ( id , is_dirty ) is_dirty -> Ok ( if is_dirty && dirty then dirtify id else id ) id
let hg_commit_ptime_s r ~ rev = let time = Topkg_cmd ( . v " log " % " -- template " % { " date ( datedate , " \% s ) } " " \ % " -- rev " % rev ) rev in run_hg r time Topkg_os . Cmd . out_string >>= fun ptime -> try Ok ( int_of_string ptime ) ptime with | Failure _ -> R ....