-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPath.hs
More file actions
345 lines (310 loc) · 10.4 KB
/
Path.hs
File metadata and controls
345 lines (310 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
{-# LANGUAGE OverloadedStrings #-}
{-|
Module : HsLua.Module.Path
Copyright : © 2021-2026 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb@hslua.org>
Lua module to work with file paths.
-}
module HsLua.Module.Path (
-- * Module
documentedModule
-- * Fields
, separator
, search_path_separator
-- * Path manipulation
, add_extension
, combine
, directory
, filename
, is_absolute
, is_relative
, join
, make_relative
, normalize
, split
, split_extension
, split_search_path
, treat_strings_as_paths
)
where
import Data.Text (Text)
import Data.Version (Version, makeVersion)
import HsLua.Core
( LuaError, getglobal, getmetatable, nth, pop, rawset, remove, top )
import HsLua.Marshalling
( Peeker, peekList, peekString, pushList, pushName, pushString )
import HsLua.Packaging
import qualified Data.Text as T
import qualified System.FilePath as Path
-- | The @path@ module specification.
documentedModule :: LuaError e => Module e
documentedModule = defmodule "path"
`withDescription` "Module for file path manipulations."
`withFields` fields
`withFunctions` functions
--
-- Fields
--
-- | Exported fields.
fields :: [Field e]
fields =
[ separator
, search_path_separator
]
-- | Wrapper for @'Path.pathSeparator'@.
separator :: Field e
separator = deffield "separator"
`withType` "string"
`withDescription` "The character that separates directories."
`withValue` pushString [Path.pathSeparator]
-- | Wrapper for @'Path.searchPathSeparator'@.
search_path_separator :: Field e
search_path_separator = deffield "search_path_separator"
`withType` "string"
`withDescription`
"The character that is used to separate the entries in " <>
"the `PATH` environment variable."
`withValue` pushString [Path.searchPathSeparator]
--
-- Functions
--
functions :: LuaError e => [DocumentedFunction e]
functions =
[ directory
, filename
, is_absolute
, is_relative
, join
, make_relative
, normalize
, split
, split_extension
, split_search_path
, treat_strings_as_paths
]
-- | See @Path.takeDirectory@
directory :: DocumentedFunction e
directory = defun "directory"
### liftPure Path.takeDirectory
<#> filepathParam
=#> filepathResult "The filepath up to the last directory separator."
#? ("Gets the directory name, i.e., removes the last directory " <>
"separator and everything after from the given path.")
`since` initialVersion
-- | See @Path.takeFilename@
filename :: DocumentedFunction e
filename = defun "filename"
### liftPure Path.takeFileName
<#> filepathParam
=#> filepathResult "File name part of the input path."
#? "Get the file name."
`since` initialVersion
-- | See @Path.isAbsolute@
is_absolute :: DocumentedFunction e
is_absolute = defun "is_absolute"
### liftPure Path.isAbsolute
<#> filepathParam
=#> boolResult ("`true` iff `filepath` is an absolute path, " <>
"`false` otherwise.")
#? "Checks whether a path is absolute, i.e. not fixed to a root."
`since` initialVersion
-- | See @Path.isRelative@
is_relative :: DocumentedFunction e
is_relative = defun "is_relative"
### liftPure Path.isRelative
<#> filepathParam
=#> boolResult ("`true` iff `filepath` is a relative path, " <>
"`false` otherwise.")
#? "Checks whether a path is relative or fixed to a root."
`since` initialVersion
-- | See @Path.joinPath@
join :: LuaError e => DocumentedFunction e
join = defun "join"
### liftPure Path.joinPath
<#> parameter (peekList peekFilePath) "{string,...}"
"filepaths" "path components"
=#> filepathResult "The joined path."
#? "Join path elements back together by the directory separator."
`since` initialVersion
make_relative :: DocumentedFunction e
make_relative = defun "make_relative"
### liftPure3 makeRelative
<#> parameter
peekFilePath
"string"
"path"
"path to be made relative"
<#> parameter
peekFilePath
"string"
"root"
"root path"
<#> opt (boolParam "unsafe" "whether to allow `..` in the result.")
=#> filepathResult "contracted filename"
#? mconcat
[ "Contract a filename, based on a relative path. Note that the "
, "resulting path will never introduce `..` paths, as the "
, "presence of symlinks means `../b` may not reach `a/b` if it "
, "starts from `a/c`. For a worked example see "
, "[this blog post](http://neilmitchell.blogspot.co.uk"
, "/2015/10/filepaths-are-subtle-symlinks-are-hard.html)."
]
`since` initialVersion
-- | See @Path.normalise@
normalize :: DocumentedFunction e
normalize = defun "normalize"
### liftPure Path.normalise
<#> filepathParam
=#> filepathResult "The normalized path."
#? T.unlines
[ "Normalizes a path."
, ""
, " - `//` makes sense only as part of a (Windows) network drive;"
, " elsewhere, multiple slashes are reduced to a single"
, " `path.separator` (platform dependent)."
, " - `/` becomes `path.separator` (platform dependent)."
, " - `./` is removed."
, " - an empty path becomes `.`"
]
`since` initialVersion
-- | See @Path.splitDirectories@.
--
-- Note that this does /not/ wrap @'Path.splitPath'@, as that function
-- adds trailing slashes to each directory, which is often inconvenient.
split :: LuaError e => DocumentedFunction e
split = defun "split"
### liftPure Path.splitDirectories
<#> filepathParam
=#> filepathListResult "List of all path components."
#? "Splits a path by the directory separator."
`since` initialVersion
-- | See @Path.splitExtension@
split_extension :: DocumentedFunction e
split_extension = defun "split_extension"
### liftPure Path.splitExtension
<#> filepathParam
=#> (functionResult (pushString . fst) "string" "filepath without extension"
++
functionResult (pushString . snd) "string" "extension or empty string")
#? ("Splits the last extension from a file path and returns the parts. "
<> "The extension, if present, includes the leading separator; "
<> "if the path has no extension, then the empty string is returned "
<> "as the extension.")
`since` initialVersion
-- | Wraps function @'Path.splitSearchPath'@.
split_search_path :: LuaError e => DocumentedFunction e
split_search_path = defun "split_search_path"
### liftPure Path.splitSearchPath
<#> Parameter
{ parameterPeeker = peekString
, parameterDoc = ParameterDoc
{ parameterName = "search_path"
, parameterType = "string"
, parameterDescription = "platform-specific search path"
, parameterIsOptional = False
}
}
=#> filepathListResult "list of directories in search path"
#? ("Takes a string and splits it on the `search_path_separator` "
<> "character. Blank items are ignored on Windows, "
<> "and converted to `.` on Posix. "
<> "On Windows path elements are stripped of quotes.")
`since` initialVersion
-- | Join two paths with a directory separator. Wraps @'Path.combine'@.
combine :: DocumentedFunction e
combine = defun "combine"
### liftPure2 Path.combine
<#> filepathParam
<#> filepathParam
=#> filepathResult "combined paths"
#? "Combine two paths with a path separator."
-- | Adds an extension to a file path. Wraps @'Path.addExtension'@.
add_extension :: DocumentedFunction e
add_extension = defun "add_extension"
### liftPure2 Path.addExtension
<#> filepathParam
<#> Parameter
{ parameterPeeker = peekString
, parameterDoc = ParameterDoc
{ parameterName = "extension"
, parameterType = "string"
, parameterDescription = "an extension, with or without separator dot"
, parameterIsOptional = False
}
}
=#> filepathResult "filepath with extension"
#? "Adds an extension, even if there is already one."
`since` initialVersion
stringAugmentationFunctions :: LuaError e => [DocumentedFunction e]
stringAugmentationFunctions =
[ directory
, filename
, is_absolute
, is_relative
, normalize
, split
, split_extension
, split_search_path
]
treat_strings_as_paths :: LuaError e => DocumentedFunction e
treat_strings_as_paths = defun "treat_strings_as_paths"
### do let addFunction fn = do
pushName (functionName fn)
pushDocumentedFunction fn
rawset (nth 3)
-- for some reason we can't just dump all functions into the
-- string metatable, but have to use the string module for
-- non-metamethods.
pushString "" *> getmetatable top *> remove (nth 2)
mapM_ addFunction
[ setName add_extension "__add", setName combine "__div"]
pop 1 -- string metatable
_ <- getglobal "string"
mapM_ addFunction stringAugmentationFunctions
pop 1 -- string module
=#> []
#? ("Augment the string module such that strings can be used as "
<> "path objects.")
`since` initialVersion
--
-- Parameters
--
-- | Retrieves a file path from the stack.
peekFilePath :: Peeker e FilePath
peekFilePath = peekString
-- | Filepath function parameter.
filepathParam :: Parameter e FilePath
filepathParam = parameter peekFilePath "string" "filepath" "path"
-- | Result of a function returning a file path.
filepathResult :: Text -- ^ Description
-> FunctionResults e FilePath
filepathResult = stringResult
-- | List of filepaths function result.
filepathListResult :: LuaError e
=> Text -- ^ Description
-> FunctionResults e [FilePath]
filepathListResult = functionResult (pushList pushString) "{string,...}"
--
-- Helpers
--
-- | Alternative version of @'Path.makeRelative'@, which introduces @..@
-- paths if desired.
makeRelative :: FilePath -- ^ path to be made relative
-> FilePath -- ^ root directory from which to start
-> Maybe Bool -- ^ whether to use unsafe relative paths.
-> FilePath
makeRelative path root (Just True)
| Path.equalFilePath root path = "."
| Path.takeDrive root /= Path.takeDrive path = path
| otherwise =
let toParts = Path.splitDirectories . Path.normalise
go (pp:pps) (rp:rps)
| pp == rp = go pps rps
go pps rps
= Path.joinPath $ replicate (length rps) ".." ++ pps
in go (toParts path) (toParts root)
makeRelative path root _unsafe = Path.makeRelative root path
-- | First published version of this library.
initialVersion :: Version
initialVersion = makeVersion [0,1,0]