From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id aG/XDwUL1majfwAA62LTzQ:P1 (envelope-from ) for ; Mon, 02 Sep 2024 18:59:17 +0000 Received: from aspmx1.migadu.com ([2001:41d0:303:e224::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id aG/XDwUL1majfwAA62LTzQ (envelope-from ) for ; Mon, 02 Sep 2024 20:59:17 +0200 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=debbugs.gnu.org header.s=debbugs-gnu-org header.b=K8u8PGdt; dkim=fail ("headers rsa verify failed") header.d=rimm.ee header.s=herman header.b=MutxtmFA; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1725303557; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=L7KreTjyUqEe6j666ZmOAiPeRLX1t+ufR4IP+vPElYE=; b=Wht7Pg8N5+ycV6P3UXooZmc6eoKP+GrEY6Ppmj2t2zLI2A/A/C/CqWRja8ugX1nh8W5as5 yYADe6gCdTA37YT5eriNCmbnd+s43I5h5oKKZMNW583Jic2CHzVtpWKmWqaiDxrq1zmYQx LTsIQiB/f015wvINnQ2F1XDFNTrIikDr5VZqun+VS7qaKCoZTPIJRUDfiNjjj/T5LzSV80 SJcGK44hgJhuNvjd3cLIW7fC1fuwT6l41KGoByTK8jZRni5HJpd0FvnOViAtn7odFvvmCE bmzfKyGETkeQ32uc5SdRQ2KAE3jdZP0mkia1VY01ksEdeUbRq6+kSsK8HHD11Q== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1725303557; a=rsa-sha256; cv=none; b=VZIDSJTeVznV0CCK4a0P9rVsKtCXYzjZyZWnkfUYbXqQ+J8WDh7eELjeCMZzNAWFyBOae5 59SWOy/eEmc7jtz0MxvFV1z2qhsalKizYxe5JNWmv/CjnIsmmkzXj+Gshm1z8moi1Peiw/ WlSpcsoOpeVDkTGzCvgixwUMY8obRVEFYBx/HdVeqV+sAVt5r0S1k0/OhYhbSKuCa4CkZ1 z5lnX0bDpuPq7b3AsMACExj85DEAoJBs24esquaJ0Wj7oBMBEngnDL/KDZ2PRDMfaWs0pj 62+D95a8U13C/hOlRTDrOevvR5MlPo2VNqngk9QWOa8DWHW/m6nBLf+4A8Ig5Q== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=debbugs.gnu.org header.s=debbugs-gnu-org header.b=K8u8PGdt; dkim=fail ("headers rsa verify failed") header.d=rimm.ee header.s=herman header.b=MutxtmFA; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id B6C26743B2 for ; Mon, 2 Sep 2024 20:59:16 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1slCGV-000267-QH; Mon, 02 Sep 2024 14:59:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1slCGT-00023p-AY for guix-patches@gnu.org; Mon, 02 Sep 2024 14:59:01 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1slCGT-0000bk-14 for guix-patches@gnu.org; Mon, 02 Sep 2024 14:59:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:In-Reply-To:References:Subject; bh=L7KreTjyUqEe6j666ZmOAiPeRLX1t+ufR4IP+vPElYE=; b=K8u8PGdtMW/6/d1NCi/92BgGnd9gafEMUnMAb8lfw/v6GO5ZmhGdxgFU1WR3PyZEgdJPyxQ74l82PfYoYMl/85XlIdfnaDMUrZhvmtfnn4vHpRNVjRDoYPs3kXLc7m/coS4mVCeABfTo3ecvbwChB68d7053NXTgv3TyW9ufkXUa+OQSKf+FyD845itK9SEtptjivvovbdZ/BpQzsANbQiJVRIfYqqb7z5WP7/pHFh760Fye7ImvQrFCqz+NJwrB/eXMJVtE02PTTrh9uLaJvfuoUu6hl4FT7HX4seUYYx2b2kmdRv9Y5qX47Muj4tZIMn9qSamChKVjVfQEHW1Pyw==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1slCHS-0001jU-5H for guix-patches@gnu.org; Mon, 02 Sep 2024 15:00:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH] scripts: style: Add 'alphabetical-sort' option. References: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> In-Reply-To: <8e0df2707343eaee0bf39fa06fcdd5bc5cd9fba2.1714992109.git.herman@rimm.ee> Resent-From: Herman Rimm Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Mon, 02 Sep 2024 19:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70800 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 70800@debbugs.gnu.org Received: via spool by 70800-submit@debbugs.gnu.org id=B70800.17253035976621 (code B ref 70800); Mon, 02 Sep 2024 19:00:02 +0000 Received: (at 70800) by debbugs.gnu.org; 2 Sep 2024 18:59:57 +0000 Received: from localhost ([127.0.0.1]:52378 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slCHN-0001ig-8L for submit@debbugs.gnu.org; Mon, 02 Sep 2024 14:59:57 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:50065 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slCHK-0001iL-Jw for 70800@debbugs.gnu.org; Mon, 02 Sep 2024 14:59:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1725303525; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=L7KreTjyUqEe6j666ZmOAiPeRLX1t+ufR4IP+vPElYE=; b=MutxtmFA+2+t0DoYy/+UXloLuo/otzXCzg3rNQH6ALwH+qse5azeUrwFSsq4UeB8SndFRf 4XtLamGui3/XkTbao0DT0lNAmQOx11BgyQ9lQhjlCj7EL6ZzN3fWBbzhbOkoc92NKHl2dC 8IZ5EmzTTKP5MeLs5S43lR9nnNAQk/MQitFXKUqT963mecKO3irjCw7IMrq6P/w0ZaUmyM TaZGO34n50PUry6nHOlCU8I3ochqfr98TTVBx50LtfdXi/mHqfkaJhrCUkALC4QY3bs1Y1 g8vZ6LdM0+I2/rraPBXVgaS8EKfxUO8LU6Tn1LbyLobegOT6bm98idhyW5W/8Q== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id 10b1b737 (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO) for <70800@debbugs.gnu.org>; Mon, 2 Sep 2024 18:58:45 +0000 (UTC) Date: Mon, 2 Sep 2024 20:58:05 +0200 Message-ID: <20240902185808.12842-1-herman@rimm.ee> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Herman Rimm X-ACL-Warn: , Herman Rimm via Guix-patches From: Herman Rimm via Guix-patches via Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Queue-Id: B6C26743B2 X-Migadu-Scanner: mx12.migadu.com X-Migadu-Spam-Score: -6.98 X-Spam-Score: -6.98 X-TUID: ieSXPkM47o3W * guix/scripts/style.scm (show-help): Describe option. (order-packages): Add procedure. (format-whole-file): Add 'order?' argument. (%options): Add 'alphabetical-sort' option. (guix-style): Alphabetically order packages in files. * tests/guix-style.sh: Test alphabetical ordering. * doc/guix.texi (Invoking guix style): Document option. Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 --- doc/guix.texi | 7 ++++ guix/scripts/style.scm | 73 ++++++++++++++++++++++++++++++++++++++---- tests/guix-style.sh | 26 +++++++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index f7dbe246a9..6f6f952f96 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -15216,6 +15216,13 @@ configuration (you need write permissions for the file): guix style -f /etc/config.scm @end example +@item --alphabetical-sort +@itemx -A +Place the top-level package definitions in the given files in +alphabetical order. Package definitions with matching names are placed +with versions in descending order. This option only has an effect in +combination with @option{--whole-file}. + @item --styling=@var{rule} @itemx -S @var{rule} Apply @var{rule}, one of the following styling rules: diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm index 1802b854d0..73e3feadee 100644 --- a/guix/scripts/style.scm +++ b/guix/scripts/style.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2021-2024 Ludovic Courtès +;;; Copyright © 2024 Herman Rimm ;;; ;;; This file is part of GNU Guix. ;;; @@ -29,6 +30,7 @@ (define-module (guix scripts style) #:autoload (gnu packages) (specification->package fold-packages) + #:use-module (guix combinators) #:use-module (guix scripts) #:use-module ((guix scripts build) #:select (%standard-build-options)) #:use-module (guix ui) @@ -485,11 +487,62 @@ (define* (format-package-definition package ;;; Whole-file formatting. ;;; -(define* (format-whole-file file #:rest rest) - "Reformat all of FILE." +(define (order-packages lst) + "Return LST, a list of top-level expressions and blanks, with +top-level package definitions in alphabetical order. Packages which +share a name are placed with versions in descending order." + (define (package-name pkg) + (match pkg + ((('define-public _ expr) _ ...) + (match expr + ((or ('package _ ('name name) _ ...) + ('package ('name name) _ ...)) + name) + (_ #f))) + (_ #f))) + + (define (package-version pkg) + (match pkg + ((('define-public _ expr) _ ...) + (match expr + ((or ('package _ _ ('version version) _ ...) + ('package _ ('version version) _ ...)) + version) + (_ #f))) + (_ #f))) + + (define (package>? lst1 lst2) + (let ((name1 (package-name lst1)) + (name2 (package-name lst2)) + (version1 (package-version lst1)) + (version2 (package-version lst2))) + (and name1 name2 (or (string>? name1 name2) + (and (string=? name1 name2) + version1 + version2 + (version>? version2 version1)))))) + + ;; Group define-public with preceding blanks and defines. + (let ((lst (fold2 (lambda (expr tail head) + (let ((head (cons expr head))) + (match expr + ((? blank?) + (values tail head)) + (('define _ ...) + (values tail head)) + (_ (values (cons head tail) '()))))) + '() '() lst))) + (reverse (concatenate (sort! lst package>?))))) + +(define* (format-whole-file file order? #:rest rest) + "Reformat all of FILE. When ORDER? is true, top-level package definitions +are put in alphabetical order." (with-fluids ((%default-port-encoding "UTF-8")) - (let ((lst (call-with-input-file file read-with-comments/sequence - #:guess-encoding #t))) + (let* ((lst (call-with-input-file file read-with-comments/sequence + #:guess-encoding #t)) + (lst (if order? + (order-packages lst) + lst))) (with-atomic-file-output file (lambda (port) (apply pretty-print-with-comments/splice port lst @@ -517,6 +570,9 @@ (define %options (option '(#\f "whole-file") #f #f (lambda (opt name arg result) (alist-cons 'whole-file? #t result))) + (option '(#\A "--alphabetical-sort") #f #f + (lambda (opt name arg result) + (alist-cons 'order? #t result))) (option '(#\S "styling") #t #f (lambda (opt name arg result) (alist-cons 'styling-procedure @@ -560,7 +616,7 @@ (define (show-help) (display (G_ " -S, --styling=RULE apply RULE, a styling rule")) (display (G_ " - -l, --list-stylings display the list of available style rules")) + -l, --list-stylings display the list of available style rules")) (newline) (display (G_ " -n, --dry-run display files that would be edited but do nothing")) @@ -575,6 +631,9 @@ (define (show-help) (newline) (display (G_ " -f, --whole-file format the entire contents of the given file(s)")) + (display (G_ " + -A, --alphabetical-sort + place the contents in alphabetical order as well")) (newline) (display (G_ " -h, --help display this help and exit")) @@ -618,7 +677,9 @@ (define (parse-options) (warning (G_ "'--styling' option has no effect in whole-file mode~%"))) (when (null? files) (warning (G_ "no files specified, nothing to do~%"))) - (for-each format-whole-file files)) + (for-each + (cute format-whole-file <> (assoc-ref opts 'order?)) + files)) (let ((packages (filter-map (match-lambda (('argument . spec) (specification->package spec)) diff --git a/tests/guix-style.sh b/tests/guix-style.sh index 2de879d5e3..9333139435 100644 --- a/tests/guix-style.sh +++ b/tests/guix-style.sh @@ -58,6 +58,24 @@ cat > "$tmpfile" <