Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit c1e1bb4

Browse files
authored
Add a subset of path manipulation functionality (#1)
* Add a subset of path manipulation functionality * Review changes and version bump * Add documentation for lua users * Update README.md
1 parent 408b8f0 commit c1e1bb4

5 files changed

Lines changed: 365 additions & 34 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/dist
22
/.ghc.environment.*
3-
/.stack-work
3+
/.stack-work
4+
/dist-newstyle
5+
6+
/stack.yaml.lock

README.md

Lines changed: 238 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
HsLua Module: System
2-
====================
1+
# HsLua Module: System
32

43
This module provides access to system information and functionality via
54
Haskell's `System` module.
@@ -8,9 +7,7 @@ Intended usage for this package is to preload it by adding the loader
87
function to `package.preload`. Note that the Lua `package` library must
98
have already been loaded before the loader can be added.
109

11-
12-
Example
13-
-------
10+
## Example
1411

1512
``` haskell
1613
loadProg :: Lua Status
@@ -22,31 +19,29 @@ loadProg = do
2219
++ "system.with_tmpdir('.', 'foo', print)"
2320
```
2421

22+
## Documentation
2523

26-
Documentation
27-
-------------
28-
29-
## Fields
24+
### Fields
3025

31-
### arch
26+
#### arch
3227

3328
The machine architecture on which the program is running.
3429

35-
### compiler_name
30+
#### compiler_name
3631

3732
The Haskell implementation with which the host program was compiled.
3833

39-
### compiler_version
34+
#### compiler_version
4035

4136
The version of `compiler_name` with which the host program was compiled.
4237

43-
### os
38+
#### os
4439

4540
The operating system on which the program is running.
4641

47-
## Functions
42+
### General Functions
4843

49-
### env
44+
#### env
5045

5146
`env ()`
5247

@@ -57,7 +52,7 @@ Returns:
5752
- A table mapping environment variables names to their string value
5853
(table).
5954

60-
### getenv
55+
#### getenv
6156

6257
`getenv (var)`
6358

@@ -74,7 +69,7 @@ Returns:
7469
- value of the variable, or nil if the variable is not defined (string
7570
or nil).
7671

77-
### getwd
72+
#### getwd
7873

7974
`getwd ()`
8075

@@ -84,7 +79,7 @@ Returns:
8479

8580
- The current working directory (string).
8681

87-
### ls
82+
#### ls
8883

8984
`ls ([directory])`
9085

@@ -101,7 +96,7 @@ Returns:
10196
- A table of all entries in `directory` without the special entries (`.`
10297
and `..`).
10398

104-
### mkdir
99+
#### mkdir
105100

106101
`mkdir (dirname [, create_parent])`
107102

@@ -123,7 +118,7 @@ Parameters:
123118
`create_parent`
124119
: create parent directories if necessary
125120

126-
### rmdir
121+
#### rmdir
127122

128123
`rmdir (dirname [, recursive])`
129124

@@ -138,7 +133,7 @@ Parameters:
138133
`recursive`
139134
: delete content recursively
140135

141-
### setenv
136+
#### setenv
142137

143138
`setenv (var, value)`
144139

@@ -152,7 +147,7 @@ Parameters:
152147
`value`
153148
: new value (string).
154149

155-
### setwd
150+
#### setwd
156151

157152
`setwd (directory)`
158153

@@ -164,7 +159,7 @@ Parameters:
164159
: Path of the directory which is to become the new working
165160
directory (string)
166161

167-
### tmpdirname
162+
#### tmpdirname
168163

169164
`tmpdirname ()`
170165

@@ -189,7 +184,7 @@ Returns:
189184

190185
- The current directory for temporary files (string).
191186

192-
### with\_env
187+
#### with\_env
193188

194189
`with_env (environment, callback)`
195190

@@ -211,9 +206,9 @@ Parameters:
211206

212207
Returns:
213208

214-
- The result(s) of the call to `callback`
209+
- The result(s) of the call to `callback`
215210

216-
### with\_tmpdir
211+
#### with\_tmpdir
217212

218213
`with_tmpdir ([parent_dir,] templ, callback)`
219214

@@ -236,9 +231,9 @@ Parameters:
236231

237232
Returns:
238233

239-
- The result of the call to `callback`.
234+
- The result of the call to `callback`.
240235

241-
### with\_wd
236+
#### with\_wd
242237

243238
`with_wd (directory, callback)`
244239

@@ -258,11 +253,223 @@ Parameters:
258253

259254
Returns:
260255

261-
- The result(s) of the call to `callback`
256+
- The result(s) of the call to `callback`
257+
258+
### Path Manipulation Functions
259+
260+
This library includes wrappers around the following functions from the [filepath](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath.html) library for the current platform (POSIX or Windows). All examples below are for [POSIX systems](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html).
261+
262+
#### take_directory
263+
264+
`take_directory(filepath)`
265+
266+
Get the directory name, move up one level.
267+
268+
```lua
269+
takeDirectory("/foo/bar/baz") == "/foo/bar"
270+
takeDirectory("/foo/bar/baz/") == "/foo/bar/baz"
271+
```
272+
273+
Parameters:
274+
275+
`filepath`
276+
: path path
277+
278+
Returns:
279+
280+
- The modified filepath.
281+
282+
This function wraps [System.FilePath.takeDirectory](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:takeDirectory).
283+
284+
#### take_filename
285+
286+
`take_filename(filepath)`
287+
288+
Get the file name.
289+
290+
```lua
291+
takeFileName("/directory/file.ext") == "file.ext"
292+
takeFileName("test/") == ""
293+
```
294+
295+
Parameters:
296+
297+
`filepath`
298+
: path
299+
300+
Returns:
301+
302+
- The file name.
303+
304+
This function wraps [System.FilePath.takeFileName](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:takeFileName).
305+
306+
#### take_extensions
307+
308+
`take_extensions(filepath)`
309+
310+
Get all extensions.
311+
312+
```lua
313+
takeExtensions("/directory/path.ext") == ".ext"
314+
takeExtensions("file.tar.gz") == ".tar.gz
315+
```
316+
317+
Parameters:
318+
319+
`filepath`
320+
: path
321+
322+
Returns:
323+
324+
- String of all extensions.
325+
326+
This function wraps [System.FilePath.takeExtensions](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:takeExtensions).
327+
328+
#### drop_extension
329+
330+
`drop_extension(filepath)`
331+
332+
Remove last extension, and the `.` preceding it.
333+
334+
```lua
335+
dropExtension("/directory/path.ext") == "/directory/path"
336+
```
337+
338+
Parameters:
339+
340+
`filepath`
341+
: path
342+
343+
Returns:
344+
345+
- The modified filepath without extension.
346+
347+
This function wraps [System.FilePath.dropExtension](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:dropExtension).
348+
349+
#### has_extension
350+
351+
`has_extensions(filepath)`
352+
353+
Does the given filename have an extension?
354+
355+
```lua
356+
hasExtension("/directory/path.ext") == true
357+
hasExtension("/directory/path") == false
358+
```
359+
360+
Parameters:
361+
362+
`filepath`
363+
: path
364+
365+
Returns:
366+
367+
- `true` if `filepath` has an extension.
368+
369+
This function wraps [System.FilePath.hasExtension](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:hasExtension).
370+
371+
#### split_directories
372+
373+
`split_directories(filepath)`
374+
375+
Split a path by the directory separator.
376+
377+
```lua
378+
splitDirectories("/directory/file.ext") == {"/","directory","file.ext"}
379+
splitDirectories("test/file") == {"test","file"}
380+
splitDirectories("/test/file") == {"/","test","file"}
381+
```
382+
383+
Parameters:
384+
385+
`filepath`
386+
: path
387+
388+
Returns:
389+
390+
- A list of all directory paths.
391+
392+
This function wraps [System.FilePath.splitDirectories](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:splitDirectories).
393+
394+
#### join_path
395+
396+
`join_path(filepath)`
397+
398+
Join path elements back together by the directory separator.
399+
400+
```lua
401+
joinPath({"/","directory/","file.ext"}) == "/directory/file.ext"
402+
```
403+
404+
Parameters:
405+
406+
`filepath`
407+
: path
408+
409+
Returns:
410+
411+
- The joined path.
412+
413+
This function wraps [System.FilePath.joinPath](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:joinPath).
414+
415+
#### is_relative
416+
417+
`ìs_relative(filepath)`
418+
419+
Is a path relative, or is it fixed to the root?
420+
421+
```lua
422+
isRelative("test/path") == true
423+
isRelative("/test") == false
424+
isRelative("/") == false
425+
```
426+
427+
Parameters:
428+
429+
`filepath`
430+
: path
431+
432+
Returns:
433+
434+
- `true` if `filepath` is a relative path.
435+
436+
This function wraps [System.FilePath.isRelative](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:isRelative).
437+
438+
#### is_absolute
439+
440+
`is_absolute(filepath)`
441+
442+
Is a path absolute? (same as `! is_relative(filepath)`)
443+
444+
Parameters:
445+
446+
`filepath`
447+
: path
448+
449+
Returns:
450+
451+
- `true` if `filepath` is an absolute path.
452+
453+
This function wraps [System.FilePath.isAbsolute](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:isAbsolute).
454+
455+
#### normalise
456+
457+
`normalise(filepath)`
458+
459+
Normalise a path. See examples [here](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:normalise).
460+
461+
Parameters:
462+
463+
`filepath`
464+
: path
465+
466+
Returns:
467+
468+
- The normalised path.
262469

470+
This function wraps [System.FilePath.normalise](https://hackage.haskell.org/package/filepath-1.4.2.1/docs/System-FilePath-Posix.html#v:normalise).
263471

264-
License
265-
-------
472+
## License
266473

267474
This package is licensed under the MIT license. See [`LICENSE`](LICENSE)
268475
for details.

0 commit comments

Comments
 (0)