Racket: how to get the path to the running file?

I need a way to get the path to run the script (the directory containing the source file), but

(current-directory) 

never points there (in this case, an external drive), but rather at some predetermined location.

I created a file to try all the "find-system-path", but not one of them is an executable! Racket docs don't help.

 #lang web-server/insta (define (start request) (local [{define (build-ul items) `(ul ,@(map itemize items))} {define (itemize item) `(li ,(some-system-path->string (find-system-path item)))}] (response/xexpr `(html (head (title "Directories")) (body (h1 ,"Some Paths") (p ,(build-ul special-paths))))))) (define special-paths (list 'home-dir 'pref-dir 'pref-file 'temp-dir 'init-dir 'init-file ;'links-file ; not available for Linux 'addon-dir 'doc-dir 'desk-dir 'sys-dir 'exec-file 'run-file 'collects-dir 'orig-dir)) 

The goal is a local web server application (music server) that will modify subdirectories in the directory that contains the source file. I will be running the application on a USB drive, so it should be able to find its own directory, as I transfer it between machines and operating systems with Racket installed.

+6
source share
2 answers

A simple way: take the current name of the script, make it the full path, and then take its directory:

 (path-only (path->complete-path (find-system-path 'run-file))) 

But you are rather interested not in the file that was used to do things (the web server), but in the actual source file into which you put your code. That is, you want some resources to be close to your source. An older way to do this:

 (require mzlib/etc) (this-expression-source-directory) 

The best way to do this is to use "runtime-path", which is a way of defining such resources:

 (require racket/runtime-path) (define-runtime-path my-picture "pic.png") 

This is the best sine, it also registers the path as something that depends on your script - therefore, if you were going to pack your code as an installer, for example, Racket would know that the package would also be downloaded to the png file.

And finally, you can use it to specify the entire directory:

 (define-runtime-path HERE ".") ... (build-path HERE "pic.png") ... 
+10
source

If you need an absolute path, I think this should do it:

 (build-path (find-system-path 'orig-dir) (find-system-path 'run-file)) 
+1
source

Source: https://habr.com/ru/post/946199/


All Articles