Skip to content

Commit

Permalink
fix: make scripts more viable under Cygwin
Browse files Browse the repository at this point in the history
Don't let the perfect be the enemy of slightly better.

Swish scripts have been broken under Cygwin, but we can at least try to
make make them work in some typical cases under Cygwin.

- don't rely on OSTYPE; bash doesn't export it by default
- be careful since installing Git Bash changes Cygwin's /tmp
- assume a Cygwin usertemp mount for /tmp
- use registry to get root directory
  • Loading branch information
owaddell-beckman committed Feb 27, 2024
1 parent d2bb587 commit 73967e1
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/swish/app.ss
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@
(software-version 'swish))])])
(printf "~a~@[ Version ~a~]~@[ (~a)~]\n" name version revision)))

(meta-cond
[windows?
(define (split-path p n)
(let ([rest (path-rest p)])
(if (or (= n 0) (equal? p rest))
(list p)
(cons (path-first p) (split-path (path-rest p) (- n 1))))))
(define (rewrite-path script)
;; Cygwin paths start with /.
;; By default, MSYS2 and Git Bash automatically convert to Windows paths.
(match (split-path script 3)
[("/" "cygdrive" ,drive ,path)
(path-combine (string-append drive ":") path)]
[("/" "tmp" . ,more)
;; assume Cygwin's fstab sets a usertemp mount for /tmp
(apply path-combine (osi_get_temp_directory) more)]
[("/" . ,_)
(let ([root (get-registry "HKEY_LOCAL_MACHINE\\Software\\Cygwin\\setup\\rootdir")])
(and root (path-combine root (path-rest script))))]
[,_ #f]))
(define (resolve script)
(if (file-exists? script)
script
(let ([new-path (rewrite-path script)])
(if (and new-path (file-exists? new-path))
new-path
script))))]
[else
(define (resolve script) script)])

(define (run)
(let* ([opt (parse-command-line-arguments cli)]
[files (or (opt 'files) '())])
Expand Down Expand Up @@ -164,7 +194,7 @@
(for-each load filenames)
(new-cafe)))]
[else ; script
(let ([script-file (car files)]
(let ([script-file (resolve (car files))]
[cmdline (command-line-arguments)])
(parameterize ([command-line cmdline]
[command-line-arguments (cdr cmdline)]
Expand Down

0 comments on commit 73967e1

Please sign in to comment.