text
stringlengths 0
601k
|
---|
let issues_uri p = Topkg_care_pkg . opam_field p " dev - repo " >>| function
|
let issue_list p = issues_uri p >>= fun issues_uri -> run_delegate p Cmd ( . v " issue " % " list " % issues_uri ) issues_uri
|
let issue_show p ~ id = issues_uri p >>= fun issues_uri -> run_delegate p Cmd ( . v " issue " % " show " % issues_uri % " id ) "
|
let issue_open p ~ title ~ body = issues_uri p >>= fun issues_uri -> match run_delegate p Cmd ( . v " issue " % " open " % issues_uri % title % body ) body with | Ok _ as v -> v | Error _ as e -> let pp_body ppf = function " " -> ( ) | body -> Fmt . pf ppf " , , @@% s " body in Logs . app ( fun m -> m [ " @< v > Your open issue message was , , :@---@% s % a ] " @ title pp_body body ) body ; e
|
let issue_close p ~ id ~ msg = issues_uri p >>= fun issues_uri -> match run_delegate p Cmd ( . v " issue " % " close " % issues_uri % id % msg ) msg with | Ok _ as v -> v | Error _ as e -> Logs . app ( fun m -> m [ " @< v > Your closing message was , , :@---@% s ] " @ msg ) msg ; e
|
let ocaml = Cmd . of_list @@ Topkg . Cmd . to_list @@ Topkg . Conf . tool " ocaml " ` Build_os
|
let pkg_must_exist pkg_file = match OS . File . must_exist pkg_file with let p = match OS . Dir . current ( ) with | Error _ -> pkg_file | Ok dir -> Fpath ( . dir // pkg_file ) pkg_file in R . error_msgf " Not a package : no file % a " Fpath . pp p
|
let ask ~ pkg_file ipc = let codec = Topkg . Private . Ipc . codec ipc in let verbosity = Logs ( . level_to_string ( level ( ) ) ) in let ipc_cmd = Cmd . of_list @@ Topkg . Cmd . to_list @@ Topkg . Private . Ipc . cmd ipc in let cmd = Cmd ( . ocaml % p pkg_file % " ipc " % verbosity %% ipc_cmd ) ipc_cmd in pkg_must_exist pkg_file >>= fun pkg_file -> begin OS . Cmd . run cmd >>= fun ( ) -> Fpath . of_string ( Topkg . Private . Ipc . answer ipc ) ipc >>= fun answer -> OS . File . read answer >>= fun data -> Topkg . Private . Codec . dec_result codec data end |> R . reword_error_msg ( fun _ -> R . msgf " Failed to load package description % a " Fpath . pp pkg_file ) pkg_file
|
let cmd = Cmd . of_list @@ Topkg . Cmd . to_list @@ Topkg . Conf . tool " ocamlbuild " ` Host_os
|
let find_packages ~ roots s = let package = String . sub " package ( package " in let not_rpar c = not ( Char . equal ' ) ' ' c ) c in let not_dot c = not ( Char . equal ' . ' c ) c in let is_comma c = Char . equal ' , ' c in let is_sep c = Char . Ascii . is_white c || is_comma c in let rec loop acc s = match String . Sub . find_sub ~ sub : package s with | None -> acc | Some s -> let rest = String . Sub ( . extend ( stop s ) s ) s in let ids , rest = String . Sub . span ~ sat : not_rpar rest in let ids = String . Sub . fields ~ empty : false ~ is_sep ids in let add_id acc id = let id = if roots then String . Sub . take ~ sat : not_dot id else id in String . Set . add ( String . Sub . to_string id ) id acc in let acc = List . fold_left add_id acc ids in loop acc ( String . Sub . tail rest ) rest in loop String . Set . empty ( String . sub s ) s
|
let package_tags ( ? roots = false ) false file = OS . File . read file >>= fun contents -> Ok ( find_packages ~ roots contents ) contents
|
let cmd = Cmd . of_list @@ Topkg . Cmd . to_list @@ Topkg . Conf . tool " opam " ` Host_os
|
let publish = let absent = Cmd ( . v " opam - publish ) " in OS . Env ( . value " TOPKG_OPAM_PUBLISH " cmd ~ absent ) absent
|
let ensure_publish ( ) = OS . Cmd . must_exist publish >>= fun cmd -> OS . Cmd . run_out Cmd ( . publish % " -- version ) " |> OS . Cmd . out_string >>= fun ( version , _ ) _ -> match Topkg . String . parse_version version with | None -> R . error_msgf " Could not determine the version of opam - publish " | Some ( m , _ , _ , _ ) _ when m < 2 -> R . error_msgf " topkg needs at least opam - publish 2 . 0 . 0 " | Some _ -> Ok ( )
|
let submit ? msg ~ opam_file = let msg = match msg with | None -> Ok ( Cmd . empty ) empty | Some msg -> OS . File . tmp " topkg - opam - submit - msg -% s " >>= fun m -> OS . File . write m msg >>= fun ( ) -> Ok Cmd ( . v " -- msg - file " % p m ) m in msg >>= fun msg -> OS . Cmd . run Cmd ( . publish %% msg % p opam_file ) opam_file
|
let ocaml_base_packages = String . Set . of_list [ " base - bigarray " ; " base - bytes " ; " base - threads " ; " base - unix " ; ]
|
module File = struct let id x = x let list f = fun v -> [ f v ] v let field name field conv = name , fun acc o -> String . Map . add name ( conv ( field o ) o ) o acc let opt_field name field conv = name , fun acc o -> match field o with | None -> acc | Some v -> String . Map . add name ( conv v ) v acc let deps_conv d = let add_pkg acc ( n , _ ) _ = OpamPackage . Name . to_string n :: acc in OpamFormula . fold_left add_pkg [ ] d let fields = [ opt_field " name " OpamFile . OPAM . name_opt ( list OpamPackage . Name . to_string ) to_string ; opt_field " version " OpamFile . OPAM . version_opt ( list OpamPackage . Version . to_string ) to_string ; field " opam - version " OpamFile . OPAM . opam_version ( list OpamVersion . to_string ) to_string ; field " available " OpamFile . OPAM . available ( list OpamFilter . to_string ) to_string ; field " maintainer " OpamFile . OPAM . maintainer id ; field " homepage " OpamFile . OPAM . homepage id ; field " authors " OpamFile . OPAM . author id ; field " license " OpamFile . OPAM . license id ; field " doc " OpamFile . OPAM . doc id ; field " tags " OpamFile . OPAM . tags id ; field " bug - reports " OpamFile . OPAM . bug_reports id ; opt_field " dev - repo " OpamFile . OPAM . dev_repo ( list OpamUrl . to_string ) to_string ; field " depends " OpamFile . OPAM . depends deps_conv ; field " depopts " OpamFile . OPAM . depopts deps_conv ; opt_field " synopsis " OpamFile . OPAM . synopsis ( list id ) id ; opt_field " description " OpamFile . OPAM . descr_body ( list id ) id ; ] let field_names = let add acc ( name , field ) field = String . Set . add name acc in List . fold_left add String . Set . empty fields let fields file = let parse file = let file = OpamFilename . of_string ( Fpath . to_string file ) file in let opam = OpamFile . OPAM . read ( OpamFile . make file ) file in let known_fields = let add_field acc ( _ , field ) field = field acc opam in List . fold_left add_field String . Map . empty fields in known_fields in Logs . info ( fun m -> m " Parsing opam file % a " Fpath . pp file ) file ; try Ok ( parse file ) file with | exn -> R . error_msgf " % a : could not parse opam file " Fpath . pp file let deps ( ? opts = true ) true fields = let deps = match String . Map . find " depends " fields with | None -> [ ] | Some deps -> deps in let dep_opts = if not opts then [ ] else match String . Map . find " depopts " fields with | None -> [ ] | Some deps -> deps in String . Set . of_list ( List . rev_append dep_opts deps ) deps end
|
module Descr = struct type t = string * string let of_string s = match String . cuts ~ sep " :\ n " s with | [ ] -> R . error_msgf " Cannot extract opam descr . " | synopsis :: descr -> Ok ( synopsis , String . concat ~ sep " :\ n " descr ) descr let to_string ( synopsis , descr ) descr = strf " % s \ n % s " synopsis descr let to_opam_fields ( synopsis , descr ) descr = strf " synopsis : " " " \\\% s " " " \\\\ ndescription : " " " \\\\\\ n % s " " " " \\\ synopsis descr let of_readme ? flavour r = let parse_synopsis l = let error l = R . error_msgf " % S : can ' t extract opam synopsis " l in let ok s = Ok String ( . Ascii . capitalize @@ String . Sub . to_string s ) s in let not_white c = not ( Char . Ascii . is_white c ) c in let skip_non_white l = String . Sub . drop ~ sat : not_white l in let skip_white l = String . Sub . drop ~ sat : Char . Ascii . is_white l in let start = String . sub l |> skip_white |> skip_non_white |> skip_white in match String . Sub . head start with | None -> error l | Some c when Char . Ascii . is_letter c -> ok start | Some c -> let start = start |> skip_non_white |> skip_white in match String . Sub . head start with | None -> error l | Some _ -> ok start in let drop_line l = String . is_prefix ~ affix " : Home page " : l || String . is_prefix ~ affix " : Homepage " : l || String . is_prefix ~ affix " : Contact " : l || String . is_prefix ~ affix " :%% VERSION " l in let keep_line l = not ( drop_line l ) l in match Topkg_care_text . head ? flavour r with | None -> R . error_msgf " Could not extract opam description . " | Some ( title , text ) text -> let sep = " \ n " in let title = Topkg_care_text . header_title ? flavour title in parse_synopsis title >>= fun synopsis -> Ok ( String . cuts ~ sep text ) text >>= fun text -> Ok ( List . filter keep_line text ) text >>= fun text -> Ok ( synopsis , String . concat ~ sep text ) text let of_readme_file file = let flavour = Topkg_care_text . flavour_of_fpath file in ( OS . File . read file >>= fun text -> of_readme ? flavour text ) text |> R . reword_error_msg ~ replace : true ( fun m -> R . msgf " % a : % s " Fpath . pp file m ) m end
|
module Url = struct type t = string let v ~ uri ~ checksum = strf " archive : " \% s " \\ nchecksum : " \% s " " \ uri checksum let with_distrib_file ~ uri distrib_file = try let checksum = Digest ( . to_hex @@ file ( Fpath . to_string distrib_file ) distrib_file ) distrib_file in Ok ( v ~ uri ~ checksum ) checksum with Failure msg | Sys_error msg -> R . error_msg msg let to_opam_section u = strf " url { \ n % s \ n } n " u end
|
let path_set_of_strings ps = try let add_excl acc p = match Fpath . of_string p with | Error ( ` Msg msg ) msg -> failwith msg | Ok p -> Fpath . Set . add p acc in Ok ( List . fold_left add_excl Fpath . Set . empty ps ) ps with Failure msg -> R . error_msg msg
|
let rec path_list_of_strings ps = let rec loop acc = function | [ ] -> Ok ( List . rev acc ) acc | p :: ps -> match Fpath . of_string p with | Error _ as e -> e | Ok p -> loop ( p :: acc ) acc ps in loop [ ] ps
|
let uri_sld uri = match Topkg_care_text . split_uri uri with match List . rev ( String . cuts ~ sep " . " : host ) host with | fst :: snd :: _ -> Some snd | _ -> None
|
let uri_append u s = match String . head ~ rev : true u with
|
let chop_ext u = match String . cut ~ rev : true ~ sep " . " : u with
|
let chop_git_prefix u = match String . cut ~ sep " : git " + u with
|
type t = { name : string option ; version : string option ; delegate : Cmd . t option ; build_dir : Fpath . t option ; opam : Fpath . t option ; opam_descr : Fpath . t option ; opam_fields : ( string list String . map , R . msg ) msg result Lazy . t ; readmes : Fpath . t list option ; change_logs : Fpath . t list option ; licenses : Fpath . t list option ; distrib_uri : string option ; distrib_file : Fpath . t option ; publish_msg : string option ; publish_artefacts : [ ` Distrib | ` Doc | ` Alt of string ] list option ; pkg_file : Fpath . t ; pkg : ( Topkg . Private . Pkg . t , R . msg ) msg result Lazy . t ; }
|
let empty p = { p with pkg = lazy ( Ok ( Topkg . Private . Pkg . empty ) empty ) empty }
|
let pkg p = Lazy . force p . pkg
|
let opam_fields p = Lazy . force p . opam_fields
|
let opam_field p f = opam_fields p >>| fun fields -> String . Map . find f fields
|
let opam_field_hd p f = opam_field p f >>| function
|
let opam_homepage_sld p = opam_field_hd p " homepage " >>| function
|
let pkg_file p = p . pkg_file
|
let name p = match p . name with
|
let version p = match p . version with
|
let delegate p = let not_found ( ) = R . error_msg " No package delegate found . \ Try ` topkg help delegate ` for more information . " in match p . delegate with | Some cmd -> Ok cmd | None -> pkg p >>| Topkg . Private . Pkg . delegate >>= function | Some cmd -> Ok ( Cmd . of_list @@ Topkg . Cmd . to_list cmd ) cmd | None -> match OS . Env ( . value " TOPKG_DELEGATE " ( some cmd ) cmd ~ absent : None ) None with | Some cmd -> Ok cmd | None -> opam_homepage_sld p >>= function | None -> not_found ( ) | Some ( _ , sld ) sld -> let exec = strf " % s - topkg - delegate " sld in let cmd = Cmd . v exec in OS . Cmd . exists cmd >>= function | true -> Ok cmd | false -> if exec <> " github - topkg - delegate " then not_found ( ) else Ok ( Cmd . v " toy - github - topkg - delegate ) "
|
let build_dir p = match p . build_dir with
|
let readmes p = match p . readmes with
|
let readme p = readmes p >>= function
|
let opam p = match p . opam with name p >>= fun name -> pkg p >>| Topkg . Private . Pkg . opam ~ name >>= Fpath . of_string
|
let opam_field_descr p = opam_field p " synopsis " >>= function | None -> Ok None | Some syn -> opam_field p " description " >>= function | None -> Ok None | Some descr -> Ok ( Some ( List . hd syn , List . hd descr ) descr ) descr
|
let opam_descr p = let descr_file_for_opam opam = if Fpath . has_ext " . opam " opam then Fpath ( . rem_ext opam + " . descr ) " else Fpath ( . parent opam / " descr ) " in let read f = OS . File . read f >>= fun c -> Topkg_care_opam . Descr . of_string c >>| fun d -> d , false in match p . opam_descr with | Some f -> read f | None -> opam_field_descr p >>= function | Some descr -> Ok ( descr , true ) true | None -> opam p >>= fun opam -> Ok ( descr_file_for_opam opam ) opam >>= fun descr_file -> OS . File . exists descr_file >>= function | true -> Logs . info ( fun m -> m " Found opam descr file % a " Fpath . pp descr_file ) descr_file ; read descr_file | false -> readme p >>= fun readme -> Logs . info ( fun m -> m " Extracting opam descr from % a " Fpath . pp readme ) readme ; Topkg_care_opam . Descr . of_readme_file readme >>| fun d -> d , false
|
let change_logs p = match p . change_logs with
|
let change_log p = change_logs p >>= function
|
let licenses p = match p . licenses with
|
let distrib_uri ( ? raw = false ) false p = let subst_uri p uri = uri >>= fun uri -> name p >>= fun name -> version p >>= fun version -> Ok ( Topkg . String . drop_initial_v version ) version >>= fun version_num -> let defs = String . Map ( . empty |> add " NAME " name |> add " VERSION " version |> add " VERSION_NUM " version_num ) version_num in Pat . of_string uri >>| fun pat -> Pat . format defs pat in let not_found ( ) = R . error_msg " no distribution URI found , see topkg ' s API documentation . " in let uri = match p . distrib_uri with | Some u -> Ok u | None -> pkg p >>= fun pkg -> match Topkg . Private . Pkg . distrib_uri pkg with | Some u -> Ok u | None -> opam_homepage_sld p >>= function | None -> not_found ( ) | Some ( uri , sld ) sld -> if sld <> " github " then ( Ok ( uri_append uri " releases ( /$ NAME ) NAME ( -$ VERSION_NUM ) VERSION_NUM . tbz ) ) " else opam_field_hd p " dev - repo " >>= function | None -> not_found ( ) | Some dev_repo -> Ok ( uri_append ( chop_git_prefix ( chop_ext dev_repo ) dev_repo ) dev_repo " releases / download ( /$ VERSION ) VERSION ( /$ NAME ) NAME ( -$ VERSION_NUM ) VERSION_NUM . tbz ) " in if raw then uri else subst_uri p uri
|
let distrib_filename ( ? opam = false ) false p = let sep = if opam then ' . ' else ' - ' in name p >>= fun name -> version p >>= fun version -> Ok ( Topkg . String . drop_initial_v version ) version >>= fun version_num -> Fpath . of_string ( strf " % s % c % s " name sep version_num ) version_num
|
let distrib_archive_path p = build_dir p >>= fun build_dir -> distrib_filename ~ opam : false p >>| fun b -> Fpath ( . build_dir // b + " . tbz ) "
|
let distrib_file p = match p . distrib_file with ( distrib_archive_path p >>= fun f -> OS . File . must_exist f ) f |> R . reword_error_msg ( fun _ -> R . msgf " Did you forget to call ' topkg distrib ' ) " ?
|
let publish_msg p = match p . publish_msg with change_log p >>= fun change_log -> Topkg_care_text . change_log_file_last_entry change_log >>= fun ( _ , ( h , txt ) txt ) txt -> Ok ( strf " % s \ n % s " h txt ) txt
|
let publish_artefacts p = match p . publish_artefacts with
|
let v ? name ? version ? delegate ? build_dir ? opam : opam_file ? opam_descr ? readme ? change_log ? license ? distrib_uri ? distrib_file ? publish_msg ? publish_artefacts pkg_file = let readmes = match readme with Some r -> Some [ r ] r | None -> None in let change_logs = match change_log with Some c -> Some [ c ] c | None -> None in let licenses = match license with Some l -> Some [ l ] l | None -> None in let pkg = lazy ( Topkg_care_ipc . ask ~ pkg_file ( Topkg . Private . Ipc . pkg ( ) ) ) in let rec opam_fields = lazy ( opam p >>= fun o -> Topkg_care_opam . File . fields o ) o and p = { name ; version ; delegate ; build_dir ; opam = opam_file ; opam_descr ; opam_fields ; readmes ; change_logs ; licenses ; distrib_uri ; distrib_file ; publish_msg ; publish_artefacts ; pkg_file ; pkg } in p
|
let distrib_prepare_ipc p ~ dist_build_dir ~ name ~ version ~ opam ~ opam_adds = let dist_build_dir = Fpath . to_string dist_build_dir in let opam = Fpath . to_string opam in R . join @@ Topkg_care_ipc . ask ~ pkg_file : p . pkg_file ( Topkg . Private . Ipc . distrib_prepare ~ dist_build_dir ~ name ~ version ~ opam ~ opam_adds ) opam_adds
|
let distrib_archive p ~ keep_dir = Topkg_care_archive . ensure_bzip2 ( ) >>= fun ( ) -> name p >>= fun name -> build_dir p >>= fun build_dir -> version p >>= fun version -> opam p >>= fun opam -> distrib_filename p >>= fun root -> Ok Fpath ( . build_dir // root + " . build ) " >>= fun dist_build_dir -> OS . Dir . delete ~ recurse : true dist_build_dir >>= fun ( ) -> Topkg_vcs . get ( ) >>= fun repo -> Topkg_vcs . commit_id repo ~ dirty : false >>= fun head -> Topkg_vcs . commit_ptime_s repo ~ commit_ish : head >>= fun mtime -> Topkg_vcs . clone repo ~ dir ( : Fpath . to_string dist_build_dir ) dist_build_dir >>= fun ( ) -> Topkg_vcs . get ~ dir ( : Fpath . to_string dist_build_dir ) dist_build_dir ( ) >>= fun clone -> Ok ( Topkg_string . strf " topkg - dist -% s " head ) head >>= fun branch -> Topkg_vcs . checkout clone ~ branch ~ commit_ish : head >>= fun ( ) -> opam_descr p >>= fun ( descr , from_opam ) from_opam -> let opam_adds = if from_opam then " " else ( Topkg_care_opam . Descr . to_opam_fields descr ^ " \ n ) " in distrib_prepare_ipc p ~ dist_build_dir ~ name ~ version ~ opam ~ opam_adds >>= fun exclude_paths -> path_set_of_strings exclude_paths >>= fun exclude_paths -> Topkg_care_archive . tar dist_build_dir ~ exclude_paths ~ root ~ mtime >>= fun tar -> distrib_archive_path p >>= fun archive -> Topkg_care_archive . bzip2 tar ~ dst : archive >>= fun ( ) -> ( if keep_dir then Ok ( ) else OS . Dir . delete ~ recurse : true dist_build_dir ) dist_build_dir >>= fun ( ) -> Ok archive
|
let run p ~ dir cmd ~ args ~ out = let pkg_file = p . pkg_file in let cmd = Cmd ( . v " ocaml " % p pkg_file % cmd %% args ) args in let run ( ) = OS . Cmd . run_out cmd |> out in R . join @@ OS . Dir . with_current dir run ( )
|
let test p ~ dir ~ args ~ out = run p ~ dir " test " ~ args ~ out
|
let build p ~ dir ~ args ~ out = run p ~ dir " build " ~ args ~ out
|
let clean p ~ dir ~ args ~ out = run p ~ dir " clean " ~ args ~ out
|
let lint_log msg = Logs . info ( fun m -> m ~ header " : LINT " " % a " Fmt . text msg ) msg ; 0
|
let lint_disabled test = Logs . info ( fun m -> m ~ header " : LINT " " Package @ disabled @ % a . " Fmt . text test ) test ; 0
|
let lint_custom p = let report errs res = let status , msg , errs = match res with | Ok ( ` Msg msg ) msg -> ` Ok , msg , errs | Error ( ` Msg msg ) msg -> ` Fail , msg , errs + 1 in Logs . app ( fun m -> m " % a [ @% a ] " @ pp_status status Fmt . text msg ) msg ; errs in begin let pkg_file = p . pkg_file in pkg p >>= fun p -> match Topkg . Private . Pkg . lint_custom p with | None -> Ok ( lint_disabled " custom linting ) " | _ -> Topkg_care_ipc . ask ~ pkg_file ( Topkg . Private . Ipc . lint_custom ( ) ) >>= function | None -> assert false | Some results -> Ok ( List . fold_left report 0 results ) results end |> Logs . on_error_msg ~ use ( : fun ( ) -> 1 ) 1
|
let lint_std_files p = let lint_exists file errs = let report exists = let status , errs = if exists then ` Ok , errs else ` Fail , errs + 1 in Logs . app ( fun m -> m " % a [ @ File % a @ is @ present ] . " @ pp_status status pp_path file ) file ; errs in ( OS . File . exists file >>= fun exists -> Ok ( report exists ) exists ) exists |> Logs . on_error_msg ~ use ( : fun ( ) -> errs + 1 ) 1 in begin pkg p >>= fun p -> match Topkg . Private . Pkg . lint_files p with | None -> Ok ( lint_disabled " standard files linting ) " | Some files -> path_set_of_strings files >>= fun files -> Ok ( Fpath . Set . fold lint_exists files 0 ) 0 end |> Logs . on_error_msg ~ use ( : fun ( ) -> 1 ) 1
|
let lint_file_with_cmd file_kind ~ cmd file errs handle_exit = let run_linter cmd file ~ exists = if not exists then Ok ( ` Fail ( strf " % a : No such file " Fpath . pp file ) file ) file else OS . Cmd ( . run_out ~ err : err_run_out Cmd ( . cmd % p file ) file |> out_string ) out_string >>| fun ( out , status ) status -> handle_exit ( snd status ) status out in begin OS . File . exists file >>= fun exists -> run_linter cmd file ~ exists >>| function | ` Ok -> Logs . app ( fun m -> m " % a [ @ lint @ % s % a ] . " @ pp_status ` Ok file_kind pp_path file ) file ; errs | ` Fail msgs -> Logs . app ( fun m -> m " % a [ @< v [ >@ lint @ % s % a ] [ , :@@@% a messages ] , :@@% a ] " @ pp_status ` Fail file_kind pp_path file Cmd . pp cmd Fmt . lines msgs ) msgs ; errs + 1 end |> Logs . on_error_msg ~ use ( : fun ( ) -> errs + 1 ) 1
|
let lint_metas p = begin pkg p >>= fun p -> match Topkg . Private . Pkg . lint_metas p with | [ ] -> Ok ( lint_log " No ocamlfind META file to lint ) " | metas -> let cmd = Cmd ( . Topkg_care_ocamlfind . cmd % " lint ) " in let handle_exit status out = match status with | ` Exited 0 -> ` Ok | _ -> ` Fail out in let lint errs ( f , lint ) lint = begin Fpath . of_string f >>| fun f -> if not lint then lint_log ( strf " ocamlfind META lint disabled for % a " Fpath . pp f ) f else lint_file_with_cmd " META file " ~ cmd f errs handle_exit end |> Logs . on_error_msg ~ use ( : fun ( ) -> errs + 1 ) 1 in Ok ( List . fold_left lint 0 metas ) metas end |> Logs . on_error_msg ~ use ( : fun ( ) -> 1 ) 1
|
let lint_opams p = begin pkg p >>= fun p -> match Topkg . Private . Pkg . lint_opams p with | [ ] -> Ok ( lint_log " No opam file to lint ) " | opams -> let cmd = Cmd ( . Topkg_care_opam . cmd % " lint ) " in let handle_exit file status out = match status , out with | ` Exited 0 , ( " " | " 5 " ) -> ` Ok | _ -> let err = OS . Cmd . err_run_out in match OS . Cmd ( . run_out ~ err Cmd ( . cmd % p file ) file |> out_string ) out_string with | Ok ( out , _ ) _ -> ` Fail out | Error ( ` Msg out ) out -> ` Fail out in let cmd = Cmd ( . cmd % " - s ) " in let lint errs ( f , lint , _ ) _ = begin Fpath . of_string f >>| fun f -> if not lint then lint_log ( strf " opam file lint disabled for % a " Fpath . pp f ) f else lint_file_with_cmd " opam file " ~ cmd f errs ( handle_exit f ) f end |> Logs . on_error_msg ~ use ( : fun ( ) -> errs + 1 ) 1 in Ok ( List . fold_left lint 0 opams ) opams end |> Logs . on_error_msg ~ use ( : fun ( ) -> 1 ) 1
|
let lint_deps_default_excludes = let exclude = [ " ocamlfind " ; " ocamlbuild " ; " topkg " ; " ocaml ] " in Topkg_care_ocamlfind . base_packages |> String . Set . union Topkg_care_opam . ocaml_base_packages |> String . Set . union ( String . Set . of_list exclude ) exclude
|
let lint_opam_deps errs ( opam , _ , exclude ) exclude = let pp_deps_mismatches ppf ( opam_only , tags_only ) tags_only = let pp_miss present absent ppf id = Fmt . pf ppf [ " @% a : % a present but % a absent ] " @ Fmt ( . styled ` Underline string ) string id Fmt ( . styled ` Green string ) string present Fmt ( . styled ` Red string ) string absent in let sep = if String . Set ( . is_empty opam_only || is_empty tags_only ) tags_only then Fmt . nop else Fmt . cut in Fmt . pf ppf [ " @< v >% a % a % a ] " @ ( String . Set . pp ( pp_miss " opam " " _tags ) ) " opam_only sep ( ) ( String . Set . pp ( pp_miss " _tags " " opam ) ) " tags_only in let lint_deps ~ exclude ~ opam ~ tags = begin Topkg_care_ocamlbuild . package_tags ~ roots : true tags >>= fun tags -> Topkg_care_opam . File . fields opam >>| fun fields -> let exclude = String . Set . union exclude lint_deps_default_excludes in let keep id = not ( String . Set . mem id exclude ) exclude && not ( String . is_prefix ~ affix " : conf " - id ) id in let opam_deps = Topkg_care_opam . File . deps ~ opts : true fields in let opam = String . Set . filter keep opam_deps in let tags = String . Set . filter keep tags in let opam_only = String . Set . diff opam tags in let tags_only = String . Set . diff tags opam in if String . Set . is_empty opam_only && String . Set . is_empty tags_only then None else Some ( opam_only , tags_only ) tags_only end |> R . reword_error_msg ~ replace : true ( fun msg -> R . msgf " could not lint dependencies : % s " msg ) msg in begin Fpath . of_string opam >>= fun opam -> match exclude with | None -> Ok ( lint_disabled @@ Fmt ( . strf_like stdout " opam dependency linting for % a " pp_path opam ) opam ) opam | Some exclude -> let tags = Fpath . v " _tags " in let exclude = String . Set . of_list exclude in lint_deps ~ exclude ~ opam ~ tags >>| function | None -> Logs . app ( fun m -> m " % a [ @ opam file % a @ and % a dependency check . " pp_status ` Ok pp_path opam pp_path tags ) tags ; errs | Some miss -> Logs . app ( fun m -> m " % a [ @< v [ >@ File % a and % a dependency check ] , :@@% a ] " @ pp_status ` Fail pp_path opam pp_path tags pp_deps_mismatches miss ) miss ; errs + 1 end |> Logs . on_error_msg ~ use ( : fun ( ) -> errs + 1 ) 1
|
let lint_deps p = begin pkg p >>= fun p -> match Topkg . Private . Pkg . lint_opams p with | [ ] -> Ok ( lint_log " No opam file to dependency lint ) " | opams -> Ok ( List . fold_left lint_opam_deps 0 opams ) opams end |> Logs . on_error_msg ~ use ( : fun ( ) -> 1 ) 1
|
type lint = [ ` Custom | ` Std_files | ` Meta | ` Opam | ` Deps ]
|
let lints = [ ` Custom , lint_custom ; ` Std_files , lint_std_files ; ` Meta , lint_metas ; ` Opam , lint_opams ; ` Deps , lint_deps ]
|
let lint_all = List . map fst lints
|
let lint ( ? ignore_pkg = false ) false p ~ dir todo = let lint pkg = let do_lint acc ( l , f ) f = acc + if List . mem l todo then f pkg else 0 in match List . fold_left do_lint 0 lints with | 0 -> Logs . app ( fun m -> m " % a lint @ % a % a " pp_status ` Ok pp_path dir ( Fmt . styled_unit ` Green " success ) " ( ) ) ; 0 | n -> Logs . app ( fun m -> m " % a lint @ % a @ % a :@ % d @ errors . " pp_status ` Fail pp_path dir ( Fmt . styled_unit ` Red " failure ) " ( ) n ) n ; 1 in let p = if ignore_pkg then Ok ( empty p ) p else pkg p >>| fun _ -> p in p >>= fun p -> OS . Dir . with_current dir lint p
|
type flavour = [ ` Markdown | ` Asciidoc ]
|
let flavour_of_fpath f = match String . Ascii . lowercase ( Fpath . get_ext f ) f with
|
let rec drop_blanks = function " " :: ls -> drop_blanks ls | ls -> ls
|
let last_line = function [ ] -> None | l :: rev_ls -> Some l
|
let simple_header hchar l before rest = match String ( . length @@ take ~ sat ( : Char . equal hchar ) hchar l ) l with | 0 -> None | n -> Some ( n , l , before , rest ) rest
|
let underline_header n uchar l before rest = let is_underline_header uchar l = String ( . length @@ take ~ sat ( : Char . equal uchar ) uchar l ) l >= 2 in if not ( is_underline_header uchar l ) l then None else match last_line before with | None -> None | Some t -> Some ( n , strf " % s \ n % s " t l , List . tl before , rest ) rest
|
let rec find_markdown_header before = function match simple_header ' # ' l before ls with | Some _ as h -> h | None -> match underline_header 1 ' = ' l before ls with | Some _ as h -> h | None -> match underline_header 2 ' - ' l before ls with | Some _ as h -> h | None -> find_markdown_header ( l :: before ) before ls
|
let rec find_asciidoc_header before = function match simple_header ' = ' l before ls with | Some _ as h -> h | None -> match underline_header 1 ' - ' l before ls with | Some _ as h -> h | None -> match underline_header 2 ' ~ ' l before ls with | Some _ as h -> h | None -> match underline_header 3 ' ^ ' l before ls with | Some _ as h -> h | None -> match underline_header 4 ' + ' l before ls with | Some _ as h -> h | None -> find_asciidoc_header ( l :: before ) before ls
|
let head find_header text = let lines = String . cuts ~ sep " :\ n " text in let ret h acc = let contents = String . concat ~ sep " :\ n " ( List . rev @@ drop_blanks acc ) acc in Some ( h , contents ) contents in match find_header [ ] lines with | None -> None | Some ( n , first , _ , rest ) rest -> let rec loop acc rest = match find_header acc rest with | None -> ret first ( List . rev_append rest acc ) acc | Some ( n ' , h , before , rest ) rest -> if n ' > n then loop ( h :: before ) before rest else ret first before in loop [ ] rest
|
let head ( ? flavour = ` Markdown ) Markdown text = match flavour with
|
let header_title ( ? flavour = ` Markdown ) Markdown h = match String . cuts ~ sep " :\ n " h with begin match flavour with | ` Markdown -> String ( . trim @@ drop ~ sat ( : Char . equal ' # ' ) ' h ) h | ` Asciidoc -> String ( . trim @@ drop ~ sat ( : Char . equal ' = ' ) ' h ) h end
|
let change_log_last_entry ? flavour text = match head ? flavour text with let title = header_title ? flavour h in match String . take ~ sat : Char . Ascii . is_graphic title with | " " -> Logs . app ( fun m -> m " % S % S " h changes ) changes ; None | version -> Some ( version , ( h , changes ) changes ) changes
|
let change_log_file_last_entry file = let flavour = flavour_of_fpath file in OS . File . read file >>= fun text -> match change_log_last_entry ? flavour text with | None -> R . error_msgf " % a : Could not parse change log . " Fpath . pp file | Some ( version , ( header , changes ) changes ) changes -> Ok ( version , ( header , changes ) changes ) changes
|
let split_uri ( ? rel = false ) false uri = match String ( . cut ~ sep " " :// ( trim uri ) uri ) uri with match String . cut ~ sep " " :/ rest with | None -> Some ( scheme , rest , ) " " | Some ( host , path ) path -> let path = if rel then path else " " / ^ path in Some ( scheme , host , path ) path
|
let find_pager ~ don ' t = if don ' t then Ok None else match OS . Env . var " TERM " with | Some " dumb " | None -> Ok None | _ -> let add_env v cmds = match OS . Env ( . value v ( some cmd ) cmd ~ absent : None ) None with | None -> cmds | Some cmd -> cmd :: cmds in let cmds = [ Cmd . v " less " ; Cmd . v " more " ] in let cmds = add_env " PAGER " cmds in let rec loop = function | [ ] -> Ok None | cmd :: cmds -> OS . Cmd . exists cmd >>= function | true -> Ok ( Some cmd ) cmd | false -> loop cmds in loop cmds
|
let edit_file f = match OS . Env ( . value " EDITOR " ( some cmd ) cmd ~ absent : None ) None with OS . Cmd . exists editor >>= function | false -> R . error_msgf " Editor % a not in search path " Cmd . pp editor | true -> OS . Cmd ( . run_status Cmd ( . editor % p f ) f ) f >>= function | ` Exited n | ` Signaled n -> Ok n
|
module Pp = struct let name = Fmt ( . styled ` Bold string ) string let version = Fmt ( . styled ` Cyan string ) string let commit = Fmt ( . styled ` Yellow string ) string let dirty = Fmt ( . styled_unit ` Red " dirty ) " let path = Fmt ( . styled ` Bold Fpath . pp ) pp let status ppf = function | ` Ok -> Fmt ( . brackets @@ styled_unit ` Green " OK ) " ppf ( ) | ` Fail -> Fmt ( . brackets @@ styled_unit ` Red " FAIL ) " ppf ( ) end
|
type t = string list
|
let is_empty = function [ ] -> true | _ -> false
|
let v a = [ a ] a
|
let ( % ) l a = a :: l
|
let ( %% ) l0 l1 = List . rev_append ( List . rev l1 ) l1 l0
|
let add_arg l a = l % a
|
let add_args l a = l %% a
|
let on bool l = if bool then l else [ ]
|
let p f = f
|
let equal l l ' = l = l '
|
let compare l l ' = compare l l '
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.