12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182(*
* 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.Syntaxletsrc=Logs.Src.create"functoria.cache"~doc:"functoria library"moduleLog=(valLogs.src_logsrc:Logs.LOG)typet=stringarrayletempty=[|""|]letis_emptyt=t=emptyletwritefileargv=Log.info(funm->m"Preserving arguments in %a:@ %a"Fpath.ppfileFmt.Dump.(arraystring)argv);(* Only keep args *)letargs=List.tl(Array.to_listargv)inletargs=List.mapString.Ascii.escapeargsinletargs=String.concat~sep:"\n"args^"\n"inAction.write_filefileargsletreadfile=Log.info(funl->l"reading cache %a"Fpath.ppfile);let*is_file=Action.is_filefileinifnotis_filethenAction.okemptyelselet*args=Action.read_filefileinletargs=String.cuts~sep:"\n"argsin(* remove trailing '\n' *)letargs=List.rev(List.tl(List.revargs))in(* Add an empty command *)letargs=""::argsinletargs=Array.of_listargsintryletargs=Array.map(funx->matchString.Ascii.unescapexwith|Somes->s|None->Fmt.failwith"%S: cannot parse"x)argsinAction.okargswithFailuree->Action.erroreletpeektterm=matchCmdliner.Cmd.eval_peek_opts~argv:ttermwith|Somec,_|_,Ok(`Okc)->Somec|_->Noneletmergetterm=letcache=matchpeekttermwithNone->Context.empty|Somec->cinletfterm=Context.merge~default:cacheterminCmdliner.Term.(constf$term)letpeek_outputt=Cli.peek_outputtletfile~nameargs=letbuild_dir=Fpath.parentargs.Cli.config_fileinmatchargs.Cli.context_filewith|Somef->f|None->Fpath.(build_dir/name/"context")