|
8 | 8 | FilePathsBase.jl provides a type based approach to working with filesystem paths in julia. |
9 | 9 |
|
10 | 10 | ## Intallation |
| 11 | + |
11 | 12 | FilePathsBase.jl is registered, so you can to use `Pkg.add` to install it. |
12 | | -```julia-repl |
| 13 | +```julia |
13 | 14 | julia> Pkg.add("FilePathsBase") |
14 | 15 | ``` |
15 | 16 |
|
16 | | -## Usage |
17 | | -```julia-repl |
18 | | -julia> using FilePathsBase; using FilePathsBase: / |
19 | | -``` |
| 17 | +## Getting Started |
20 | 18 |
|
21 | | -The first important difference about working with paths in FilePathsBase.jl is that path |
22 | | -segments are represented as an immutable tuple of strings. |
| 19 | +Here are some common operations that you may want to perform with file paths. |
23 | 20 |
|
24 | | -Path creation: |
25 | | -```julia-repl |
26 | | -julia> Path("~/repos/FilePathsBase.jl/") |
27 | | -p"~/repos/FilePathsBase.jl/" |
28 | | -``` |
29 | | -or |
30 | | -```julia-repl |
31 | | -julia> p"~/repos/FilePathsBase.jl/" |
32 | | -p"~/repos/FilePathsBase.jl/" |
33 | | -``` |
| 21 | +```julia |
| 22 | +#= |
| 23 | +NOTE: We're loading our `/` operator for path concatenation into the currect scope, but non-path division operations will still fallback to the base behaviour. |
| 24 | +=# |
| 25 | +julia> using FilePathsBase; using FilePathsBase: / |
34 | 26 |
|
35 | | -Human readable file status info: |
36 | | -```julia-repl |
37 | | -julia> stat(p"README.md") |
| 27 | +julia> cwd() |
| 28 | +p"/Users/rory/repos/FilePathsBase.jl" |
| 29 | + |
| 30 | +julia> walkpath(cwd() / "docs") |> collect |
| 31 | +23-element Array{Any,1}: |
| 32 | + p"/Users/rory/repos/FilePathsBase.jl/docs/.DS_Store" |
| 33 | + p"/Users/rory/repos/FilePathsBase.jl/docs/Manifest.toml" |
| 34 | + p"/Users/rory/repos/FilePathsBase.jl/docs/Project.toml" |
| 35 | + p"/Users/rory/repos/FilePathsBase.jl/docs/build" |
| 36 | + p"/Users/rory/repos/FilePathsBase.jl/docs/build/api.html" |
| 37 | + p"/Users/rory/repos/FilePathsBase.jl/docs/build/assets" |
| 38 | + p"/Users/rory/repos/FilePathsBase.jl/docs/build/assets/arrow.svg" |
| 39 | + p"/Users/rory/repos/FilePathsBase.jl/docs/build/assets/documenter.css" |
| 40 | + ... |
| 41 | + |
| 42 | +julia> stat(p"docs/src/index.md") |
38 | 43 | Status( |
39 | | - device = 16777220, |
40 | | - inode = 48428965, |
| 44 | + device = 16777223, |
| 45 | + inode = 32240108, |
41 | 46 | mode = -rw-r--r--, |
42 | 47 | nlink = 1, |
43 | | - uid = 501, |
44 | | - gid = 20, |
| 48 | + uid = 501 (rory), |
| 49 | + gid = 20 (staff), |
45 | 50 | rdev = 0, |
46 | | - size = 1880 (1.8K), |
| 51 | + size = 2028 (2.0K), |
47 | 52 | blksize = 4096 (4.0K), |
48 | 53 | blocks = 8, |
49 | | - mtime = 2016-02-16T00:49:27, |
50 | | - ctime = 2016-02-16T00:49:27, |
| 54 | + mtime = 2020-04-20T17:20:38.612, |
| 55 | + ctime = 2020-04-20T17:20:38.612, |
51 | 56 | ) |
52 | | -``` |
53 | 57 |
|
54 | | -Working with permissions: |
55 | | -```julia-repl |
56 | | -julia> m = mode(p"README.md") |
57 | | --rw-r--r-- |
| 58 | +julia> relative(p"docs/src/index.md", p"src/") |
| 59 | +p"../docs/src/index.md" |
58 | 60 |
|
59 | | -julia> m - readable(:ALL) |
60 | | ---w------- |
| 61 | +julia> normalize(p"src/../docs/src/index.md") |
| 62 | +p"docs/src/index.md" |
61 | 63 |
|
62 | | -julia> m + executable(:ALL) |
63 | | --rwxr-xr-x |
| 64 | +julia> absolute(p"docs/src/index.md") |
| 65 | +p"/Users/rory/repos/FilePathsBase.jl/docs/src/index.md" |
64 | 66 |
|
65 | | -julia> chmod(p"README.md", "+x") |
| 67 | +julia> islink(p"docs/src/index.md") |
| 68 | +true |
66 | 69 |
|
67 | | -julia> mode(p"README.md") |
68 | | --rwxr-xr-x |
| 70 | +julia> canonicalize(p"docs/src/index.md") |
| 71 | +p"/Users/rory/repos/FilePathsBase.jl/README.md" |
69 | 72 |
|
70 | | -julia> chmod(p"README.md", m) |
| 73 | +julia> parents(p"./docs/src") |
| 74 | +2-element Array{PosixPath,1}: |
| 75 | + p"." |
| 76 | + p"./docs" |
71 | 77 |
|
72 | | -julia> m = mode(p"README.md") |
73 | | --rw-r--r-- |
| 78 | +julia> parents(absolute(p"./docs/src")) |
| 79 | +6-element Array{PosixPath,1}: |
| 80 | + p"/" |
| 81 | + p"/Users" |
| 82 | + p"/Users/rory" |
| 83 | + p"/Users/rory/repos" |
| 84 | + p"/Users/rory/repos/FilePathsBase.jl" |
| 85 | + p"/Users/rory/repos/FilePathsBase.jl/docs" |
74 | 86 |
|
75 | | -julia> chmod(p"README.md", user=(READ+WRITE+EXEC), group=(READ+WRITE), other=READ) |
| 87 | +julia> absolute(p"./docs/src")[1:end-1] |
| 88 | +("Users", "rory", "repos", "FilePathsBase.jl", "docs") |
76 | 89 |
|
77 | | -julia> mode(p"README.md") |
78 | | --rwxrw-r-- |
| 90 | +julia> tmpfp = mktempdir(SystemPath) |
| 91 | +p"/var/folders/vz/zx_0gsp9291dhv049t_nx37r0000gn/T/jl_1GCBFT" |
79 | 92 |
|
80 | | -``` |
| 93 | +julia> sync(p"/Users/rory/repos/FilePathsBase.jl/docs", tmpfp / "docs") |
| 94 | +p"/var/folders/vz/zx_0gsp9291dhv049t_nx37r0000gn/T/jl_1GCBFT/docs" |
| 95 | + |
| 96 | +julia> exists(tmpfp / "docs" / "make.jl") |
| 97 | +true |
| 98 | + |
| 99 | +julia> m = mode(tmpfp / "docs" / "make.jl") |
| 100 | +Mode("-rw-r--r--") |
| 101 | + |
| 102 | +julia> m - readable(:ALL) |
| 103 | +Mode("--w-------") |
| 104 | + |
| 105 | +julia> m + executable(:ALL) |
| 106 | +Mode("-rwxr-xr-x") |
| 107 | + |
| 108 | +julia> chmod(tmpfp / "docs" / "make.jl", "+x") |
| 109 | +"/var/folders/vz/zx_0gsp9291dhv049t_nx37r0000gn/T/jl_1GCBFT/docs/make.jl" |
| 110 | + |
| 111 | +julia> mode(tmpfp / "docs" / "make.jl") |
| 112 | +Mode("-rwxr-xr-x") |
| 113 | + |
| 114 | +# Count LOC |
| 115 | +julia> mapreduce(+, walkpath(cwd() / "src")) do x |
| 116 | + extension(x) == "jl" ? count("\n", read(x, String)) : 0 |
| 117 | + end |
| 118 | +3020 |
| 119 | + |
| 120 | +# Concatenate multiple files. |
| 121 | +julia> str = mapreduce(*, walkpath(tmpfp / "docs" / "src")) do x |
| 122 | + read(x, String) |
| 123 | + end |
| 124 | +"# API\n\nAll the standard methods for working with paths in base julia exist in the FilePathsBase.jl. The following describes the rough mapping of method names. Use `?` at the REPL to get the documentation and arguments as they may be different than the base implementations.\n\n..." |
| 125 | + |
| 126 | +# Could also write the result to a file with `write(newfile, str)`) |
81 | 127 |
|
82 | | -Reading and writing directly to file paths: |
83 | | -```julia-repl |
84 | | -julia> write(p"testfile", "foobar") |
85 | | -6 |
| 128 | +julia> rm(tmpfp; recursive=true) |
86 | 129 |
|
87 | | -julia> read(p"testfile") |
88 | | -"foobar" |
| 130 | +julia> exists(tmpfp) |
| 131 | +false |
89 | 132 | ``` |
0 commit comments