-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These are inspired by a plan described in a comment in #2522, and only implement a quite limited form of recursive matching: only a single ** wildcard is accepted, it must be the final directory, and, if a ** wildcard is present, the file name must include a wildcard. Or-patterns are not implemented, for simplicity. Closes #3178, #2030.
- Loading branch information
1 parent
c750ef8
commit 8a00e00
Showing
18 changed files
with
387 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
|
||
----------------------------------------------------------------------------- | ||
-- | | ||
-- Module : Distribution.Simple.Glob | ||
-- Copyright : Isaac Jones, Simon Marlow 2003-2004 | ||
-- License : BSD3 | ||
-- portions Copyright (c) 2007, Galois Inc. | ||
-- | ||
-- Maintainer : [email protected] | ||
-- Portability : portable | ||
-- | ||
-- Simple file globbing. | ||
|
||
module Distribution.Simple.Glob ( | ||
matchFileGlob, | ||
matchDirFileGlob, | ||
isLiteralFileGlob, | ||
fileGlobMatches, | ||
parseFileGlob, | ||
explainGlobSyntaxError, | ||
GlobSyntaxError(..), | ||
GlobPat, | ||
) where | ||
|
||
import Prelude () | ||
import Distribution.Compat.Prelude | ||
|
||
import Distribution.Simple.Utils | ||
import Distribution.Verbosity | ||
import Distribution.Version | ||
|
||
import System.FilePath (splitExtensions, splitPath, takeExtensions) | ||
|
||
data GlobSyntaxError | ||
= StarInDirectory | ||
| StarInFileName | ||
| StarInExtension | ||
| NoExtensionOnStar | ||
| EmptyGlob | ||
| LiteralFileNameGlobStar | ||
| VersionDoesNotSupportGlobStar | ||
deriving (Eq, Show) | ||
|
||
explainGlobSyntaxError :: FilePath -> GlobSyntaxError -> String | ||
explainGlobSyntaxError filepath StarInDirectory = | ||
"invalid file glob '" ++ filepath | ||
++ "'. A wildcard '**' is only allowed as the final parent" | ||
++ " directory. Stars must not otherwise appear in the parent" | ||
++ " directories." | ||
explainGlobSyntaxError filepath StarInExtension = | ||
"invalid file glob '" ++ filepath | ||
++ "'. Wildcards '*' are only allowed as the" | ||
++ " file's base name, not in the file extension." | ||
explainGlobSyntaxError filepath StarInFileName = | ||
"invalid file glob '" ++ filepath | ||
++ "'. Wildcards '*' may only totally replace the" | ||
++ " file's base name, not only parts of it." | ||
explainGlobSyntaxError filepath NoExtensionOnStar = | ||
"invalid file glob '" ++ filepath | ||
++ "'. If a wildcard '*' is used it must be with an file extension." | ||
explainGlobSyntaxError filepath LiteralFileNameGlobStar = | ||
"invalid file glob '" ++ filepath | ||
++ "'. If a wildcard '**' is used as a parent directory, the" | ||
++ " file's base name must be a wildcard '*'." | ||
explainGlobSyntaxError _ EmptyGlob = | ||
"invalid file glob. A glob cannot be the empty string." | ||
explainGlobSyntaxError filepath VersionDoesNotSupportGlobStar = | ||
"invalid file glob '" ++ filepath | ||
++ "'. Using the double-star syntax requires 'cabal-version: 2.4'" | ||
++ " or greater. Alternatively, for compatibility with earlier Cabal" | ||
++ " versions, list the included directories explicitly." | ||
|
||
data IsRecursive = Recursive | NonRecursive | ||
|
||
data GlobPat = PatStem String GlobPat -- ^ A single subdirectory component + remainder. | ||
| PatMatch IsRecursive -- ^ Is this a **/*.ext pattern? | ||
String -- ^ Extensions | ||
| PatLit FilePath -- ^ Literal file name. | ||
|
||
isLiteralFileGlob :: FilePath -> Bool | ||
isLiteralFileGlob filepath = case parseFileGlob (mkVersion [2,4]) filepath of | ||
Left _ -> False | ||
Right pat -> check pat | ||
where | ||
check (PatStem _ pat) = check pat | ||
check (PatMatch _ _) = False | ||
check (PatLit _) = True | ||
|
||
fileGlobMatches :: GlobPat -> FilePath -> Bool | ||
fileGlobMatches pat = fileGlobMatchesSegments pat . splitPath | ||
|
||
fileGlobMatchesSegments :: GlobPat -> [FilePath] -> Bool | ||
fileGlobMatchesSegments _ [] = False | ||
fileGlobMatchesSegments pat (seg : segs) = case pat of | ||
PatStem dir pat' -> | ||
dir == seg && fileGlobMatchesSegments pat' segs | ||
PatMatch Recursive ext -> | ||
ext == takeExtensions (foldl' (flip const) seg segs) | ||
PatMatch NonRecursive ext -> | ||
null segs && ext == takeExtensions seg | ||
PatLit filename -> | ||
null segs && filename == seg | ||
|
||
parseFileGlob :: Version -> FilePath -> Either GlobSyntaxError GlobPat | ||
parseFileGlob version filepath = case reverse (splitPath filepath) of | ||
[] -> | ||
Left EmptyGlob | ||
(filename : "**/" : segments) | ||
| allowGlobStar -> do | ||
ext <- case splitExtensions filename of | ||
("*", ext) | '*' `elem` ext -> Left StarInExtension | ||
| null ext -> Left NoExtensionOnStar | ||
| otherwise -> Right ext | ||
_ -> Left LiteralFileNameGlobStar | ||
foldM addStem (PatMatch Recursive ext) segments | ||
| otherwise -> Left VersionDoesNotSupportGlobStar | ||
(filename : segments) -> do | ||
pat <- case splitExtensions filename of | ||
("*", ext) | '*' `elem` ext -> Left StarInExtension | ||
| null ext -> Left NoExtensionOnStar | ||
| otherwise -> Right (PatMatch NonRecursive ext) | ||
(_, ext) | '*' `elem` ext -> Left StarInExtension | ||
| '*' `elem` filename -> Left StarInFileName | ||
| otherwise -> Right (PatLit filename) | ||
foldM addStem pat segments | ||
where | ||
allowGlobStar = version >= mkVersion [2,4] | ||
addStem pat seg | ||
| '*' `elem` seg = Left StarInDirectory | ||
| otherwise = Right (PatStem seg pat) | ||
|
||
matchFileGlob :: Verbosity -> Version -> FilePath -> IO [FilePath] | ||
matchFileGlob verbosity version = matchDirFileGlob verbosity version "." | ||
|
||
matchDirFileGlob :: Verbosity -> Version -> FilePath -> FilePath -> IO [FilePath] | ||
matchDirFileGlob verbosity version dir filepath = case parseFileGlob version filepath of | ||
Left err -> die' verbosity $ explainGlobSyntaxError filepath err | ||
Right pat -> do | ||
files <- getDirectoryContentsRecursive dir | ||
case filter (fileGlobMatches pat) files of | ||
[] -> die' verbosity $ | ||
"filepath wildcard '" ++ filepath | ||
++ "' does not match any files." | ||
matches -> return matches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.