Subject: [bug#68935] [PATCH 3/3] guix: import: Put packages into modules in alphabetical order.

From: Ludovic Courtès <[email-masked]>
Date: Wed, 07 Feb 2024 22:57:12 +0100

Herman Rimm <[email-masked]> skribis:

> * guix/scripts/import.scm (guix-import): Add 'put' option.
> (try-import): Add variable.
> * doc/guix.texi (Invoking guix import): Describe 'put' option.
>
> Change-Id: Ied4fc95899c31fb2523a01139780fc9744aa63cb

Nice!  This can certainly simplify people’s work!

>  @example
> -guix import @var{importer} @var{options}@dots{}
> +guix import @var{option} @var{importer} @var{options}@dots{}

Uh?  It looks weird at first.  :-)

For clarity, we should change it to something like:

  guix import [@var{global-options}@dots{}] @var{importer} @var{package} [@var{options}@dots{}]

… though I admit this doesn’t look great either.

> +@item --put=@var{file}
> +@itemx -p @var{file}
> +Insert the package definition(s) that the @var{importer} generated into the

How about calling it ‘--insert’?  It seems more natural.

> +(define (try-import importer args finish)

Please add a docstring for top-level procedures like this one:

  https://guix.gnu.org/manual/devel/en/html_node/Formatting-Code.html

> +  (if (member importer importers)
> +        (match (apply (resolve-importer importer) args)

Indentation of the second line is off.

> +          ((and expr ('package _ ...))
> +           (finish (package->definition expr)))
> +          ((and expr (or ('let _ ...)
> +                         ('define-public _ ...)))
> +           (finish expr))

Again (let …) should be treated like (package …).

> +          ((? list? expressions)
> +           (for-each (lambda (expr)
> +               (match expr
> +                      ((and expr ('package _ ...))
> +                       (finish (package->definition expr)))
> +                      ((and expr (or ('let _ ...)
> +                                     ('define-public _ ...)))
> +                       (finish expr))))

Likewise.  Also indentation of ‘match’ and that of the subsequent
clauses is off.

> -    ((or ("-h") ("--help"))
> +    ((or ("-h" _ ...) ("--help" _ ...))
>       (leave-on-EPIPE (show-help))
>       (exit 0))
> -    ((or ("-V") ("--version"))
> +    ((or ("-V" _ ...) ("--version" _ ...))
>       (show-version-and-exit "guix import"))

This change seems to be unrelated, right?

> +    ((or ("-p" module importer args ...)
> +         ("--put" module importer args ...))
> +     (let ((put (lambda (expr)
> +                  (let ((line
> +                    (call-with-input-file module
> +                      (lambda (port)
> +                        (do ((se (read-syntax port)
> +                                 (read-syntax port)))
> +                          ((match (syntax->datum se)
> +                             (('define-public symbol _ ...)
> +                              (string> (symbol->string symbol)
> +                                       (symbol->string (cadr expr))))
> +                             ((? eof-object?) #t)
> +                             (_ #f))
> +                           (if (eof-object? se)
> +                             #f
> +                             (car (string-split
> +                                    (object->string*
> +                                      (syntax->datum se) 0)
> +                                    #\newline)))))))))
> +                    (if line
> +                        (substitute* module
> +                          (((string-append "\\" line))
> +                           (string-append
> +                             (object->string* expr 0)
> +                             "\n\n" line)))
> +                        (let ((port (open-file module "a")))
> +                          (pretty-print-with-comments port expr)
> +                          (newline port)
> +                          (close-port port)))))))

This whole thing should be in a separate procedure, probably in (guix
utils), close to ‘edit-expression’, and we could call it
‘insert-expression’ and it would take the location where the caller
wants to insert an expression.

Then, separately, we need a procedure to determine that location based
on the alphabetical order to top-level ‘define-public’ occurrences.

Does that make sense?  We should try and add tests for these in
‘tests/utils.scm’.

Also please check out the coding style regarding use of ‘car’ etc.:

  https://guix.gnu.org/manual/devel/en/html_node/Data-Types-and-Pattern-Matching.html

Could you send an updated series?

Please let us know if anything’s unclear or if you need guidance.

Thank you!

Ludo’.