From patchwork Thu Jul 4 15:55:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Herman Rimm X-Patchwork-Id: 65917 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id ADBF827BBEA; Thu, 4 Jul 2024 16:58:03 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 15C4927BBE9 for ; Thu, 4 Jul 2024 16:58:02 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sPOoT-0000Ah-U0; Thu, 04 Jul 2024 11:56:01 -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 1sPOoS-000069-9j for guix-patches@gnu.org; Thu, 04 Jul 2024 11:56:00 -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 1sPOoS-0003Ek-0I for guix-patches@gnu.org; Thu, 04 Jul 2024 11:56:00 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sPOoU-0005nu-6g for guix-patches@gnu.org; Thu, 04 Jul 2024 11:56:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#70800] [PATCH v2] 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: Thu, 04 Jul 2024 15:56: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.172010854422286 (code B ref 70800); Thu, 04 Jul 2024 15:56:02 +0000 Received: (at 70800) by debbugs.gnu.org; 4 Jul 2024 15:55:44 +0000 Received: from localhost ([127.0.0.1]:42808 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sPOoB-0005nN-Rw for submit@debbugs.gnu.org; Thu, 04 Jul 2024 11:55:44 -0400 Received: from 81-205-150-117.fixed.kpn.net ([81.205.150.117]:42489 helo=email.rimm.ee) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sPOo6-0005mw-Gp for 70800@debbugs.gnu.org; Thu, 04 Jul 2024 11:55:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rimm.ee; s=herman; t=1720108526; 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=bGqr3gQ6FDVFNhb4WmFAuKtR4M5eI5TMoW2vT1u8dtw=; b=krQAJ9w0TCRIHebe2F2f+4grB4zYdUZpW6h4c36/t94VFADyhmEta3D7sJzCoI0KTXNnVh JaLfxaO+nZn4yItHSWr5bOw7p2URLm0GrwSjwlXCQcvHJ8SgLydETlETUDTuoC7XZpfVrz W0gp3akWx7v0yxVkA6IKdH0HyQg7ENFn9lTw/RTVzY/Ny2UQ2Hw0JGnzTLQlb5aacPEHsc tjfbf4pxPJyP9H9scuhN+NbHDhXw62DlEM7PGFr5PHN1jQzdRGxwXSOSXQEpESyd5tpmag kSA8HhAOWjH4zi1jbtUlg77kQNO6hMWcGT2OAhjfYughfSgauVtTTHhR5tivBA== Received: by 81-205-150-117.fixed.kpn.net (OpenSMTPD) with ESMTPSA id 541905ae (TLSv1.3:TLS_CHACHA20_POLY1305_SHA256:256:NO) for <70800@debbugs.gnu.org>; Thu, 4 Jul 2024 15:55:26 +0000 (UTC) Date: Thu, 4 Jul 2024 17:55:06 +0200 Message-ID: <20240704155507.20383-1-herman@rimm.ee> X-Mailer: git-send-email 2.45.2 MIME-Version: 1.0 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 X-Patchwork-Original-From: Herman Rimm via Guix-patches via From: Herman Rimm Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * 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. * doc/guix.texi (Invoking guix style): Document option. Change-Id: I4aa7c0bd0b6d42529ae7d304587ffb10bf5f4006 --- Hi, I should have mentioned it back then, but in May and again today I tried to not use a reverse. If you can do a sort without it, please amend the patch. This revision takes the other feedback into account. Cheers, Herman doc/guix.texi | 7 ++++ guix/scripts/style.scm | 73 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 6770c9d664..3e598f812b 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -15190,6 +15190,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 0727ac1480..5f4ee4a492 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) @@ -494,11 +496,62 @@ (define (package-location? 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 @@ -526,6 +579,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 @@ -569,7 +625,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")) @@ -584,6 +640,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")) @@ -627,7 +686,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))