text
stringlengths 0
601k
|
---|
let on_error_msg ( ? level = Error ) Error ~ use = function
|
let exec = match Array . length Sys . argv with
|
let usage ( ) = Topkg_string . strf " Usage : % s COMMAND [ OPTION ] OPTION . . . " exec
|
let try_help ( ) = Topkg_string . strf " % s \ nTry ` % s -- help ' for more information . " ( usage ( ) ) exec
|
let pp_help ppf ( ) = let prf = Format . fprintf in prf ppf [ " @< v , " ; >@ prf ppf " Commands , " ; :@ prf ppf " build [ OPTION ] OPTION . . . , " ; @ prf ppf " [ @ Build the package , see ` Build options ' below ] . , " ; @@ prf ppf " test [ OPTION ] OPTION . . . [ TEST ] TEST . . . [ -- [ ARG ] ARG ] . . . , " ; @ prf ppf " [ @ Run all or the given built package tests , \ see ` Test options ' below ] . , " ; @@ prf ppf " clean [ OPTION ] OPTION . . . , " ; @ prf ppf " [ @ Clean the package build , see ` Clean options ' below ] . , " ; @@ prf ppf " help , " ; @ prf ppf " [ @ Show this help ] . , " ; @@ prf ppf " ipc VERBOSITY [ ARG ] ARG . . . , " ; @ prf ppf " [ @ Interprocess communication with topkg care tools ] . , , " ; @@@ prf ppf " Options , " ; :@ prf ppf " - h , - help , -- help , " ; @ prf ppf " Show this help . , " ; @ prf ppf " - q , -- quiet , " ; @ prf ppf " Be quiet . Takes over - v . , " ; @ prf ppf " - v , -- verbose ( absent = warning or TOPKG_VERBOSITY env ) env , " ; @ prf ppf " Increase verbosity . Repeatable , more than twice is useless . , " ; @ prf ppf " -- version , " ; @ prf ppf " Show topkg ' s version information . , , " ; @@ prf ppf " Build options , " ; :@ prf ppf " - d , -- dry - run , " ; @ prf ppf " [ @ Do not run build instructions , @ only @ determine @ and @ \ write @ the @ opam @ install @ file ] . , " ; @@ prf ppf " - r ARG , -- raw ARG ( repeatable ) repeatable , " ; @ prf ppf " [ @ Do not run build instructions or write @ the @ opam @ \ install @ file , @ only @ invoke @ the build @ system @ with @ \ the given ARG argument ] . , , " ; @@@ prf ppf " [ @% a ] , , " @@@ Topkg_conf . pp_keys_cli_opts ( ) ; prf ppf " Test options , " ; :@ prf ppf " -- build - dir BUILD_DIR ( absent = discovered ) discovered , " ; @ prf ppf " [ @ Specifies the build directory BUILD_DIR ] . , " ; @@ prf ppf " - l , -- list , " ; @ prf ppf " [ @ Do not run the tests , list them ] . , , " ; @@@ prf ppf " Clean options , " ; :@ prf ppf " -- build - dir BUILD_DIR ( absent = discovered ) discovered , " ; @ prf ppf " [ @ Specifies the build directory BUILD_DIR ] . , " ; @@ prf ppf " - n NAME , -- pkg - name NAME ( absent = discovered ) discovered , " ; @ prf ppf " [ @ The name NAME of the package ( and hence the opam \ install file ) file . @ If absent provided by the package @ \ description ] . , " ; @@ ( )
|
let help_cmd pkg = let pr = Format . printf in let name = Topkg_pkg . name pkg in pr " % s ' s % s - Describes the % s package . . " @ name exec name ; pr " % s . " @ ( usage ( ) ) ; pr " % a . " @ pp_help ( ) ; Ok 0
|
let version_cmd pkg = print_endline " topkg %% VERSION " ; %% Ok 0
|
let build_cmd pkg kind args = let log_conf c = Topkg_log . info ( fun m -> m " Build configuration :@\ n % a " Topkg_conf . dump c ) c in let adjust_pkg_to_conf pkg c = let name = Topkg_conf . pkg_name c in let build_dir = Topkg_conf . build_dir c in Topkg_pkg . with_name_and_build_dir pkg ~ name ~ build_dir in let pkg_name = Topkg_pkg . name pkg in let build_dir = Topkg_pkg . build_dir pkg in Topkg_conf . of_cli_args ~ pkg_name ~ build_dir args >>= fun c -> Ok ( log_conf c ; adjust_pkg_to_conf pkg c ) c >>= fun pkg -> Topkg_pkg . build pkg ~ kind c ` Host_os
|
let test_cmd pkg name build_dir list tests args = let pkg = Topkg_pkg . with_name_and_build_dir ? name ? build_dir pkg in Topkg_pkg . test pkg ~ list ~ tests ~ args
|
let clean_cmd pkg name build_dir = let pkg = Topkg_pkg . with_name_and_build_dir ? name ? build_dir pkg in Topkg_pkg . clean pkg ` Host_os
|
let ipc_cmd pkg args = Topkg_ipc . write_answer ( Topkg_cmd . of_list args ) args pkg >>= fun ( ) -> Ok 0
|
let run_cmd pkg cmd args = match cmd with test_cmd pkg pkg_name bdir list tests args
|
let default_verb = Topkg_log . level ( )
|
let is_opt s = Topkg_string ( . is_prefix ~ affix " " :- s || is_prefix ~ affix " " :-- s ) s
|
let parse_cli_help_version_verbosity args = let is_help = function " - h " | " -- help " | " - help " -> true | _ -> false in let is_verb = function " - v " | " -- verbose " -> true | _ -> false in let is_quiet = function " - q " | " -- quiet " -> true | _ -> false in let is_version = function " -- version " -> true | _ -> false in let rec loop cmd verb acc = function | a :: args when is_help a -> loop ` Help verb acc args | a :: args when is_verb a -> loop cmd ( incr_verb verb ) verb acc args | a :: args when is_quiet a -> loop cmd None acc args | a :: args when is_version a -> let cmd = if cmd = ` Help then ` Help else ` Version in loop cmd verb acc args | ( " " -- :: _ | [ ] as rest ) rest -> cmd , verb , List . rev ( List . rev_append rest acc ) acc | a :: args -> loop cmd verb ( a :: acc ) acc args in match args with | " help " :: args -> loop ` Help default_verb [ ] args | args -> loop ` Cmd default_verb [ ] args
|
let parse_build_args args = let rec loop dry_run raws acc = function | ( " - r " | " -- raw " as opt ) opt :: args -> if args = [ ] then R . error_msgf " option ` % s ' : missing argument " opt else loop dry_run ( List . hd args :: raws ) raws acc ( List . tl args ) args | ( " - d " | " -- dry - run ) " :: args -> loop true raws acc args | a :: args -> loop dry_run raws ( a :: acc ) acc args | [ ] -> let args = List . rev acc in match dry_run , raws with | false , [ ] -> Ok ( ` Build , args ) args | false , raws -> Ok ( ` Raw ( List . rev raws ) raws , args ) args | true , [ ] -> Ok ( ` Dry_run , args ) args | true , _ -> R . error_msg " option ` -- dry - run ' and ` -- raw ' are mutually exclusive " in loop false [ ] [ ] args
|
let parse_test_args args = let rec loop pkg_name build_dir list tests = function | ( " " -- :: args ) args -> Ok ( pkg_name , build_dir , list , List . rev tests , Some ( Topkg_cmd . of_list args ) args ) args | [ ] -> Ok ( pkg_name , build_dir , list , List . rev tests , None ) None | " -- build - dir " :: bdir :: args -> loop pkg_name ( Some bdir ) bdir list tests args | ( " - l " | " -- list ) " :: args -> loop pkg_name build_dir true tests args | ( " - n " | " -- pkg - name ) " :: n :: args -> loop ( Some n ) n build_dir list tests args | a :: args -> if is_opt a then R . error_msgf " unknown option ` % s ' " a else loop pkg_name build_dir list ( a :: tests ) tests args in loop None None false [ ] args
|
let parse_clean_args args = let rec loop pkg_name build_dir = function | " -- build - dir " :: bdir :: args -> loop pkg_name ( Some bdir ) bdir args | ( " - n " | " -- pkg - name ) " :: n :: args -> loop ( Some n ) n build_dir args | [ ] -> Ok ( pkg_name , build_dir ) build_dir | a :: args -> R . error_msgf " don ' t know what to do with ` % s ' " a in loop None None args
|
let parse_ipc_args args = begin match args with | [ ] -> R . error_msg " missing verbosity and IPC arguments " | verbosity :: args -> Topkg_log . level_of_string verbosity >>= fun verbosity -> match args with | [ ] -> R . error_msg " no IPC arguments specified " | args -> Ok ( verbosity , args ) args end |> R . reword_error_msg ~ replace : true ( fun e -> R . msgf " ipc : % s " e ) e
|
let parse_cli ( ) = let args = List . tl ( Array . to_list Sys . argv ) argv in begin match args with | " ipc " :: args -> parse_ipc_args args >>= fun ( verb , args ) args -> Ok ( ` Ipc , verb , args ) args | args -> match parse_cli_help_version_verbosity args with | ` Help , _ , _ as cmd -> Ok cmd | ` Version , _ , _ as cmd -> Ok cmd | ` Cmd , verbosity , args -> match args with | " build " :: args -> parse_build_args args >>= fun ( kind , args ) args -> Ok ( ` Build kind , verbosity , args ) args | " test " :: args -> parse_test_args args >>= fun ( pkg_name , bdir , list , tests , args ) args -> Ok ( ` Test ( pkg_name , bdir , list , tests , args ) args , verbosity , [ ] ) | " clean " :: args -> parse_clean_args args >>= fun ( pkg_name , bdir ) bdir -> Ok ( ` Clean ( pkg_name , bdir ) bdir , verbosity , [ ] ) | cmd :: _ -> R . error_msgf " Unknown command ' % s ' " cmd | [ ] -> R . error_msg " No command specified " end |> R . reword_error_msg ~ replace : true ( fun e -> R . msgf " % s \ n % s " e ( try_help ( ) ) )
|
let check_log ret = let msg = format_of_string " Package description has % d % s , see log above . " in let log kind count = if count > 0 then match kind with | ` Errs -> Topkg_log . err ( fun m -> m msg count " error ( errors ) errors ) " | ` Warns -> Topkg_log . warn ( fun m -> m msg count " warning ( warnings ) warnings ) " in let errs = Topkg_log . err_count ( ) in let warns = Topkg_log . warn_count ( ) in log ` Errs errs ; log ` Warns warns ; Ok ( if ret + errs > 0 then 1 else 0 ) 0
|
let setup_log_level level = Topkg_log . set_level level ; Topkg_log . info ( fun m -> m " topkg %% VERSION , %% running main ) " ; Ok ( )
|
let main pkg = begin parse_cli ( ) >>= fun ( cmd , log_level , args ) args -> setup_log_level log_level >>= fun ( ) -> run_cmd pkg cmd args >>= fun ret -> match cmd with | ` Build _ -> ( check_log ret ) ret | _ -> Ok ret end |> Topkg_log . on_error_msg ~ use ( : fun ( ) -> 1 ) 1
|
let must_run_main = ref true
|
let disable ( ) = must_run_main := false
|
let pkg = ref None
|
let describe ? delegate ? readmes ? licenses ? change_logs ? metas ? opams ? lint_files ? lint_custom ? distrib ? publish ? build name installs = match ! pkg with | Some _ -> invalid_arg " Topkg . Pkg . describe already called once " | None -> let p = Topkg_pkg . v ? delegate ? readmes ? licenses ? change_logs ? metas ? opams ? lint_files ? lint_custom ? distrib ? publish ? build name installs in pkg := Some p ; if ! must_run_main then ( must_run_main := false ; exit ( main p ) p ) p else ( )
|
let check_something_useful_happened ( ) = if ! must_run_main then Topkg_log . err ( fun m -> m " % a " Topkg_string . pp_text " No package description found . A syntax error may have \ occured or did you forget to call Topkg . Pkg . describe ) " ?
|
let ( ) = at_exit check_something_useful_happened
|
module File = struct type t = ( string * string list ) list list let codec = Topkg_codec . version 0 @@ Topkg_codec . with_kind " opam fields " @@ Topkg_codec ( . list ( pair string ( list string ) string ) string ) string let topkg_cmd = Topkg_cmd . v " topkg " let topkg_cmd_available ( ) = Topkg_os . Cmd . must_exist topkg_cmd |> R . reword_error_msg ~ replace : true ( fun m -> R . msgf " % s . Did you install topkg - care " ? m ) m let ipc_cmd file = let level = Topkg_log ( . level_to_string ( level ( ) ) ) in let verbosity = Topkg_string . strf " -- verbosity =% s " level in Topkg_cmd ( . v " ipc " % verbosity % " opam - fields " % file ) file let fields file = begin let cmd = Topkg_cmd ( . topkg_cmd %% ipc_cmd file ) file in topkg_cmd_available ( ) >>= fun _ -> Topkg_os . File . must_exist file >>= fun _ -> Topkg_os . Cmd ( . run_out cmd |> to_string ) to_string >>= fun s -> ( Topkg_codec . dec_result codec s ) s end |> R . reword_error_msg ~ replace : true ( fun msg -> R . msgf " opam fields of % s : % s " file msg ) msg end
|
module Install = struct type field = [ ` Bin | ` Doc | ` Etc | ` Lib | ` Lib_root | ` Libexec | ` Libexec_root | ` Man | ` Misc | ` Sbin | ` Share | ` Share_root | ` Stublibs | ` Toplevel | ` Unknown of string ] let field_to_string = function | ` Bin -> " bin " | ` Doc -> " doc " | ` Etc -> " etc " | ` Lib -> " lib " | ` Lib_root -> " lib_root " | ` Libexec -> " libexec " | ` Libexec_root -> " libexec_root " | ` Man -> " man " | ` Misc -> " misc " | ` Sbin -> " sbin " | ` Share -> " share " | ` Share_root -> " share_root " | ` Stublibs -> " stublibs " | ` Toplevel -> " toplevel " | ` Unknown name -> name type move = { src : string ; dst : string option ; maybe : bool ; } let move ( ? maybe = false ) false ? dst src = { src ; dst ; maybe } type t = [ ` Header of string option ] * ( field * move ) move list let to_string ( ` Header h , mvs ) mvs = let b = Buffer . create 1024 in let pr b fmt = Printf . bprintf b fmt in let pr_header b = function None -> ( ) | Some h -> pr b " # % s \ n \ n " h in let pr_src b src maybe = pr b " " \% s % s " " \ ( if maybe then " " ? else ) " " src in let pr_dst b dst = match dst with | None -> ( ) | Some dst -> pr b " { " \% s } " " \ dst in let pr_field_end b last = if last <> " " then pr b " ] \ n " in let pr_field b last field = if last = field then pr b " \ n " else ( pr_field_end b last ; pr b " % s : [ \ n " field ) field in let pr_move b last ( field , { src ; dst ; maybe } ) = pr_field b last field ; pr_src b src maybe ; pr_dst b dst ; field in let sortable ( field , mv ) mv = ( field_to_string field , mv ) mv in let moves = List . sort compare ( List . rev_map sortable mvs ) mvs in pr_header b h ; let last = List . fold_left ( pr_move b ) b " " moves in pr_field_end b last ; Buffer . contents b end
|
module Env = struct let var name = try Some ( Sys . getenv name ) name with Not_found -> None let opt_var name ~ absent = match var name with None -> absent | Some v -> v end
|
module Dir = struct let exists dir = try Ok ( Sys ( . file_exists dir && is_directory dir ) dir ) dir with | Sys_error e -> R . error_msgf " % s : % s " dir e let must_exist dir = exists dir >>= function | true -> Ok dir | false -> R . error_msgf " % s : no such directory " dir let current ( ) = try Ok ( Sys . getcwd ( ) ) with Sys_error e -> R . error_msg e let set_current d = try Ok ( Sys . chdir d ) d with Sys_error e -> R . error_msgf " % s : % s " d e let with_current dir f v = current ( ) >>= fun original -> set_current dir >>= fun ( ) -> let r = f v in set_current original >>= fun ( ) -> Ok r let contents ( ? dotfiles = false ) false ( ? rel = false ) false p = try let files = Array . to_list @@ Sys . readdir p in if rel && dotfiles then Ok files else let rec loop acc = function | [ ] -> List . rev acc | f :: fs -> let acc = if not dotfiles && Topkg_string . is_prefix ~ affix " . " : f then acc else if rel then f :: acc else Topkg_fpath . append p f :: acc in loop acc fs in Ok ( loop [ ] files ) files with | Sys_error e -> R . error_msgf " % s : % s " p e end
|
module File = struct let null = match Sys . os_type with | " Win32 " -> " NUL " | _ -> " / dev / null " let dash = " " - let exists f = try Ok ( Sys ( . file_exists f && not ( is_directory f ) f ) f ) f with | Sys_error e -> R . error_msgf " % s : % s " f e let must_exist f = exists f >>= function | true -> Ok f | false -> R . error_msgf " % s : no such file " f let delete ( ? must_exist = false ) false f = try if not must_exist && not ( Sys . file_exists f ) f then Ok ( ) else Ok ( Sys . remove f ) f with | Sys_error e -> R . error_msgf " % s : % s " f e let fold ( ? skip = fun _ -> false ) false f acc paths = let is_dir d = try Sys . is_directory d with Sys_error _ -> false in let readdir d = try Array . to_list ( Sys . readdir d ) d with Sys_error _ -> [ ] in let keep p = not ( skip p ) p in let process acc file = f file acc in let rec aux f acc = function | ( d :: ds ) ds :: up -> let paths = List . rev_map ( Filename . concat d ) d ( readdir d ) d in let paths = List . find_all keep paths in let dirs , files = List . partition is_dir paths in let acc = List . fold_left process acc files in aux f acc ( dirs :: ds :: up ) up | [ ] :: [ ] -> acc | [ ] :: up -> aux f acc up | _ -> assert false in let paths = List . find_all keep paths in let dirs , files = List . partition is_dir paths in let acc = List . fold_left process acc files in Ok ( aux f acc ( dirs :: [ ] ) ) let with_parent_check op op_name file = let err_no_parent op_name file = Topkg_string . strf " % s : Cannot % s file , parent directory does not exist " file op_name in ( Dir . must_exist ( Topkg_fpath . dirname file ) file >>= fun _ -> Ok ( op file ) file ) file |> R . reword_error @@ fun _ -> ` Msg ( err_no_parent op_name file ) file let safe_open_in_bin = with_parent_check open_in_bin " read " let safe_open_out_bin = with_parent_check open_out_bin " write " let read file = try let close ic = if file = dash then ( ) else close_in_noerr ic in ( if file = dash then Ok stdin else safe_open_in_bin file ) file >>= fun ic -> try let len = in_channel_length ic in let buf = Bytes . create len in really_input ic buf 0 len ; close ic ; Ok ( Bytes . unsafe_to_string buf ) buf with exn -> close ic ; raise exn with Sys_error e -> R . error_msgf " % s : % s " file e let write file s = try let close oc = if file = dash then ( ) else close_out_noerr oc in ( if file = dash then Ok stdout else safe_open_out_bin file ) file >>= fun oc -> try output_string oc s ; flush oc ; close oc ; Ok ( ) with exn -> close oc ; raise exn with Sys_error e -> R . error_msgf " % s : % s " file e let write_subst file vars s = try let close oc = if file = dash then ( ) else close_out_noerr oc in ( if file = dash then Ok stdout else safe_open_out_bin file ) file >>= fun oc -> try let start = ref 0 in let last = ref 0 in let len = String . length s in while ( ! last < len - 4 ) 4 do if not ( s [ . ! last ] last = ' % ' && s [ . ! last + 1 ] 1 = ' % ' ) ' then incr last else begin let start_subst = ! last in let last_id = ref ( ! last + 2 ) 2 in let stop = ref false in while ( ! last_id < len - 1 && not ! stop ) stop do if not ( s [ . ! last_id ] last_id = ' % ' && s [ . ! last_id + 1 ] 1 = ' % ' ) ' then begin if s [ . ! last_id ] last_id <> ' ' then ( incr last_id ) last_id else ( stop := true ; last := ! last_id ) last_id end else begin let id_start = start_subst + 2 in let id = String . sub s ( id_start ) id_start ( ! last_id - id_start ) id_start in try let subst = List . assoc id vars in output oc ( Bytes . unsafe_of_string s ) s ! start ( start_subst - ! start ) start ; output_string oc subst ; stop := true ; start := ! last_id + 2 ; last := ! last_id + 2 ; with Not_found -> stop := true ; last := ! last_id end done ; if not ! stop then last := ! last_id end done ; output oc ( Bytes . unsafe_of_string s ) s ! start ( len - ! start ) start ; flush oc ; close oc ; Ok ( ) with exn -> close oc ; raise exn with Sys_error e -> R . error_msgf " % s : % s " file e let tmp ( ) = try let f = Filename . temp_file ( Filename . basename Sys . argv ( . 0 ) 0 ) 0 " topkg " in at_exit ( fun ( ) -> ignore ( delete f ) f ) f ; Ok f with Sys_error e -> R . error_msg e end
|
module Cmd = struct let err_empty_line = " no command , empty command line " let line ? stdout ? stderr cmd = let strf = Printf . sprintf in if Topkg_cmd . is_empty cmd then failwith err_empty_line else let cmd = List . rev_map Filename . quote ( Topkg_cmd . to_rev_list cmd ) cmd in let cmd = String . concat " " cmd in let redirect fd f = strf " % d >% s " fd ( Filename . quote f ) f in let stdout = match stdout with None -> " " | Some f -> redirect 1 f in let stderr = match stderr with None -> " " | Some f -> redirect 2 f in let win_quote = if Sys . win32 then " " " \ else " " in strf " % s % s % s % s % s " win_quote cmd stdout stderr win_quote let exec ? stdout ? stderr cmd = try let line = line ? stdout ? stderr cmd in Topkg_log . debug ( fun m -> m ~ header " : EXEC " [ " @< 1 [ >% s ] s ] " @ line ) line ; Ok ( ( ) , ( cmd , ` Exited ( Sys . command line ) line ) line ) line with Sys_error e | Failure e -> R . error_msg e let test_cmd = match Sys . os_type with | " Win32 " -> Topkg_cmd . v " where " | _ -> Topkg_cmd ( . v " command " % " - v ) " let cmd_bin cmd = try List . hd ( Topkg_cmd . to_list cmd ) cmd with | Failure _ -> failwith err_empty_line let exists cmd = try let bin = cmd_bin cmd in let cmd = Topkg_cmd ( . test_cmd % bin ) bin in match exec ~ stdout : File . null ~ stderr : File . null cmd with | Ok ( _ , ( _ , ` Exited 0 ) 0 ) 0 -> Ok true | Ok _ -> Ok false | Error _ as e -> e with Failure e -> R . error_msg e let must_exist cmd = exists cmd >>= function | false -> R . error_msgf " % s : no such command " ( cmd_bin cmd ) cmd | true -> Ok cmd type run_status = Topkg_cmd . t * [ ` Exited of int ] let success r = r >>= function | ( v , ( _ , ` Exited 0 ) 0 ) 0 -> Ok v | ( v , ( cmd , ` Exited c ) c ) c -> R . error_msgf " cmd % a : exited with % d " Topkg_cmd . dump cmd c let run ? err : stderr cmd = exec ? stderr cmd |> success let run_status ? err : stderr cmd = exec ? stderr cmd >>= function ( ( ) , ( _ , st ) st ) st -> Ok st type run_out = { cmd : Topkg_cmd . t ; err : Topkg_fpath . t option } let out_string ( ? trim = true ) true o = File . tmp ( ) >>= fun file -> exec ? stderr : o . err ~ stdout : file o . cmd >>= fun ( ( ) , st ) st -> File . read file >>= fun out -> Ok ( ( if trim then String . trim out else out ) out , st ) st let out_lines ? trim o = out_string ? trim o >>= function ( s , st ) st -> Ok ( ( if s = " " then [ ] else Topkg_string . cuts ~ sep : ' \ n ' s ) s , st ) st let out_file stdout o = exec ? stderr : o . err ~ stdout o . cmd let out_stdout o = exec ? stderr : o . err ? stdout : None o . cmd let to_string ? trim o = out_string ? trim o |> success let to_lines ? trim o = out_lines ? trim o |> success let to_file stdout o = out_file stdout o |> success let run_out ? err cmd = { cmd ; err } end
|
type std_file = Topkg_fpath . t * bool
|
let std_file ( ? install = true ) true file = file , install
|
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 ( add Topkg_install . doc ) doc change_logs @@ List . fold_right ( fun ( m , _ ) _ -> add Topkg_install . lib m ) m metas @@ List . fold_right ( fun ( o , _ , _ ) _ -> add Topkg_install . lib o ) o opams @@ [ ]
|
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 ) list option ; distrib : Topkg_distrib . t ; publish : Topkg_publish . t ; build : Topkg_build . t ; installs : Topkg_conf . t -> Topkg_install . t list result ; }
|
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 ] ) " ( ? opams = [ opam_file " opam ] ) " ( ? lint_files = Some [ ] ) ? lint_custom ( ? distrib = Topkg_distrib . v ( ) ) ( ? publish = Topkg_publish . v ( ) ) ( ? build = Topkg_build . v ( ) ) name installs = { name ; delegate ; readmes ; licenses ; change_logs ; metas ; opams ; lint_files ; lint_custom ; distrib ; publish ; build ; installs }
|
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 ) opams ) opams
|
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 <> p . name then Topkg_log . warn ( fun m -> m " No opam file for % s , using ' opam ' " p . name ) name ; " opam "
|
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_file = Topkg_codec ( . t3 std_file bool ( string_list_option ) string_list_option ) string_list_option in let name = Topkg_codec ( . with_kind " name " @@ string ) string in let delegate = Topkg_codec ( . with_kind " delegate " @@ option cmd ) cmd in let readmes = Topkg_codec ( . with_kind " readmes " @@ list std_file ) std_file in let licenses = Topkg_codec ( . with_kind " licenses " @@ list std_file ) std_file in let change_logs = Topkg_codec ( . with_kind " change_logs " @@ list std_file ) std_file in let metas = Topkg_codec ( . with_kind " metas " @@ list meta_file ) meta_file in let opams = Topkg_codec ( . with_kind " opams " @@ list opam_file ) opam_file in let lint_files = Topkg_codec ( . with_kind " lint_files " @@ string_list_option ) string_list_option in let lint_custom = let kind = " lint_custom " in let enc = function None -> " \ x00 " | Some _ -> " \ x01 " in let dec = function | " \ x00 " -> None | " \ x01 " -> Some stub | s -> Topkg_codec . err ~ kind s in Topkg_codec . v ~ kind ~ enc ~ dec in let distrib = Topkg_distrib . codec in let publish = Topkg_publish . codec in let build = Topkg_build . codec in let fields = ( fun p -> ( p . name , p . delegate , p . readmes , p . licenses , p . change_logs ) change_logs , ( p . metas , p . opams , p . lint_files , p . lint_custom , p . distrib ) distrib , ( p . publish , p . build ) build ) build , ( fun ( ( name , delegate , readmes , licenses , change_logs ) change_logs , ( metas , opams , lint_files , lint_custom , distrib ) distrib , ( publish , build ) build ) build -> { name ; delegate ; readmes ; licenses ; change_logs ; metas ; opams ; lint_files ; lint_custom ; distrib ; publish ; build ; installs = stub } ) in Topkg_codec . version 0 @@ Topkg_codec ( . view ~ kind : " package " fields ( t3 ( t5 name delegate readmes licenses change_logs ) change_logs ( t5 metas opams lint_files lint_custom distrib ) distrib ( t2 publish build ) build ) build ) build
|
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 " \\ n % s % s " version opam_adds o ) o >>= fun o -> Topkg_os . File . write file o in List . fold_left version_opam_file ( Ok ( ) ) p . opams
|
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_to_watermark d ( ) >>= fun files -> Topkg_distrib . watermark_files ws_defs files >>= fun ( ) -> distrib_version_opam_files p ~ version ~ opam_adds >>= fun ( ) -> Topkg_distrib . massage d ( ) >>= fun ( ) -> Topkg_distrib . exclude_paths d ( )
|
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 ~ opam ) opam >>= fun ws_defs -> Topkg_distrib . files_to_watermark d ( ) >>= fun files -> Topkg_distrib . watermark_files ws_defs files >>= fun ( ) -> distrib_version_opam_files p ~ version ~ opam_adds " " : >>= fun ( ) -> Topkg_distrib . massage d ( )
|
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 ( fun m -> m " Running test % s " exec ) exec ; ( Topkg_os . Cmd . run_status cmd >>| fun ( ` Exited c ) c -> c ) c |> Topkg_log . on_error_msg ~ use ( : fun _ -> 1 ) 1 in let res = match Topkg_test . dir t with | None -> Ok ( run ( ) ) | Some dir -> Topkg_os . Dir . with_current dir run ( ) in acc + ( res |> Topkg_log . on_error_msg ~ use ( : fun _ -> 1 ) 1 ) 1 in Topkg_os . Dir . current ( ) >>= fun root_dir -> match List . fold_left ( run_test root_dir ) root_dir 0 tests with | 0 -> Ok 0 | n -> Topkg_log . err ( fun m -> m " Some tests failed ) . " ; Ok 1
|
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 print_endline ( Topkg_string . strf " % s % s % s " exec args run ) run in List . iter list_test tests
|
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 match List . fold_left add_test [ ] tests with | [ ] -> Topkg_log . warn ( fun m -> m " No test matching ` % s ' " q ) q ; [ ] | acc -> List . rev acc in List . flatten ( List . map query queries ) queries
|
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 | None -> R . error_msgf " The tests were not built . " | Some tests -> match list with | true -> tests_list tests ; Ok 0 | false -> let no_test = format_of_string " No tests to run . " in match tests_select queries tests with | [ ] when queries = [ ] -> Topkg_log . app ( fun m -> m no_test ) no_test ; Ok 0 | [ ] -> Topkg_log . err ( fun m -> m no_test ) no_test ; Ok 1 | tests -> tests_run tests ~ args
|
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 -> let is_pin = Topkg_conf . build_context c = ` Pin in let prepare = match Topkg_build . prepare_on_pin p . build && is_pin with | false -> Ok ( ) | true -> ( distrib_prepare_pin p ) p |> R . reword_error_msg ~ replace : true ( fun e -> R . msgf " Pin distribution preparation failed : % s " e ) e in prepare >>= fun ( ) -> run_build_hook " Pre " ( Topkg_build . pre p . build ) build c >>= fun ( ) -> ( Topkg_build . cmd p . build ) build c os targets >>= fun ( ) -> write_opam_install_file p install >>= fun ( ) -> write_tests_file p tests >>= fun ( ) -> run_build_hook " Post " ( Topkg_build . post p . build ) build c >>= fun ( ) -> Ok 0
|
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 ( ` Msg m ) m let error_msgf fmt = let kerr _ = Error ( ` Msg ( Format . flush_str_formatter ( ) ) ) in Format . kfprintf kerr Format . str_formatter fmt let reword_error_msg ( ? replace = false ) false reword = function | Ok _ as r -> r | Error ( ` Msg e ) e -> let ( ` Msg e ' as v ) v = reword e in if replace then Error v else error_msgf " % s \ n % s " e e ' end
|
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 in loop 0
|
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 + 1 ) 1
|
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 - i - 1 ) 1 ) 1
|
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 :: acc ) acc ) acc in loop [ ] 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 , ( Some _ as i ) i -> i | Some i , Some i ' -> Some ( if i < i ' then i else i ' ) i ' in match cut with | None -> None | Some i -> Some ( with_index_range ~ last ( : i - 1 ) 1 s , with_index_range ~ first : i s ) s in try match cut ~ sep : ' . ' version with | None -> None | Some ( maj , rest ) rest -> let maj = int_of_string maj in match cut ~ sep : ' . ' rest with | None -> begin match cut_left_plus_or_tilde rest with | None -> Some ( maj , int_of_string rest , 0 , None ) None | Some ( min , i ) i -> Some ( maj , int_of_string min , 0 , Some i ) i end | Some ( min , rest ) rest -> let min = int_of_string min in begin match cut_left_plus_or_tilde rest with | None -> Some ( maj , min , int_of_string rest , None ) None | Some ( p , i ) i -> Some ( maj , min , int_of_string p , Some i ) i end with | Failure _ -> 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 " " else if start = 0 && stop > max then s else String . sub s start ( stop - start ) start in let max = String . length s - 1 in let rec loop start s = match stop_at is_nl_or_sp ~ start ~ max s with | stop when stop > max -> Format . pp_print_string ppf ( sub s start stop ~ max ) max | stop -> Format . pp_print_string ppf ( sub s start stop ~ max ) max ; begin match s [ . stop ] stop with | ' ' -> Format . pp_print_space ppf ( ) | ' \ n ' -> Format . pp_force_newline ppf ( ) | _ -> assert false end ; loop ( stop + 1 ) 1 s in loop 0 s
|
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 ( ` Msg msg ) msg
|
type kind = [ ` Git | ` Hg ]
|
let pp_kind ppf = function
|
let dirtify id = id ^ " - dirty "
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.