|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
| 2 | +{-# LANGUAGE RankNTypes #-} |
| 3 | + |
| 4 | +----------------------------------------------------------------------------- |
| 5 | +-- | |
| 6 | +-- Module : Distribution.Simple.Glob |
| 7 | +-- Copyright : Isaac Jones, Simon Marlow 2003-2004 |
| 8 | +-- License : BSD3 |
| 9 | +-- portions Copyright (c) 2007, Galois Inc. |
| 10 | +-- |
| 11 | + |
| 12 | +-- Portability : portable |
| 13 | +-- |
| 14 | +-- Simple file globbing. |
| 15 | + |
| 16 | +module Distribution.Simple.Glob ( |
| 17 | + matchFileGlob, |
| 18 | + matchDirFileGlob, |
| 19 | + fileGlobMatches, |
| 20 | + parseFileGlob, |
| 21 | + explainGlobSyntaxError, |
| 22 | + GlobSyntaxError(..), |
| 23 | + GlobPat, |
| 24 | + ) where |
| 25 | + |
| 26 | +import Prelude () |
| 27 | +import Distribution.Compat.Prelude |
| 28 | + |
| 29 | +import Distribution.Simple.Utils |
| 30 | +import Distribution.Verbosity |
| 31 | +import Distribution.Version |
| 32 | + |
| 33 | +import System.FilePath (joinPath, splitExtensions, splitDirectories, takeExtensions, (</>)) |
| 34 | + |
| 35 | +-- Note throughout that we use splitDirectories, not splitPath. On |
| 36 | +-- Posix, this makes no difference, but, because Windows accepts both |
| 37 | +-- slash and backslash as its path separators, if we left in the |
| 38 | +-- separators from the glob we might not end up properly normalised. |
| 39 | + |
| 40 | +data GlobSyntaxError |
| 41 | + = StarInDirectory |
| 42 | + | StarInFileName |
| 43 | + | StarInExtension |
| 44 | + | NoExtensionOnStar |
| 45 | + | EmptyGlob |
| 46 | + | LiteralFileNameGlobStar |
| 47 | + | VersionDoesNotSupportGlobStar |
| 48 | + | VersionDoesNotSupportGlob |
| 49 | + deriving (Eq, Show) |
| 50 | + |
| 51 | +explainGlobSyntaxError :: FilePath -> GlobSyntaxError -> String |
| 52 | +explainGlobSyntaxError filepath StarInDirectory = |
| 53 | + "invalid file glob '" ++ filepath |
| 54 | + ++ "'. A wildcard '**' is only allowed as the final parent" |
| 55 | + ++ " directory. Stars must not otherwise appear in the parent" |
| 56 | + ++ " directories." |
| 57 | +explainGlobSyntaxError filepath StarInExtension = |
| 58 | + "invalid file glob '" ++ filepath |
| 59 | + ++ "'. Wildcards '*' are only allowed as the" |
| 60 | + ++ " file's base name, not in the file extension." |
| 61 | +explainGlobSyntaxError filepath StarInFileName = |
| 62 | + "invalid file glob '" ++ filepath |
| 63 | + ++ "'. Wildcards '*' may only totally replace the" |
| 64 | + ++ " file's base name, not only parts of it." |
| 65 | +explainGlobSyntaxError filepath NoExtensionOnStar = |
| 66 | + "invalid file glob '" ++ filepath |
| 67 | + ++ "'. If a wildcard '*' is used it must be with an file extension." |
| 68 | +explainGlobSyntaxError filepath LiteralFileNameGlobStar = |
| 69 | + "invalid file glob '" ++ filepath |
| 70 | + ++ "'. If a wildcard '**' is used as a parent directory, the" |
| 71 | + ++ " file's base name must be a wildcard '*'." |
| 72 | +explainGlobSyntaxError _ EmptyGlob = |
| 73 | + "invalid file glob. A glob cannot be the empty string." |
| 74 | +explainGlobSyntaxError filepath VersionDoesNotSupportGlobStar = |
| 75 | + "invalid file glob '" ++ filepath |
| 76 | + ++ "'. Using the double-star syntax requires 'cabal-version: 3.0'" |
| 77 | + ++ " or greater. Alternatively, for compatibility with earlier Cabal" |
| 78 | + ++ " versions, list the included directories explicitly." |
| 79 | +explainGlobSyntaxError filepath VersionDoesNotSupportGlob = |
| 80 | + "invalid file glob '" ++ filepath |
| 81 | + ++ "'. Using star wildcards requires 'cabal-version: >= 1.6'. " |
| 82 | + ++ "Alternatively if you require compatibility with earlier Cabal " |
| 83 | + ++ "versions then list all the files explicitly." |
| 84 | + |
| 85 | +data IsRecursive = Recursive | NonRecursive |
| 86 | + |
| 87 | +data GlobPat = PatStem String GlobPat |
| 88 | + -- ^ A single subdirectory component + remainder. |
| 89 | + | PatMatch IsRecursive String |
| 90 | + -- ^ First argument: Is this a @**/*.ext@ pattern? |
| 91 | + -- Second argument: the extensions to accept. |
| 92 | + | PatLit FilePath |
| 93 | + -- ^ Literal file name. |
| 94 | + |
| 95 | +fileGlobMatches :: GlobPat -> FilePath -> Bool |
| 96 | +fileGlobMatches pat = fileGlobMatchesSegments pat . splitDirectories |
| 97 | + |
| 98 | +fileGlobMatchesSegments :: GlobPat -> [FilePath] -> Bool |
| 99 | +fileGlobMatchesSegments _ [] = False |
| 100 | +fileGlobMatchesSegments pat (seg : segs) = case pat of |
| 101 | + PatStem dir pat' -> |
| 102 | + dir == seg && fileGlobMatchesSegments pat' segs |
| 103 | + PatMatch Recursive ext -> |
| 104 | + ext == takeExtensions (foldl' (flip const) seg segs) |
| 105 | + PatMatch NonRecursive ext -> |
| 106 | + null segs && ext == takeExtensions seg |
| 107 | + PatLit filename -> |
| 108 | + null segs && filename == seg |
| 109 | + |
| 110 | +parseFileGlob :: Version -> FilePath -> Either GlobSyntaxError GlobPat |
| 111 | +parseFileGlob version filepath = case reverse (splitDirectories filepath) of |
| 112 | + [] -> |
| 113 | + Left EmptyGlob |
| 114 | + (filename : "**" : segments) |
| 115 | + | allowGlobStar -> do |
| 116 | + ext <- case splitExtensions filename of |
| 117 | + ("*", ext) | '*' `elem` ext -> Left StarInExtension |
| 118 | + | null ext -> Left NoExtensionOnStar |
| 119 | + | otherwise -> Right ext |
| 120 | + _ -> Left LiteralFileNameGlobStar |
| 121 | + foldM addStem (PatMatch Recursive ext) segments |
| 122 | + | otherwise -> Left VersionDoesNotSupportGlobStar |
| 123 | + (filename : segments) -> do |
| 124 | + pat <- case splitExtensions filename of |
| 125 | + ("*", ext) | not allowGlob -> Left VersionDoesNotSupportGlob |
| 126 | + | '*' `elem` ext -> Left StarInExtension |
| 127 | + | null ext -> Left NoExtensionOnStar |
| 128 | + | otherwise -> Right (PatMatch NonRecursive ext) |
| 129 | + (_, ext) | '*' `elem` ext -> Left StarInExtension |
| 130 | + | '*' `elem` filename -> Left StarInFileName |
| 131 | + | otherwise -> Right (PatLit filename) |
| 132 | + foldM addStem pat segments |
| 133 | + where |
| 134 | + allowGlob = version >= mkVersion [1,6] |
| 135 | + allowGlobStar = version >= mkVersion [3,0] |
| 136 | + addStem pat seg |
| 137 | + | '*' `elem` seg = Left StarInDirectory |
| 138 | + | otherwise = Right (PatStem seg pat) |
| 139 | + |
| 140 | +matchFileGlob :: Verbosity -> Version -> FilePath -> IO [FilePath] |
| 141 | +matchFileGlob verbosity version = matchDirFileGlob verbosity version "." |
| 142 | + |
| 143 | +-- The returned values do not include the supplied @dir@ prefix. |
| 144 | +matchDirFileGlob :: Verbosity -> Version -> FilePath -> FilePath -> IO [FilePath] |
| 145 | +matchDirFileGlob verbosity version dir filepath = case parseFileGlob version filepath of |
| 146 | + Left err -> die' verbosity $ explainGlobSyntaxError filepath err |
| 147 | + Right pat -> do |
| 148 | + -- This function might be called from the project root with dir as |
| 149 | + -- ".". Walking the tree starting there involves going into .git/ |
| 150 | + -- and dist-newstyle/, which is a lot of work for no reward, so |
| 151 | + -- extract the constant prefix from the pattern and start walking |
| 152 | + -- there. If the pattern is **/*.blah, then of course we'll have |
| 153 | + -- to walk the whole thing anyway, but that's what the user asked |
| 154 | + -- for! |
| 155 | + let (prefixSegments, pat') = splitConstantPrefix pat |
| 156 | + joinedPrefix = joinPath prefixSegments |
| 157 | + files <- getDirectoryContentsRecursive (dir </> joinedPrefix) |
| 158 | + case filter (fileGlobMatches pat') files of |
| 159 | + [] -> die' verbosity $ |
| 160 | + "filepath wildcard '" ++ filepath |
| 161 | + ++ "' does not match any files." |
| 162 | + matches -> return $ fmap (joinedPrefix </>) matches |
| 163 | + |
| 164 | +unfoldr' :: (a -> Either r (b, a)) -> a -> ([b], r) |
| 165 | +unfoldr' f a = case f a of |
| 166 | + Left r -> ([], r) |
| 167 | + Right (b, a') -> case unfoldr' f a' of |
| 168 | + (bs, r) -> (b : bs, r) |
| 169 | + |
| 170 | +-- | Extract the (possibly null) constant prefix from the pattern. |
| 171 | +-- This has the property that, if @(pref, pat') = splitConstantPrefix pat@, |
| 172 | +-- then @pat === foldr PatStem pat' pref@. |
| 173 | +splitConstantPrefix :: GlobPat -> ([FilePath], GlobPat) |
| 174 | +splitConstantPrefix = unfoldr' step |
| 175 | + where |
| 176 | + step (PatStem seg pat) = Right (seg, pat) |
| 177 | + step pat = Left pat |
0 commit comments