eshell-port-helper.el (5696B)
1 ;;; eshell-port-helper.el 2 3 ;; Copyright (C) 2023 Anton Konyahin 4 5 ;; Author: Anton Konyahin <me@konyahin.xyz> 6 ;; URL: https://git.konyahin.xyz/eshell-port-helper/log.html 7 ;; Keywords: eshell port OpenBSD 8 ;; Version: 0.0.1 9 ;; Created: 29/04/2023 10 11 ;; This program is free software; you can redistribute it and/or modify 12 ;; it under the terms of the GNU General Public License as published by 13 ;; the Free Software Foundation, either version 3 of the License, or 14 ;; (at your option) any later version. 15 16 ;; This program is distributed in the hope that it will be useful, 17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 ;; GNU General Public License for more details. 20 21 ;; You should have received a copy of the GNU General Public License 22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. 23 24 ;;; Commentary: 25 ;; This package provides functions for working with OpenBSD ports tree. 26 27 ;;; Code: 28 29 (require 'eshell) 30 31 (defvar port-dir "/usr/ports/" 32 "Base directory for port tree") 33 34 (defvar port-ignore-port-dirs 35 (list "." "CVS" "tests" "pobj" "distfiles" 36 "infrastructure" "packages" "plist") 37 "This directories will be ignore when we search for ports") 38 39 (defun port-string-contain (str args) 40 "Return true if str contain something from args" 41 (cond 42 ((null args) nil) 43 ((string-search (car args) str) t) 44 (t (port-string-contain str (cdr args))))) 45 46 (defun port-dir (dir) 47 "Return path to dir in port dir" 48 (concat port-dir dir)) 49 50 (defun port-in-pobj-p () 51 "Return non-nil if we in pobj port directory" 52 (string-prefix-p (port-dir "pobj/") (eshell/pwd))) 53 54 (defun port-in-port-p () 55 "Return non-nil if we in port directory" 56 (and (not (port-in-pobj-p)) 57 (string-prefix-p port-dir (eshell/pwd)))) 58 59 (defun port-ports-list () 60 "Return list of all ports in system" 61 (let* ((filter (lambda (dir) 62 (and (file-directory-p dir) 63 (not (port-string-contain 64 dir port-ignore-port-dirs))))) 65 (category-dirs (seq-filter filter (directory-files port-dir t))) 66 (port-dirs (flatten-list 67 (mapcar (lambda (dir) (directory-files dir t)) category-dirs))) 68 (filtered-dirs (seq-filter filter port-dirs))) 69 (mapcar #'file-name-nondirectory filtered-dirs))) 70 71 (defun port-current-port-name () 72 "Get current port name by current dir path. Return nil 73 if we not in port directory." 74 (cond 75 ((port-in-pobj-p) 76 (if (string-match (port-dir "pobj/\\([^/-]*\\)") (eshell/pwd)) 77 (match-string 1 (eshell/pwd)))) 78 ((port-in-port-p) 79 (if (string-match (port-dir "[^/]*/\\([^/]*\\)/?") (eshell/pwd)) 80 (match-string 1 (eshell/pwd)))) 81 (t nil))) 82 83 (defun port-get-port-dir (port-name) 84 "Get port dir" 85 (let ((dir (file-expand-wildcards 86 (concat (port-dir "*/") port-name)))) 87 (cond 88 ((null dir) 89 (message "Can't find such port: %s" port-name) 90 nil) 91 ((equal 1 (length dir)) 92 (car dir)) 93 (t (message "Find more than one port with name %s" port-name) 94 nil)))) 95 96 (defun eshell/port-jump-to-port-dir (&optional port-name) 97 "Change current dir to port dir" 98 (let* ((port-name (or port-name (port-current-port-name))) 99 (dir (port-get-port-dir port-name))) 100 (if dir (cd dir)))) 101 102 (defun eshell/port-jump-to-src-dir (&optional port-name) 103 "Change current dir to port source dir" 104 (let ((port-name (or port-name (port-current-port-name)))) 105 (eshell/port-jump-to-port-dir port-name) 106 (cd (string-trim (eshell-command-result "make show=WRKSRC"))))) 107 108 (defun eshell/port-jump () 109 "Jump between port directory and source directory" 110 (cond 111 ((port-in-port-p) 112 (eshell/port-jump-to-src-dir)) 113 ((port-in-pobj-p) 114 (eshell/port-jump-to-port-dir)) 115 (t (message "You are not in port directory")))) 116 117 (defun eshell/port-patch (file) 118 "Make backup file with right suffix and open file for editing" 119 (let ((backup (concat file ".orig.port"))) 120 (if (not (file-exists-p backup)) 121 (copy-file file backup)) 122 (find-file file))) 123 124 (defun eshell/port-unpatch (file) 125 "Restore original port file from backup" 126 (let ((backup (concat file ".orig.port"))) 127 (if (not (file-exists-p backup)) 128 (message "This file hasn't backup") 129 (eshell/mv backup file)))) 130 131 (defun eshell/port-make (command) 132 "Run make command with arguments in port directory" 133 (let ((cur-dir (eshell/pwd))) 134 (eshell/port-jump-to-port-dir) 135 (eshell-do-eval (eshell-parse-command (concat "make " command)) 't) 136 (cd cur-dir))) 137 138 (defun port-jump-to-port (port) 139 "Open eshell in port directory" 140 (interactive 141 (list (completing-read "Enter port name: " (port-ports-list)))) 142 (let ((default-directory (port-get-port-dir port))) 143 (if default-directory 144 (eshell)))) 145 146 (defun eshell/port-update-patches () 147 "Update patches for current port" 148 (with-environment-variables (("EDIT_PATCHES" "Yes") 149 ("EDITOR" "echo files:")) 150 (eshell/port-make "update-patches")) 151 (let ((cur-dir (eshell/pwd)) 152 (patch-path (progn 153 (eshell/port-jump-to-port-dir) 154 (string-trim (eshell-command-result "make show=PATCHDIR")))) 155 (output (buffer-substring (eshell-end-of-output) (point-max)))) 156 (progn 157 (cd cur-dir) 158 (if (string-match "^files: \\(.*\\)$" output) 159 (mapcar (lambda (patch) 160 (find-file (concat patch-path "/" patch))) 161 (split-string (match-string 1 output))) 162 (message "No changes in patches"))))) 163 164 (provide 'eshell-port-helper)