ob-powershell.el (2455B)
1 ;;; ob-powershell.el --- Run Powershell from org mode source blocks 2 ;;; SPDX-License-Identifier: MIT 3 4 ;; Copyright (C) 2022 Rob Kiggen 5 6 ;; Author: Rob Kiggen <robby.kiggen@essential-it.be> 7 ;; Maintainer: Mois Moshev <mois.moshev@bottleshipvfx.com> 8 ;; Version: 1.0 9 ;; Package-Version: 20220314.1359 10 ;; Package-Commit: f351429590ed68b26a9c8f9847066ca4205e524b 11 ;; Package-Requires: ((emacs "26.1")) 12 ;; Keywords: powershell, shell, execute, outlines, processes 13 ;; URL: https://github.com/rkiggen/ob-powershell 14 15 ;;; Commentary: 16 17 ;; Currently this only supports the external compilation and execution 18 ;; of Powershell code blocks (i.e., no session support). 19 ;;; Code: 20 21 (require 'ob) 22 23 (defvar org-babel-tangle-lang-exts) 24 (add-to-list 'org-babel-tangle-lang-exts '("powershell" . "ps1")) 25 26 (defcustom ob-powershell-powershell-command "powershell" 27 "Name of command used to evaluate powershell blocks." 28 :group 'org-babel 29 :version "24.3" 30 :type 'string) 31 32 33 (defun org-babel-execute:powershell (body params) 34 "Execute a block of Powershell code BODY with Babe passing PARAMS. 35 This function is called by `org-babel-execute-src-block'." 36 (let ((scriptfile (org-babel-temp-file "powershell-script-" ".ps1")) 37 (full-body (org-babel-expand-body:generic 38 body params (org-babel-variable-assignments:powershell params)))) 39 (message full-body) 40 (with-temp-file scriptfile (insert full-body)) 41 (org-babel-eval (concat ob-powershell-powershell-command " " scriptfile) ""))) 42 43 (defun org-babel-variable-assignments:powershell (params) 44 "Return a list of Powershell statements parsed from PARAMS, assigning the block's variables." 45 (mapcar 46 (lambda (pair) 47 (format "$env:%s=%s" 48 (car pair) 49 (ob-powershell-var-to-powershell (cdr pair)))) 50 (org-babel--get-vars params))) 51 52 (defun ob-powershell-var-to-powershell (var) 53 "Convert :var into a powershell variable. 54 Convert an elisp value, VAR, into a string of poershell source code 55 specifying a variable of the same value." 56 (if (listp var) 57 (concat "[" (mapconcat #'ob-powershell-var-to-powershell var ", ") "]") 58 (format "$%S" var))) 59 60 (defun org-babel-prep-session:powershell (session params) 61 "Return an error because Powershell does not support sessions. 62 SESSION refers to the babel session. 63 PARAMS are the passed parameters." 64 (error "Sessions are not (yet) supported for Powershell")) 65 66 67 (provide 'ob-powershell) 68 ;;; ob-powershell.el ends here