12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091(*
* Copyright (c) 2013-2020 Thomas Gazagnaire <thomas@gazagnaire.org>
* Copyright (c) 2013-2020 Anil Madhavapeddy <anil@recoil.org>
* Copyright (c) 2015-2020 Gabriel Radanne <drupyog@zoho.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)openAstringopenAction.SyntaxmoduletypePROJECT=sigvalname:stringvalversion:stringendmoduleMake(P:PROJECT)=structletlangpath=letbase,ext=Fpath.split_extpathinletbase=Fpath.basenamebaseinmatch(base,ext)with|_,(".ml"|".mli")->Some`OCaml|_,(".opam"|".install")->Some`Opam|"Makefile",_->Some`Make|("dune"|"dune-project"|"dune-workspace"),_->Some`Sexp|_->Noneletheaderslang=letline=Fmt.str"Generated by %s.%s"P.nameP.versioninmatchlangwith|`Sexp->Fmt.str";; %s"line|`Opam|`Make->Fmt.str"# %s"line|`OCaml->Fmt.str"(* %s *)"lineletshort_headerslang=matchlangwith|`Sexp->Fmt.str";; Generated by"|`Opam|`Make->"# Generated by"|`OCaml->"(* Generated by"lethas_headersfilecontents=matchFpath.basenamefilewith|"dune-project"|"dune-workspace"->(letlines=String.cuts~sep:"\n"~empty:true(String.trimcontents)inmatchList.revlineswith|x::_->String.is_infix~affix:(short_headers`Sexp)x|_->false)|_->(matchlangfilewith|None->false|Somelang->letaffix=short_headerslanginString.is_infix~affixcontents)letcan_overwritefile=let*is_file=Action.is_filefileinifis_filethenlet+content=Action.read_filefileinhas_headersfilecontentelseAction.oktrueletrmfile=let*can_overwrite=can_overwritefileinifnotcan_overwritethenAction.ok()elseAction.rmfileletwith_headersfilecontents=ifhas_headersfilecontentsthencontentselsematchFpath.basenamefilewith|"dune-project"|"dune-workspace"|"dune-workspace.config"->Fmt.str"%s\n%s\n"contents(headers`Sexp)|_->(matchlangfilewith|None->Fmt.invalid_arg"%a: invalide lang"Fpath.ppfile|Somelang->Fmt.str"%s\n\n%s"(headerslang)contents)letwritefilecontents=let*can_overwrite=can_overwritefileinifnotcan_overwritethenAction.ok()elseAction.write_filefile(with_headersfilecontents)end