forked from awwx/ar
/
05 paths.arc
executable file
·100 lines (79 loc) · 2.27 KB
/
05 paths.arc
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
;;(load:string %.exec-dir "lib/utils.arc")
;;(load:string %.exec-dir "lib/parameters.arc")
(%require racket/path)
(make-parameter cwd
(%.make-derived-parameter %.current-directory
(fn (v) (zap string v)
(if empty.v
(%.current-directory)
(%.expand-user-path v)))
(fn (v) (%.path->string v))))
;; TODO: inefficient
(make-parameter script-args
(%.make-derived-parameter %.current-command-line-arguments
%.list->vector
%.vector->list))
(= script-src (car script-args))
(zap cdr script-args)
(def extension (x)
(aand (%.filename-extension string.x) %.bytes->string/utf-8.it))
(def no-extension (x)
(cut x 0 (-:+ (len extension.x) 1)))
(def expandpath (x)
(zap string x)
(if empty.x
x
(%.path->string %.expand-user-path.x)))
(def path args
(%.path->string:apply %.build-path
((afn (x acc)
(if no.x
rev.acc ;; nrev
(let c (expandpath car.x)
(if (empty c)
(self cdr.x acc)
(is c.0 #\/)
(self cdr.x (cons c nil))
(self cdr.x (cons c acc))))))
args nil)))
(= exec-dir %.exec-dir)
(def exec-path args
(apply path exec-dir args))
(def make-path->string (converter)
(fn (x)
(zap string x)
(unless empty.x
(aand converter.x %.path->string.it))))
(= dirname (make-path->string:compose %.path-only expandpath))
(= basename (make-path->string:compose %.file-name-from-path expandpath))
(def abspath ((o x))
(let x expandpath.x
(if empty.x
cwd
;; TODO: is this necessary? fix it if not
file-exists.x
(%.path->string %.normalize-path.x)
x))) ;path->complete-path
(def absdir ((o x))
(dirname abspath.x))
(def dirall ((o x ".") (o f idfn))
(w/cwd x
;; alet
((afn (d)
(mappend (fn (x)
(let x (path d x)
(when f.x
;; dirname
(if (dir-exists x)
(self x)
(list x)))))
(dir (or d "."))))
nil)))
(def todir (x)
(zap string x)
;; TODO: last
(if (or empty.x (is (x:- len.x 1) #\/))
x
(string x "/")))
(def hidden-file? (x)
(is string.x.0 #\.))