[racket] define-runtime-path and raco distrib

From: Kieron Hardy (kieron.hardy at gmail.com)
Date: Tue Jul 17 18:09:47 EDT 2012

Thanks for the hint Matt, I noodled around a bit and the code below is what
I've come up with, posted here in case it's of use to others.

Basically, to find the data file:
If the (find-system-path 'run-file) or (find-system-path 'exec-file) refer
to a (Windows) racket binary (racket.exe, gracket.exe, DrRacket.exe, or the
CGC variants) then the source code is being executed, so use the directory
containing the source module,
otherwise a generated (i.e. with raco exe) binary is being executed, so use
the directory containing the (find-system-path 'run-file).

I've tested running the source and binary in every fashion I could think
(from DrRacket, from command line with racket.exe and pals, from windows
icon, from windows shortcut icon, from various different directories) and
the data file is located reliable.

Unfortunately, I could not come up with a way to use define-runtime-path,
and so I have lost the ability for 'raco distrib' to bundle up the data
file automatically, and so will have to add a copy of the data file to the
directory after 'raco distrib' completes. (It seems
...\collects\compiler\distribute.rkt copies and changes the names (paths)
of the runtime files into the distribution directory as part of
assemble-distribution procedure and changing the destination location
doesn't look trivial).

Cheers,

Kieron.

****

#lang racket

(require racket/runtime-path)
(require syntax/location)

; Access a file "data.txt" at run-time that is
; located in the same directory as the module source file
; if executing with racket binaries then use the source module location
; otherwise use the location of the compiled exe
(define (get-data-file-name)
  ; check for Windows binaries - change for other OS's
  (define DrRacket? (regexp-match? #rx".*(?i:DrRacket)(?i:cgc)?\\.(?i:exe)"
(find-system-path 'run-file)))
  (define racket? (regexp-match? #rx".*(?i:racket)(?i:cgc)?\\.(?i:exe)"
(find-system-path 'exec-file)))
  (let-values
    ([(p f b)
      (split-path
        (path->complete-path
          (if (or racket? DrRacket?)
            (quote-module-name)
            (find-system-path 'run-file))))])
;     (printf "get-data-file-name: returning:\"~a\"~n" (build-path p
"data.txt"))
     (build-path p "data.txt")
     ))

(define (read-data)
  (let ([data-file (get-data-file-name)])
    (printf "read-data: data-file:\"~a\"~n" data-file)
    (with-input-from-file data-file
      (lambda ()
        (read-bytes (file-size data-file))
        ))))

(read-data)


On Tue, Jul 17, 2012 at 6:39 AM, Matt Jadud <jadudm at gmail.com> wrote:

> Hi Kieron,
>
> On Mon, Jul 16, 2012 at 5:13 PM, Kieron Hardy <kieron.hardy at gmail.com>
> wrote:
> > How do I get a run-time path in the executable created by 'raco distrib',
> > that is the same, relative to the directory containing the source racket
> > code and the executable created by 'raco exe'?
>
> I used something along the lines of
>
> (define (UMBRELLA)
>   (simplify-path
>    (build-path
>     (find-system-path 'run-file) 'up 'up)))
>
> which allowed me to run my application both as a double-clickable Mac
> app as well as from the command line with
>
> racket foo.rkt
>
> I think some relevant (recent) discussion on this was here:
>
> https://groups.google.com/d/topic/racket-users/CqTiuhY6Aks/discussion
>
> By "I think" I mean "I asked this roughly a week ago, and I think it
> answers your question.
>
> Cheers,
> Matt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120717/9e91f95a/attachment.html>

Posted on the users mailing list.