Subject: [PATCH] website: Don't append version to URL if latest

From: Alexandros Theodotou <[email-masked]>
Date: Fri, 27 Mar 2020 23:54:27 +0000

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
From f960cc51bf594afc4afe86e773a7536369208c8e Mon Sep 17 00:00:00 2001
From: Alexandros Theodotou <[email-masked]>
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 <[email-masked]>
 ;;; Copyright © 2017 Eric Bavier <[email-masked]>
+;;; Copyright © 2020 Alexandros Theodotou <[email-masked]>
 ;;;
 ;;; 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