diff mbox series

[bug#40266] website: Don't append version to URL if latest

Message ID 10fd7f79c9a8d6d6510b7cfcec6b9b96640aff28.camel@zrythm.org
State Not Applicable
Headers show
Series [bug#40266] website: Don't append version to URL if latest | expand

Checks

Context Check Description
cbaines/applying patch fail View Laminar job

Commit Message

Alexandros Theodotou March 27, 2020, 11:54 p.m. UTC
Hi, 

This patch uses /packages/NAME instead of /packages/NAME-VERSION if the
package is the latest version available, so that URLs are future-proof.

Thanks,
Alex

Comments

Luis Felipe Nov. 19, 2022, 9:55 p.m. UTC | #1
Hi Alexandros,

The new https://packages.guix.gnu.org/ serves package pages without the version number now. For example:

https://packages.guix.gnu.org/packages/gobby/

Sorry to have let your patch go to waste :(
diff mbox series

Patch

From f960cc51bf594afc4afe86e773a7536369208c8e Mon Sep 17 00:00:00 2001
From: Alexandros Theodotou <alex@zrythm.org>
Date: Fri, 27 Mar 2020 23:49:25 +0000
Subject: [PATCH] website: Don't append version to URL if latest

* website/apps/packages/utils.scm (package-url-path): Return only
package name when the package is the latest available version.
---
 website/apps/packages/utils.scm | 54 ++++++++++++++++++++++++++++++---
 1 file changed, 49 insertions(+), 5 deletions(-)

diff --git a/website/apps/packages/utils.scm b/website/apps/packages/utils.scm
index fb9d3cf..9c2456d 100644
--- a/website/apps/packages/utils.scm
+++ b/website/apps/packages/utils.scm
@@ -1,6 +1,7 @@ 
 ;;; GNU Guix web site
 ;;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2017 Eric Bavier <bavier@member.fsf.org>
+;;; Copyright © 2020 Alexandros Theodotou <alex@zrythm.org>
 ;;;
 ;;; Initially written by sirgazil
 ;;; who waives all copyright interest on this file.
@@ -30,6 +31,7 @@ 
   #:use-module (guix build utils)
   #:use-module (guix build download)
   #:use-module (guix download)
+  #:use-module (semver)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:use-module (texinfo)
@@ -223,15 +225,57 @@  vocabulary."
       patches))
 
 
+(define (package-is-latest-ver? package)
+  "Return whether the PACKAGE is the latest version available.
+
+   PACKAGE (<package>)
+     A package object as defined in the GNU Guix API reference."
+
+  ;; return whether version string a is newer than b
+  (define (is-newer pkgver-a pkgver-b)
+    (if (and (string->semver pkgver-a)
+             (string->semver pkgver-b))
+      (semver>?
+        (string->semver pkgver-a)
+        (string->semver pkgver-b))
+      (string>? pkgver-a pkgver-b)))
+
+  ;; return packages with same name and different versions
+  (define (get-same-pkgs-with-different-ver pkg)
+    (fold
+      (lambda (x accumulator)
+        (if
+          (and
+            (not (string=? (package-version x)
+                           (package-version pkg)))
+            (string=? (package-name x)
+                      (package-name pkg)))
+          (cons x accumulator)
+          accumulator))
+      '()
+      (all-packages)))
+
+  (let loop ((x (get-same-pkgs-with-different-ver package)))
+    (cond
+      ((not (pair? x))
+       #t)
+      ((is-newer (package-version (car x))
+                 (package-version package))
+       #f)
+      (else
+        (loop (cdr x))))))
+
 (define (package-url-path package)
-  "Return a URL path for the PACKAGE in the form packages/NAME-VERSION/.
+  "Return a URL path for the PACKAGE in the form packages/NAME-VERSION/
+(or packages/NAME if the package is the latest version).
 
    PACKAGE (<package>)
      A package object as defined in the GNU Guix API reference."
-  (url-path-join "packages"
-		 (string-append (package-name package)
-				"-"
-				(package-version package))))
+  (if (package-is-latest-ver? package)
+    (url-path-join "packages" (package-name package))
+    (url-path-join "packages"
+      (string-append (package-name package)
+        "-" (package-version package)))))
 
 
 (define (packages/group-by-letter packages)
-- 
2.26.0