1
1
defmodule Path do
2
2
@ moduledoc """
3
3
This module provides conveniences for manipulating or
4
- retrieving filesystem paths.
4
+ retrieving file system paths.
5
5
6
- The functions on this module may receive a char list or
7
- a binary as argument and will return the given type.
6
+ The functions in this module may receive a char list or
7
+ a binary as argument and will return a value of the same
8
+ type.
8
9
9
10
The majority of the functions in this module do not
10
- interact with the file system, unless some few functions
11
- that needs to query the filesystem to retrieve paths
12
- (like `Path.wildcard` and `Path.expand`).
11
+ interact with the file system, except for a few functions
12
+ that require it (like `Path.wildcard` and `Path.expand`).
13
13
"""
14
14
15
15
alias :filename , as: FN
@@ -18,9 +18,8 @@ defmodule Path do
18
18
@ type r :: char_list | binary
19
19
20
20
@ doc """
21
- Converts the given filename and returns an absolute name.
22
- Differently from `Path.expand/1`, no attempt is made to
23
- resolve `..`, `.` or `~`.
21
+ Converts the given path to an absolute one. Differently from
22
+ `Path.expand/1`, no attempt is made to resolve `..`, `.` or `~`.
24
23
25
24
## Unix examples
26
25
@@ -43,9 +42,8 @@ defmodule Path do
43
42
end
44
43
45
44
@ doc """
46
- Converts the given filename and returns an absolute name
47
- relative to the given location. If the path is already
48
- an absolute path, the relative path is ignored.
45
+ Builds a path from `relative_to` to `path`. If `path` is already
46
+ an absolute path, `relative_to` is ignored. See also `Path.relative/2`.
49
47
50
48
Differently from `Path.expand/2`, no attempt is made to
51
49
resolve `..`, `.` or `~`.
@@ -64,8 +62,8 @@ defmodule Path do
64
62
end
65
63
66
64
@ doc """
67
- Expands the path by returning its absolute name and expanding
68
- any `.` and `..` characters.
65
+ Converts the path to an absolute one and expands
66
+ any `.` and `..` characters and a leading `~` .
69
67
70
68
## Examples
71
69
@@ -78,20 +76,29 @@ defmodule Path do
78
76
end
79
77
80
78
@ doc """
81
- Expands the path to the relative location and expanding
82
- any `.` and `..` characters. If the path is already an
83
- absolute path, the relative location is ignored.
79
+ Expands the path relative to the path given as the second argument
80
+ expanding any `.` and `..` characters. If the path is already an
81
+ absolute path, `relative_to` is ignored.
82
+
83
+ Note, that this function treats `path` with leading `~` as
84
+ an absolute one.
85
+
86
+ The second argument is first expanded to an absolute path.
84
87
85
88
## Examples
86
89
90
+ # Assuming that the absolute path to baz is /quux/baz
91
+ Path.expand("foo/bar/../bar", "baz")
92
+ #=> "/quux/baz/foo/bar"
93
+
87
94
iex> Path.expand("foo/bar/../bar", "/baz")
88
95
"/baz/foo/bar"
89
96
iex> Path.expand("/foo/bar/../bar", "/baz")
90
97
"/foo/bar"
91
98
92
99
"""
93
100
def expand ( path , relative_to ) do
94
- normalize FN . absname ( FN . absname ( expand_home ( path ) , relative_to ) , get_cwd ( path ) )
101
+ normalize FN . absname ( FN . absname ( expand_home ( path ) , expand_home ( relative_to ) ) , get_cwd ( path ) )
95
102
end
96
103
97
104
@ doc """
@@ -102,6 +109,7 @@ defmodule Path do
102
109
Path.type("/usr/local/bin") #=> :absolute
103
110
Path.type("usr/local/bin") #=> :relative
104
111
Path.type("../usr/local/bin") #=> :relative
112
+ Path.type("~/file") #=> :relative
105
113
106
114
## Windows examples
107
115
@@ -185,8 +193,9 @@ defmodule Path do
185
193
186
194
@ doc """
187
195
Returns the given `path` relative to the given `from` path.
196
+ In other words, it tries to strip the `from` prefix from `path`.
188
197
189
- This function does not query the filesystem , so it assumes
198
+ This function does not query the file system , so it assumes
190
199
no symlinks in between the paths.
191
200
192
201
In case a direct relative path cannot be found, it returns
@@ -267,20 +276,22 @@ defmodule Path do
267
276
end
268
277
269
278
@ doc """
270
- Return the ` directory` component of `path`.
279
+ Returns the directory component of `path`.
271
280
272
281
## Examples
273
282
274
283
Path.dirname("/foo/bar.ex")
275
- #=> "foo"
284
+ #=> "/foo"
285
+ Path.dirname("/foo/bar/baz.ex")
286
+ #=> "/foo/bar"
276
287
277
288
"""
278
289
def dirname ( path ) do
279
290
FN . dirname ( path )
280
291
end
281
292
282
293
@ doc """
283
- Return the ` extension` of the last component of `path`.
294
+ Returns the extension of the last component of `path`.
284
295
285
296
## Examples
286
297
@@ -326,8 +337,8 @@ defmodule Path do
326
337
end
327
338
328
339
@ doc """
329
- Returns a string with one or more paths components joint by the path separator.
330
- This function should be used to convert a list of strings in a path.
340
+ Returns a string with one or more path components joined by the path separator.
341
+ This function should be used to convert a list of strings to a path.
331
342
332
343
## Examples
333
344
@@ -409,8 +420,8 @@ defmodule Path do
409
420
:lists . reverse ( name )
410
421
411
422
@ doc """
412
- Returns a list with the path splitted by the path separator.
413
- If an empty string is given, then it returns the root path.
423
+ Returns a list with the path split by the path separator.
424
+ If an empty string is given, returns the root path.
414
425
415
426
## Examples
416
427
@@ -450,7 +461,7 @@ defmodule Path do
450
461
451
462
Imagine you have a directory called `projects` with three Elixir projects
452
463
inside of it: `elixir`, `ex_doc` and `dynamo`. You can find all `.beam` files
453
- inside their ebin directories all projects as follows:
464
+ inside the ebin directory of each project as follows:
454
465
455
466
Path.wildcard("projects/*/ebin/**/*.beam")
456
467
@@ -520,4 +531,4 @@ defmodule Path do
520
531
defp normalize ( [ ] , acc ) do
521
532
join Enum . reverse ( acc )
522
533
end
523
- end
534
+ end
0 commit comments