--------------------------------------------------------------------------------- Module : Distribution.PackageDescription.PrettyPrint-- Copyright : Jürgen Nicklisch-Franken 2010-- License : AllRightsReserved---- Maintainer : cabal-devel@haskell.org-- Stability : provisional-- Portability : portable---- | Pretty printing for cabal files-------------------------------------------------------------------------------{- All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Isaac Jones nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}moduleDistribution.PackageDescription.PrettyPrint(writeGenericPackageDescription,showGenericPackageDescription,)whereimportDistribution.PackageDescription(TestSuite(..),TestSuiteInterface(..),testType,SourceRepo(..),customFieldsBI,CondTree(..),Condition(..),FlagName(..),ConfVar(..),Executable(..),Library(..),Flag(..),PackageDescription(..),GenericPackageDescription(..))importText.PrettyPrint(hsep,comma,punctuate,fsep,parens,char,nest,empty,isEmpty,($$),(<+>),colon,(<>),text,vcat,($+$),Doc,render)importDistribution.Simple.Utils(writeUTF8File)importDistribution.ParseUtils(showFreeText,FieldDescr(..))importDistribution.PackageDescription.Parse(pkgDescrFieldDescrs,binfoFieldDescrs,libFieldDescrs,sourceRepoFieldDescrs)importDistribution.Package(Dependency(..))importDistribution.Text(Text(..))importData.Maybe(isJust,fromJust,isNothing)indentWith::IntindentWith=4-- | Recompile with false for regression testingsimplifiedPrinting::BoolsimplifiedPrinting=False-- | Writes a .cabal file from a generic package descriptionwriteGenericPackageDescription::FilePath->GenericPackageDescription->IO()writeGenericPackageDescriptionfpathpkg=writeUTF8Filefpath(showGenericPackageDescriptionpkg)-- | Writes a generic package description to a stringshowGenericPackageDescription::GenericPackageDescription->StringshowGenericPackageDescription=render.ppGenericPackageDescriptionppGenericPackageDescription::GenericPackageDescription->DocppGenericPackageDescriptiongpd=ppPackageDescription(packageDescriptiongpd)$+$ppGenPackageFlags(genPackageFlagsgpd)$+$ppLibrary(condLibrarygpd)$+$ppExecutables(condExecutablesgpd)$+$ppTestSuites(condTestSuitesgpd)ppPackageDescription::PackageDescription->DocppPackageDescriptionpd=ppFieldspkgDescrFieldDescrspd$+$ppCustomFields(customFieldsPDpd)$+$ppSourceRepos(sourceRepospd)ppSourceRepos::[SourceRepo]->DocppSourceRepos[]=emptyppSourceRepos(hd:tl)=ppSourceRepohd$+$ppSourceRepostlppSourceRepo::SourceRepo->DocppSourceReporepo=emptyLine$text"source-repository"<+>disp(repoKindrepo)$+$(nestindentWith(ppFieldssourceRepoFieldDescrs'repo))wheresourceRepoFieldDescrs'=[fd|fd<-sourceRepoFieldDescrs,fieldNamefd/="kind"]ppFields::[FieldDescra]->a->DocppFieldsfieldsx=vcat[ppFieldname(getterx)|FieldDescrnamegetter_<-fields]ppField::String->Doc->DocppFieldnamefielddoc|isEmptyfielddoc=empty|otherwise=textname<>colon<+>fielddocppDiffFields::[FieldDescra]->a->a->DocppDiffFieldsfieldsxy=vcat[ppFieldname(getterx)|FieldDescrnamegetter_<-fields,render(getterx)/=render(gettery)]ppCustomFields::[(String,String)]->DocppCustomFieldsflds=vcat[ppCustomFieldf|f<-flds]ppCustomField::(String,String)->DocppCustomField(name,val)=textname<>colon<+>showFreeTextvalppGenPackageFlags::[Flag]->DocppGenPackageFlagsflds=vcat[ppFlagf|f<-flds]ppFlag::Flag->DocppFlag(MkFlagnamedescdfltmanual)=emptyLine$text"flag"<+>ppFlagNamename$+$(nestindentWith((ifnulldescthenemptyelsetext"Description: "<+>showFreeTextdesc)$+$(ifdfltthenemptyelsetext"Default: False")$+$(ifmanualthentext"Manual: True"elseempty)))ppLibrary::(Maybe(CondTreeConfVar[Dependency]Library))->DocppLibraryNothing=emptyppLibrary(JustcondTree)=emptyLine$text"library"$+$nestindentWith(ppCondTreecondTreeNothingppLib)whereppLiblibNothing=ppFieldslibFieldDescrslib$$ppCustomFields(customFieldsBI(libBuildInfolib))ppLiblib(Justplib)=ppDiffFieldslibFieldDescrslibplib$$ppCustomFields(customFieldsBI(libBuildInfolib))ppExecutables::[(String,CondTreeConfVar[Dependency]Executable)]->DocppExecutablesexes=vcat[emptyLine$text("executable "++n)$+$nestindentWith(ppCondTreecondTreeNothingppExe)|(n,condTree)<-exes]whereppExe(Executable_modulePath'buildInfo')Nothing=(ifmodulePath'==""thenemptyelsetext"main-is:"<+>textmodulePath')$+$ppFieldsbinfoFieldDescrsbuildInfo'$+$ppCustomFields(customFieldsBIbuildInfo')ppExe(Executable_modulePath'buildInfo')(Just(Executable_modulePath2buildInfo2))=(ifmodulePath'==""||modulePath'==modulePath2thenemptyelsetext"main-is:"<+>textmodulePath')$+$ppDiffFieldsbinfoFieldDescrsbuildInfo'buildInfo2$+$ppCustomFields(customFieldsBIbuildInfo')ppTestSuites::[(String,CondTreeConfVar[Dependency]TestSuite)]->DocppTestSuitessuites=emptyLine$vcat[text("test-suite "++n)$+$nestindentWith(ppCondTreecondTreeNothingppTestSuite)|(n,condTree)<-suites]whereppTestSuitetestsuiteNothing=text"type:"<+>disp(testTypetestsuite)$+$maybeempty(\f->text"main-is:"<+>textf)(testSuiteMainIstestsuite)$+$maybeempty(\m->text"test-module:"<+>dispm)(testSuiteModuletestsuite)$+$ppFieldsbinfoFieldDescrs(testBuildInfotestsuite)$+$ppCustomFields(customFieldsBI(testBuildInfotestsuite))ppTestSuite(TestSuite__buildInfo'_)(Just(TestSuite__buildInfo2_))=ppDiffFieldsbinfoFieldDescrsbuildInfo'buildInfo2$+$ppCustomFields(customFieldsBIbuildInfo')testSuiteMainIstest=casetestInterfacetestofTestSuiteExeV10_f->Justf_->NothingtestSuiteModuletest=casetestInterfacetestofTestSuiteLibV09_m->Justm_->NothingppCondition::ConditionConfVar->DocppCondition(Varx)=ppConfVarxppCondition(Litb)=text(showb)ppCondition(CNotc)=char'!'<>(ppConditionc)ppCondition(COrc1c2)=parens(hsep[ppConditionc1,text"||"<+>ppConditionc2])ppCondition(CAndc1c2)=parens(hsep[ppConditionc1,text"&&"<+>ppConditionc2])ppConfVar::ConfVar->DocppConfVar(OSos)=text"os"<>parens(dispos)ppConfVar(Archarch)=text"arch"<>parens(disparch)ppConfVar(Flagname)=text"flag"<>parens(ppFlagNamename)ppConfVar(Implcv)=text"impl"<>parens(dispc<+>dispv)ppFlagName::FlagName->DocppFlagName(FlagNamename)=textnameppCondTree::CondTreeConfVar[Dependency]a->Maybea->(a->Maybea->Doc)->DocppCondTreect@(CondNodeitdepsifs)mbItppIt=letres=ppDepsdeps$+$(vcat$mapppIfifs)$+$ppItitmbItinifisJustmbIt&&isEmptyresthenppCondTreectNothingppItelsereswhereppIf(c,thenTree,mElseTree)=((emptyLine$text"if"<+>ppConditionc)$$nestindentWith(ppCondTreethenTree(ifsimplifiedPrintingthen(Justit)elseNothing)ppIt))$+$(ifisNothingmElseTreethenemptyelsetext"else"$$nestindentWith(ppCondTree(fromJustmElseTree)(ifsimplifiedPrintingthen(Justit)elseNothing)ppIt))ppDeps::[Dependency]->DocppDeps[]=emptyppDepsdeps=text"build-depends:"<+>fsep(punctuatecomma(mapdispdeps))emptyLine::Doc->DocemptyLined=text" "$+$d