From 868f7c4042aa75db58ac00c8bd4948a29b1aee4f Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 25 May 2021 14:06:41 -0700 Subject: add DEFPACKAGE-CONSFIG Signed-off-by: Sean Whitton --- doc/introduction.rst | 14 +++++++------- src/package.lisp | 1 + src/util.lisp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/doc/introduction.rst b/doc/introduction.rst index 47122d7..656e7eb 100644 --- a/doc/introduction.rst +++ b/doc/introduction.rst @@ -28,13 +28,10 @@ Try it out / quick start (in-package :cl-user) - (defpackage :com.example.consfig - (:use #:cl #:alexandria #:consfigurator) - (:local-nicknames (#:os #:consfigurator.property.os) - (#:apt #:consfigurator.property.apt) - (#:cmd #:consfigurator.property.cmd) - (#:file #:consfigurator.property.file) - (#:chroot #:consfigurator.property.chroot))) + ;; this macro is a simple wrapper of DEFPACKAGE which sets up local + ;; nicknames for packages providing properties and data sources + (consfigurator:defpackage-consfig :com.example.consfig + (:use #:cl #:alexandria #:consfigurator)) 4. Define some hosts and deployments. @@ -282,6 +279,9 @@ recommended package nicknaming schemes for use in consfigs, e.g.:: (#:cmd #:consfigurator.property.cmd) (#:data.pgp #:consfigurator.data.pgp))) +You can use the ``DEFPACKAGE-CONSFIG`` macro to set up all these local +nicknames. + Portability and stability ------------------------- diff --git a/src/package.lisp b/src/package.lisp index 4edd9a3..61662ef 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -78,6 +78,7 @@ #:in-chroot-pathname #:escape-sh-token #:escape-sh-command + #:defpackage-consfig #:*consfigurator-debug-level* #:with-indented-inform diff --git a/src/util.lisp b/src/util.lisp index 1feb97a..79d669c 100644 --- a/src/util.lisp +++ b/src/util.lisp @@ -196,6 +196,39 @@ one-dimensional collections of values." (re:scan-to-strings "^uid=[0-9]+\\(([^)]+)" output) (and match (elt groups 0)))) +;; not DEFCONSFIG because a consfig is a system not a package +(defmacro defpackage-consfig (name &body forms) + "Convenience wrapper around DEFPACKAGE for consfigs. +Adds recommended local nicknames for all the property and data source packages +that come with Consfigurator. Either use this directly or use its macro +expansion as a starting point for your own DEFPACKAGE form for your consfig." + (let ((forms (copy-list forms)) + (local-nicknames + (cons :local-nicknames + (loop for package in (list-all-packages) + for name = (package-name package) + if (string-prefix-p "CONSFIGURATOR.PROPERTY." name) + collect (list (make-symbol (subseq name 23)) + (make-symbol name)) + else if (string-prefix-p "CONSFIGURATOR.DATA." name) + collect (list (make-symbol (subseq name 14)) + (make-symbol name)))))) + (if-let ((form (loop for form on forms + when (and (listp (car form)) + (eql :local-nicknames (caar form))) + return form))) + (rplaca form (nconc local-nicknames (cdar form))) + (push local-nicknames forms)) + ;; Not much benefit to importing CONSFIGURATOR for the user as it only + ;; needs to be done once, and some users will prefer to import qualified, + ;; but we could also have: + ;; (if-let ((form (loop for form on forms + ;; when (and (listp (car form)) (eql :use (caar form))) + ;; return form))) + ;; (rplaca form (list* :use '#:consfigurator (cdar form))) + ;; (push '(:use '#:cl '#:consfigurator) forms)) + `(defpackage ,name ,@forms))) + ;;;; Progress & debug printing -- cgit v1.2.3