From patchwork Sun Mar 23 07:28:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hilton Chain X-Patchwork-Id: 40714 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 97F5027BBEA; Sun, 23 Mar 2025 07:31:12 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,FROM_SUSPICIOUS_NTLD,MAILING_LIST_MULTI, PDS_OTHER_BAD_TLD,RCVD_IN_DNSWL_BLOCKED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE,SPF_HELO_PASS, URIBL_BLOCKED autolearn=unavailable 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 11E0627BBE2 for ; Sun, 23 Mar 2025 07:31:12 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1twFn3-0001b6-SY; Sun, 23 Mar 2025 03:30:37 -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 1twFmt-0001RV-FE for guix-patches@gnu.org; Sun, 23 Mar 2025 03:30:27 -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 1twFmt-00005A-1y; Sun, 23 Mar 2025 03:30:27 -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:References:In-Reply-To:Date:From:To:Subject; bh=czT8FP4fmhCvvCipLKqanBFPpsXWTXv5dSi/2ifeg3g=; b=oIKpP3KDwCHfMmd6qD1hdVss+HRE5sbTSDQgfS0yAwayej2dSPF8XGdMkaQbWa2h5Um6aFpA0gwjd/0UUZ3hsYMhhIDNSOXalA521iGeh425JAwBsTG7R3BodLYrY+8CknYMIZ2uP2rmopL13EW/2xLyYdD7+Ttc9JqOktifdiJUE4CSNGw5Mz3RZwP1/Fh0CfLPGmpLGhQ2+LLSTV5DRknSVSIBAqJVsrCpwIxA6tzBsIM5OSLxQt1koiSDbjSszdsrxqVpTbIffp66XA6qDVWAyGQUtHVbbu7RSELytyS6dNlNa+n3tFLuMEetHJoLm11VaW5hg4B+AWbU+y7fkg==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1twFmq-0004Qg-0w; Sun, 23 Mar 2025 03:30:24 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#77093] [PATCH rust-team v3 10/17] scripts: import: Support expressions defined by 'define. Resent-From: Hilton Chain Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Sun, 23 Mar 2025 07:30:23 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 77093 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 77093@debbugs.gnu.org Cc: Hilton Chain , Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 77093-submit@debbugs.gnu.org id=B77093.174271500016375 (code B ref 77093); Sun, 23 Mar 2025 07:30:23 +0000 Received: (at 77093) by debbugs.gnu.org; 23 Mar 2025 07:30:00 +0000 Received: from localhost ([127.0.0.1]:47347 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1twFmR-0004Fu-6i for submit@debbugs.gnu.org; Sun, 23 Mar 2025 03:29:59 -0400 Received: from mx.boiledscript.com ([2a01:4f8:10b:392::42]:54430) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1twFm5-0004C5-4W for 77093@debbugs.gnu.org; Sun, 23 Mar 2025 03:29:38 -0400 From: Hilton Chain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ultrarare.space; s=mail; t=1742714976; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=p3wLcBT+ZuzStmxt7XCA9mvvWfKLGl3I0jdiYr76vbc=; b=D+TTar/4Q7i7A98/cdw95/la5YMy+qdfsYroy+b20tAQ1xhan2+Z3TPvf9F57GdxPj4DWk +/7ApXXBAc2uyQPAIM8GIdlR9282RDvYLaN1KAlarjH0/zZr31L/0FMbjgrPc6+2USYDoq 3NVUhzWxWPiBNLK0lkKNo1Qr2M7UrzDInEhvBR3k3ymCZ5Ye2jIvR9KW7u3vE9/SUj0Gi4 YHS8LDbi/kqhFxtnBLNf5eftKnCYyrgX4ypxyApHyDjn7u027lkttBYtTjU3II0HxXSXUt mn3PHOyOtqzXAkj9J5tnQ7PIg2vgPa/55VKQBnf1R5wElZkKIXQ7Kj7PrXY7/g== Date: Sun, 23 Mar 2025 15:28:32 +0800 Message-ID: <00ffa2da7f970ea1c54cf3bd36574381cae5fd23.1742713356.git.hako@ultrarare.space> In-Reply-To: References: 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: , 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/utils.scm (find-definition-location): New procedure. (find-definition-insertion-location): Define with it. * guix/scripts/import.scm (import-as-definitions, guix-import): Support expressions defined by 'define. Change-Id: I03118e1a3372028b4f0530964aba871b4a1a4d25 --- guix/scripts/import.scm | 21 +++++++++++++++------ guix/utils.scm | 29 +++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/guix/scripts/import.scm b/guix/scripts/import.scm index 58a84d0db7..6f6354928e 100644 --- a/guix/scripts/import.scm +++ b/guix/scripts/import.scm @@ -30,6 +30,7 @@ (define-module (guix scripts import) #:use-module (guix read-print) #:use-module (guix utils) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-26) #:use-module (ice-9 format) #:use-module (ice-9 match) #:export (%standard-import-options @@ -83,7 +84,8 @@ (define (import-as-definitions importer args proc) ((and expr (or ('package _ ...) ('let _ ...))) (proc (package->definition expr))) - ((and expr ('define-public _ ...)) + ((and expr (or ('define-public _ ...) + ('define _ ...))) (proc expr)) ((expressions ...) (for-each (lambda (expr) @@ -91,7 +93,8 @@ (define (import-as-definitions importer args proc) ((and expr (or ('package _ ...) ('let _ ...))) (proc (package->definition expr))) - ((and expr ('define-public _ ...)) + ((and expr (or ('define-public _ ...) + ('define _ ...))) (proc expr)))) expressions)) (x @@ -117,13 +120,19 @@ (define-command (guix-import . args) (show-version-and-exit "guix import")) ((or ("-i" file importer args ...) ("--insert" file importer args ...)) - (let ((find-and-insert + (let* ((define-prefixes + `(,@(if (member importer '("crate")) + '(define) + '()) + define-public)) + (define-prefix? (cut member <> define-prefixes)) + (find-and-insert (lambda (expr) (match expr - (('define-public term _ ...) + (((? define-prefix? define-prefix) term _ ...) (let ((source-properties - (find-definition-insertion-location - file term))) + (find-definition-insertion-location + file term #:define-prefix define-prefix))) (if source-properties (insert-expression source-properties expr) (let ((port (open-file file "a"))) diff --git a/guix/utils.scm b/guix/utils.scm index c7c23d9d5b..3f85320845 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -154,6 +154,7 @@ (define-module (guix utils) edit-expression delete-expression insert-expression + find-definition-location find-definition-insertion-location filtered-port @@ -520,24 +521,36 @@ (define (insert-expression source-properties expr) (string-append expr "\n\n" str)))) (edit-expression source-properties insert))) -(define (find-definition-insertion-location file term) - "Search in FILE for a top-level public definition whose defined term -alphabetically succeeds TERM. Return the location if found, or #f -otherwise." - (let ((search-term (symbol->string term))) +(define* (find-definition-location file term + #:key (define-prefix 'define-public) + (pred string=)) + "Search in FILE for a top-level definition defined by DEFINE-PREFIX with +defined term comparing to TERM through PRED. Return the location if PRED +returns #t, or #f otherwise." + (let ((search-term (symbol->string term)) + (define-prefix? (cut eq? define-prefix <>))) (call-with-input-file file (lambda (port) (do ((syntax (read-syntax port) (read-syntax port))) ((match (syntax->datum syntax) - (('define-public current-term _ ...) - (string> (symbol->string current-term) - search-term)) + (((? define-prefix?) current-term _ ...) + (pred (symbol->string current-term) + search-term)) ((? eof-object?) #t) (_ #f)) (and (not (eof-object? syntax)) (syntax-source syntax)))))))) +(define* (find-definition-insertion-location file term + #:key + (define-prefix 'define-public)) + "Search in FILE for a top-level definition defined by DEFINE-PREFIX with +defined term alphabetically succeeds TERM. Return the location if found, or #f +otherwise." + (find-definition-location + file term #:define-prefix define-prefix #:pred string>)) + ;;; ;;; Keyword arguments.