unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* What's the right way to extract a package's version from the header metadata?
@ 2021-12-29  6:29 Bozhidar Batsov
  2021-12-29  7:42 ` Stefan Monnier
  2021-12-30  1:13 ` Jonas Bernoulli
  0 siblings, 2 replies; 7+ messages in thread
From: Bozhidar Batsov @ 2021-12-29  6:29 UTC (permalink / raw)
  To: Emacs Devel

[-- Attachment #1: Type: text/plain, Size: 1084 bytes --]

Recently I replaced pkg-info (a now abandoned package) with the following snippet in some of my packages:

(defconst cider-version
  (eval-when-compile
    (lm-version (or load-file-name buffer-file-name)))
  "The current version of CIDER.")

The above snippet is from CIDER, but I've used this code in several places.
Basically I want to extract the package version from the *Version:* header instead of having to duplicate it. I know of  package-get-package-version, but I assume it won't work for people who didn't install my packages via package.el.  

However, a bunch of users immediately started to complain that cider-version became nil for them, so clearly I made some mistake. The problem is that I'm not sure what's wrong and how to best proceed, so I hope that someone here might give me some pointers. My best guess is that I shouldn't use load-file-name, but rather something else. More context - https://github.com/clojure-emacs/cider/commit/096c18418e1e7c379f4d1f3d3622baaf3308859a and https://github.com/clojure-emacs/sayid/pull/59

Thanks in advance for your help!


[-- Attachment #2: Type: text/html, Size: 2235 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: What's the right way to extract a package's version from the header metadata?
  2021-12-29  6:29 What's the right way to extract a package's version from the header metadata? Bozhidar Batsov
@ 2021-12-29  7:42 ` Stefan Monnier
  2021-12-29  7:54   ` Bozhidar Batsov
  2021-12-29 18:22   ` Thierry Volpiatto
  2021-12-30  1:13 ` Jonas Bernoulli
  1 sibling, 2 replies; 7+ messages in thread
From: Stefan Monnier @ 2021-12-29  7:42 UTC (permalink / raw)
  To: Bozhidar Batsov; +Cc: Emacs Devel

> Basically I want to extract the package version from the *Version:*
>  header instead of having to duplicate it. I know of
>  package-get-package-version, but I assume it won't work for people
>  who didn't install my packages via package.el.  

`package-get-version` should work but only if they had the foresight to
put it in a directory whose name ends in "-<version>" or whose name is
just `cider` (i.e. the name of your package) :-(

The patch below (which I plan to push to `master` soon) should make it
work in other cases as well if the call is in the same file where the
`Version:` can be found.


        Stefan


diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
index 9b6b5d4dc0d..d477266a7ef 100644
--- a/lisp/emacs-lisp/package.el
+++ b/lisp/emacs-lisp/package.el
@@ -4077,7 +4077,9 @@ package-get-version
   "Return the version number of the package in which this is used.
 Assumes it is used from an Elisp file placed inside the top-level directory
 of an installed ELPA package.
-The return value is a string (or nil in case we can't find it)."
+The return value is a string (or nil in case we can't find it).
+It works in more cases if the call is in the file which contains
+the `Version:' header."
   ;; In a sense, this is a lie, but it does just what we want: precompute
   ;; the version at compile time and hardcodes it into the .elc file!
   (declare (pure t))
@@ -4096,6 +4098,7 @@ package-get-version
       (let* ((pkgdir (file-name-directory file))
              (pkgname (file-name-nondirectory (directory-file-name pkgdir)))
              (mainfile (expand-file-name (concat pkgname ".el") pkgdir)))
+        (unless (file-readable-p mainfile) (setq mainfile file))
         (when (file-readable-p mainfile)
           (require 'lisp-mnt)
           (with-temp-buffer




^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: What's the right way to extract a package's version from the header metadata?
  2021-12-29  7:42 ` Stefan Monnier
@ 2021-12-29  7:54   ` Bozhidar Batsov
  2021-12-29 18:22   ` Thierry Volpiatto
  1 sibling, 0 replies; 7+ messages in thread
From: Bozhidar Batsov @ 2021-12-29  7:54 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Emacs Devel

[-- Attachment #1: Type: text/plain, Size: 2150 bytes --]

Thanks for the quick response! It would be great indeed if  `package-get-version` worked in more cases and we could just consider it the canonical solution the version extraction problem. 

On Wed, Dec 29, 2021, at 9:42 AM, Stefan Monnier wrote:
> > Basically I want to extract the package version from the *Version:*
> >  header instead of having to duplicate it. I know of
> >  package-get-package-version, but I assume it won't work for people
> >  who didn't install my packages via package.el.  
> 
> `package-get-version` should work but only if they had the foresight to
> put it in a directory whose name ends in "-<version>" or whose name is
> just `cider` (i.e. the name of your package) :-(
> 
> The patch below (which I plan to push to `master` soon) should make it
> work in other cases as well if the call is in the same file where the
> `Version:` can be found.
> 
> 
>         Stefan
> 
> 
> diff --git a/lisp/emacs-lisp/package.el b/lisp/emacs-lisp/package.el
> index 9b6b5d4dc0d..d477266a7ef 100644
> --- a/lisp/emacs-lisp/package.el
> +++ b/lisp/emacs-lisp/package.el
> @@ -4077,7 +4077,9 @@ package-get-version
>    "Return the version number of the package in which this is used.
> Assumes it is used from an Elisp file placed inside the top-level directory
> of an installed ELPA package.
> -The return value is a string (or nil in case we can't find it)."
> +The return value is a string (or nil in case we can't find it).
> +It works in more cases if the call is in the file which contains
> +the `Version:' header."
>    ;; In a sense, this is a lie, but it does just what we want: precompute
>    ;; the version at compile time and hardcodes it into the .elc file!
>    (declare (pure t))
> @@ -4096,6 +4098,7 @@ package-get-version
>        (let* ((pkgdir (file-name-directory file))
>               (pkgname (file-name-nondirectory (directory-file-name pkgdir)))
>               (mainfile (expand-file-name (concat pkgname ".el") pkgdir)))
> +        (unless (file-readable-p mainfile) (setq mainfile file))
>          (when (file-readable-p mainfile)
>            (require 'lisp-mnt)
>            (with-temp-buffer
> 
> 

[-- Attachment #2: Type: text/html, Size: 3359 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: What's the right way to extract a package's version from the header metadata?
  2021-12-29  7:42 ` Stefan Monnier
  2021-12-29  7:54   ` Bozhidar Batsov
@ 2021-12-29 18:22   ` Thierry Volpiatto
  2021-12-29 18:42     ` Bozhidar Batsov
  1 sibling, 1 reply; 7+ messages in thread
From: Thierry Volpiatto @ 2021-12-29 18:22 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Bozhidar Batsov, Emacs Devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Basically I want to extract the package version from the *Version:*
>>  header instead of having to duplicate it. I know of
>>  package-get-package-version, but I assume it won't work for people
>>  who didn't install my packages via package.el.  
>
> `package-get-version` should work but only if they had the foresight to
> put it in a directory whose name ends in "-<version>" or whose name is
> just `cider` (i.e. the name of your package) :-(

Why doing this instead of adding such infos to *.pkg.el file of package?
IIRC package.el is creating such file from source file headers instead
of using the existing file if it exists (I already asked for this in the
past).

-- 
Thierry



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: What's the right way to extract a package's version from the header metadata?
  2021-12-29 18:22   ` Thierry Volpiatto
@ 2021-12-29 18:42     ` Bozhidar Batsov
  2021-12-30  9:04       ` Thierry Volpiatto
  0 siblings, 1 reply; 7+ messages in thread
From: Bozhidar Batsov @ 2021-12-29 18:42 UTC (permalink / raw)
  To: Emacs Devel

[-- Attachment #1: Type: text/plain, Size: 1274 bytes --]

I don't see how manually generating the pkg manifest will help; perhaps I'm missing the point you're trying to make. In the case of MELPA the version will always be overridden regardless of the presence of a package manifest, and in most of my packages I'm adding show-package-version command to help with the bug reports. It seems to me that package version extraction is a pretty common problem that deserves a simple solution.  

On Wed, Dec 29, 2021, at 8:22 PM, Thierry Volpiatto wrote:
> Stefan Monnier <monnier@iro.umontreal.ca> writes:
> 
> >> Basically I want to extract the package version from the *Version:*
> >>  header instead of having to duplicate it. I know of
> >>  package-get-package-version, but I assume it won't work for people
> >>  who didn't install my packages via package.el.  
> >
> > `package-get-version` should work but only if they had the foresight to
> > put it in a directory whose name ends in "-<version>" or whose name is
> > just `cider` (i.e. the name of your package) :-(
> 
> Why doing this instead of adding such infos to *.pkg.el file of package?
> IIRC package.el is creating such file from source file headers instead
> of using the existing file if it exists (I already asked for this in the
> past).
> 
> -- 
> Thierry
> 
> 

[-- Attachment #2: Type: text/html, Size: 1890 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: What's the right way to extract a package's version from the header metadata?
  2021-12-29  6:29 What's the right way to extract a package's version from the header metadata? Bozhidar Batsov
  2021-12-29  7:42 ` Stefan Monnier
@ 2021-12-30  1:13 ` Jonas Bernoulli
  1 sibling, 0 replies; 7+ messages in thread
From: Jonas Bernoulli @ 2021-12-30  1:13 UTC (permalink / raw)
  To: Bozhidar Batsov, Emacs Devel

"Bozhidar Batsov" <bozhidar@batsov.dev> writes:

> (defconst cider-version
>   (eval-when-compile
>     (lm-version (or load-file-name buffer-file-name)))
>   "The current version of CIDER.")

Cute ;D

Also check out magit's horror show; the *function* magit-version.
(No, I won't post that code here.)

That's what happens if you also want to support "installed using
a shallow git repo", "installed using symlinks into a git repo",
and all kinds of other funky installation methods.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: What's the right way to extract a package's version from the header metadata?
  2021-12-29 18:42     ` Bozhidar Batsov
@ 2021-12-30  9:04       ` Thierry Volpiatto
  0 siblings, 0 replies; 7+ messages in thread
From: Thierry Volpiatto @ 2021-12-30  9:04 UTC (permalink / raw)
  To: Bozhidar Batsov; +Cc: Emacs Devel

"Bozhidar Batsov" <bozhidar@batsov.dev> writes:

> I don't see how manually generating the pkg manifest will help;

It wont as long as package.el ignore it.


-- 
Thierry



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-12-30  9:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-12-29  6:29 What's the right way to extract a package's version from the header metadata? Bozhidar Batsov
2021-12-29  7:42 ` Stefan Monnier
2021-12-29  7:54   ` Bozhidar Batsov
2021-12-29 18:22   ` Thierry Volpiatto
2021-12-29 18:42     ` Bozhidar Batsov
2021-12-30  9:04       ` Thierry Volpiatto
2021-12-30  1:13 ` Jonas Bernoulli

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).