-
Notifications
You must be signed in to change notification settings - Fork 2
/
filesets.scm
196 lines (163 loc) · 6.41 KB
/
filesets.scm
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
;-------------------------------------------------------------------------------
; Filesets and combinators
;-------------------------------------------------------------------------------
(define (extension=? ext)
(ends-with? ext))
(define (ends-with? end)
(lambda (name)
(and (>= (string-length name) (string-length end))
(string=? (substring name (- (string-length name) (string-length end)) (string-length name))
end))))
(define (newer-than? filename1 #!key
(dir (current-build-directory)))
(lambda (filename2)
(let ((filename1 (string-append
dir
(path-strip-extension (path-strip-directory filename2)))))
(or (not (file-exists? filename1))
(> (time->seconds (file-last-modification-time filename2))
(time->seconds (file-last-modification-time filename1)))))))
(define (newer-than/extension? ext #!key
(dir (current-build-directory)))
(lambda (filename2)
(let ((filename1 (string-append
dir
(path-strip-extension (path-strip-directory filename2))
ext)))
(or (not (file-exists? filename1))
(>= (time->seconds (file-last-modification-time filename2))
(time->seconds (file-last-modification-time filename1)))))))
(define (f-and . ts)
(lambda (name)
(let f-and ((ts ts))
(or (null? ts)
(and ((car ts) name)
(f-and (cdr ts)))))))
(define (f-or . ts)
(lambda (name)
(let f-or ((ts ts))
(and (pair? ts)
(or ((car ts) name)
(f-or (cdr ts)))))))
(define (shift fn)
(lambda (t)
(lambda (name)
(fn (t name)))))
(define f-not (shift not))
(define (any? name) #t)
(define (none? name) #f)
(##define (fileset #!key
(dir (current-directory))
(test any?)
(recursive #f))
(let ((dir (path-add-trailing-directory-separator dir)))
(reduce append '()
(map (lambda (name)
(let* ((f (string-append dir name))
(childs (if (and recursive (directory? f))
(fileset dir: (path-add-trailing-directory-separator f)
test: test
recursive: recursive)
'())))
(if (test f)
(cons f childs)
childs)))
(directory-files `(path: ,dir ignore-hidden: dot-and-dot-dot))))))
;-------------------------------------------------------------------------------
; File handling
;-------------------------------------------------------------------------------
(define (path-add-trailing-directory-separator dir)
(string-append (path-strip-trailing-directory-separator dir) "/"))
(define (path-relative dir)
(path-normalize dir 'shortest (current-directory)))
(define (directory? name)
(eq? (file-type name) 'directory))
(define (regular? name)
(eq? (file-type name) 'regular))
;;; Make directory
(define (ssrun#make-directory dir)
(let ((dir0 (path-strip-trailing-directory-separator dir)))
(if (file-exists? dir0) #t
(begin
(ssrun#make-directory (path-directory dir0))
(create-directory dir0)))))
;;; Improved delete-file
(define (ssrun#delete-file file #!key (recursive #f) (force #t))
(let ((file (path-expand file)))
(info "deleting " file)
(cond
((not (file-exists? file)) 'ok)
((directory? file)
(ssrun#delete-directory file recursive: recursive force: force))
(else
(##delete-file file)))))
;;; Improved delete-directory
(define (ssrun#delete-directory dir #!key (recursive #t) (force #t))
(if force (for-each ##delete-file (fileset dir: dir recursive: #f test: regular?)))
(if recursive (for-each (lambda (dir) (ssrun#delete-file
(path-add-trailing-directory-separator dir)
recursive: recursive
force: force))
(fileset dir: dir recursive: #t test: directory?)))
(if (null? (fileset dir: dir recursive: #t test: any?))
(##delete-directory dir)
(warn dir " is not empty")))
;;; Delete a list of files
(define (ssrun#delete-files files)
(for-each (lambda (f) (ssrun#delete-file f recursive: #t)) files))
;;; Improved copy-file
(define (ssrun#copy-file file dest #!key (force #t))
(let ((file (path-expand file)))
(cond
((directory? file)
(info "copying " file " to " dest)
(ssrun#copy-directory file dest force: force))
((and force (file-exists? dest))
(ssrun#delete-file dest recursive: #t)
(ssrun#copy-file file dest force: #f))
((not (file-exists? dest))
(info "copying " file " to " dest)
(##copy-file file dest))
(else
(warn dest " already exists")))))
;;; Copy a directory
(define (ssrun#copy-directory file dest #!key (force #t))
(cond
((and force (file-exists? dest))
(ssrun#delete-file dest recursive: #t force: #t)
(ssrun#copy-directory file dest force: force))
((not (file-exists? dest))
(create-directory dest)
(for-each
(lambda (filename)
(ssrun#copy-file filename
(string-append
(path-strip-trailing-directory-separator dest)
"/" (path-strip-directory filename))))
(fileset dir: file recursive: #f)))
(else
(warn dest " already exists"))))
;;; Copy a list of files and directories
(define (ssrun#copy-files files dest #!key (force #t))
(for-each
(lambda (file)
(ssrun#copy-file file
(string-append (path-strip-trailing-directory-separator dest)
"/"
(path-strip-directory file))
force: force))
files))
(define (ssrun#read-file file)
(call-with-input-file (path-expand file)
(lambda (in) (read-line in #f))))
(define (ssrun#read-files files)
(call-with-output-string
""
(lambda (out)
(for-each (lambda (file) (display (ssrun#read-file file) out))
files))))
(define (ssrun#append-files files dest)
(call-with-output-file dest
(lambda (out)
(for-each (lambda (file) (display (ssrun#read-file file) out))
files))))