Skip to content

Commit

Permalink
Always set Package-{Version,Revision} header in main library only
Browse files Browse the repository at this point in the history
- Set these headers in the main library of the package, even for
  packages that are distributed as tarballs.  Do this to ensure that the
  information in that library is accurate and does not conflict with the
  information in "*-pkg.el".

- Remove these headers from all other libraries, to ensure they do not
  contain inaccurate information.  We could instead correct the values
  in these libraries, but since only the main library should feature
  these headers, we don't do that.

- When distributing a singe-library package without using a tarball
  (which Melpa stopped doing a long time ago, but which theoretically
  is still supported), no longer set the X-Original-[Package-]Version
  header go the original value of [Package-]Version, and use the same
  Package-Revision header we now use in multi-library packages instead
  of Package-Commit, which we used before.

- Try harder to find the best possible location to insert these headers.
  • Loading branch information
tarsius committed Sep 28, 2024
1 parent 721a7a1 commit 3d00901
Showing 1 changed file with 47 additions and 42 deletions.
89 changes: 47 additions & 42 deletions package-build.el
Original file line number Diff line number Diff line change
Expand Up @@ -1177,37 +1177,48 @@ is the same as the value of `export_file_name'."
(package-build--call-process
rcp "install-info" "--dir=dir" info)))))))

;;; Patch Libraries

(defun package-build--update-or-insert-header (name value)
"Ensure current buffer has NAME header with the given VALUE.
Any existing header will be preserved and given the \"X-Original-\" prefix.
If VALUE is nil, the new header will not be inserted, but any original will
still be renamed."
(goto-char (point-min))
(cond
((let ((case-fold-search t))
(re-search-forward (format "^;+* *%s *: *" (regexp-quote name)) nil t))
(move-beginning-of-line nil)
(search-forward "V" nil t)
(backward-char)
(insert "X-Original-")
(move-beginning-of-line nil))
(t
;; Put the new header in a sensible place if we can.
(re-search-forward
"^;+* *\\(Version\\|Package-Requires\\|Keywords\\|URL\\) *:" nil t)
(forward-line)))
(insert (format ";; %s: %s\n" name value)))

(defun package-build--ensure-ends-here-line (file)
"Add the \"FILE ends here\" trailing line if it is missing."
(save-excursion
(goto-char (point-min))
(let ((trailer (format ";;; %s ends here" (file-name-nondirectory file))))
(unless (re-search-forward (format "^%s" (regexp-quote trailer)) nil t)
(goto-char (point-max))
(insert ?\n trailer ?\n)))))
(defun package-build--set-version-headers (rcp file-or-dir)
(pcase-let (((eieio name version revdesc) rcp)
(single (file-regular-p file-or-dir)))
(dolist (file (if single
(list file-or-dir)
(directory-files file-or-dir t "\\.el\\'")))
(with-temp-buffer
(insert-file-contents file)
(let ((end (lm-code-start))
(pos nil))
(dolist (header '("Package-Version" "Version" "Package-Revision"))
(goto-char (point-min))
(when (re-search-forward (format "^;;+ +%s:.+" header) end t)
(unless pos
(setq pos (copy-marker (match-beginning 0))))
(delete-region (match-beginning 0) (1+ (line-end-position)))))
(when (or single
(equal (file-name-nondirectory file) (concat name ".el")))
(unless pos
(cl-dolist (header '("Package-Requires" "SPDX-License-Identifier"
"License" "URL" "Homepage" "Keywords"))
(goto-char (point-min))
(when (re-search-forward (format "^;;+ +%s:.+" header) end t)
(setq pos (copy-marker (match-beginning 0)))
(cl-return-from nil))))
(unless pos
(cl-dolist (header '("Commentary" "Code"))
(goto-char (point-min))
(when (re-search-forward (format "^;;; %s:$" header) end t)
(goto-char (match-beginning 0))
(insert "\n")
(setq pos (copy-marker (match-beginning 0)))
(cl-return-from nil))))
(when pos
(goto-char pos)
(insert (format ";; Package-Version: %s\n" version))
(insert (format ";; Package-Revision: %s\n" revdesc))))
(when (and single
(not (search-forward (format ";;; %s.el ends here" name))))
(goto-char (point-max))
(insert (format "\n;;; %s.el ends here\n" name)))
(write-region nil nil file))))))

;;; Extract Metadata

Expand Down Expand Up @@ -1576,6 +1587,7 @@ in `package-build-archive-dir'."
(unwind-protect
(progn
(package-build--copy-package-files files target)
(package-build--set-version-headers rcp target)
(package-build--write-pkg-file rcp target)
(package-build--generate-info-files rcp files target)
(package-build--create-tar rcp tmpdir)
Expand All @@ -1585,7 +1597,7 @@ in `package-build-archive-dir'."

(defun package-build--build-single-file-package (rcp files)
(oset rcp tarballp nil)
(pcase-let* (((eieio name version commit revdesc) rcp)
(pcase-let* (((eieio name version) rcp)
(file (caar files))
(source (expand-file-name file))
(target (expand-file-name (concat name "-" version ".el")
Expand All @@ -1594,18 +1606,10 @@ in `package-build-archive-dir'."
name)
(package-build--error name
"Single file %s does not match package name %s" file name))
(package-build--extract-from-library rcp files)
(package-build--extract-from-library rcp target)
(unless package-build--inhibit-build
(copy-file source target t)
(let ((enable-local-variables nil)
(make-backup-files nil)
(before-save-hook nil))
(with-current-buffer (find-file target)
(package-build--update-or-insert-header "Package-Commit" commit)
(package-build--update-or-insert-header "Package-Version" version)
(package-build--ensure-ends-here-line source)
(write-file target nil)
(kill-buffer)))
(package-build--set-version-headers rcp target)
(package-build--write-pkg-readme rcp files))
(package-build--write-archive-entry rcp)))

Expand All @@ -1623,6 +1627,7 @@ in `package-build-archive-dir'."
(unwind-protect
(progn
(package-build--copy-package-files files target)
(package-build--set-version-headers rcp target)
(package-build--write-pkg-file rcp target)
(package-build--generate-info-files rcp files target)
(package-build--create-tar rcp tmpdir)
Expand Down

0 comments on commit 3d00901

Please sign in to comment.