|
| 1 | +module Spago.Glob (gitignoringGlob) where |
| 2 | + |
| 3 | +import Spago.Prelude |
| 4 | + |
| 5 | +import Data.Array as Array |
| 6 | +import Data.String as String |
| 7 | +import Effect.Aff as Aff |
| 8 | +import Effect.Ref as Ref |
| 9 | +import Node.FS.Sync as SyncFS |
| 10 | +import Node.Path as Path |
| 11 | +import Record as Record |
| 12 | +import Type.Proxy (Proxy(..)) |
| 13 | + |
| 14 | +type MicroMatchOptions = { ignore :: Array String } |
| 15 | + |
| 16 | +foreign import micromatch :: MicroMatchOptions -> Array String -> String -> Boolean |
| 17 | + |
| 18 | +type Entry = { name :: String, path :: String, dirent :: DirEnt } |
| 19 | +type FsWalkOptions = { entryFilter :: Entry -> Effect Boolean, deepFilter :: Entry -> Effect Boolean } |
| 20 | + |
| 21 | +foreign import data DirEnt :: Type |
| 22 | +foreign import isFile :: DirEnt -> Boolean |
| 23 | +foreign import fsWalkImpl |
| 24 | + :: (forall a b. a -> Either a b) |
| 25 | + -> (forall a b. b -> Either a b) |
| 26 | + -> (Either Error (Array Entry) -> Effect Unit) |
| 27 | + -> FsWalkOptions |
| 28 | + -> String |
| 29 | + -> Effect Unit |
| 30 | + |
| 31 | +gitignoreToMicromatchPatterns :: String -> String -> { ignore :: Array String, patterns :: Array String } |
| 32 | +gitignoreToMicromatchPatterns base = |
| 33 | + String.split (String.Pattern "\n") |
| 34 | + >>> map String.trim |
| 35 | + >>> Array.filter (not <<< or [ String.null, isComment ]) |
| 36 | + >>> partitionMap |
| 37 | + ( \line -> do |
| 38 | + let negated = isJust $ String.stripPrefix (String.Pattern "!") line |
| 39 | + let pattern = Path.concat [ base, gitignorePatternToMicromatch line ] |
| 40 | + if negated then Left pattern else Right pattern |
| 41 | + ) |
| 42 | + >>> Record.rename (Proxy :: Proxy "left") (Proxy :: Proxy "ignore") |
| 43 | + >>> Record.rename (Proxy :: Proxy "right") (Proxy :: Proxy "patterns") |
| 44 | + |
| 45 | + where |
| 46 | + isComment = isJust <<< String.stripPrefix (String.Pattern "#") |
| 47 | + dropSuffixSlash str = fromMaybe str $ String.stripSuffix (String.Pattern "/") str |
| 48 | + dropPrefixSlash str = fromMaybe str $ String.stripPrefix (String.Pattern "/") str |
| 49 | + |
| 50 | + gitignorePatternToMicromatch :: String -> String |
| 51 | + gitignorePatternToMicromatch pattern |
| 52 | + -- Git matches every pattern that does not include a `/` by basename. |
| 53 | + | not $ String.contains (String.Pattern "/") pattern = "**/" <> pattern |
| 54 | + | otherwise = |
| 55 | + -- Micromatch treats every pattern like git treats those starting with '/'. |
| 56 | + dropPrefixSlash pattern |
| 57 | + -- ".spago/" in a .gitignore is the same as ".spago". Micromatch does interpret them differently. |
| 58 | + # dropSuffixSlash |
| 59 | + |
| 60 | +fsWalk :: String -> Array String -> Array String -> Aff (Array Entry) |
| 61 | +fsWalk cwd ignorePatterns includePatterns = Aff.makeAff \cb -> do |
| 62 | + let includeMatcher = micromatch { ignore: [] } includePatterns -- The Stuff we are globbing for. |
| 63 | + |
| 64 | + -- Pattern for directories which can be outright ignored. |
| 65 | + -- This will be updated whenver a .gitignore is found. |
| 66 | + ignoreMatcherRef <- Ref.new $ micromatch { ignore: [] } ignorePatterns |
| 67 | + |
| 68 | + -- If this Ref contains `true` because this Aff has been canceled, then deepFilter will always return false. |
| 69 | + canceled <- Ref.new false |
| 70 | + let |
| 71 | + -- Should `fsWalk` recurse into this directory? |
| 72 | + deepFilter :: Entry -> Effect Boolean |
| 73 | + deepFilter entry = Ref.read canceled >>= |
| 74 | + if _ then |
| 75 | + -- The Aff has been canceled, don't recurse into any further directories! |
| 76 | + pure false |
| 77 | + else do |
| 78 | + matcher <- Ref.read ignoreMatcherRef |
| 79 | + pure $ not $ matcher (Path.relative cwd entry.path) |
| 80 | + |
| 81 | + -- Should `fsWalk` retain this entry for the result array? |
| 82 | + entryFilter :: Entry -> Effect Boolean |
| 83 | + entryFilter entry = do |
| 84 | + when (isFile entry.dirent && entry.name == ".gitignore") do -- A .gitignore was encountered |
| 85 | + let gitignorePath = entry.path |
| 86 | + |
| 87 | + -- directory of this .gitignore relative to the directory being globbed |
| 88 | + let base = Path.relative cwd (Path.dirname gitignorePath) |
| 89 | + |
| 90 | + try (SyncFS.readTextFile UTF8 gitignorePath) >>= case _ of |
| 91 | + Left _ -> pure unit |
| 92 | + Right gitignore -> do |
| 93 | + let { ignore, patterns } = gitignoreToMicromatchPatterns base gitignore |
| 94 | + let matcherForThisGitignore = micromatch { ignore } patterns |
| 95 | + |
| 96 | + -- Instead of composing the matcher functions, we could also keep a growing array of |
| 97 | + -- patterns and regenerate the matcher on every append. I don't know which option is |
| 98 | + -- more performant, but composing functions is more conventient. |
| 99 | + let addMatcher currentMatcher = or [ currentMatcher, matcherForThisGitignore ] |
| 100 | + void $ Ref.modify addMatcher ignoreMatcherRef |
| 101 | + |
| 102 | + ignoreMatcher <- Ref.read ignoreMatcherRef |
| 103 | + let path = Path.relative cwd entry.path |
| 104 | + pure $ includeMatcher path && not (ignoreMatcher path) |
| 105 | + |
| 106 | + options = { entryFilter, deepFilter } |
| 107 | + |
| 108 | + fsWalkImpl Left Right cb options cwd |
| 109 | + |
| 110 | + pure $ Aff.Canceler \_ -> |
| 111 | + void $ liftEffect $ Ref.write true canceled |
| 112 | + |
| 113 | +gitignoringGlob :: String -> Array String -> Aff (Array String) |
| 114 | +gitignoringGlob dir patterns = |
| 115 | + map (Path.relative dir <<< _.path) |
| 116 | + <$> fsWalk dir [ ".git" ] patterns |
0 commit comments