2 | module Data.FilePath.File
4 | import public Data.FilePath
12 | record File (t : PathType) where
17 | public export %inline
18 | toPath : File t -> Path t
19 | toPath (MkF p b) = p /> b
22 | public export %inline
23 | (</>) : Path t -> File Rel -> File t
24 | (</>) p (MkF p2 f) = MkF (p </> p2) f
28 | (/>) : Path t -> Body -> File t
33 | public export %inline
34 | split : File t -> (Path t, Body)
35 | split (MkF p b) = (p,b)
41 | (<.>) : File t -> Body -> File t
42 | (<.>) (MkF p f) ext = MkF p (f <.> ext)
46 | isAbsolute : File t -> Bool
47 | isAbsolute = isAbsolute . parent
51 | isHidden : File t -> Bool
52 | isHidden (MkF p f) = isHidden f
56 | basename : File t -> Body
61 | parentDirs : File t -> List (Path t)
62 | parentDirs (MkF p f) = FilePath.parentDirs $
p /> f
67 | splitFileName : File t -> Maybe (File t, Body)
68 | splitFileName (MkF p f) = do
70 | pure $
(MkF p b, ext)
75 | stemAndExt : File t -> Maybe (Body, Body)
76 | stemAndExt = map (\(MkF _ s, ext) => (s,ext)) . splitFileName
80 | fileStem : File t -> Maybe Body
81 | fileStem = map fst . stemAndExt
85 | extension : File t -> Maybe Body
86 | extension = map snd . stemAndExt
94 | normalize : File t -> File t
95 | normalize (MkF p b) = MkF (normalize p) b
102 | Show (File t) where
103 | showPrec p (MkF q b) = showCon p "MkF" $
showArg q ++ showArg b
106 | Interpolation (File t) where
107 | interpolate = interpolate . toPath
111 | f == g = toPath f == toPath g
113 | public export %inline
115 | compare f g = compare (toPath f) (toPath g)
127 | parse : String -> Maybe (File Abs)
128 | parse s = case AbsPath.parse s of
129 | Just (PAbs at $
sx :< x) => Just $
MkF (PAbs at sx) x
135 | -> {auto 0 prf : IsJust (AbsFile.parse s)}
137 | fromString s = fromJust (parse s)
145 | parse : String -> Maybe (File Rel)
146 | parse s = case RelPath.parse s of
147 | Just (PRel $
sx :< x) => Just $
MkF (PRel sx) x
153 | -> {auto 0 prf : IsJust (RelFile.parse s)}
155 | fromString s = fromJust (parse s)
163 | record AnyFile where
165 | {0 pathType : PathType}
166 | file : File pathType
168 | public export %inline
170 | AF p1 == AF p2 = heq (toPath p1) (toPath p2)
174 | compare (AF p1) (AF p2) = hcomp (toPath p1) (toPath p2)
177 | Show AnyFile where show (AF p) = show p
180 | Interpolation AnyFile where interpolate (AF p) = interpolate p
189 | parse : String -> Maybe AnyFile
190 | parse str = AF <$> AbsFile.parse str
191 | <|> AF <$> RelFile.parse str
196 | split : AnyFile -> (FilePath, Body)
197 | split (AF p) = let (f',b) := split p in (FP f', b)
203 | (<.>) : AnyFile -> (b : Body) -> AnyFile
204 | AF fp <.> s = AF $
fp <.> s
208 | isAbsolute : AnyFile -> Bool
209 | isAbsolute (AF p) = isAbsolute p.parent
213 | basename : AnyFile -> Body
214 | basename = snd . split
218 | parentDir : AnyFile -> FilePath
219 | parentDir = fst . split
223 | splitFileName : AnyFile -> Maybe (AnyFile, Body)
224 | splitFileName (AF p) = mapFst AF <$> splitFileName p
229 | stemAndExt : AnyFile -> Maybe (Body, Body)
230 | stemAndExt (AF p) = stemAndExt p
234 | fileStem : AnyFile -> Maybe Body
235 | fileStem (AF p) = fileStem p
239 | extension : AnyFile -> Maybe Body
240 | extension (AF p) = extension p
244 | isHidden : AnyFile -> Bool
245 | isHidden (AF p) = isHidden p