Pārlūkot izejas kodu

More robust directory changing

     - Doesn't fail when the directory is missing a trailing '/'
     - When directory doesn't exist it signals an informative
       condition.
Javier Olaechea 10 gadi atpakaļ
vecāks
revīzija
8304e6b74b
2 mainītis faili ar 12 papildinājumiem un 2 dzēšanām
  1. 3 1
      src/packages.lisp
  2. 9 1
      src/util.lisp

+ 3 - 1
src/packages.lisp

@@ -8,7 +8,9 @@
8 8
   (:import-from :closure-template #:compile-template)
9 9
   (:import-from :local-time #:format-rfc1123-timestring)
10 10
   (:import-from :uiop #:getcwd
11
-                      #:chdir)
11
+                      #:chdir
12
+                      #:ensure-directory-pathname
13
+                      #:directory-exists-p)
12 14
   (:export #:main
13 15
            #:preview
14 16
            #:*config*

+ 9 - 1
src/util.lisp

@@ -30,9 +30,17 @@ BODY on files that match the given extension."
30 30
                                         #',extension-p
31 31
                                         (constantly t))))))
32 32
 
33
+(define-condition directory-does-not-exist (error)
34
+  ((directory :initarg dir :reader dir))
35
+  (:report (lambda (c stream)
36
+             (format stream "The directory '~A' does not exist" (dir c)))))
37
+
33 38
 (defun (setf getcwd) (path)
34 39
   "Change the operating system's current directory to PATH."
35
-  (chdir path)
40
+  (setf path (ensure-directory-pathname path))
41
+  (or (and (directory-exists-p path)
42
+           (chdir path))
43
+      (error 'directory-does-not-exist :dir path))
36 44
   path)
37 45
 
38 46
 (defmacro with-current-directory (path &body body)