changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/bin/homer.lisp

changeset 348: 3bce51a08b5c
parent: 724218ecea4b
child: 1b3761849c98
author: Richard Westhaver <ellis@rwest.io>
date: Mon, 13 May 2024 22:10:24 -0400
permissions: -rw-r--r--
description: bufixes
1 ;;; homer.lisp --- homectl utility
2 
3 ;;; Code:
4 (defpackage :bin/homer
5  (:nicknames :homer)
6  (:use :cl :std :log :sxp :rdb :skel :packy :cli :obj/id :krypt :vc)
7  (:export :main :home-config))
8 
9 (in-package :bin/homer)
10 (defvar *user* (sb-posix:getenv "USER"))
11 (defvar *user-homedir* (user-homedir-pathname))
12 (defvar *default-user-homerc* (merge-pathnames ".homerc" *user-homedir*))
13 (declaim (type home-config *home-config*))
14 (defvar *home-config*)
15 
16 (defclass home-config (sxp id)
17  ((user :initform *user* :initarg :user :type string)
18  (path :initform nil :initarg :path :type (or pathname null))
19  (src ::initform nil :initarg :src :type (or null pathname vc-repo))
20  (skel :initform (load-user-skelrc) :initarg :skel :type (or null pathname sk-user-config))
21  (krypt :initform (load-kryptrc) :initarg :krypt :type (or null pathname krypt-config))
22  (packy :initform nil :initarg :packy :type (or null pathname pk-user-config))
23  (mail :initarg :mail :type pathname)
24  (shell :initarg :shell :type (or pathname shell-user-config))
25  (editor :initarg :editor :type (or pathname editor-user-config))
26  (wm :initarg :wm :type (or pathname wm-user-config))
27  (browser :initarg :browser :type (or pathname browser-user-config))))
28 
29 
30 (defmethod print-object ((self home-config) stream)
31  (print-unreadable-object (self stream :type t)
32  (format stream "~S ~A" :id (format-sxhash (id self)))))
33 
34 (defun find-homer-symbol (s)
35  (find-symbol* (symbol-name s) :homer nil))
36 
37 (defmethod load-ast ((self home-config))
38  (with-slots (ast) self
39  (if (formp ast)
40  ;; ast is valid, modify object, set ast nil
41  (progn
42  (sb-int:doplist (k v) ast
43  (when-let ((s (find-homer-symbol k))) ;; needs to be correct package
44  (setf (slot-value self s) v)))
45  (setf (ast self) nil)
46  self)
47  ;; invalid ast, signal error
48  (error 'sxp-syntax-error))))
49 
50 ;; obj -> ast
51 (defmethod build-ast ((self home-config) &key (nullp nil) (exclude '(ast id)))
52  (setf (ast self)
53  (unwrap-object self
54  :slots t
55  :methods nil
56  :nullp nullp
57  :exclude exclude)))
58 
59 (defun load-homerc (&optional (file *default-user-homerc*))
60  "Load a homerc configuration from FILE. Defaults to ~/.homerc."
61  (unless (null (probe-file file))
62  (let ((form (file-read-forms file)))
63  (setq *home-config* (load-ast (make-instance 'home-config :ast form :path file :id (sxhash form))))
64  (with-slots (src) *home-config*
65  (if (info! src)
66  (setf src (pathname src))
67  (setf src (pathname (sb-posix:getenv "HOMER"))))))))
68 
69 ;;; CLI
70 (defopt homer-help (print-help $cli))
71 (defopt homer-version (print-version $cli))
72 (defopt homer-log-level (when $val (setq *log-level* :debug)))
73 
74 (defcmd homer-show
75  (describe *home-config*))
76 
77 (defun mtime (path) (sb-posix:stat-mtime (sb-posix:stat path)))
78 
79 (defun compare-to-home (src)
80  "Compare a SRC path to what is stored in the user's home. Return a cons with
81 the last modified timestamp of each file (SRC . HOME) or NIL."
82  (let* ((name (enough-namestring src))
83  (home (merge-pathnames name (user-homedir-pathname)))
84  (m1 (mtime src))
85  (m2 (when (probe-file home) (mtime home)))
86  (status (if (null m2) :new
87  (if (< m2 m1) :pull
88  :push))))
89  (cons name (list status m1 m2))))
90 
91 (defcmd homer-check
92  (let ((cfg *home-config*))
93  (with-slots (src) cfg
94  (if-let ((src (probe-file src)))
95  (let ((*default-pathname-defaults* src))
96  (debug!
97  (mapcar #'compare-to-home
98  (std/file:find-files *default-pathname-defaults* (push "readme.org" *hidden-paths*)))))
99  (error 'file-error :pathname src)))))
100 
101 (defcmd homer-push)
102 (defcmd homer-pull)
103 (defcmd homer-clean)
104 
105 (define-cli $cli
106  :name "homer"
107  :version "0.1.0"
108  :description "user home manager"
109  :thunk homer-show
110  :opts (make-opts
111  (:name "level" :global t :description "set the log level" :thunk homer-log-level)
112  (:name "help" :global t :description "print help" :thunk homer-help)
113  (:name "version" :global t :description "print version" :thunk homer-version))
114  :cmds (make-cmds
115  (:name show :thunk homer-show)
116  (:name check :thunk homer-check)))
117 
118 (defun run ()
119  (let ((*log-level* :info))
120  (with-cli (opts cmds args) $cli
121  (load-homerc)
122  (do-cmd $cli)
123  (debug-opts $cli))))
124 
125 (defmain ()
126  (let ((*print-readably* t))
127  (run)
128  (sb-ext:exit :code 0)))