123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228(* Registration of (sub)formats for use from the command line *)(* This file is part of ocp-ocamlres - formats & subformats registry
* (C) 2013 OCamlPro - Benjamin CANOU
*
* ocp-ocamlres is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later
* version, with linking exception.
*
* ocp-ocamlres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the LICENSE file for more details *)openOCamlResFormatsopenOCamlResSubFormatsmoduleSM=Map.Make(String)(*-- Formats registry ---------------------------------------------*)moduletypeFormat=sigvaloutput:stringOCamlRes.Res.root->unitvalinfo:stringvaloptions:(Arg.key*Arg.spec*Arg.doc)listendletformats:(moduleFormat)SM.tref=refSM.emptyletregister_formatname(format:(moduleFormat))=formats:=SM.addnameformat!formatsletfind_formatname:(moduleFormat)=SM.findname!formatsletformats()=!formats(*-- SubFormats registry ------------------------------------------*)moduletypeSubFormat=sigincludeSubFormatvalinfo:stringvaloptions:(Arg.key*Arg.spec*Arg.doc)listendletsubformats:(moduleSubFormat)SM.tref=refSM.emptyletregister_subformatnamem=letmoduleM=(valm:SubFormat)insubformats:=SM.addnamem!subformatsletfind_subformatname=SM.findname!subformatsletsubformats()=!subformats(*-- Predefined Subformats -----------------------------------------*)moduleRaw=structincludeOCamlResSubFormats.Rawletinfo="raw file contents as a string"letoptions=[]endmoduleInt=structincludeOCamlResSubFormats.Intletinfo="for files containing only an integer"letoptions=[]endmoduleLines=structincludeOCamlResSubFormats.Linesletinfo="splits the input into lines"letoptions=[]endlet_=register_subformat"raw"(moduleRaw:SubFormat);register_subformat"int"(moduleInt:SubFormat);register_subformat"lines"(moduleLines:SubFormat)(*-- Predefined Formats -------------------------------------------*)modulePredefOptions=structletwidth=ref80letsubformats=refSM.emptyletoutput_file=refNoneletadd_ext,add_mod=(* ugly hackery to work around Arg,Tuple's behaviour *)letr_ext=refNoneandr_mod=refNoneinletupdate()=match!r_ext,!r_modwith|None,_|_,None->()|Somee,Somer->r_ext:=None;r_mod:=None;trysubformats:=SM.adde(find_subformatr)!subformatswithNot_found->Printf.eprintf"Subformat %s not found.\n%!"r;exit1in((fune->r_ext:=Somee;update()),(funm->r_mod:=Somem;update()))letoptions=["-width",Arg.Set_intwidth,"set the maximum chars per line of generated code";"-subformat",Arg.(Tuple[Stringadd_ext;Stringadd_mod]),"\"ext\" \"subformat\"&\
preprocess files ending by \"ext\" with \"subformat\"";"-o",Arg.String(funf->output_file:=Somef),"\"file name\"&\
print in a file instead of stdout"]endmoduleExtensionDispatcherSubFormat=structletfind_subformat(dirs,name)=(trymatchnamewith|Some(name,Someext)->SM.findext!PredefOptions.subformats|_->raiseNot_foundwithNot_found->(moduleRaw))typet=stringletfrom_rawpathdata=dataletto_rawpathdata=dataletpprintpathdata=letmoduleSF=(valfind_subformatpath)inSF.pprintpath(SF.from_rawpathdata)letpprint_headerpathdata=letmoduleSF=(valfind_subformatpath)inSF.pprint_footerpath(SF.from_rawpathdata)letpprint_footerpathdata=letmoduleSF=(valfind_subformatpath)inSF.pprint_footerpath(SF.from_rawpathdata)letnamepathdata=letmoduleSF=(valfind_subformatpath)inSF.namepath(SF.from_rawpathdata)lettype_namepathdata=letmoduleSF=(valfind_subformatpath)inSF.type_namepath(SF.from_rawpathdata)letmod_namepathdata=letmoduleSF=(valfind_subformatpath)inSF.mod_namepath(SF.from_rawpathdata)endletdisclaimer="(* This file has been generated by ocp-ocamlres *)\n"moduleOCaml=structmoduleF=OCamlResFormats.OCaml(ExtensionDispatcherSubFormat)letoptions=PredefOptions.optionsletinfo="produces static ocaml bindings (modules for dirs, values for files)"letoutputroot=match!PredefOptions.output_filewith|None->letout_channel=stdoutinletparams=F.({width=!PredefOptions.width;out_channel})inF.outputparamsroot|Somefn->letout_channel=open_outfninletparams=F.({width=!PredefOptions.width;out_channel})inoutput_stringout_channeldisclaimer;F.outputparamsroot;close_outout_channelendmoduleTree(Params:sigvalvariants:boolvalinfo:stringend)=structletuse_variants=reftruemoduleF=OCamlResFormats.Res(ExtensionDispatcherSubFormat)letoptions=PredefOptions.options@["-no-variants",Arg.Clearuse_variants,"use a plain sum type instead of polymorphic variants";]letinfo=Params.infoletoutputroot=letuse_variants_for_leaves=!use_variantsanduse_variants_for_nodes=Params.variantsandwidth=!PredefOptions.widthinmatch!PredefOptions.output_filewith|None->letout_channel=stdoutinletparams=F.({width;out_channel;use_variants_for_nodes;use_variants_for_leaves})inF.outputparamsroot|Somefn->letout_channel=open_outfninletparams=F.({width;out_channel;use_variants_for_nodes;use_variants_for_leaves})inoutput_stringout_channeldisclaimer;F.outputparamsroot;close_outout_channelendmoduleRes=Tree(structletvariants=falseletinfo="produces the OCaml source representation \
of the OCamlRes tree"end)moduleVariants=Tree(structletvariants=trueletinfo="produces the OCaml source representation \
of the resource tree using polymorphic variant type\n\
([ `File of (* depends *) | `Dir of 'a list ] as 'a)"end)moduleFiles=structmoduleF=OCamlResFormats.Files(OCamlResSubFormats.Raw)letbase_output_dir=ref"."letinfo="reproduces the original files"letoptions=["-output-dir",Arg.Set_stringbase_output_dir,"\"dir\"&set the base output directory (defaults to \".\")"]letoutputroot=letparams=F.({base_output_dir=!base_output_dir})inF.outputparamsrootendlet_=register_format"ocaml"(moduleOCaml:Format);register_format"ocamlres"(moduleRes:Format);register_format"variants"(moduleVariants:Format);register_format"files"(moduleFiles:Format)