~brown121407/f.scm

ref: 965e86688df29ba68ba3c68f16d33fa5cc5f1f3a f.scm/f.scm -rw-r--r-- 6.8 KiB
965e8668Xinglu Chen Add replace-extension. 1 year, 4 months ago
                                                                                
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
;; Copyright 2020 Alexandru-Sergiu Marton <brown121407@posteo.ro>
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(define-module (f)
  #:use-module ((ice-9 binary-ports) #:prefix i9:)
  #:use-module ((ice-9 textual-ports) #:prefix i9:)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 regex)
  #:use-module ((f ports) #:prefix p:)
  #:use-module ((f re-exports) #:prefix re:)
  #:export (read-bytes
            read-text
            read-line
            read-lines
            write-bytes
            write-text
            write-line
            write-lines
            ls
            mkdir
            delete
            traverse
            copy
            no-extension
            replace-extension)
  #:re-export (chown
               chmod
               (rename-file . move)
               link
               symlink
               rmdir))

(define (read-bytes path)
  (let* ((file (open-file path "rb"))
         (bytes (i9:get-bytevector-all file)))
    (close-port file)
    bytes))

(define (read-text path)
  (let* ((file (open-file path "r"))
         (text (i9:get-string-all file)))
    (close-port file)
    text))

(define (read-line path)
  (let* ((file (open-file path "r"))
         (line (p:read-line file)))
    (close-port file)
    line))

(define (read-lines path)
  (let* ((file (open-file path "r"))
         (lines (p:read-lines file)))
    (close-port file)
    lines))

(define* (write-bytes path bytes #:key (append #f))
  (let ((file (open-file path (if append "a" "w"))))
    (i9:put-bytevector file bytes)
    (close-port file)))

(define* (write-text path text #:key (append #f))
  (let ((file (open-file path (if append "a" "w"))))
    (i9:put-string file text)
    (close-port file)))

(define* (write-line path text #:key (append #f))
  (let ((file (open-file path (if append "a" "w"))))
    (i9:put-string file (format #f "~a~%" text))
    (close-port file)))

(define* (write-lines path lines #:key (append #f))
  (let ((file (open-file path (if append "a" "w"))))
    (for-each (lambda (l)
                (i9:put-string file (format #f "~a~%" l)))
              lines)
    (close-port file)))

(define* (mkdir path #:key (parents #f))
  (if (not parents)
      (re:mkdir path)
      (letrec ((is-dir? (lambda (path)
                          (and (file-exists? path) (file-is-directory? path))))
               (create-if-doesnt-exist (lambda (path)
                                         (when (not (is-dir? path))
                                           (re:mkdir path))))
               (mkdir-p (lambda (path segments)
                          (if (null? segments)
                              (create-if-doesnt-exist path)
                              (begin
                                (create-if-doesnt-exist path)
                                (mkdir-p (string-append path file-name-separator-string (car segments)) (cdr segments))))))
               (segments (string-split path #\/))) ;; TODO: split on file-name-separator-string
        (mkdir-p (car segments) (cdr segments)))))

(define* (ls #:optional (dir (getcwd)) #:key (hidden #f))
  (letrec ((d (opendir dir))
           (ls' (lambda (f fs)
                  (if (eof-object? f)
                      fs
                      (ls' (readdir d) (cons f fs))))))
    (let ((files (ls' (readdir d) '())))
      (closedir d)
      (if hidden
          (remove (lambda (x) (or (equal? x "..") (equal? x "."))) files)
          (remove (lambda (x) (equal? (string-ref x 0) #\.)) files)))))

(define* (traverse path f #:key (files-only #f))
  (if (file-is-directory? path)
      (letrec ((contents (ls path #:hidden #t))
               (loop (lambda (contents)
                       (when (not (null? contents))
                         (let ((file (string-append path "/" (car contents))))
                           (traverse file f #:files-only files-only))
                         (loop (cdr contents))))))
        (when (not files-only)
          (f path))
        (loop contents))
      (f path)))

(define (dir-is-empty? path)
  (null? (ls path #:hidden #t)))

(define* (delete path #:optional (recursive #f))
  (if (not (file-is-directory? path))
      (delete-file path)
      (let* ((contents (ls path #:hidden #t))
             (empty (null? contents)))
        (if empty
            (rmdir path)
            (if recursive
                (letrec ((loop (lambda (contents)
                                 (when (not (null? contents))
                                   (delete
                                    (string-append path "/" (car contents))
                                    ;; TODO: replace ^ with smarter path building
                                    recursive)
                                   (loop (cdr contents))))))
                  (loop contents)
                  (rmdir path))
                (throw 'f.scm "Can't delete: directory not empty. Try making the call recursive by
appending a #t at the end."))))))

(define* (copy src dest #:optional (recursive #f))
  (if (file-is-directory? src)
      (let* ((contents (ls src #:hidden #t))
             (empty (null? contents)))
        (if empty
            (mkdir dest)
            (if recursive
                (letrec ((loop (lambda (contents)
                                 (when (not (null? contents))
                                   (copy
                                    (string-append src "/" (car contents))
                                    (string-append dest "/" (car contents))
                                    recursive)
                                   (loop (cdr contents))))))
                  (mkdir dest)
                  (loop contents))
                (throw 'f.scm "Can't copy: directory not empty. Try with making the call recursive
by appending a #t at the end."))))
      (copy-file src dest)))

(define (no-extension file)
  "Return FILE path without extension."
  (let ((without-ext (string-match "(.+)\\..*" (basename file))))
    (if without-ext
        (regexp-substitute #f without-ext 1)
        file)))

(define (replace-extension file ext)
  "Replace file extension in FILE with EXT.
EXT can include or exclude the beginning \".\"."
  (let ((ext (cond
              ((string-null? ext) "")
              ((string-match "^\\..+" ext) ext)
              (else (string-append "." ext)))))
    (string-append (no-extension file) ext)))