{-# OPTIONS -cpp #-}------------------------------------------------------------------------------- |-- Module : Distribution.Simple.SrcDist-- Copyright : Simon Marlow 2004-- -- Maintainer : Isaac Jones <ijones@syntaxpolice.org>-- Stability : alpha-- Portability : portable---- Implements the \"@.\/setup sdist@\" command, which creates a source-- distribution for this package. That is, packs up the source code-- into a tarball.{- Copyright (c) 2003-2004, Simon Marlow
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. -}-- NOTE: FIX: we don't have a great way of testing this module, since-- we can't easily look inside a tarball once its created.moduleDistribution.Simple.SrcDist(sdist,createArchive,prepareTree,tarBallName,copyFileTo#ifdef DEBUG ,hunitTests#endif)whereimportDistribution.PackageDescription(PackageDescription(..),BuildInfo(..),Executable(..),Library(..),withLib,withExe,setupMessage)importDistribution.Package(showPackageId,PackageIdentifier(pkgVersion))importDistribution.Version(Version(versionBranch),VersionRange(AnyVersion))importDistribution.Simple.Utils(createDirectoryIfMissingVerbose,smartCopySources,die,warn,notice,findPackageDesc,findFile,copyFileVerbose)importDistribution.Simple.Setup(SDistFlags(..))importDistribution.Simple.PreProcess(PPSuffixHandler,ppSuffixes,preprocessSources)importDistribution.Simple.LocalBuildInfo(LocalBuildInfo(..))importDistribution.Simple.Program(defaultProgramConfiguration,requireProgram,rawSystemProgram,tarProgram)#ifndef __NHC__importControl.Exception(finally)#endifimportControl.Monad(when)importData.Char(isSpace,toLower)importData.List(isPrefixOf)importSystem.Time(getClockTime,toCalendarTime,CalendarTime(..))importDistribution.Compat.Directory(doesFileExist,doesDirectoryExist,getCurrentDirectory,removeDirectoryRecursive)importDistribution.VerbosityimportSystem.FilePath((</>),takeDirectory,isAbsolute)#ifdef DEBUGimportTest.HUnit(Test)#endif#ifdef __NHC__finally::IOa->IOb->IOax`finally`y=do{a<-x;y;returna}#endif-- |Create a source distribution.sdist::PackageDescription-- ^information from the tarball->MaybeLocalBuildInfo-- ^Information from configure->SDistFlags-- ^verbosity & snapshot->FilePath-- ^build prefix (temp dir)->FilePath-- ^TargetPrefix->[PPSuffixHandler]-- ^ extra preprocessors (includes suffixes)->IO()sdistpkg_descr_origmb_lbi(SDistFlagssnapshotverbosity)tmpDirtargetPrefpps=dotime<-getClockTimect<-toCalendarTimetimeletdate=ctYearct*10000+(fromEnum(ctMonthct)+1)*100+ctDayctletpkg_descr|snapshot=updatePackage(updatePkgVersion(updateVersionBranch(++[date])))pkg_descr_orig|otherwise=pkg_descr_origprepareTreepkg_descrverbositymb_lbisnapshottmpDirppsdatecreateArchivepkg_descrverbositymb_lbitmpDirtargetPrefreturn()whereupdatePackagefpd=pd{package=f(packagepd)}updatePkgVersionfpkg=pkg{pkgVersion=f(pkgVersionpkg)}updateVersionBranchfv=v{versionBranch=f(versionBranchv)}-- |Prepare a directory tree of source files.prepareTree::PackageDescription-- ^info from the cabal file->Verbosity-- ^verbosity->MaybeLocalBuildInfo->Bool-- ^snapshot->FilePath-- ^source tree to populate->[PPSuffixHandler]-- ^extra preprocessors (includes suffixes)->Int-- ^date->IOFilePathprepareTreepkg_descrverbositymb_lbisnapshottmpDirppsdate=dosetupMessageverbosity"Building source dist for"pkg_descrex<-doesDirectoryExisttmpDirwhenex(die$"Source distribution already in place. please move: "++tmpDir)lettargetDir=tmpDir</>(nameVersionpkg_descr)createDirectoryIfMissingVerboseverbosityTruetargetDir-- maybe move the library files into placewithLibpkg_descr()$\l->prepareDirverbositytargetDirpps(exposedModulesl)(libBuildInfol)-- move the executables into placewithExepkg_descr$\(Executable_mainPathexeBi)->doprepareDirverbositytargetDirpps[]exeBisrcMainFile<-findFile(hsSourceDirsexeBi)mainPathcopyFileToverbositytargetDirsrcMainFileflipmapM_(dataFilespkg_descr)$\file->doletdir=takeDirectoryfilecreateDirectoryIfMissingVerboseverbosityTrue(targetDir</>dir)copyFileVerboseverbosityfile(targetDir</>file)when(not(null(licenseFilepkg_descr)))$copyFileToverbositytargetDir(licenseFilepkg_descr)flipmapM_(extraSrcFilespkg_descr)$\fpath->docopyFileToverbositytargetDirfpath-- copy the install-include fileswithLibpkg_descr()$\l->doletlbi=libBuildInfolrelincdirs=".":filter(not.isAbsolute)(includeDirslbi)incs<-mapM(findIncrelincdirs)(installIncludeslbi)flipmapM_incs$\(_,fpath)->copyFileToverbositytargetDirfpath-- we have some preprocessors specified, try to generate those fileswhen(not(nullpps))$casemb_lbiofJustlbi->preprocessSourcespkg_descr(lbi{buildDir=targetDir})TrueverbosityppsNothing->warnverbosity"Cannot run preprocessors. Run 'configure' command first."-- setup isn't listed in the description file.hsExists<-doesFileExist"Setup.hs"lhsExists<-doesFileExist"Setup.lhs"ifhsExiststhencopyFileToverbositytargetDir"Setup.hs"elseiflhsExiststhencopyFileToverbositytargetDir"Setup.lhs"elsewriteFile(targetDir</>"Setup.hs")$unlines["import Distribution.Simple","main = defaultMainWithHooks defaultUserHooks"]-- the description file itselfdescFile<-getCurrentDirectory>>=findPackageDescverbositylettargetDescFile=targetDir</>descFile-- We could just writePackageDescription targetDescFile pkg_descr,-- but that would lose comments and formatting.ifsnapshotthendocontents<-readFiledescFilewriteFiletargetDescFile$unlines$map(appendVersiondate)$lines$contentselsecopyFileVerboseverbositydescFiletargetDescFilereturntargetDirwhereappendVersion::Int->String->StringappendVersionnline|"version:"`isPrefixOf`maptoLowerline=trimTrailingSpaceline++"."++shown|otherwise=linetrimTrailingSpace::String->StringtrimTrailingSpace=reverse.dropWhileisSpace.reversefindInc[]f=die("can't find include file "++f)findInc(d:ds)f=doletpath=(d</>f)b<-doesFileExistpathifbthenreturn(f,path)elsefindIncdsf-- |Create an archive from a tree of source files, and clean up the tree.createArchive::PackageDescription-- ^info from cabal file->Verbosity-- ^verbosity->MaybeLocalBuildInfo-- ^info from configure->FilePath-- ^source tree to archive->FilePath-- ^name of archive to create->IOFilePathcreateArchivepkg_descrverbositymb_lbitmpDirtargetPref=dolettarBallFilePath=targetPref</>tarBallNamepkg_descr(tarProg,_)<-requireProgramverbositytarProgramAnyVersion(maybedefaultProgramConfigurationwithProgramsmb_lbi)-- Hmm: I could well be skating on thinner ice here by using the -C option (=> GNU tar-specific?)-- [The prev. solution used pipes and sub-command sequences to set up the paths correctly,-- which is problematic in a Windows setting.]rawSystemProgramverbositytarProg["-C",tmpDir,"-czf",tarBallFilePath,nameVersionpkg_descr]-- XXX this should be done back where tmpDir is made, not here`finally`removeDirectoryRecursivetmpDirnoticeverbosity$"Source tarball created: "++tarBallFilePathreturntarBallFilePath-- |Move the sources into place based on buildInfoprepareDir::Verbosity-- ^verbosity->FilePath-- ^TargetPrefix->[PPSuffixHandler]-- ^ extra preprocessors (includes suffixes)->[String]-- ^Exposed modules->BuildInfo->IO()prepareDirverbosityinPrefppsmodsBuildInfo{hsSourceDirs=srcDirs,otherModules=mods',cSources=cfiles}=doletsuff=ppSuffixespps++["hs","lhs"]smartCopySourcesverbositysrcDirsinPref(mods++mods')suffTrueTruemapM_(copyFileToverbosityinPref)cfilescopyFileTo::Verbosity->FilePath->FilePath->IO()copyFileToverbositydirfile=dolettargetFile=dir</>filecreateDirectoryIfMissingVerboseverbosityTrue(takeDirectorytargetFile)copyFileVerboseverbosityfiletargetFile-------------------------------------------------------------- |The file name of the tarballtarBallName::PackageDescription->FilePathtarBallNamep=(nameVersionp)++".tar.gz"nameVersion::PackageDescription->StringnameVersion=showPackageId.package-- -------------------------------------------------------------- * Testing-- ------------------------------------------------------------#ifdef DEBUGhunitTests::[Test]hunitTests=[]#endif