tilia-0.2.0.0: src/Tilia/Fixity/Archive.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
-- | What a package's source tarball holds that a module could be read out
-- of.
module Tilia.Fixity.Archive
( Archive (..),
readArchive,
InArchive (..),
moduleIn,
pathOfModule,
moduleEndings,
writtenForHsc,
haskellIn,
)
where
import Codec.Archive.Tar qualified as Tar
import Codec.Compression.GZip qualified as GZip
import Control.DeepSeq (NFData, force)
import Control.Exception (evaluate)
import Data.ByteString qualified as BS
import Data.ByteString.Lazy qualified as BL
import Data.Choice (Choice)
import Data.Functor.Identity (Identity (..))
import Data.List (isSuffixOf)
import Data.Maybe (listToMaybe, mapMaybe)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Distribution.PackageDescription (GenericPackageDescription)
import GHC.Generics (Generic)
import System.FilePath.Posix qualified as Posix
import Tilia.Cabal.Package (packageSourceDirs)
import Tilia.Cpp.Directives (withIncluded)
import Tilia.Fixity.Cabal (entryPosixPath, ownCabalFile)
import Tilia.Utils (quietly)
-- | What a tarball holds that a module could be read out of.
data Archive = Archive
{ -- | What the package's @.cabal@ file describes, the first one at the top
-- that parses.
archiveCabal :: Maybe GenericPackageDescription,
-- | Every file that could hold a module, by where it sits, in the order
-- the tarball has them.
archiveFiles :: [(FilePath, BS.ByteString)]
}
deriving (Generic)
instance NFData Archive
-- | Read a tarball, keeping what a module could be read out of.
readArchive :: FilePath -> IO (Maybe Archive)
readArchive tarball = quietly Nothing $ do
bytes <- BL.readFile tarball
Just
<$> evaluate (force (sweep Nothing [] (Tar.read (GZip.decompress bytes))))
where
sweep cabal found = \case
Tar.Next entry rest
| Nothing <- cabal,
Just own <- ownCabalFile entry ->
sweep (Just own) found rest
| Tar.NormalFile content _ <- Tar.entryContent entry,
any (`isSuffixOf` entryPosixPath entry) moduleEndings ->
sweep
cabal
((entryPosixPath entry, BL.toStrict content) : found)
rest
| otherwise -> sweep cabal found rest
_ -> Archive cabal (reverse found)
-- | Find a module in what a tarball holds and say what was found.
moduleIn :: Text -> Archive -> Maybe InArchive
moduleIn modName archive = listToMaybe (mapMaybe pick moduleEndings)
where
dirs = maybe [] packageSourceDirs (archiveCabal archive)
pick ending =
listToMaybe
[ inArchive ending file (T.decodeUtf8Lenient content)
| d <- dirs,
let file =
Posix.normalise (d <> "/" <> pathOfModule modName <> ending),
Just content <- [held file]
]
inArchive ending file text
| writtenForHsc ending = ForHsc
| otherwise =
uncurry Haskell . runIdentity $
withIncluded nextTo file (haskellIn ending text)
nextTo including name =
let file = collapsed (Posix.takeDirectory including Posix.</> name)
in Identity ((file,) . T.decodeUtf8Lenient <$> held file)
held file =
listToMaybe
[ content
| (path, content) <- archiveFiles archive,
drop 1 (dropWhile (/= '/') path) == file
]
-- | A path with each @..@ in it taking off the directory before it.
collapsed :: FilePath -> FilePath
collapsed = Posix.joinPath . reverse . foldl' up [] . Posix.splitDirectories
where
up (d : ds) ".." | d /= ".." = ds
up ds "." = ds
up ds d = d : ds
-- | Where a module's file is under the directory that holds it, without an
-- extension.
pathOfModule :: Text -> FilePath
pathOfModule = T.unpack . T.replace "." "/"
-- | The endings a package may write a module under, in the order they are
-- tried.
moduleEndings :: [String]
moduleEndings = [".hs", ".lhs", ".hsc"]
-- | What an archive holds for a module.
data InArchive
= -- | Haskell, as the package wrote it, with what each Haskell file it
-- includes that the tarball holds in place, and whether it includes
-- another file.
Haskell Text (Choice "includesUnread")
| -- | A module written for @hsc2hs@. Its text is not kept: there is
-- nothing to be done with it, and it is taken to declare nothing.
ForHsc
-- | Is this a module @hsc2hs@ writes rather than one anybody compiles?
writtenForHsc :: FilePath -> Bool
writtenForHsc = isSuffixOf ".hsc"
-- | The Haskell that a module's file holds, which is the code of a literate
-- one, each line where it was written.
haskellIn ::
-- | The file, or the ending of its name.
FilePath ->
-- | What it holds.
Text ->
Text
haskellIn path
| ".lhs" `isSuffixOf` path = T.unlines . unlit False . T.lines
| otherwise = id
where
unlit inCode = \case
[] -> []
l : ls
| inCode, written "\\end{code}" -> "" : unlit False ls
| inCode -> l : unlit True ls
| written "\\begin{code}" -> "" : unlit True ls
| Just code <- T.stripPrefix ">" l -> (" " <> code) : unlit False ls
| "#" `T.isPrefixOf` l -> l : unlit False ls
| otherwise -> "" : unlit False ls
where
written = (`T.isPrefixOf` T.stripStart l)