Leif Andersen
Leif Andersen

Reputation: 22332

How do I get the version number of an installed package in Racket

In Racket, version numbers are stored in a package's root directory in info.rkt. The file looks something like this:

#lang info

....
(define version "0.5")

The question is, how can I programmatically get that package's version?

The version function and library seem to be specifically for the version of Racket, rather than any particular package.

For single collection packages I can use the get-info function:

> ((get-info '("my-single-collection-package")) 'version)
"0.5"

But if the package has multiple collections in it, that doesn't work.

> ((get-info '("my-multi-collection-package")) 'version)
racket/racket/collects/racket/private/collect.rkt:11:53: collection-path: collection not found
  collection: "my-multi-collection-package"
  in collection directories:
   /home/leif/.racket/development/collects
   /home/leif/racket/racket/collects
   ... [216 additional linked and package directories]

So how can I programmatically get the version string from a package?

Upvotes: 0

Views: 156

Answers (1)

Leif Andersen
Leif Andersen

Reputation: 22332

You're on the right path. You can still use get-info to retrieve the data from an info file, but instead use get-info/full, which allows you to pass in a path to the directory of the info file. In this case, the package's base directory.

The catch is you just need to find the folder where your package is. For that, you can use pkg-directory, which takes the name of a package, and returns its location:

> (pkg-directory "my-pkg")
#<path:/home/leif/racket/racket/share/pkgs/my-pkg">

So now you can just pass this into get-info/full and apply the result to 'version to get the package's version:

> ((get-info/full (pkg-directory "my-pkg")) 'version)
"0.5"

Upvotes: 1

Related Questions